Removed need for semicolons after some statements
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 21 Jun 2020 21:16:43 +0000 (16:16 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 21 Jun 2020 21:16:43 +0000 (16:16 -0500)
docs/plan
misc/onyx.vim
onyx
progs/minimal.onyx
src/onyx.c
src/onyxparser.c

index bb41161ee90762d3adfc9293d5f368a81c1ddc80..fa9396ab7173b95485bf59e025d2697e305328d2 100644 (file)
--- 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:
index 89d22c44991415c7670fd2ff3afea9791506332b..5b008a9714b8cd91dae05fa37cf07f1f0bca8078 100644 (file)
@@ -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 7b7f254aa9559225a1e62fc185bd033150fc1f3a..cfd3f20a370ecf787b69acd09c5917471cdf4918 100755 (executable)
Binary files a/onyx and b/onyx differ
index 7114a4cc42f03dbff5862618cfbd850a1afe437d..b6610b33e23836674e5cbf5ff62ab6a1210b0792 100644 (file)
@@ -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());
index 816a38a778008652879f76baf8d275e49038b713..2a57a80e3c912279ffaa6625137049f7e58b6346 100644 (file)
@@ -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");
     }
 
index bfe4a3c639543dc70e67017bb5442eadf6f783fa..0d0146ecddbfd450c359b059b78c720a0323600a 100644 (file)
@@ -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);