From: Brendan Hansen Date: Sun, 21 Jun 2020 21:16:43 +0000 (-0500) Subject: Removed need for semicolons after some statements X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2c6b2c41b8257b5a71e1a58a740ead1792f29a0e;p=onyx.git Removed need for semicolons after some statements --- diff --git a/docs/plan b/docs/plan index bb41161e..fa9396ab 100644 --- a/docs/plan +++ b/docs/plan @@ -56,12 +56,12 @@ HOW: [X] Numeric literals are parsed [X] Numeric literals have the minimum type detected [X] Foreign imports (functions only) - [ ] Comparison operators + [X] Comparison operators [ ] Proper boolean type - [ ] Conditional branching works as expected + [X] Conditional branching works as expected [ ] Simple while loop is functioning as expected [ ] break and continue semantics - [ ] Function calling works for the builtin types + [X] Function calling works for the builtin types [ ] Function return values are type checked Stage 2: diff --git a/misc/onyx.vim b/misc/onyx.vim index 89d22c44..5b008a97 100644 --- a/misc/onyx.vim +++ b/misc/onyx.vim @@ -10,18 +10,10 @@ endif let s:cpo_save = &cpo set cpo&vim -syn keyword onyxKeyword struct -syn keyword onyxKeyword use -syn keyword onyxKeyword if -syn keyword onyxKeyword elseif -syn keyword onyxKeyword else -syn keyword onyxKeyword export -syn keyword onyxKeyword proc -syn keyword onyxKeyword foreign -syn keyword onyxKeyword for +syn keyword onyxKeyword struct proc use export foreign global +syn keyword onyxKeyword if elseif else +syn keyword onyxKeyword for return do syn keyword onyxKeyword return -syn keyword onyxKeyword do -syn keyword onyxKeyword global syn keyword onyxKeyword as syn keyword onyxType i32 diff --git a/onyx b/onyx index 7b7f254a..cfd3f20a 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/minimal.onyx b/progs/minimal.onyx index 7114a4cc..b6610b33 100644 --- a/progs/minimal.onyx +++ b/progs/minimal.onyx @@ -4,7 +4,7 @@ print_float :: foreign "host" "print" proc (value f32) --- print_if :: foreign "host" "print" proc (i i32, f f32) --- foo :: proc -> i32 { - return 10 as i32; + return 10; } add :: proc (a i32, b i32) -> i32 { @@ -16,7 +16,7 @@ add :: proc (a i32, b i32) -> i32 { // is possible that all code paths are covered with returning // an i32. This will need to be fixed. abs :: proc (val i32) -> i32 { - if val <= 0 { return -val; }; + if val <= 0 { return -val; } // else { return val; }; return val; } @@ -29,10 +29,9 @@ diff_square :: proc (a i32, b i32) -> i32 { return c * d; } -export do_stuff :: proc -> i32 { - res := diff_square((4 + 5) as i32, (2 + 3) as i32); +do_stuff :: proc -> i32 { + res := diff_square(4 + 5, 2 + 3); res = res + foo(); - // res should be -66 return res * -1; } @@ -47,10 +46,12 @@ export main :: proc { new_output :: abs(output) * 2; print(new_output); + } elseif output == -67 { + print(1234); + } else { print(-1); - - }; + } print(output); print_float(float_test()); diff --git a/src/onyx.c b/src/onyx.c index 816a38a7..2a57a80e 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) { onyx_message_print(&msgs); goto main_exit; } else { - // onyx_ast_print(program, 0); + onyx_ast_print(program, 0); bh_printf("\nNo errors.\n"); } diff --git a/src/onyxparser.c b/src/onyxparser.c index bfe4a3c6..0d0146ec 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -77,7 +77,7 @@ static OnyxAstNodeNumLit* parse_numeric_literal(OnyxParser* parser); static OnyxAstNode* parse_factor(OnyxParser* parser); static OnyxAstNode* parse_bin_op(OnyxParser* parser, OnyxAstNode* left); static OnyxAstNode* parse_expression(OnyxParser* parser); -static OnyxAstNode* parse_if_stmt(OnyxParser* parser); +static OnyxAstNodeIf* parse_if_stmt(OnyxParser* parser); static b32 parse_symbol_statement(OnyxParser* parser, OnyxAstNode** ret); static OnyxAstNode* parse_return_statement(OnyxParser* parser); static OnyxAstNodeBlock* parse_block(OnyxParser* parser, b32 belongs_to_function); @@ -430,7 +430,7 @@ expression_done: return root; } -static OnyxAstNode* parse_if_stmt(OnyxParser* parser) { +static OnyxAstNodeIf* parse_if_stmt(OnyxParser* parser) { expect(parser, TOKEN_TYPE_KEYWORD_IF); OnyxAstNode* cond = parse_expression(parser); @@ -440,7 +440,8 @@ static OnyxAstNode* parse_if_stmt(OnyxParser* parser) { OnyxAstNodeIf* root_if = if_node; if_node->cond = cond; - if_node->true_block = true_block->as_block.body; + if (true_block != NULL) + if_node->true_block = true_block->as_block.body; while (parser->curr_token->type == TOKEN_TYPE_KEYWORD_ELSEIF) { parser_next_token(parser); @@ -450,7 +451,8 @@ static OnyxAstNode* parse_if_stmt(OnyxParser* parser) { true_block = (OnyxAstNode *) parse_block(parser, 0); elseif_node->cond = cond; - elseif_node->true_block = true_block->as_block.body; + if (true_block != NULL) + elseif_node->true_block = true_block->as_block.body; if_node->false_block = (OnyxAstNode *) elseif_node; if_node = elseif_node; @@ -460,10 +462,11 @@ static OnyxAstNode* parse_if_stmt(OnyxParser* parser) { parser_next_token(parser); OnyxAstNode* false_block = (OnyxAstNode *) parse_block(parser, 0); - if_node->false_block = false_block->as_block.body; + if (false_block != NULL) + if_node->false_block = false_block->as_block.body; } - return (OnyxAstNode *) root_if; + return root_if; } // Returns 1 if the symbol was consumed. Returns 0 otherwise @@ -595,19 +598,22 @@ static OnyxAstNode* parse_return_statement(OnyxParser* parser) { } static OnyxAstNode* parse_statement(OnyxParser* parser) { + b32 needs_semicolon = 1; + OnyxAstNode* retval = NULL; + switch (parser->curr_token->type) { case TOKEN_TYPE_KEYWORD_RETURN: - return parse_return_statement(parser); + retval = parse_return_statement(parser); + break; case TOKEN_TYPE_OPEN_BRACE: - return (OnyxAstNode *) parse_block(parser, 0); + needs_semicolon = 0; + retval = (OnyxAstNode *) parse_block(parser, 0); + break; case TOKEN_TYPE_SYMBOL: - { - OnyxAstNode* ret = NULL; - if (parse_symbol_statement(parser, &ret)) return ret; - // fallthrough - } + if (parse_symbol_statement(parser, &retval)) break; + // fallthrough case TOKEN_TYPE_OPEN_PAREN: case TOKEN_TYPE_SYM_PLUS: @@ -615,14 +621,32 @@ static OnyxAstNode* parse_statement(OnyxParser* parser) { case TOKEN_TYPE_SYM_BANG: case TOKEN_TYPE_LITERAL_NUMERIC: case TOKEN_TYPE_LITERAL_STRING: - return parse_expression(parser); + retval = parse_expression(parser); + break; case TOKEN_TYPE_KEYWORD_IF: - return parse_if_stmt(parser); + needs_semicolon = 0; + retval = (OnyxAstNode *) parse_if_stmt(parser); + break; default: - return NULL; + break; } + + if (needs_semicolon) { + if (parser->curr_token->type != TOKEN_TYPE_SYM_SEMICOLON) { + onyx_message_add(parser->msgs, + ONYX_MESSAGE_TYPE_EXPECTED_TOKEN, + parser->curr_token->pos, + onyx_get_token_type_name(TOKEN_TYPE_SYM_SEMICOLON), + onyx_get_token_type_name(parser->curr_token->type)); + + find_token(parser, TOKEN_TYPE_SYM_SEMICOLON); + } + parser_next_token(parser); + } + + return retval; } static OnyxAstNodeBlock* parse_block(OnyxParser* parser, b32 belongs_to_function) { @@ -651,17 +675,6 @@ static OnyxAstNodeBlock* parse_block(OnyxParser* parser, b32 belongs_to_function *next = stmt; next = &stmt->next; } - - if (parser->curr_token->type != TOKEN_TYPE_SYM_SEMICOLON) { - onyx_message_add(parser->msgs, - ONYX_MESSAGE_TYPE_EXPECTED_TOKEN, - parser->curr_token->pos, - onyx_get_token_type_name(TOKEN_TYPE_SYM_SEMICOLON), - onyx_get_token_type_name(parser->curr_token->type)); - - find_token(parser, TOKEN_TYPE_SYM_SEMICOLON); - } - parser_next_token(parser); } expect(parser, TOKEN_TYPE_CLOSE_BRACE);