aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c66
1 files changed, 40 insertions, 26 deletions
diff --git a/src/parser.c b/src/parser.c
index b61a968..fdf34ce 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -30,12 +30,12 @@ int parser_thread_data_init(ParserThreadData *self) {
am_memset(&self->allocator, 0, sizeof(self->allocator));
am_memset(&self->thread, 0, sizeof(self->thread));
self->status = PARSER_THREAD_STATUS_NEW;
- return scoped_allocator_init(&self->allocator);
+ return arena_allocator_init(&self->allocator);
}
void parser_thread_data_deinit(ParserThreadData *self) {
ignore_result_int(amal_thread_deinit(&self->thread));
- scoped_allocator_deinit(&self->allocator);
+ arena_allocator_deinit(&self->allocator);
}
int parser_thread_data_start(ParserThreadData *self, AmalThreadCallbackFunc callback_func, void *userdata) {
@@ -51,7 +51,7 @@ int parser_thread_data_join(ParserThreadData *self, void **result) {
return amal_thread_join(&self->thread, result);
}
-int parser_init(Parser *self, amal_compiler *compiler, ScopedAllocator *allocator) {
+int parser_init(Parser *self, amal_compiler *compiler, ArenaAllocator *allocator) {
self->allocator = allocator;
self->compiler = compiler;
self->ssa = NULL;
@@ -151,15 +151,15 @@ static CHECK_RESULT FunctionSignature* parser_parse_function_signature(Parser *s
/* TODO: Parse return types */
}
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(FunctionSignature), (void**)&signature));
- signature->params = 0;
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(FunctionSignature), (void**)&signature));
+ function_signature_init(signature);
return signature;
}
/*
VAR_TYPE_DEF = ':' TOK_IDENTIFIER|FUNC_SIGNATURE
*/
-static CHECK_RESULT int parser_parse_var_type_def(Parser *self, VariableType *result) {
+static void parser_parse_var_type_def(Parser *self, VariableType *result) {
bool match;
result->type = VARIABLE_TYPE_NONE;
@@ -167,14 +167,14 @@ static CHECK_RESULT int parser_parse_var_type_def(Parser *self, VariableType *re
throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_COLON, &match));
if(!match)
- return -1;
+ return;
throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_IDENTIFIER, &match));
if(match) {
result->type = VARIABLE_TYPE_VARIABLE;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Variable), (void**)&result->value.variable));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(Variable), (void**)&result->value.variable));
variable_init(result->value.variable, self->tokenizer.value.identifier);
- return 0;
+ return;
}
result->type = VARIABLE_TYPE_SIGNATURE;
@@ -185,7 +185,6 @@ static CHECK_RESULT int parser_parse_var_type_def(Parser *self, VariableType *re
"Expected type or closure signature");
throw(PARSER_UNEXPECTED_TOKEN);
}
- return 0;
}
/*
@@ -232,10 +231,10 @@ static CHECK_RESULT LhsExpr* parser_parse_declaration_lhs(Parser *self) {
throw_if_error(tokenizer_accept(&self->tokenizer, TOK_IDENTIFIER));
var_name = self->tokenizer.value.identifier;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(LhsExpr), (void**)&result));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(LhsExpr), (void**)&result));
lhsexpr_init(result, is_extern, is_pub, is_const, var_name);
- ignore_result_int(parser_parse_var_type_def(self, &result->type));
+ parser_parse_var_type_def(self, &result->type);
return result;
}
@@ -252,7 +251,7 @@ static CHECK_RESULT FunctionDecl* parser_parse_closure(Parser *self) {
if(!signature)
return NULL;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(FunctionDecl), (void**)&result));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(FunctionDecl), (void**)&result));
throw_if_error(funcdecl_init(result, signature, self->current_scope, self->allocator));
self->current_scope = &result->body;
@@ -277,7 +276,7 @@ static CHECK_RESULT StructDecl* parser_parse_struct_decl(Parser *self) {
if(!match)
return result;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(StructDecl), (void**)&result));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(StructDecl), (void**)&result));
throw_if_error(structdecl_init(result, self->current_scope, self->allocator));
self->current_scope = &result->body;
@@ -329,13 +328,13 @@ static CHECK_RESULT Ast* parser_parse_function_call_or_variable(Parser *self) {
throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_OPEN_PAREN, &match));
if(!match) {
Variable *variable;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Variable), (void**)&variable));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(Variable), (void**)&variable));
variable_init(variable, identifier);
throw_if_error(ast_create(self->allocator, variable, AST_VARIABLE, &result));
return result;
}
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(FunctionCall), (void**)&func_call));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(FunctionCall), (void**)&func_call));
throw_if_error(funccall_init(func_call, self->tokenizer.value.identifier, self->allocator));
throw_if_error(ast_create(self->allocator, func_call, AST_FUNCTION_CALL, &result));
/* Ends after TOK_CLOSING_PAREN */
@@ -355,7 +354,7 @@ static CHECK_RESULT Import* parser_parse_import(Parser *self) {
if(!match)
return result;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Import), (void**)&result));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(Import), (void**)&result));
import_init(result, self->tokenizer.value.string);
return result;
}
@@ -371,7 +370,7 @@ static CHECK_RESULT ElseIfStatement* parser_parse_else_if_statement(Parser *self
if(!match)
return NULL;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(ElseIfStatement), (void**)&result));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(ElseIfStatement), (void**)&result));
throw_if_error(else_if_statement_init(result, self->current_scope, self->allocator));
throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_IF, &match));
@@ -417,7 +416,7 @@ static CHECK_RESULT IfStatement* parser_parse_if_statement(Parser *self) {
if(!match)
return NULL;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(IfStatement), (void**)&result));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(IfStatement), (void**)&result));
throw_if_error(if_statement_init(result, self->current_scope, self->allocator));
result->condition = parser_parse_rhs_binop(self);
@@ -446,7 +445,7 @@ static CHECK_RESULT WhileStatement* parser_parse_while_statement(Parser *self) {
if(!match)
return NULL;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(WhileStatement), (void**)&result));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(WhileStatement), (void**)&result));
throw_if_error(while_statement_init(result, self->current_scope, self->allocator));
result->condition = parser_parse_rhs_binop(self);
@@ -473,7 +472,7 @@ static CHECK_RESULT Ast* parser_parse_number(Parser *self) {
if(!match)
return result;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Number), (void**)&number));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(Number), (void**)&number));
number_init(number, self->tokenizer.value.integer, self->tokenizer.number_is_integer,
create_buffer_view(self->tokenizer.code.data + self->tokenizer.prev_index, self->tokenizer.index - self->tokenizer.prev_index));
throw_if_error(ast_create(self->allocator, number, AST_NUMBER, &result));
@@ -491,7 +490,7 @@ static Ast* parser_parse_rhs_single_expr(Parser *self) {
throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_STRING, &match));
if(match) {
String *string;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(String), (void**)&string));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(String), (void**)&string));
throw_if_error(string_init(string, self->tokenizer.value.string));
throw_if_error(ast_create(self->allocator, string, AST_STRING, &result));
return result;
@@ -549,7 +548,7 @@ Ast* parser_parse_rhs_binop(Parser *self) {
binop_type = self->tokenizer.value.binop_type;
rhs = parser_parse_rhs_binop(self);
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Binop), (void**)&binop));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(Binop), (void**)&binop));
binop_init(binop);
binop->type = binop_type;
binop->lhs = lhs;
@@ -645,11 +644,23 @@ Ast* parser_parse_body(Parser *self) {
throw_if_error(ast_create(self->allocator, lhs_expr, AST_LHS, &result));
if(lhs_expr->is_extern) {
throw_if_error(tokenizer_accept(&self->tokenizer, TOK_SEMICOLON));
+ if (lhs_expr->type.type == VARIABLE_TYPE_NONE) {
+ self->error = tokenizer_create_error(&self->tokenizer, self->tokenizer.prev_index, "A variable can't be declared without a type or assignment");
+ throw(PARSER_UNEXPECTED_TOKEN);
+ }
return result;
} else {
throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_SEMICOLON, &match));
- if(match)
+ if(match) {
+ if(lhs_expr->type.type == VARIABLE_TYPE_SIGNATURE) {
+ self->error = tokenizer_create_error(&self->tokenizer, self->tokenizer.prev_index, "Expected function declaration. Only extern functions can have empty declarations.");
+ throw(PARSER_UNEXPECTED_TOKEN);
+ } else if (lhs_expr->type.type == VARIABLE_TYPE_NONE) {
+ self->error = tokenizer_create_error(&self->tokenizer, self->tokenizer.prev_index, "A variable can't be declared without a type or assignment");
+ throw(PARSER_UNEXPECTED_TOKEN);
+ }
return result;
+ }
}
throw_if_error(tokenizer_accept(&self->tokenizer, TOK_EQUALS));
@@ -683,14 +694,16 @@ Ast* parser_parse_body(Parser *self) {
self->error_context = ERROR_CONTEXT_RHS_STANDALONE;
rhs_expr = parser_parse_rhs(self);
self->error_context = ERROR_CONTEXT_NONE;
+ /* Variable declaration with lhs and rhs */
if(lhs_expr) {
lhs_expr->rhs_expr = rhs_expr;
} else {
bool match;
throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_EQUALS, &match));
+ /* Variable assignment */
if(match) {
AssignmentExpr *assign_expr;
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(AssignmentExpr), (void**)&assign_expr));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(AssignmentExpr), (void**)&assign_expr));
throw_if_error(ast_create(self->allocator, assign_expr, AST_ASSIGN, &result));
assign_expr->lhs_expr = rhs_expr;
@@ -698,6 +711,7 @@ Ast* parser_parse_body(Parser *self) {
assign_expr->rhs_expr = parser_parse_rhs(self);
self->error_context = ERROR_CONTEXT_NONE;
} else {
+ /* Only rhs (for example function call, binop etc...) */
result = rhs_expr;
}
}
@@ -721,7 +735,7 @@ Ast* parser_parse_struct_body(Parser *self) {
throw_if_error(tokenizer_accept(&self->tokenizer, TOK_IDENTIFIER));
type_name = self->tokenizer.value.identifier;
throw_if_error(tokenizer_accept(&self->tokenizer, TOK_SEMICOLON));
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(LhsExpr), (void**)&struct_field));
+ throw_if_error(arena_allocator_alloc(self->allocator, sizeof(LhsExpr), (void**)&struct_field));
structfield_init(struct_field, var_name, type_name);
throw_if_error(ast_create(self->allocator, struct_field, AST_STRUCT_FIELD, &result));
return result;