cvsuser 04/08/04 00:48:47
Modified: ast ast.h ast.y astparser.c node.c
languages/python ast2past.py
Log:
ast 11 - more cleanup; start documenting that mess
* opcodes are bare words now - similar to Name
* add empty nodes in ast2past.y
* cleanup in ast.h and node.c, better variable names
* documentation
Revision Changes Path
1.8 +20 -33 parrot/ast/ast.h
Index: ast.h
===================================================================
RCS file: /cvs/public/parrot/ast/ast.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -w -r1.7 -r1.8
--- ast.h 3 Aug 2004 11:21:30 -0000 1.7
+++ ast.h 4 Aug 2004 07:48:42 -0000 1.8
@@ -5,7 +5,7 @@
#include "astparser.h"
typedef enum {
- UNK_CTX
+ UNK_CTX /* unknown context */
} context_type;
typedef struct nodeType_t* (*node_opt_t) (struct nodeType_t*);
@@ -15,48 +15,35 @@
typedef void (*node_dump_t) (struct nodeType_t*, int level);
typedef context_type (*node_context_t)(struct nodeType_t*, context_type);
+/* imcc forward declarations */
struct _SymReg;
struct _IMC_Unit;
+typedef enum {
+ NODE_HAS_CHILD
+} node_flags_enum;
+
typedef struct nodeType_t {
- node_expand_t expand;
- node_opt_t opt;
- node_dump_t dump;
- node_context_t context;
- context_type up_ctx; /* ctx coming from upper */
- context_type ctx;
- const char* d;
- struct nodeType_t *up; /* parent */
+ node_expand_t expand; /* create code for node */
+ node_opt_t opt; /* optimize node */
+ node_dump_t dump; /* debug dump node */
+ node_context_t context; /* TODO create register type context */
+ context_type up_ctx; /* context coming from upper node */
+ context_type ctx; /* context of this node */
+ const char* description; /* visible name of the node */
+ struct nodeType_t *parent; /* parent of node */
struct nodeType_t *next; /* next statement */
struct nodeType_t *dest; /* destination or result */
- struct _IMC_Unit *unit;
- YYLTYPE loc;
+ struct _IMC_Unit *unit; /* IMCC compilation unit to place code */
+ YYLTYPE loc; /* yacc/bison code location */
+ node_flags_enum flags; /* NODE_HAS_CHILD ... */
union {
- struct _SymReg *r;
- struct nodeType_t *child;
+ struct _SymReg *r; /* var, temp, const node */
+ struct nodeType_t *child; /* contained node */
} u;
} nodeType;
-#define NODE0(p) (p)->u.child
-#define NODE1(p) (p)->u.child->u.child
-
-#ifdef AST_TEST
-enum VARTYPE { /* variable type can be */
- VTCONST = 1 << 0, /* constant */
- VTREG = 1 << 1, /* register */
- VT_UNICODE = 1 << 16 /* unicode string constant */
-};
-
-typedef struct _SymReg {
- char * name;
- enum VARTYPE type; /* Variable type */
- int set; /* Which register set/file it belongs to */
- struct _SymReg * next; /* used in the symbols hash */
-} SymReg;
-SymReg * mk_const(char * name, int t);
-SymReg * mk_symreg(char * name, int t);
-
-#endif
+#define CHILD(p) (p)->u.child
nodeType * IMCC_new_const_node(Interp*, char *name, int set, YYLTYPE *loc);
nodeType * IMCC_new_var_node(Interp*, char *name, int set, YYLTYPE *loc);
1.7 +5 -1 parrot/ast/ast.y
Index: ast.y
===================================================================
RCS file: /cvs/public/parrot/ast/ast.y,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -w -r1.6 -r1.7
--- ast.y 3 Aug 2004 12:51:02 -0000 1.6
+++ ast.y 4 Aug 2004 07:48:42 -0000 1.7
@@ -47,7 +47,7 @@
%token <s> STRINGC INTC FLOATC USTRINGC NAME
%token <t> IDENTIFIER MODULE FUNCTION
-%type <n> program nodes nodes0 node const var
+%type <n> program nodes nodes0 node const var opcode
%type <t> type
%pure_parser
@@ -68,6 +68,7 @@
| nodes node { $$ = IMCC_append_node(interp, $1, $2, &@1); }
| const
| var
+ | opcode
;
node: IDENTIFIER '(' nodes0 ')' { $$ = IMCC_new_node(interp, $1, $3, &@1); }
@@ -92,6 +93,9 @@
type: ':' { $$ = 'P'; }
;
+opcode: NAME { $$ = IMCC_new_const_node(interp, $1, 'o', &@1); }
+ ;
+
%%
static void
1.9 +71 -64 parrot/ast/astparser.c
Index: astparser.c
===================================================================
RCS file: /cvs/public/parrot/ast/astparser.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -w -r1.8 -r1.9
--- astparser.c 3 Aug 2004 12:51:02 -0000 1.8
+++ astparser.c 4 Aug 2004 07:48:42 -0000 1.9
@@ -263,17 +263,17 @@
#endif
/* YYFINAL -- State number of the termination state. */
-#define YYFINAL 19
+#define YYFINAL 21
#define YYLAST 44
/* YYNTOKENS -- Number of terminals. */
#define YYNTOKENS 14
/* YYNNTS -- Number of nonterminals. */
-#define YYNNTS 10
+#define YYNNTS 11
/* YYNRULES -- Number of rules. */
-#define YYNRULES 20
+#define YYNRULES 22
/* YYNRULES -- Number of states. */
-#define YYNSTATES 31
+#define YYNSTATES 33
/* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
#define YYUNDEFTOK 2
@@ -320,27 +320,27 @@
static const unsigned char yyprhs[] =
{
0, 0, 3, 5, 7, 9, 10, 12, 15, 17,
- 19, 24, 25, 31, 32, 38, 40, 42, 44, 46,
- 49
+ 19, 21, 26, 27, 33, 34, 40, 42, 44, 46,
+ 48, 51, 53
};
/* YYRHS -- A `-1'-separated list of the rules' RHS. */
static const yysigned_char yyrhs[] =
{
15, 0, -1, 17, -1, 1, -1, 17, -1, -1,
- 18, -1, 17, 18, -1, 21, -1, 22, -1, 8,
- 11, 16, 12, -1, -1, 10, 19, 11, 17, 12,
- -1, -1, 9, 20, 11, 17, 12, -1, 3, -1,
- 4, -1, 5, -1, 6, -1, 23, 7, -1, 13,
- -1
+ 18, -1, 17, 18, -1, 21, -1, 22, -1, 24,
+ -1, 8, 11, 16, 12, -1, -1, 10, 19, 11,
+ 17, 12, -1, -1, 9, 20, 11, 17, 12, -1,
+ 3, -1, 4, -1, 5, -1, 6, -1, 23, 7,
+ -1, 13, -1, 7, -1
};
/* YYRLINE[YYN] -- source line where rule number YYN was defined. */
static const unsigned char yyrline[] =
{
0, 59, 59, 60, 63, 64, 67, 68, 69, 70,
- 73, 74, 74, 78, 78, 82, 84, 85, 86, 89,
- 92
+ 71, 74, 75, 75, 79, 79, 83, 85, 86, 87,
+ 90, 93, 96
};
#endif
@@ -352,7 +352,7 @@
"$end", "error", "$undefined", "STRINGC", "INTC", "FLOATC", "USTRINGC",
"NAME", "IDENTIFIER", "MODULE", "FUNCTION", "'('", "')'", "':'",
"$accept", "program", "nodes0", "nodes", "node", "@1", "@2", "const",
- "var", "type", 0
+ "var", "type", "opcode", 0
};
#endif
@@ -370,16 +370,16 @@
static const unsigned char yyr1[] =
{
0, 14, 15, 15, 16, 16, 17, 17, 17, 17,
- 18, 19, 18, 20, 18, 21, 21, 21, 21, 22,
- 23
+ 17, 18, 19, 18, 20, 18, 21, 21, 21, 21,
+ 22, 23, 24
};
/* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
static const unsigned char yyr2[] =
{
0, 2, 1, 1, 1, 0, 1, 2, 1, 1,
- 4, 0, 5, 0, 5, 1, 1, 1, 1, 2,
- 1
+ 1, 4, 0, 5, 0, 5, 1, 1, 1, 1,
+ 2, 1, 1
};
/* YYDEFACT[STATE-NAME] -- Default rule to reduce with in state
@@ -387,33 +387,35 @@
means the default is an error. */
static const unsigned char yydefact[] =
{
- 0, 3, 15, 16, 17, 18, 0, 13, 11, 20,
- 0, 2, 6, 8, 9, 0, 5, 0, 0, 1,
- 7, 19, 0, 4, 0, 0, 10, 0, 0, 14,
- 12
+ 0, 3, 16, 17, 18, 19, 22, 0, 14, 12,
+ 21, 0, 2, 6, 8, 9, 0, 10, 5, 0,
+ 0, 1, 7, 20, 0, 4, 0, 0, 11, 0,
+ 0, 15, 13
};
/* YYDEFGOTO[NTERM-NUM]. */
static const yysigned_char yydefgoto[] =
{
- -1, 10, 22, 11, 12, 18, 17, 13, 14, 15
+ -1, 11, 24, 12, 13, 20, 19, 14, 15, 16,
+ 17
};
/* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
STATE-NUM. */
-#define YYPACT_NINF -12
+#define YYPACT_NINF -13
static const yysigned_char yypact[] =
{
- 0, -12, -12, -12, -12, -12, -9, -12, -12, -12,
- 7, 34, -12, -12, -12, 4, 15, 3, 16, -12,
- -12, -12, 14, 34, 15, 15, -12, 24, 29, -12,
- -12
+ 1, -13, -13, -13, -13, -13, -13, -10, -13, -13,
+ -13, 3, 34, -13, -13, -13, 5, -13, 16, 4,
+ 17, -13, -13, -13, 21, 34, 16, 16, -13, 22,
+ 29, -13, -13
};
/* YYPGOTO[NTERM-NUM]. */
static const yysigned_char yypgoto[] =
{
- -12, -12, -12, 6, -11, -12, -12, -12, -12, -12
+ -13, -13, -13, 9, -12, -13, -13, -13, -13, -13,
+ -13
};
/* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
@@ -423,19 +425,19 @@
#define YYTABLE_NINF -1
static const unsigned char yytable[] =
{
- 20, 1, 16, 2, 3, 4, 5, 19, 6, 7,
- 8, 21, 20, 9, 24, 0, 20, 20, 2, 3,
- 4, 5, 23, 6, 7, 8, 26, 25, 9, 0,
- 27, 28, 6, 7, 8, 0, 29, 6, 7, 8,
- 0, 30, 6, 7, 8
+ 22, 18, 1, 21, 2, 3, 4, 5, 6, 7,
+ 8, 9, 23, 22, 10, 26, 0, 22, 22, 2,
+ 3, 4, 5, 6, 7, 8, 9, 25, 27, 10,
+ 7, 8, 9, 28, 31, 29, 30, 7, 8, 9,
+ 0, 32, 7, 8, 9
};
static const yysigned_char yycheck[] =
{
- 11, 1, 11, 3, 4, 5, 6, 0, 8, 9,
- 10, 7, 23, 13, 11, -1, 27, 28, 3, 4,
- 5, 6, 16, 8, 9, 10, 12, 11, 13, -1,
- 24, 25, 8, 9, 10, -1, 12, 8, 9, 10,
+ 12, 11, 1, 0, 3, 4, 5, 6, 7, 8,
+ 9, 10, 7, 25, 13, 11, -1, 29, 30, 3,
+ 4, 5, 6, 7, 8, 9, 10, 18, 11, 13,
+ 8, 9, 10, 12, 12, 26, 27, 8, 9, 10,
-1, 12, 8, 9, 10
};
@@ -443,10 +445,10 @@
symbol of state STATE-NUM. */
static const unsigned char yystos[] =
{
- 0, 1, 3, 4, 5, 6, 8, 9, 10, 13,
- 15, 17, 18, 21, 22, 23, 11, 20, 19, 0,
- 18, 7, 16, 17, 11, 11, 12, 17, 17, 12,
- 12
+ 0, 1, 3, 4, 5, 6, 7, 8, 9, 10,
+ 13, 15, 17, 18, 21, 22, 23, 24, 11, 20,
+ 19, 0, 18, 7, 16, 17, 11, 11, 12, 17,
+ 17, 12, 12
};
#if ! defined (YYSIZE_T) && defined (__SIZE_TYPE__)
@@ -1030,68 +1032,73 @@
{ yyval.n = IMCC_append_node(interp, yyvsp[-1].n, yyvsp[0].n, &yylsp[-1]); }
break;
- case 10:
-#line 73 "ast/ast.y"
- { yyval.n = IMCC_new_node(interp, yyvsp[-3].t, yyvsp[-1].n, &yylsp[-3]); }
- break;
-
case 11:
#line 74 "ast/ast.y"
- { cur_unit = imc_open_unit(interp, IMC_PCCSUB); }
+ { yyval.n = IMCC_new_node(interp, yyvsp[-3].t, yyvsp[-1].n, &yylsp[-3]); }
break;
case 12:
#line 75 "ast/ast.y"
- { yyval.n = IMCC_new_node(interp, yyvsp[-4].t, yyvsp[-1].n, &yylsp[-4]);
- yyval.n->unit = cur_unit;
- cur_unit = cur_unit->prev; }
+ { cur_unit = imc_open_unit(interp, IMC_PCCSUB); }
break;
case 13:
-#line 78 "ast/ast.y"
- { cur_unit = imc_open_unit(interp, IMC_PCCSUB); }
+#line 76 "ast/ast.y"
+ { yyval.n = IMCC_new_node(interp, yyvsp[-4].t, yyvsp[-1].n, &yylsp[-4]);
+ yyval.n->unit = cur_unit;
+ cur_unit = cur_unit->prev; }
break;
case 14:
#line 79 "ast/ast.y"
- { yyval.n = IMCC_new_node(interp, yyvsp[-4].t, yyvsp[-1].n, &yylsp[-4]); }
+ { cur_unit = imc_open_unit(interp, IMC_PCCSUB); }
break;
case 15:
-#line 83 "ast/ast.y"
- { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'S', &yylsp[0]); }
+#line 80 "ast/ast.y"
+ { yyval.n = IMCC_new_node(interp, yyvsp[-4].t, yyvsp[-1].n, &yylsp[-4]); }
break;
case 16:
#line 84 "ast/ast.y"
- { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'I', &yylsp[0]); }
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'S', &yylsp[0]); }
break;
case 17:
#line 85 "ast/ast.y"
- { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'N', &yylsp[0]); }
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'I', &yylsp[0]); }
break;
case 18:
#line 86 "ast/ast.y"
- { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'U', &yylsp[0]); }
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'N', &yylsp[0]); }
break;
case 19:
-#line 89 "ast/ast.y"
- { yyval.n = IMCC_new_var_node(interp, yyvsp[0].s, yyvsp[-1].t, &yylsp[0]); }
+#line 87 "ast/ast.y"
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'U', &yylsp[0]); }
break;
case 20:
-#line 92 "ast/ast.y"
+#line 90 "ast/ast.y"
+ { yyval.n = IMCC_new_var_node(interp, yyvsp[0].s, yyvsp[-1].t, &yylsp[0]); }
+ break;
+
+ case 21:
+#line 93 "ast/ast.y"
{ yyval.t = 'P'; }
break;
+ case 22:
+#line 96 "ast/ast.y"
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'o', &yylsp[0]); }
+ break;
+
}
/* Line 1016 of /usr/share/bison/yacc.c. */
-#line 1095 "ast/astparser.c"
+#line 1102 "ast/astparser.c"
yyvsp -= yylen;
yyssp -= yylen;
@@ -1310,7 +1317,7 @@
}
-#line 95 "ast/ast.y"
+#line 99 "ast/ast.y"
static void
1.12 +192 -72 parrot/ast/node.c
Index: node.c
===================================================================
RCS file: /cvs/public/parrot/ast/node.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -w -r1.11 -r1.12
--- node.c 3 Aug 2004 14:39:20 -0000 1.11
+++ node.c 4 Aug 2004 07:48:42 -0000 1.12
@@ -1,6 +1,6 @@
/*
Copyright: 2001-2004 The Perl Foundation. All Rights Reserved.
-$Id: node.c,v 1.11 2004/08/03 14:39:20 leo Exp $
+$Id: node.c,v 1.12 2004/08/04 07:48:42 leo Exp $
=head1 NAME
@@ -29,32 +29,20 @@
static nodeType* create_Func(int nr, nodeType *self, nodeType *p);
static nodeType* create_Name(int nr, nodeType *self, nodeType *p);
+/*
+ * constant node handling
+ * constant nodes point to a SymReg holding the constant
+ */
static void
-pr(nodeType *p)
+dump_sym(nodeType *p)
{
- if (!p)
- return;
fprintf(stderr, "%s", p->u.r->name);
}
static void
dump_Const(nodeType *p, int l)
{
- pr(p);
-}
-
-static void
-dump_Op(nodeType *p, int l)
-{
- pr(NODE0(p));
- fprintf(stderr, "'");
-}
-
-static void
-dump_Var(nodeType *p, int l)
-{
- fprintf(stderr, ":");
- pr(p);
+ dump_sym(p);
}
static nodeType*
@@ -69,32 +57,20 @@
p->expand = exp_Const;
/* p->context = const_context; */
p->dump = dump_Const;
- p->opt = NULL;
- p->d = "Const";
+ p->description = "Const";
}
-static nodeType*
-create_Op(int nr, nodeType *self, nodeType *p)
-{
- char *s;
- s = p->u.r->name;
- p->u.r->set = 'o'; /* don't create const segment entries */
- s[strlen(s) - 1] = '\0';
- create_1(nr, self, p);
- return self;
-}
-
static void
dump(nodeType *p, int l)
{
nodeType *child;
fprintf(stderr, "\n%*s", l*2, "");
- fprintf(stderr, "%s(", p->d);
+ fprintf(stderr, "%s(", p->description);
if (p->dump)
p->dump(p, l);
else {
- child = NODE0(p);
+ child = CHILD(p);
if (child)
dump(child, l + 1);
}
@@ -125,22 +101,35 @@
return p;
}
+/*
+ * vars and temps
+ */
static nodeType*
exp_Temp(Interp* interpreter, nodeType *p)
{
return p;
}
+static void
+dump_Var(nodeType *p, int l)
+{
+ fprintf(stderr, ":");
+ dump_sym(p);
+}
+
static nodeType *
new_temp(YYLTYPE *loc)
{
nodeType *p = new_node(loc);
p->expand = exp_Temp;
p->dump = dump_Var;
- p->d = "Temp";
+ p->description = "Temp";
return p;
}
+/*
+ * instruction insert helper function
+ */
static Instruction *
insINS(Parrot_Interp interpreter, IMC_Unit * unit, Instruction *ins,
const char *name, SymReg **regs, int n)
@@ -156,12 +145,15 @@
return tmp;
}
+/*
+ * new var, pmc_type := new P, Ic
+ */
static Instruction *
-insert_new(Interp* interpreter, nodeType *var, const char *name)
+insert_new(Interp* interpreter, nodeType *var, const char *pmy_type)
{
Instruction *ins;
SymReg *regs[IMCC_MAX_REGS], *r;
- int type = pmc_type(interpreter, const_string(interpreter, name));
+ int type = pmc_type(interpreter, const_string(interpreter, pmy_type));
char ireg[8];
ins = cur_unit->last_ins;
@@ -173,6 +165,9 @@
return insINS(interpreter, cur_unit, ins, "new", regs, 2);
}
+/*
+ * var = global "var" := find_global var, "var"
+ */
static Instruction *
insert_find_global(Interp* interpreter, nodeType *var)
{
@@ -189,6 +184,7 @@
regs[1] = r;
return insINS(interpreter, cur_unit, ins, "find_global", regs, 2);
}
+
/*
* node expand aka code creation functions
*/
@@ -199,6 +195,9 @@
return p;
}
+/*
+ * create code for a sequence of nodes likes chained statements
+ */
static nodeType*
exp_next(Interp* interpreter, nodeType *p)
{
@@ -210,24 +209,40 @@
return p;
}
+/*
+ * like above but expand child nodes
+ */
static nodeType*
exp_default(Interp* interpreter, nodeType *p)
{
nodeType *next;
- next = NODE0(p);
+ next = CHILD(p);
for (; next; next = next->next) {
if (next->expand)
next->expand(interpreter, next);
}
return p;
}
+/*
+ * statement nodes don't have a result
+ * expression nodes return the result node
+ *
+ * assign returns the rhs so that assignes can get chained together
+ * [Python] assign is a statement with possibly multiple LHS
+ * ast2past.py has converted multiple LHS to chained
+ * assignment operations so that this matches a more "natural"
+ * behavior.
+ */
+/*
+ * set var, rhs
+ */
static nodeType*
exp_Assign(Interp* interpreter, nodeType *p)
{
Instruction *ins;
SymReg *regs[IMCC_MAX_REGS], *r;
- nodeType *var = NODE0(p);
+ nodeType *var = CHILD(p);
nodeType *rhs = var->next;
rhs = rhs->expand(interpreter, rhs);
@@ -242,12 +257,22 @@
return rhs;
}
+/*
+ * TODO
+ */
static nodeType*
exp_Args(Interp* interpreter, nodeType *p)
{
return NULL;
}
+/*
+ * Op
+ * left
+ * right
+ *
+ * Op(opocde) has the bare name of the Parrot opcode
+ */
static nodeType*
exp_Binary(Interp* interpreter, nodeType *p)
{
@@ -255,13 +280,28 @@
Instruction *ins;
SymReg *regs[IMCC_MAX_REGS];
- op = NODE0(p);
+ op = CHILD(p);
left = op->next;
right = left->next;
+ /*
+ * first create code for left and right
+ */
left = left->expand(interpreter, left);
right = right->expand(interpreter, right);
+ /*
+ * then get the current instruction pointer
+ * and append the binary
+ */
ins = cur_unit->last_ins;
if (!p->dest) {
+ /*
+ * p->dest is currently unused - if the optimizer can figure out that
+ * the destination can get assigned directly, C<dest> will
+ * hold the destination of the binary
+ *
+ * else creae a temp of the same type as the left operand
+ * TODO check mixed types
+ */
dest = IMCC_new_temp_node(interpreter, left->u.r->set, &p->loc);
if (dest->u.r->set == 'P')
ins = insert_new(interpreter, dest, "Undef");
@@ -270,16 +310,31 @@
regs[0] = dest->u.r;
regs[1] = left->u.r;
regs[2] = right->u.r;
- insINS(interpreter, cur_unit, ins, NODE0(op)->u.r->name + 1, regs, 3);
+ insINS(interpreter, cur_unit, ins, op->u.r->name, regs, 3);
return dest;
}
+/*
+ * TODO
+ */
static nodeType*
exp_Defaults(Interp* interpreter, nodeType *p)
{
return NULL;
}
+/*
+ * Function
+ * Name
+ * Params
+ * Name
+ * ...
+ * Defaults
+ * ...
+ * py_var_args
+ * py_kw_args
+ * Body
+ */
static nodeType*
exp_Function(Interp* interpreter, nodeType *p)
{
@@ -290,7 +345,7 @@
cur_unit = p->unit;
- name = NODE0(p);
+ name = CHILD(p);
params = name->next;
body = params->next;
sub = mk_sub_address(str_dup(name->u.r->name));
@@ -306,12 +361,23 @@
return NULL; /* XXX return retval */
}
+/*
+ * TODO
+ */
static nodeType*
exp_Params(Interp* interpreter, nodeType *p)
{
return NULL;
}
+/*
+ * Py_Call
+ * function
+ * Args
+ * ...
+ * Star_Args
+ * DStar_Args
+ */
static nodeType*
exp_Py_Call(Interp* interpreter, nodeType *p)
{
@@ -319,14 +385,19 @@
Instruction *ins;
SymReg *regs[IMCC_MAX_REGS];
- name = NODE0(p);
+ name = CHILD(p);
args = name->next;
args = args->expand(interpreter, args);
+ /* TODO */
ins = IMCC_create_itcall_label(interpreter);
IMCC_itcall_sub(interpreter, name->u.r);
return NULL;
}
+/*
+ * Py_Local
+ * Name
+ */
static nodeType*
exp_Py_Local(Interp* interpreter, nodeType *var)
{
@@ -337,16 +408,20 @@
return NULL;
}
+/*
+ * this is the main init code
+ * Py_Module
+ * Py_doc,
+ * Py_local,
+ * ...
+ * Stmts
+ */
static nodeType*
exp_Py_Module(Interp* interpreter, nodeType *p)
{
nodeType *doc;
SymReg *sub;
Instruction *i;
- /*
- * this is the main init code
- * (Py_doc, Py_local, Stmts)
- */
if (!cur_unit)
fatal(1, "exp_Py_Module", "no cur_unit");
sub = mk_sub_address(str_dup("__main__"));
@@ -358,12 +433,17 @@
return exp_default(interpreter, p);
}
+/*
+ * Py_Print
+ * exp
+ * ...
+ */
static nodeType*
exp_Py_Print(Interp* interpreter, nodeType *p)
{
Instruction *ins ;
SymReg *regs[IMCC_MAX_REGS];
- nodeType * child = NODE0(p), *d;
+ nodeType * child = CHILD(p), *d;
if (!child)
fatal(1, "exp_Py_Print", "nothing to print");
for (; child; child = child->next) {
@@ -372,13 +452,17 @@
if (d->dump == dump_Const || d->dump == dump_Var)
regs[0] = d->u.r;
else
- fatal(1, "exp_Py_Print", "unknown node to print: '%s'", d->d);
+ fatal(1, "exp_Py_Print", "unknown node to print: '%s'",
+ d->description);
ins = cur_unit->last_ins;
insINS(interpreter, cur_unit, ins, "print_item", regs, 1);
}
return NULL;
}
+/*
+ * Py_Print_nl
+ */
static nodeType*
exp_Py_Print_nl(Interp* interpreter, nodeType *p)
{
@@ -388,12 +472,18 @@
return NULL;
}
+/*
+ * TODO
+ */
static nodeType*
exp_Src_File(Interp* interpreter, nodeType *p)
{
return exp_next(interpreter, p);
}
+/*
+ * TODO
+ */
static nodeType*
exp_Src_Lines(Interp* interpreter, nodeType *p)
{
@@ -425,7 +515,7 @@
{ "Defaults", create_1, exp_Defaults, NULL, NULL },
{ "Function", create_Func, exp_Function, NULL, NULL },
{ "Name", create_Name, NULL, NULL, NULL },
- { "Op", create_Op, NULL, NULL, dump_Op },
+ { "Op", create_Name, NULL, NULL, NULL },
{ "Params", create_1, exp_Params, NULL, NULL },
{ "Py_Call", create_1, exp_Py_Call, NULL, NULL },
{ "Py_Local", create_Name, exp_Py_Local, NULL, NULL },
@@ -440,6 +530,9 @@
#define CONST_NODE 5
};
+/*
+ * compare to entries pointing into ast_list
+ */
static int
ast_comp(const void *a, const void *b)
{
@@ -448,40 +541,55 @@
return strcmp(pa->name, pb->name);
}
+/*
+ * set function pointers for ast entry nr
+ */
static void
set_fptrs(nodeType *self, int nr)
{
- self->d = ast_list[nr].name;
+ self->description = ast_list[nr].name;
self->expand = ast_list[nr].expand;
self->opt = ast_list[nr].opt;
self->dump = ast_list[nr].dump;
}
+/*
+ * init a node w/o child
+ */
static nodeType*
-create_0(int nr, nodeType *self, nodeType *p)
+create_0(int nr, nodeType *self, nodeType *child)
{
set_fptrs(self, nr);
return self;
}
+/*
+ * init a node with child
+ */
static nodeType*
-create_1(int nr, nodeType *self, nodeType *p)
+create_1(int nr, nodeType *self, nodeType *child)
{
- if (p) {
- p->up = self;
- NODE0(self) = p;
+ if (child) {
+ child->parent= self;
+ CHILD(self) = child;
+ self->flags |= NODE_HAS_CHILD;
}
set_fptrs(self, nr);
return self;
}
+/*
+ * make a function node
+ * this sets the function symbol of the calle to VTADDRESS
+ * which is used to create the find_global of the caller
+ */
static nodeType*
-create_Func(int nr, nodeType *self, nodeType *p)
+create_Func(int nr, nodeType *self, nodeType *child)
{
SymReg *r;
IMC_Unit *last;
- self = create_1(nr, self, p);
- r = p->u.r;
+ self = create_1(nr, self, child);
+ r = child->u.r;
last = cur_unit->prev; /* XXX ->caller */
r = _get_sym(last->hash, r->name);
/* mark the name being a subroutine name
@@ -491,13 +599,19 @@
return self;
}
+/*
+ * don't need Name(Var(foo))
+ * so copy things over and return just one node, free the other
+ */
static nodeType*
-create_Name(int nr, nodeType *self, nodeType *p)
+create_Name(int nr, nodeType *self, nodeType *child)
{
- p->d = ast_list[nr].name;
- p->expand = ast_list[nr].expand;
+ child->description = ast_list[nr].name;
+ child->expand = ast_list[nr].expand;
+ if (!child->expand)
+ child->expand = exp_Var;
mem_sys_free(self);
- return p;
+ return child;
}
/*
* API
@@ -507,7 +621,8 @@
=item C<int IMCC_find_node_type(const char *name)>
-Returns the index in C<ast_list> of the given node name. The node must exist.
+Returns the index in C<ast_list> of the given node name or 0 if the node name
+doesn't exist.
=cut
@@ -533,7 +648,8 @@
/*
-=item C<nodeType *IMCC_new_const_node(Interp*, char *name, int set, YYLTYPE *loc)>
+=item C<nodeType *IMCC_new_const_node(Interp*, char *name, int set,
+YYLTYPE *loc)>
Create a new Const node for the given constant C<name> and Parrot register
set C<set>. Set may be:
@@ -542,6 +658,13 @@
'N' ... Number
'S' ... String
'U' ... String treated as unicode string
+ 'P' ... PMCs (PMC constants aren't finished)
+ 'o' ... opcode name
+
+=item C<nodeType *IMCC_new_var_node(Interp*, char *name, int set,
+YYLTYPE *loc)>
+
+Create a new variable node.
=item C<nodeType *IMCC_new_temp_node(Interp*, int set, YYLTYPE *loc)>
@@ -571,7 +694,7 @@
if (r->type != VTADDRESS)
r->type = VTIDENTIFIER;
p->expand = exp_Var;
- p->d = "Var";
+ p->description = "Var";
p->dump = dump_Var;
return p;
}
@@ -673,11 +796,8 @@
IMCC_free_nodes(Interp* interpreter, nodeType *p)
{
nodeType *child, *next, *dest;
- if (p->dump == dump_Const || p->dump == dump_Var)
- ;
- else {
- child = NODE0(p);
- if (child)
+ if (p->flags & NODE_HAS_CHILD) {
+ child = CHILD(p);
IMCC_free_nodes(interpreter, child);
}
dest = p->dest;
1.5 +14 -6 parrot/languages/python/ast2past.py
Index: ast2past.py
===================================================================
RCS file: /cvs/public/parrot/languages/python/ast2past.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -w -r1.4 -r1.5
--- ast2past.py 3 Aug 2004 11:21:36 -0000 1.4
+++ ast2past.py 4 Aug 2004 07:48:47 -0000 1.5
@@ -156,7 +156,7 @@
# .left .right
self.set_lineno(node)
self.begin("Binary(")
- self.append("Op('%s')" % op)
+ self.append("Op(%s)" % op)
self.visit(node.left)
self.visit(node.right)
self.end(") # Binary")
@@ -170,7 +170,7 @@
l = nodes[0]
r = nodes[1]
if op:
- self.append("Op(%s)" % `op`)
+ self.append("Op(%s)" % op)
self.visit(l)
n = nodes[1:]
if len(n) >= 2:
@@ -280,10 +280,14 @@
self.begin("Star_Args(")
self.visit(node.star_args)
self.end(") # StarArgs")
+ else:
+ self.append("_()")
if node.dstar_args:
self.begin("DStar_Args(")
self.visit(node.dstar_args)
self.end(") # DStarArgs")
+ else:
+ self.append("_()")
self.end(") # Py_Call")
def visitClass(self, node):
@@ -397,8 +401,12 @@
self.end(") # Defaults")
if node.varargs:
self.append("py_var_args(1)")
+ else:
+ self.append("_()")
if node.kwargs:
self.append("py_kw_args(1)")
+ else:
+ self.append("_()")
self.end(") # Params")
self.find_locals(node.code)
self.visit(node.code)
@@ -453,7 +461,7 @@
# .expr
self.set_lineno(node)
self.begin("Unary(")
- self.append("Op('~')")
+ self.append("Op(bnot)")
self.visit(node.expr)
self.end(") # Unary")
@@ -515,7 +523,7 @@
# .expr
self.set_lineno(node)
self.begin("Unary(")
- self.append("Op('!')")
+ self.append("Op(not)")
self.visit(node.expr)
self.end(") # Unary")
@@ -649,7 +657,7 @@
# .expr
self.set_lineno(node)
self.begin("Unary(")
- self.append("Op('+')")
+ self.append("Op(add)")
self.visit(node.expr)
self.end(") # UnaryAdd")
@@ -657,7 +665,7 @@
# .expr
self.set_lineno(node)
self.begin("Unary(")
- self.append("Op('-')")
+ self.append("Op(sub)")
self.visit(node.expr)
self.end(") # UnarySub")