[Python-checkins] python/dist/src/Python asdl.c,NONE,1.1.2.1 ast.c,NONE,1.1.2.1

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Sun, 07 Jul 2002 10:34:46 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv28375/Python

Added Files:
      Tag: ast-branch
	asdl.c ast.c 
Log Message:
New files needed for AST

XXX Where to put asdl.py code? Perhaps new AST directory would be
better than Parser.


--- NEW FILE: asdl.c ---
#include "Python.h"
#include "asdl.h"

asdl_seq *
asdl_seq_new(int size)
{
    asdl_seq *seq = (asdl_seq *)malloc(sizeof(asdl_seq));
    if (!seq)
	return NULL;
    seq->elements = malloc(sizeof(void *) * size);
    if (!seq->elements) {
	free(seq);
	return NULL;
    }
    seq->size = size;
    seq->used = 0;
    return seq;
}

void *
asdl_seq_get(asdl_seq *seq, int offset)
{
    if (offset > seq->used)
	return NULL;
    return seq->elements[offset];
}

int
asdl_seq_append(asdl_seq *seq, void *elt)
{
    if (seq->size == seq->used) {
	void *newptr;
	int nsize = seq->size * 2;
	newptr = realloc(seq->elements, sizeof(void *) * nsize);
	if (!newptr)
	    return 0;
	seq->elements = newptr;
    }
    seq->elements[seq->used++] = elt;
    return 1;
}

void
asdl_seq_free(asdl_seq *seq)
{
    if (seq->elements)
	free(seq->elements);
    free(seq);
}

--- NEW FILE: ast.c ---
#include "Python.h"
#include "Python-ast.h"
#include "grammar.h"
#include "node.h"
#include "ast.h"
#include "token.h"
#include "parsetok.h"
#include "graminit.h"

#include <assert.h>

/* XXX TO DO
   re-indent this file
   internal error checking
   syntax errors
*/

static asdl_seq *seq_for_testlist(node *);
static expr_ty ast_for_expr(node *);
[...1211 lines suppressed...]
	switch (TYPE(ch)) {
	case if_stmt:
	    return ast_for_if_stmt(ch);
	case while_stmt:
	    return ast_for_while_stmt(ch);
	case for_stmt:
	    return ast_for_for_stmt(ch);
	case try_stmt:
	    return ast_for_try_stmt(ch);
	case funcdef:
	    return ast_for_funcdef(ch);
	case classdef:
	    return ast_for_classdef(ch);
	default:
	    return NULL;
	}
    }
    return NULL;
}