long long ast_asint(AstNode* node);
double ast_asreal(AstNode* node);
-AstNode* ast_binop(int op, AstNode* left, AstNode* right);
-AstNode* ast_unop(int op, AstNode* operand);
+AstNode* ast_op(int op, AstNode* left, AstNode* right);
AstNode* ast_store(AstNode* dest, AstNode* value);
AstNode* ast_fieldref(Parser* p, AstNode* record, char* fname);
AstNode* ast_index(Parser* p, AstNode* array, AstNode* index);
return ((AstValue*)node)->val.f;
}
-AstNode* ast_binop(int op, AstNode* left, AstNode* right)
+static AstNode* ast_binop(int op, AstNode* left, AstNode* right)
{
assert(left);
assert(right);
return ret;
}
-AstNode* ast_unop(int op, AstNode* operand)
+static AstNode* ast_unop(int op, AstNode* operand)
{
assert(operand);
AstNode* ret = NULL;
return ret;
}
+AstNode* ast_op(int op, AstNode* left, AstNode* right)
+{
+ return (right
+ ? ast_binop(op, left, right)
+ : ast_unop(op, left));
+}
+
AstNode* ast_store(AstNode* dest, AstNode* value)
{
/* TODO: validate left-hand side is assignable */
case IDENT:
{
Symbol* s = symbol_getbyid(p, ((AstValue*)node)->val.i);
- printf("S:%s", s->name);
+ printf("%s.%lld", s->name, ((AstValue*)node)->tag);
}
break;
case '.':
- printf("(field-ref)");
+ printf("(field-ref\n");
+ print(p, node->links[0], indent+1);
+ print(p, node->links[1], indent+1);
+ print_indent(indent, ")");
break;
case '[':
- printf("(array-index)");
+ printf("(array-index\n");
+ print(p, node->links[0], indent+1);
+ print(p, node->links[1], indent+1);
+ print_indent(indent, ")");
break;
case BEGIN:
case NOT:
consume(p);
- expr = ast_unop(NOT, factor(p));
+ expr = ast_op(NOT, factor(p), NULL);
break;
case IDENT:
case '/':
case '%':
check_nums(p, expr, right);
- expr = ast_binop(op, expr, right);
+ expr = ast_op(op, expr, right);
break;
case AND:
check_bools(p, expr, right);
- expr = ast_binop(op, expr, right);
+ expr = ast_op(op, expr, right);
break;
default:
int op = consume(p);
AstNode* operand = term(p);
check_num(p, operand);
- expr = ast_unop(op, operand);
+ expr = ast_op(op, operand, NULL);
}
else
{
{
check_nums(p, expr, right);
}
- expr = ast_binop(op, expr, right);
+ expr = ast_op(op, expr, right);
}
EXIT_RULE();
int op = consume(p);
AstNode* right = simple_expr(p);
check_nums(p, expr, right);
- expr = ast_binop(op, expr, right);
+ expr = ast_op(op, expr, right);
}
EXIT_RULE();
long long length = ast_asint(lnode);
expect(p, OF);
Type* base = type(p);
-// ret = calloc(1, sizeof(Type));
ret = symbol_newtype(p);
ret->form = FORM_ARRAY;
ret->size = length;
else if (accept(p, RECORD))
{
long offset = 0;
-// ret = calloc(1, sizeof(Type));
ret = symbol_newtype(p);
ret->form = FORM_RECORD;
expect(p, END);
symbol_closescope(p, scope);
-// ast_print(p, proc->value);
+ ast_print(p, proc->value);
(void)proc->value;
EXIT_RULE();
if (accept(p, BEGIN))
{
AstNode* block = statement_seq(p);
-// ast_print(p, block);
+ ast_print(p, block);
(void)block;
expect(p, END);
}
error(p, "expected end of file");
}
- symbol_export(p, NULL);
+// symbol_export(p, NULL);
symbol_closescope(p, scope);
EXIT_RULE();
}