static AST* if_expression(Parser* p);
static AST* identifier(Parser* p);
static AST* func_expr_list(Parser* p);
-static Type* get_typedef(Parser* p, char* typename);
-static AST* add_type(Parser* p, AST* ast, char* typename);
-static AST* token_to_tree(Parser* p, Tok* tok);
/* Parsing Routines
*****************************************************************************/
Tok* tok = peek(parser);
va_list args;
va_start(args, fmt);
- fprintf(stderr, "<file>:%zu:%zu: error: ", tok->line, tok->col);
+ fprintf(stderr, "<file>:%zu: error: ", tok->offset);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
return Var(str, NULL, type, SF_TYPEDEF);
}
-static AST* func_definition(Parser* p) {
+static AST* func_definition(Parser* p) { // TODO: Function AST nodes
expect(p, T_FUN);
char* str = strdup(expect_val(p, T_ID)->text);
expect(p, '(');
}
static AST* constant(Parser* p) {
- AST* ret = NULL;
+ AST* tree = NULL;
Tok* tok = peek(p);
switch (tok->type) {
- case T_BOOL:
- case T_CHAR:
- case T_STRING:
- case T_INT:
- case T_FLOAT:
- ret = token_to_tree(p, tok);
- tok->type = T_NONE;
- break;
- default:
- error(p, "expected an expression");
+ case T_BOOL: tree = Bool(tok);
+ case T_CHAR: tree = Char(tok);
+ case T_STRING: tree = String(tok);
+ case T_INT: tree = Integer(tok);
+ case T_FLOAT: tree = Float(tok);
+ case T_ID: tree = Ident(tok);
+ default: error(p, "expected an expression");
}
- return ret;
+ accept(p, tok->type);
+ return tree;
}
/* Type Expressions
return NULL;
}
-/* Helper Functions for Constants
- ****************************************************************************/
-static Type* get_typedef(Parser* p, char* typename) {
- Sym* sym = sym_get(&(p->syms), typename);
- if (!sym) error(p, "unknown type '%s'", typename);
- return sym->type;
-}
-
-static AST* add_type(Parser* p, AST* ast, char* typename) {
- ast->datatype = get_typedef(p, typename);
- return ast;
-}
-
-static AST* token_to_tree(Parser* p, Tok* tok) {
- switch (tok->type) {
- case T_BOOL: return add_type(p, Bool(tok), "bool");
- case T_CHAR: return add_type(p, Char(tok), "char");
- case T_STRING: return add_type(p, String(tok), "string");
- case T_INT: return add_type(p, Integer(tok), "int");
- case T_FLOAT: return add_type(p, Float(tok), "float");
- case T_ID: return add_type(p, Ident(tok), tok->text);
- default: return NULL;
- }
-}
-