cvsuser 04/08/04 09:27:48
Modified: ast node.c
languages/python/t/basic 02_expr.t
Log:
ast 16 - create code for If
Revision Changes Path
1.17 +67 -1 parrot/ast/node.c
Index: node.c
===================================================================
RCS file: /cvs/public/parrot/ast/node.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -w -r1.16 -r1.17
--- node.c 4 Aug 2004 15:39:21 -0000 1.16
+++ node.c 4 Aug 2004 16:27:44 -0000 1.17
@@ -1,6 +1,6 @@
/*
Copyright: 2001-2004 The Perl Foundation. All Rights Reserved.
-$Id: node.c,v 1.16 2004/08/04 15:39:21 leo Exp $
+$Id: node.c,v 1.17 2004/08/04 16:27:44 leo Exp $
=head1 NAME
@@ -516,6 +516,70 @@
return NULL; /* XXX return retval */
}
+static SymReg*
+gen_label(Interp *interpreter, const char *prefix)
+{
+ static int nr;
+ char buf[128];
+
+ sprintf(buf, "%s_%d", prefix, ++nr);
+ return mk_local_label(cur_unit, str_dup(buf));
+}
+
+/*
+ * If
+ * Tests if foo
+ * test
+ * stmts
+ * ... else if bar
+ * Else_ else
+ */
+static nodeType*
+exp_If(Interp* interpreter, nodeType *p)
+{
+ nodeType *tests, *else_, *test, *stmts, *true_;
+ SymReg *else_label, *endif_label;
+ Instruction *ins;
+ SymReg *regs[IMCC_MAX_REGS];
+
+ tests = CHILD(p);
+ else_ = tests->next;
+ endif_label = gen_label(interpreter, "endif");
+ for (test = CHILD(tests); test; test = stmts->next) {
+ stmts = test->next;
+ else_label = gen_label(interpreter, "else");
+ /*
+ * TODO if test == Compare create compare ops
+ */
+ true_ = test->expand(interpreter, test);
+ ins = cur_unit->last_ins;
+ regs[0] = true_->u.r;
+ regs[1] = else_label;
+ insINS(interpreter, cur_unit, ins, "unless", regs, 2);
+ /*
+ * statements of this test
+ */
+ stmts->expand(interpreter, stmts);
+ /*
+ * branch past all
+ */
+ ins = cur_unit->last_ins;
+ regs[0] = endif_label;
+ insINS(interpreter, cur_unit, ins, "branch", regs, 1);
+ /*
+ * insert label for next test
+ */
+ INS_LABEL(cur_unit, else_label, 1);
+ }
+ /* final else block if present */
+ if (else_ && else_->expand)
+ else_->expand(interpreter, else_);
+ /* endif label */
+ INS_LABEL(cur_unit, endif_label, 1);
+
+ return NULL;
+}
+
/*
* TODO
*/
@@ -651,6 +715,7 @@
{ "Const", NULL, exp_Const, NULL, dump_Const, ctx_Const },
{ "Defaults", create_1, exp_Defaults, NULL, NULL, NULL },
{ "Function", create_Func, exp_Function, NULL, NULL, NULL },
+ { "If", create_1, exp_If, NULL, NULL, NULL },
{ "Line_no", create_1, NULL, NULL, NULL, NULL },
{ "Name", create_Name, NULL, NULL, NULL, ctx_Var },
{ "Op", create_Name, NULL, NULL, NULL, NULL },
@@ -664,6 +729,7 @@
{ "Src_File", create_1, NULL, NULL, NULL, NULL },
{ "Src_Line", create_1, NULL, NULL, NULL, NULL },
{ "Stmts", create_1, exp_default, NULL, NULL, NULL },
+ { "Tests", create_1, NULL, NULL, NULL, NULL },
{ "Void", create_1, exp_default, NULL, NULL, NULL },
{ "_", create_0, NULL, NULL, NULL, NULL },
{ "_options", create_1, NULL, NULL, NULL, NULL },
1.11 +23 -2 parrot/languages/python/t/basic/02_expr.t
Index: 02_expr.t
===================================================================
RCS file: /cvs/public/parrot/languages/python/t/basic/02_expr.t,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -w -r1.10 -r1.11
--- 02_expr.t 4 Aug 2004 09:00:41 -0000 1.10
+++ 02_expr.t 4 Aug 2004 16:27:48 -0000 1.11
@@ -1,9 +1,9 @@
-# $Id: 02_expr.t,v 1.10 2004/08/04 09:00:41 leo Exp $
+# $Id: 02_expr.t,v 1.11 2004/08/04 16:27:48 leo Exp $
use strict;
use lib '../../lib';
-use Parrot::Test tests => 13;
+use Parrot::Test tests => 16;
sub test {
language_output_is('python', $_[0], '', $_[1]);
@@ -65,6 +65,27 @@
print "ok"
CODE
+test( <<'CODE', 'simple if' );
+if 1:
+ print "ok"
+CODE
+
+test( <<'CODE', 'simple if, elif' );
+if 0:
+ print "nok"
+elif 1:
+ print "ok"
+CODE
+
+test( <<'CODE', 'simple if, elif, else' );
+if 0:
+ print "nok"
+elif 0:
+ print "nok"
+else:
+ print "ok"
+CODE
+
SKIP: {
skip("Not yet", 3);