AST* String(Tok* val) {
AST* node = ast(AST_STRING);
- node->value.text = val->value.text;
+ node->value.text = val->text;
return node;
}
return val->value.text;
}
-AST* Var(Tok* name, AST* value, bool constant) {
+AST* Var(char* name, AST* value, AST* type, int flags) {
AST* node = ast(AST_VAR);
- node->value.var.name = name->value.text;
+ node->value.var.name = name;
node->value.var.value = value;
- node->value.var.constant = constant;
+ node->value.var.type = type;
+ node->value.var.flags = flags;
return node;
}
bool var_const(AST* var) {
assert(var->nodetype == AST_VAR);
- return var->value.var.constant;
+ return (var->value.var.flags == SF_CONSTANT);
}
static void require_list(Parser* p);
static void provide_list(Parser* p);
-static AST* definition_list(Parser* p);
+static void definition_list(Parser* p);
static AST* definition(Parser* p);
static AST* type_definition(Parser* p);
static AST* func_definition(Parser* p);
expect(p, ')');
}
-static AST* definition_list(Parser* p) {
+static void definition_list(Parser* p) {
while (!matches(p, T_END_FILE)) {
+ AST* def = NULL;
if (matches(p, T_LET) || matches(p, T_VAR)) {
- definition(p);
+ def = definition(p);
} else if (matches(p, T_TYPE)) {
- type_definition(p);
+ def = type_definition(p);
} else if (matches(p, T_FUN)) {
- func_definition(p);
+ def = func_definition(p);
} else {
error(p, "only definitions are allowed at the top level");
}
- pkg_add_definition(&(p->pkg), NULL);
+ pprint_tree(stdin, def, 0);
+ pkg_add_definition(&(p->pkg), def);
}
- return NULL;
}
static AST* definition(Parser* p) {
+ bool is_const = matches(p, T_LET);
if (!accept(p, T_LET) && !accept(p, T_VAR))
error(p, "constant or variable definition expected");
- expect(p, T_ID);
- type_expression(p);
+ char* str = strdup(expect_val(p, T_ID)->text);
+ AST* type = type_expression(p);
expect(p, '=');
- expression(p);
- return NULL;
+ AST* val = expression(p);
+ return Var(str, val, type, SF_CONSTANT);
}
static AST* type_definition(Parser* p) {
expect(p, T_TYPE);
- expect(p, T_ID);
+ char* str = strdup(expect_val(p, T_ID)->text);
expect(p, '=');
- type_expression(p);
- return NULL;
+ AST* type = type_expression(p);
+ return Var(str, NULL, type, SF_TYPEDEF);
}
static AST* func_definition(Parser* p) {
expect(p, T_FUN);
- expect(p, T_ID);
+ char* str = strdup(expect_val(p, T_ID)->text);
expect(p, '(');
if (!matches(p, ')')) {
while (true) {
}
}
expect(p, ')');
- type_expression(p);
+ AST* type = type_expression(p);
expression_block(p);
- return NULL;
+ return Var(str, NULL, type, SF_CONSTANT);
}
static AST* expression(Parser* p) {
+ AST* exp;
if (matches(p, '(')) {
expect(p, '(');
- expression(p);
+ exp = expression(p);
expect(p, ')');
} else if (matches(p, '{')) {
- expression_block(p);
+ exp = expression_block(p);
} else if (matches(p, T_IF)) {
- if_expression(p);
+ exp = if_expression(p);
} else if (matches(p, T_ID)) {
- identifier(p);
+ exp = identifier(p);
} else {
- constant(p);
+ exp = constant(p);
}
/* determine if this is a function call */
- if (matches(p, '(')) {
+ if (matches(p, '(')) { // TODO
func_expr_list(p);
} else if (accept(p, '.')) {
identifier(p);
func_expr_list(p);
}
- return NULL;
+ return exp;
}
static AST* constant(Parser* p) {
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->value.text);
+ case T_ID: return add_type(p, Ident(tok), tok->text);
default: return NULL;
}
}
if (i == 5) fprintf(file, "\\%c", ch);
}
-void pprint_token_type(FILE* file, Tok* token) {
+static void pprint_token_type(FILE* file, Tok* token) {
if (token->type > 256)
fprintf(file, "%s", token_type_to_string(token->type));
else
fprintf(file, "%c", token->type);
}
-void pprint_token_value(FILE* file, Tok* token) {
+static void pprint_token_value(FILE* file, Tok* token) {
#define TOK(name) case (name): fprintf(file, "%s", #name); break
switch(token->type) {
/* value tokens */
void pprint_tree(FILE* file, AST* tree, int depth)
{
- puts("tree");
- if (tree == NULL) {
- return;
- }
+ if (tree == NULL) return;
print_indent(file, depth);
switch (tree->nodetype) {
case AST_VAR:
/* Definition Node */
struct {
char* name;
+ int flags;
struct AST* value;
- bool constant;
+ struct AST* type;
} var;
/* String, Symbol, Identifier */
char* text;
char* ident_value(AST* val);
/* Definition */
-AST* Var(Tok* name, AST* value, bool constant);
+AST* Var(char* name, AST* value, AST* type, int flags);
char* var_name(AST* var);
AST* var_value(AST* var);
bool var_const(AST* var);
/* Pretty Printing
*****************************************************************************/
-void pprint_token_type(FILE* file, Tok* token);
-void pprint_token_value(FILE* file, Tok* token);
void pprint_token(FILE* file, Tok* token, bool print_loc);
void pprint_tree(FILE* file, AST* tree, int depth);