[Python-checkins] python/nondist/sandbox/ast asdl.c,NONE,1.1 asdl.h,1.1,1.2 setup.py,1.1,1.2

jhylton@sourceforge.net jhylton@sourceforge.net
Fri, 12 Apr 2002 08:56:45 -0700


Update of /cvsroot/python/python/nondist/sandbox/ast
In directory usw-pr-cvs1:/tmp/cvs-serv29606

Modified Files:
	asdl.h setup.py 
Added Files:
	asdl.c 
Log Message:
Add the beginnings of an ASDL sequence type (like PyListObject).


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

static asdl_seq *
asdl_list_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;
}

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

static int
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->size++] = elt;
    return 1;
}

Index: asdl.h
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/asdl.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** asdl.h	9 Apr 2002 19:13:19 -0000	1.1
--- asdl.h	12 Apr 2002 15:56:43 -0000	1.2
***************
*** 1,3 ****
! #define identifier const char *
  #define bool int
  #define string const char *
--- 1,20 ----
! #define identifier PyObject *
  #define bool int
  #define string const char *
+ 
+ /* It would be nice if the code generated by asdl_c.py was completely
+    independent of Python, but it is a goal the requires too much work
+    at this stage.  So, for example, I'll represent identifiers as
+    interned Python strings.
+ */
+ 
+ typedef struct {
+     int size;
+     int used;
+     void **elements;
+ } asdl_seq;
+ 
+ asdl_seq *asdl_list_new(int size);
+ void *asdl_get(asdl_seq *seq, int offset);
+ int append(asdl_seq *seq, void *elt);
+ 

Index: setup.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/setup.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** setup.py	11 Apr 2002 21:20:19 -0000	1.1
--- setup.py	12 Apr 2002 15:56:43 -0000	1.2
***************
*** 3,7 ****
  
  ast = Extension(name="ast",
!                 sources=["astmodule.c", "Python-ast.c"])
  
  setup(name="ast",
--- 3,7 ----
  
  ast = Extension(name="ast",
!                 sources=["astmodule.c", "Python-ast.c", "asdl.c"])
  
  setup(name="ast",
***************
*** 9,11 ****
  
  import ast
! ast.transform("1 + 2")
--- 9,11 ----
  
  import ast
! ast.transform("global xyz")