From: Brendan Hansen Date: Tue, 23 Jun 2020 03:20:05 +0000 (-0500) Subject: Various improvements and bug fixes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c25f887bc2eec174d8287335ac0fc4f9176d5883;p=onyx.git Various improvements and bug fixes --- diff --git a/include/onyxmsgs.h b/include/onyxmsgs.h index b6e0a8fb..c2919ea9 100644 --- a/include/onyxmsgs.h +++ b/include/onyxmsgs.h @@ -20,6 +20,7 @@ typedef enum OnyxMessageType { ONYX_MESSAGE_TYPE_BINOP_MISMATCH_TYPE, ONYX_MESSAGE_TYPE_ASSIGNMENT_TYPE_MISMATCH, ONYX_MESSAGE_TYPE_EXPECTED_EXPRESSION, + ONYX_MESSAGE_TYPE_CALL_NON_FUNCTION, ONYX_MESSAGE_TYPE_FUNCTION_RETURN_MISMATCH, ONYX_MESSAGE_TYPE_FUNCTION_PARAM_TYPE_MISMATCH, diff --git a/misc/onyx.vim b/misc/onyx.vim index 5b008a97..4e85c159 100644 --- a/misc/onyx.vim +++ b/misc/onyx.vim @@ -12,7 +12,7 @@ set cpo&vim syn keyword onyxKeyword struct proc use export foreign global syn keyword onyxKeyword if elseif else -syn keyword onyxKeyword for return do +syn keyword onyxKeyword for while loop return do syn keyword onyxKeyword return syn keyword onyxKeyword as @@ -21,14 +21,17 @@ syn keyword onyxType i64 syn keyword onyxType f32 syn keyword onyxType f64 +syn keyword onyxConstant true false + syn keyword onyxCommentStart contained TODO NOTE BUG HACK syn region onyxComment start="//" end="$" keepend contains=onyxCommentStart -hi def link onyxKeyword Statement -hi def link onyxType Type -hi def link onyxComment Comment +hi def link onyxKeyword Statement +hi def link onyxType Type +hi def link onyxComment Comment hi def link onyxCommentStart Todo +hi def link onyxConstant Constant let b:current_syntax = "onyx" let &cpo = s:cpo_save diff --git a/onyx b/onyx index 06ca0405..8c6c53a7 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/game.onyx b/progs/game.onyx index 85ac24da..5f90fd72 100644 --- a/progs/game.onyx +++ b/progs/game.onyx @@ -1,16 +1,12 @@ gfx_draw_rect :: foreign "gfx" "draw_rect" proc (x i32, y i32, w i32, h i32) --- -update :: proc --- - -draw :: proc { - draw_rect(0, 0, 100, 100); +export main :: proc { } -export main :: proc { +update :: proc {} - while true { - update(); - draw(); - } +draw :: proc { + gfx_draw_rect(0, 0, 100, 100); } + diff --git a/src/onyx.c b/src/onyx.c index f86095d6..4699a7e6 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -50,9 +50,6 @@ int main(int argc, char *argv[]) { OnyxParser parser = onyx_parser_create(ast_alloc, &tokenizer, &msgs); OnyxAstNode* program = onyx_parse(&parser); - bh_printf("BEFORE SYMBOL_RESOLUTION: "); - onyx_ast_print(program, 1); - bh_arena sp_arena; bh_arena_init(&sp_arena, alloc, 16 * 1024); bh_allocator sp_alloc = bh_arena_allocator(&sp_arena); @@ -63,12 +60,10 @@ int main(int argc, char *argv[]) { // NOTE: if there are errors, assume the parse tree was generated wrong, // even if it may have still been generated correctly. if (onyx_message_has_errors(&msgs)) { - bh_printf("\n\n"); onyx_message_print(&msgs); goto main_exit; } else { - bh_printf("\n\nAFTER SYMBOL RESOLUTION: "); - onyx_ast_print(program, 1); + // onyx_ast_print(program, 1); bh_printf("\nNo errors.\n"); } diff --git a/src/onyxmsgs.c b/src/onyxmsgs.c index 3f87b874..3c6018a2 100644 --- a/src/onyxmsgs.c +++ b/src/onyxmsgs.c @@ -6,13 +6,14 @@ static const char* msg_formats[] = { "expected token '%s', got '%s'", "unexpected token '%s'", "unknown type '%s'", - "expected lval '%s'", - "attempt to assign to constant '%s'", + "expected lval '%b'", + "attempt to assign to constant '%b'", "unknown symbol '%s'", "redefinition of function '%s'", "mismatched types for binary operator, '%s', '%s'", "mismatched types on assignment, expected '%s', got '%s'", "expected expression, got '%s'", + "attempt to call non-function, '%b'", "returning '%s' from function that returns '%s'", "function '%b' expected type '%s' in position '%d', got '%s'", diff --git a/src/onyxparser.c b/src/onyxparser.c index 6286e4ce..a0edfa65 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -448,13 +448,14 @@ static b32 parse_symbol_statement(OnyxParser* parser, OnyxAstNode** ret) { // NOTE: Assignment case TOKEN_TYPE_SYM_EQUALS: { + OnyxAstNode* assignment = onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_ASSIGNMENT); + assignment->token = parser->curr_token; parser_next_token(parser); OnyxAstNode* lval = onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_SYMBOL); lval->token = symbol; OnyxAstNode* rval = parse_expression(parser); - OnyxAstNode* assignment = onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_ASSIGNMENT); assignment->right = rval; assignment->left = lval; *ret = assignment; @@ -540,20 +541,20 @@ static OnyxAstNode* parse_statement(OnyxParser* parser) { } static OnyxAstNodeBlock* parse_block(OnyxParser* parser) { + OnyxAstNodeBlock* block = (OnyxAstNodeBlock *) onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_BLOCK); + OnyxAstNodeScope* scope = (OnyxAstNodeScope *) onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_SCOPE); + block->scope = scope; + // --- is for an empty block if (parser->curr_token->type == TOKEN_TYPE_SYM_MINUS) { expect(parser, TOKEN_TYPE_SYM_MINUS); expect(parser, TOKEN_TYPE_SYM_MINUS); expect(parser, TOKEN_TYPE_SYM_MINUS); - return NULL; + return block; } expect(parser, TOKEN_TYPE_OPEN_BRACE); - OnyxAstNodeBlock* block = (OnyxAstNodeBlock *) onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_BLOCK); - OnyxAstNodeScope* scope = (OnyxAstNodeScope *) onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_SCOPE); - block->scope = scope; - OnyxAstNode** next = &block->body; OnyxAstNode* stmt = NULL; while (parser->curr_token->type != TOKEN_TYPE_CLOSE_BRACE) { diff --git a/src/onyxsempass.c b/src/onyxsempass.c index d593cb30..0426789b 100644 --- a/src/onyxsempass.c +++ b/src/onyxsempass.c @@ -17,7 +17,12 @@ OnyxSemPassState onyx_sempass_create(bh_allocator alloc, bh_allocator node_alloc return state; } +static void collapse_scopes(OnyxAstNode* root_node, OnyxAstNodeScope* prev_scope) { + +} + void onyx_sempass(OnyxSemPassState* state, OnyxAstNode* root_node) { onyx_resolve_symbols(state, root_node); onyx_type_check(state, root_node); + collapse_scopes(root_node, NULL); } diff --git a/src/onyxtypecheck.c b/src/onyxtypecheck.c index 28b1f72e..9a620468 100644 --- a/src/onyxtypecheck.c +++ b/src/onyxtypecheck.c @@ -20,6 +20,22 @@ static void typecheck_assignment(OnyxSemPassState* state, OnyxAstNode* assign) { return; } + if ((assign->left->flags & ONYX_AST_FLAG_LVAL) == 0) { + onyx_message_add(state->msgs, + ONYX_MESSAGE_TYPE_NOT_LVAL, + assign->token->pos, + assign->left->token->token, assign->left->token->length); + return; + } + + if ((assign->left->flags & ONYX_AST_FLAG_CONST) != 0 && assign->left->type->is_known) { + onyx_message_add(state->msgs, + ONYX_MESSAGE_TYPE_ASSIGN_CONST, + assign->token->pos, + assign->left->token->token, assign->left->token->length); + return; + } + typecheck_expression(state, assign->right); if (!assign->left->type->is_known) { @@ -72,6 +88,14 @@ static void typecheck_call(OnyxSemPassState* state, OnyxAstNodeCall* call) { return; } + if (callee->kind != ONYX_AST_NODE_KIND_FUNCDEF) { + onyx_message_add(state->msgs, + ONYX_MESSAGE_TYPE_CALL_NON_FUNCTION, + call->token->pos, + callee->token->token, callee->token->length); + return; + } + call->type = callee->return_type; OnyxAstNodeParam* formal_param = callee->params;