cvsuser 04/08/02 03:33:08
Modified: ast ast.h ast.y ast_main.c astparser.c hello.past
node.c
languages/python ast2past.py
Log:
ast 3 - binary node; use opnames for binary
Revision Changes Path
1.4 +10 -6 parrot/ast/ast.h
Index: ast.h
===================================================================
RCS file: /cvs/public/parrot/ast/ast.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -w -r1.3 -r1.4
--- ast.h 1 Aug 2004 16:10:12 -0000 1.3
+++ ast.h 2 Aug 2004 10:33:04 -0000 1.4
@@ -27,8 +27,9 @@
context_type up_ctx; /* ctx coming from upper */
context_type ctx;
const char* d;
- struct nodeType_t *up;
- struct nodeType_t *next;
+ struct nodeType_t *up; /* parent */
+ struct nodeType_t *next; /* next statement */
+ struct nodeType_t *dest; /* destination or result */
YYLTYPE loc;
union {
struct _SymReg *r;
@@ -57,12 +58,15 @@
#endif
-nodeType * IMCC_new_const_node(char *name, int set, YYLTYPE *loc);
-nodeType * IMCC_new_node(int nr, nodeType *child, YYLTYPE *loc);
-nodeType * IMCC_append_node(nodeType *head, nodeType *tail, YYLTYPE *loc);
+nodeType * IMCC_new_const_node(Interp*, char *name, int set, YYLTYPE *loc);
+nodeType * IMCC_new_node(Interp*, int nr, nodeType *child, YYLTYPE *loc);
+nodeType * IMCC_append_node(Interp*, nodeType *head, nodeType *tail,
+ YYLTYPE *loc);
+nodeType * IMCC_new_temp_node(Interp*, int set, YYLTYPE *loc);
void IMCC_dump_nodes(nodeType *);
-void IMCC_expand_nodes(Interp*, nodeType *);
+nodeType * IMCC_expand_nodes(Interp*, nodeType *);
+void IMCC_free_nodes(Interp*, nodeType *);
int IMCC_find_node_type(const char *name);
1.2 +6 -6 parrot/ast/ast.y
Index: ast.y
===================================================================
RCS file: /cvs/public/parrot/ast/ast.y,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- ast.y 31 Jul 2004 10:03:33 -0000 1.1
+++ ast.y 2 Aug 2004 10:33:04 -0000 1.2
@@ -62,18 +62,18 @@
| error { pr_error(@1, "Bug"); YYABORT; }
;
nodes: node
- | nodes node { $$ = IMCC_append_node($1, $2, &@1); }
+ | nodes node { $$ = IMCC_append_node(interp, $1, $2, &@1); }
| term
;
-node: IDENTIFIER '(' nodes ')' { $$ = IMCC_new_node($1, $3, &@1); }
+node: IDENTIFIER '(' nodes ')' { $$ = IMCC_new_node(interp, $1, $3, &@1); }
;
term: /* empty */ { $$ = 0; }
- | STRINGC { $$ = IMCC_new_const_node($1, 'S', &@1); }
- | INTC { $$ = IMCC_new_const_node($1, 'I', &@1); }
- | FLOATC { $$ = IMCC_new_const_node($1, 'N', &@1); }
- | USTRINGC { $$ = IMCC_new_const_node($1, 'U', &@1); }
+ | STRINGC { $$ = IMCC_new_const_node(interp, $1, 'S', &@1); }
+ | INTC { $$ = IMCC_new_const_node(interp, $1, 'I', &@1); }
+ | FLOATC { $$ = IMCC_new_const_node(interp, $1, 'N', &@1); }
+ | USTRINGC { $$ = IMCC_new_const_node(interp, $1, 'U', &@1); }
;
%%
1.2 +3 -2 parrot/ast/ast_main.c
Index: ast_main.c
===================================================================
RCS file: /cvs/public/parrot/ast/ast_main.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- ast_main.c 1 Aug 2004 16:10:12 -0000 1.1
+++ ast_main.c 2 Aug 2004 10:33:04 -0000 1.2
@@ -1,6 +1,6 @@
/*
Copyright: 2001-2004 The Perl Foundation. All Rights Reserved.
-$Id: ast_main.c,v 1.1 2004/08/01 16:10:12 leo Exp $
+$Id: ast_main.c,v 1.2 2004/08/02 10:33:04 leo Exp $
=head1 NAME
@@ -49,8 +49,9 @@
{
ASTin = fp;
ASTparse(interpreter);
+ top_node = IMCC_expand_nodes(interpreter, top_node);
/* IMCC_dump_nodes(top_node); TODO commandline switches */
- IMCC_expand_nodes(interpreter, top_node);
+ IMCC_free_nodes(interpreter, top_node);
}
static void
1.4 +6 -6 parrot/ast/astparser.c
Index: astparser.c
===================================================================
RCS file: /cvs/public/parrot/ast/astparser.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -w -r1.3 -r1.4
--- astparser.c 1 Aug 2004 16:10:12 -0000 1.3
+++ astparser.c 2 Aug 2004 10:33:04 -0000 1.4
@@ -997,12 +997,12 @@
case 5:
#line 65 "ast/ast.y"
- { yyval.n = IMCC_append_node(yyvsp[-1].n, yyvsp[0].n, &yylsp[-1]); }
+ { yyval.n = IMCC_append_node(interp, yyvsp[-1].n, yyvsp[0].n, &yylsp[-1]); }
break;
case 7:
#line 69 "ast/ast.y"
- { yyval.n = IMCC_new_node(yyvsp[-3].t, yyvsp[-1].n, &yylsp[-3]); }
+ { yyval.n = IMCC_new_node(interp, yyvsp[-3].t, yyvsp[-1].n, &yylsp[-3]); }
break;
case 8:
@@ -1012,22 +1012,22 @@
case 9:
#line 73 "ast/ast.y"
- { yyval.n = IMCC_new_const_node(yyvsp[0].s, 'S', &yylsp[0]); }
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'S', &yylsp[0]); }
break;
case 10:
#line 74 "ast/ast.y"
- { yyval.n = IMCC_new_const_node(yyvsp[0].s, 'I', &yylsp[0]); }
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'I', &yylsp[0]); }
break;
case 11:
#line 75 "ast/ast.y"
- { yyval.n = IMCC_new_const_node(yyvsp[0].s, 'N', &yylsp[0]); }
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'N', &yylsp[0]); }
break;
case 12:
#line 76 "ast/ast.y"
- { yyval.n = IMCC_new_const_node(yyvsp[0].s, 'U', &yylsp[0]); }
+ { yyval.n = IMCC_new_const_node(interp, yyvsp[0].s, 'U', &yylsp[0]); }
break;
1.2 +3 -2 parrot/ast/hello.past
Index: hello.past
===================================================================
RCS file: /cvs/public/parrot/ast/hello.past,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- hello.past 1 Aug 2004 16:10:12 -0000 1.1
+++ hello.past 2 Aug 2004 10:33:04 -0000 1.2
@@ -1,8 +1,9 @@
-# generated by pie-rate on Sun Aug 1 17:59:03 2004
+# generated by ast2past.py on Mon Aug 2 07:40:16 2004
-Src_File("f.py")
+Src_File("hello.py")
Py_Module(
+ _()
Stmts(
Py_Print(
Const('Hello PAST')
1.5 +164 -31 parrot/ast/node.c
Index: node.c
===================================================================
RCS file: /cvs/public/parrot/ast/node.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -w -r1.4 -r1.5
--- node.c 1 Aug 2004 16:10:12 -0000 1.4
+++ node.c 2 Aug 2004 10:33:04 -0000 1.5
@@ -1,6 +1,6 @@
/*
Copyright: 2001-2004 The Perl Foundation. All Rights Reserved.
-$Id: node.c,v 1.4 2004/08/01 16:10:12 leo Exp $
+$Id: node.c,v 1.5 2004/08/02 10:33:04 leo Exp $
=head1 NAME
@@ -24,6 +24,9 @@
#include "ast.h"
#include "../imcc/imc.h"
+static nodeType* create_0(int nr, nodeType *self, nodeType *p);
+static nodeType* create_1(int nr, nodeType *self, nodeType *p);
+
static void
pr(nodeType *p)
{
@@ -33,7 +36,20 @@
}
static void
-const_dump(nodeType *p, int l)
+dump_Const(nodeType *p, int l)
+{
+ pr(p);
+}
+
+static void
+dump_Op(nodeType *p, int l)
+{
+ pr(NODE0(p));
+ printf("'");
+}
+
+static void
+dump_Var(nodeType *p, int l)
{
pr(p);
}
@@ -49,11 +65,23 @@
{
p->expand = exp_Const;
/* p->context = const_context; */
- p->dump = const_dump;
+ p->dump = dump_Const;
p->opt = NULL;
p->d = "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 segmenet entries */
+ s[strlen(s) - 1] = '\0';
+ create_1(nr, self, p);
+ return self;
+}
+
static void
dump(nodeType *p, int l)
{
@@ -68,7 +96,7 @@
dump(child, l + 1);
}
printf(")");
- if (!p->dump && p->next )
+ if (p->next )
dump(p->next, l);
}
@@ -88,11 +116,33 @@
return p;
}
+static nodeType*
+exp_Temp(Interp* interpreter, nodeType *p)
+{
+ return p;
+}
+
+static nodeType *
+new_temp(YYLTYPE *loc)
+{
+ nodeType *p = new_node(loc);
+ p->expand = exp_Temp;
+ p->dump = dump_Var;
+ p->d = "Temp";
+ return p;
+}
+
static Instruction *
insINS(Parrot_Interp interpreter, IMC_Unit * unit, Instruction *ins,
const char *name, SymReg **regs, int n)
{
- Instruction *tmp = INS(interpreter, unit, name, NULL, regs, n, 0, 0);
+ Instruction *tmp;
+ union {
+ const void * __c_ptr;
+ void * __ptr;
+ } __ptr_u;
+ char *s = const_cast(name);
+ tmp = INS(interpreter, unit, s, NULL, regs, n, 0, 0);
insert_ins(unit, ins, tmp);
return tmp;
}
@@ -129,10 +179,36 @@
}
static nodeType*
+exp_Binary(Interp* interpreter, nodeType *p)
+{
+ nodeType *op, *left, *right, *dest;
+ IMC_Unit *unit;
+ Instruction *ins;
+ SymReg *regs[IMCC_MAX_REGS];
+
+ op = NODE0(p);
+ left = op->next;
+ right = left->next;
+ left = left->expand(interpreter, left);
+ right = right->expand(interpreter, right);
+ unit = interpreter->imc_info->last_unit;
+ ins = unit->last_ins;
+ dest = IMCC_new_temp_node(interpreter, left->u.r->set, &p->loc);
+ p->dest = dest;
+ regs[0] = dest->u.r;
+ regs[1] = left->u.r;
+ regs[2] = right->u.r;
+ insINS(interpreter, unit, ins, NODE0(op)->u.r->name + 1, regs, 3);
+ return dest;
+}
+
+static nodeType*
exp_Py_Module(Interp* interpreter, nodeType *p)
{
+ nodeType *doc;
/*
* this is the main init code
+ * (Py_doc, Stmts)
*/
IMC_Unit *unit = cur_unit = imc_open_unit(interpreter, IMC_PCCSUB);
SymReg *sub = mk_sub_address(str_dup("__main__"));
@@ -142,28 +218,34 @@
add_namespace(interpreter, i->r[1]);
i->r[1]->pcc_sub->pragma = P_MAIN|P_PROTOTYPED ;
/*
- * Py_Module has a child node which are statements
+ * Py_Module has a Py_doc node and Stmts
*/
- return exp_default(interpreter, p);
+ doc = NODE0(p);
+ /* TODO */
+ return exp_default(interpreter, doc);
}
static nodeType*
exp_Py_Print(Interp* interpreter, nodeType *p)
{
- IMC_Unit *unit = interpreter->imc_info->last_unit;
- Instruction *ins = unit->last_ins;
+ IMC_Unit *unit;
+ Instruction *ins;
SymReg *regs[IMCC_MAX_REGS];
- nodeType * child = NODE0(p);
+ nodeType * child = NODE0(p), *d;
if (!child)
fatal(1, "exp_Py_Print", "nothing to print");
- child = child->expand(interpreter, child);
+ for (; child; child = child->next) {
+ d = child->expand(interpreter, child);
+ unit = interpreter->imc_info->last_unit;
+ ins = unit->last_ins;
/* TODO file handle node */
- if (child->expand == exp_Const || child->expand == exp_Var)
- regs[0] = child->u.r;
+ if (d->expand == exp_Const || d->expand == exp_Var ||
+ d->expand == exp_Temp)
+ regs[0] = d->u.r;
else
- fatal(1, "exp_Py_Print", "unknown node to print");
-
- insINS(interpreter, unit, ins, "print_item", regs, 1);
+ fatal(1, "exp_Py_Print", "unknown node to print: '%s'", d->d);
+ ins = insINS(interpreter, unit, ins, "print_item", regs, 1);
+ }
return exp_next(interpreter, p);
}
@@ -190,8 +272,6 @@
}
-static nodeType* create_0(int nr, nodeType *self, nodeType *p);
-static nodeType* create_1(int nr, nodeType *self, nodeType *p);
typedef struct {
const char *name;
@@ -208,14 +288,17 @@
static node_names ast_list[] = {
{ "-no-node-", NULL, NULL, NULL, NULL },
- { "Const", NULL, exp_Const, NULL, const_dump },
-#define CONST_NODE 1
+ { "Binary", create_1, exp_Binary, NULL, NULL },
+ { "Const", NULL, exp_Const, NULL, dump_Const },
+#define CONST_NODE 2
+ { "Op", create_Op, NULL, NULL, dump_Op },
{ "Py_Module", create_1, exp_Py_Module, NULL, NULL },
{ "Py_Print" , create_1, exp_Py_Print, NULL, NULL },
{ "Py_Print_nl", create_0, exp_Py_Print_nl, NULL, NULL },
{ "Src_File", create_1, exp_Src_File, NULL, NULL },
{ "Src_Line", create_1, exp_Src_Lines, NULL, NULL },
- { "Stmts", create_1, exp_default, NULL, NULL }
+ { "Stmts", create_1, exp_default, NULL, NULL },
+ { "_", create_0, exp_next, NULL, NULL }
};
static int
@@ -287,7 +370,7 @@
/*
-=item C<nodeType *IMCC_new_const_node(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:
@@ -297,12 +380,16 @@
'S' ... String
'U' ... String treated as unicode string
+=item C<nodeType *IMCC_new_temp_node(Interp*, int set, YYLTYPE *loc)>
+
+Create a new temporary node.
+
=cut
*/
nodeType *
-IMCC_new_const_node(char *name, int set, YYLTYPE *loc)
+IMCC_new_const_node(Interp* interp, char *name, int set, YYLTYPE *loc)
{
nodeType *p = new_con(loc);
SymReg *r = mk_const(name, set);
@@ -310,9 +397,22 @@
return p;
}
+nodeType *
+IMCC_new_temp_node(Interp* interp, int set, YYLTYPE *loc)
+{
+ nodeType *p = new_temp(loc);
+ SymReg *r;
+ char buf[128];
+ static int temp;
+ sprintf(buf, "$%c%d", set, ++temp);
+ r = mk_symreg(str_dup(buf), set);
+ p->u.r = r;
+ return p;
+}
+
/*
-=item C<nodeType *IMCC_new_node(int nr, nodeType *child, YYLTYPE *loc)>
+=item C<nodeType *IMCC_new_node(Interp*, int nr, nodeType *child, YYLTYPE *loc)>
Create a new node with node type number C<nr> and the given child.
@@ -321,7 +421,7 @@
*/
nodeType *
-IMCC_new_node(int nr, nodeType *child, YYLTYPE *loc)
+IMCC_new_node(Interp* interp, int nr, nodeType *child, YYLTYPE *loc)
{
nodeType * n;
if (nr == CONST_NODE) {
@@ -333,7 +433,8 @@
/*
-=item C<nodeType * IMCC_append_node(nodeType *a, nodeType *b, YYLTYPE *loc)>
+=item C<nodeType *
+IMCC_append_node(Interp *, nodeType *a, nodeType *b, YYLTYPE *loc)>
Append node C<b> add the end of C<a> and return C<a>.
@@ -343,7 +444,7 @@
nodeType *
-IMCC_append_node(nodeType *a, nodeType *b, YYLTYPE *loc)
+IMCC_append_node(Interp *interpreter, nodeType *a, nodeType *b, YYLTYPE *loc)
{
nodeType *last = a;
/*
@@ -362,6 +463,15 @@
Dump nodes to C<stdout>
+=item C<void IMCC_expand_nodes(Interp* interpreter, nodeType *p)>
+
+Create code for the passed node. The code is appended to the current
+unit's instructions.
+
+=item C<void IMCC_free_nodes(Interp* interpreter, nodeType *p)>
+
+Free the nodes.
+
=cut
*/
@@ -373,11 +483,34 @@
printf("\n");
}
-void
+nodeType *
IMCC_expand_nodes(Interp* interpreter, nodeType *p)
{
- p->expand(interpreter, p);
+ return p->expand(interpreter, p);
+}
+
+void
+IMCC_free_nodes(Interp* interpreter, nodeType *p)
+{
+ nodeType *child, *next, *dest;
+ if (p->expand == exp_Const ||
+ p->expand == exp_Temp ||
+ p->expand == exp_Var)
+ ;
+ else {
+ child = NODE0(p);
+ if (child)
+ IMCC_free_nodes(interpreter, child);
+ }
+ dest = p->dest;
+ if (dest && dest->expand == exp_Temp)
+ mem_sys_free(dest);
+ next = p->next;
+ mem_sys_free(p);
+ if (next)
+ IMCC_free_nodes(interpreter, next);
}
+
/*
=back
1.2 +14 -12 parrot/languages/python/ast2past.py
Index: ast2past.py
===================================================================
RCS file: /cvs/public/parrot/languages/python/ast2past.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- ast2past.py 31 Jul 2004 09:10:53 -0000 1.1
+++ ast2past.py 2 Aug 2004 10:33:08 -0000 1.2
@@ -162,7 +162,7 @@
self.end(") # Binary")
def visitAdd(self, node):
- self.binary(node, '+')
+ self.binary(node, 'add')
def expr_helper(self, nodes, txt, op=None):
"""Convert the lhs tuple to a series of expressions"""
@@ -248,17 +248,17 @@
def visitBitand(self, node):
# .nodes
self.set_lineno(node)
- self.expr_helper(node.nodes, "Binary", '&')
+ self.expr_helper(node.nodes, "Binary", 'band')
def visitBitor(self, node):
# .nodes
self.set_lineno(node)
- self.expr_helper(node.nodes, "Binary", '|')
+ self.expr_helper(node.nodes, "Binary", 'bor')
def visitBitxor(self, node):
# .nodes
self.set_lineno(node)
- self.expr_helper(node.nodes, "Binary", '^')
+ self.expr_helper(node.nodes, "Binary", 'bxor')
def visitBreak(self, node):
self.set_lineno(node)
@@ -341,7 +341,7 @@
def visitDiv(self, node):
# .left .right
- self.binary(node, '/')
+ self.binary(node, 'div')
def visitEllipsis(self, node):
self.set_lineno(node)
@@ -467,7 +467,7 @@
self.function(node, 1)
def visitLeftShift(self, node):
- self.binary(node, '<<')
+ self.binary(node, 'shl')
def visitList(self, node):
# .nodes
@@ -485,7 +485,7 @@
assert(0) # TODO
def visitMod(self, node):
- self.binary(node, '%')
+ self.binary(node, 'mod')
def visitModule(self, node):
self.set_lineno(node)
@@ -493,12 +493,14 @@
self.begin("Py_Module(")
if node.doc:
self.append("Py_doc(" + `node.doc` + ")")
+ else:
+ self.append("_()")
self.find_locals(node.node)
self.visit(node.node)
self.end(") # Py_Module")
def visitMul(self, node):
- self.binary(node, '*')
+ self.binary(node, 'mul')
def visitName(self, node):
# .name
@@ -521,7 +523,7 @@
pass
def visitPow(self, node):
- self.binary(node, '**')
+ self.binary(node, 'pow')
def visitPrint(self, node):
# .nodes .dest
@@ -561,7 +563,7 @@
self.end(") # Return")
def visitRightShift(self, node):
- self.binary(node, '>>')
+ self.binary(node, 'shr')
def visitSlice(self, node):
# .expr .flags .lower .upper
@@ -591,7 +593,7 @@
self.end(") # Stmts")
def visitSub(self, node):
- self.binary(node, '-')
+ self.binary(node, 'sub')
def visitSubscript(self, node):
# .expr .flags .subs
@@ -680,7 +682,7 @@
import time
HEAD=\
'''
-# generated by pie-rate on %s
+# generated by ast2past.py on %s
''' % time.asctime()
def compile(src, fn, name="__main__", dump=False, debug=False,