aboutsummaryrefslogtreecommitdiff
path: root/src/ast.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ast.c')
-rw-r--r--src/ast.c114
1 files changed, 81 insertions, 33 deletions
diff --git a/src/ast.c b/src/ast.c
index cfc1369..9605b57 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -44,7 +44,7 @@ BufferView ast_get_name(Ast *self) {
name = self->value.lhs_expr->var_name;
break;
case AST_FUNCTION_CALL:
- name = self->value.func_call->name;
+ name = self->value.func_call->func.name;
break;
case AST_VARIABLE:
name = self->value.variable->name;
@@ -53,13 +53,17 @@ BufferView ast_get_name(Ast *self) {
return name;
}
-int funcdecl_init(FunctionDecl *self, ScopedAllocator *allocator) {
+static BufferView ast_get_code_reference(Ast *self) {
+ return ast_get_name(self);
+}
+
+int funcdecl_init(FunctionDecl *self, Scope *parent, ScopedAllocator *allocator) {
self->ssa_func_index = 0;
- return scope_init(&self->body, allocator);
+ return scope_init(&self->body, parent, allocator);
}
int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator) {
- self->name = name;
+ variable_init(&self->func, name);
return buffer_init(&self->args, allocator);
}
@@ -97,24 +101,26 @@ void binop_init(Binop *self) {
self->grouped = bool_false;
}
-int scope_init(Scope *self, ScopedAllocator *allocator) {
+int scope_init(Scope *self, Scope *parent, ScopedAllocator *allocator) {
return_if_error(buffer_init(&self->ast_objects, allocator));
return_if_error(hash_map_init(&self->named_objects, allocator, sizeof(Ast), hash_compare_string, amal_hash_string));
+ self->parent = parent;
return 0;
}
int scope_add_child(Scope *self, Ast *child) {
- BufferView child_name;
Ast existing_child;
bool child_already_exists;
- child_name = ast_get_name(child);
- if(child_name.data) {
- child_already_exists = hash_map_get(&self->named_objects, child_name, &existing_child);
+ /* TODO: Implement for parameter */
+ if(child->type == AST_LHS) {
+ BufferView var_name;
+ var_name = child->value.lhs_expr->var_name;
+ child_already_exists = hash_map_get(&self->named_objects, var_name, &existing_child);
if(child_already_exists)
return AST_ERR_DEF_DUP;
- cleanup_if_error(hash_map_insert(&self->named_objects, child_name, child));
+ cleanup_if_error(hash_map_insert(&self->named_objects, var_name, child));
}
cleanup_if_error(buffer_append(&self->ast_objects, child, sizeof(Ast)));
return 0;
@@ -126,56 +132,99 @@ int scope_add_child(Scope *self, Ast *child) {
void scope_resolve(Scope *self, AstCompilerContext *context) {
Ast *ast;
Ast *ast_end;
+ Scope *prev_scope = context->scope;
ast = buffer_start(&self->ast_objects);
ast_end = buffer_end(&self->ast_objects);
+ assert(self->parent == context->scope);
+ context->scope = self;
for(; ast != ast_end; ++ast) {
ast_resolve(ast, context);
}
+ context->scope = self->parent;
+ assert(context->scope == prev_scope);
+}
+
+static Ast scope_get_resolved_variable(Scope *self, AstCompilerContext *context, BufferView name) {
+ Ast result;
+ bool exists;
+ Scope *prev_scope;
+
+ assert(self);
+ exists = hash_map_get(&self->named_objects, name, &result);
+ if(!exists) {
+ if(self->parent)
+ return scope_get_resolved_variable(self->parent, context, name);
+
+ tokenizer_get_code_reference_index(&context->parser->tokenizer, name.data);
+ tokenizer_print_error(&context->parser->tokenizer,
+ tokenizer_get_code_reference_index(&context->parser->tokenizer, name.data),
+ "Undefined reference to variable \"%.*s\"", name.size, name.data);
+ throw(AST_ERR);
+ }
+
+ /*
+ Need to change scope here because we are changing the visible scope
+ and the ast object may be in another scope than the current
+ resolving ast.
+ */
+ prev_scope = context->scope;
+ context->scope = self;
+ ast_resolve(&result, context);
+ context->scope = prev_scope;
+
+ assert(result.type == AST_LHS);
+ return result.value.lhs_expr->rhs_expr;
}
-static void lhs_resolve(LhsExpr *self, AstCompilerContext *context) {
+static void variable_resolve(Variable *self, AstCompilerContext *context) {
/* TODO: Implement */
+ amal_log_debug("variable resolve, var name: %.*s", self->name.size, self->name.data);
+ self->resolved_variable = scope_get_resolved_variable(context->scope, context, self->name);
+}
+
+static void lhs_resolve(Ast *self, AstCompilerContext *context) {
+ /* TODO: Implement */
+ LhsExpr *lhs_expr;
+ assert(self->type == AST_LHS);
+ lhs_expr = self->value.lhs_expr;
amal_log_debug("Lhs resolve %s name: %.*s, type: %.*s",
- self->is_const ? "const" : "var",
- self->var_name.size, self->var_name.data,
- self->type_name.size, self->type_name.data);
- ast_resolve(&self->rhs_expr, context);
+ lhs_expr->is_const ? "const" : "var",
+ lhs_expr->var_name.size, lhs_expr->var_name.data,
+ lhs_expr->type_name.size, lhs_expr->type_name.data);
+ self->resolve_status = AST_RESOLVED;
+ ast_resolve(&lhs_expr->rhs_expr, context);
}
-static void funcdecl_resolve(FunctionDecl *self, AstCompilerContext *context) {
+static void funcdecl_resolve(Ast *self, AstCompilerContext *context) {
/* TODO: Implement */
+ FunctionDecl *func_decl;
+ assert(self->type == AST_FUNCTION_DECL);
+ func_decl = self->value.func_decl;
amal_log_debug("funcdecl resolve");
- scope_resolve(&self->body, context);
+ self->resolve_status = AST_RESOLVED;
+ scope_resolve(&func_decl->body, context);
}
static void funccall_resolve(FunctionCall *self, AstCompilerContext *context) {
/* TODO: Implement */
Ast *ast;
Ast *ast_end;
+
+ variable_resolve(&self->func, context);
+
ast = buffer_start(&self->args);
ast_end = buffer_end(&self->args);
- amal_log_debug("funccall resolve, func name: %.*s", self->name.size, self->name.data);
+ amal_log_debug("funccall resolve, func name: %.*s", self->func.name.size, self->func.name.data);
for(; ast != ast_end; ++ast) {
ast_resolve(ast, context);
}
}
-static void variable_resolve(Variable *self, AstCompilerContext *context) {
- /* TODO: Implement */
- amal_log_debug("variable resolve, var name: %.*s", self->name.size, self->name.data);
- (void)context;
-}
-
static void binop_resolve(Binop *self, AstCompilerContext *context) {
/* TODO: Implement */
ast_resolve(&self->lhs, context);
ast_resolve(&self->rhs, context);
}
-/*
-static BufferView ast_get_code_reference(Ast *self) {
- return ast_get_name(self);
-}
-*/
void ast_resolve(Ast *self, AstCompilerContext *context) {
/*
@@ -184,7 +233,7 @@ void ast_resolve(Ast *self, AstCompilerContext *context) {
instead of the whole function declaration including the body
because the body can have function call that calls functions that are resolving
or even recursive function call, which should be allowed.
-
+ */
if(self->resolve_status == AST_RESOLVED) {
return;
} else if(self->resolve_status == AST_RESOLVING) {
@@ -194,7 +243,6 @@ void ast_resolve(Ast *self, AstCompilerContext *context) {
"Found recursive dependency");
throw(AST_ERR);
}
- */
self->resolve_status = AST_RESOLVING;
switch(self->type) {
@@ -203,13 +251,13 @@ void ast_resolve(Ast *self, AstCompilerContext *context) {
/* Nothing to resolve for numbers */
break;
case AST_FUNCTION_DECL:
- funcdecl_resolve(self->value.func_decl, context);
+ funcdecl_resolve(self, context);
break;
case AST_FUNCTION_CALL:
funccall_resolve(self->value.func_call, context);
break;
case AST_LHS:
- lhs_resolve(self->value.lhs_expr, context);
+ lhs_resolve(self, context);
break;
case AST_IMPORT:
/* TODO: When @import(...).data syntax is added, implement the resolve for it */