Skip to content

Commit

Permalink
Make built-in print statement accept multiple arguments. Add a test f…
Browse files Browse the repository at this point in the history
…or this as well. Fix up order of arguments in AST.
  • Loading branch information
kjs committed Jun 16, 2012
1 parent cf042f0 commit 037f2af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/gencode.c
Expand Up @@ -1554,17 +1554,20 @@ gencode_print(M1_compiler *comp, m1_expression *expr) {
m1_reg reg;
m1_reg one;

gencode_expr(comp, expr);
m1_expression *iter = expr;

reg = popreg(comp->regstack);

/* register to hold value "1" */
one = alloc_reg(comp, VAL_INT);

fprintf(OUT, "\tset_imm\tI%d, 0, 1\n", one.no);
fprintf(OUT, "\tprint_%c\tI%d, %c%d, x\n", type_chars[(int)reg.type], one.no,
reg_chars[(int)reg.type], reg.no);


while (iter != NULL) {
gencode_expr(comp, iter);
reg = popreg(comp->regstack);

fprintf(OUT, "\tprint_%c\tI%d, %c%d, x\n", type_chars[(int)reg.type], one.no,
reg_chars[(int)reg.type], reg.no);
iter = iter->next;
}
free_reg(comp, one);
free_reg(comp, reg);
}
Expand Down
9 changes: 6 additions & 3 deletions src/m1.y
Expand Up @@ -666,7 +666,7 @@ catch_block : "catch" '(' TK_IDENT ')' block
throw_stat : "throw" expression ';'
;

print_stat : "print" '(' expression ')' ';'
print_stat : "print" '(' arguments ')' ';'
{ $$ = printexpr((M1_compiler *)yyget_extra(yyscanner), $3); }
;

Expand Down Expand Up @@ -828,8 +828,11 @@ expr_list : expression
| expr_list ',' expression
{
/* link them in reverse order for now. */
$3->next = $1;
$$ = $3;
m1_expression *iter = $1;
while (iter->next != NULL)
iter = iter->next;
iter->next = $3;
$$ = $1;
}
;

Expand Down
3 changes: 3 additions & 0 deletions t/multiprint.m1
@@ -0,0 +1,3 @@
void main() {
print("1..3\n", "ok 1\n", "ok 2\n", "ok 3\n");
}

0 comments on commit 037f2af

Please sign in to comment.