Implemented #12
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 26 May 2020 21:11:06 +0000 (16:11 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 26 May 2020 21:11:06 +0000 (16:11 -0500)
docs/thoughts
onyx
onyx.c
onyxlex.c
onyxparser.c
onyxparser.h
onyxutils.c
onyxutils.h
progs/minimal.onyx

index ddf255fb984f03caaa78bc205db7e5d57533a6cc..86a7ba5de4de424bd3026763e2a03a800087c09f 100644 (file)
@@ -4,7 +4,7 @@ Type checking at parse time:
        * This requires that functions are declared in a particular order like C\r
        * This also requires immediate evaluation of external symbols (C #include style)\r
                - Don't like this at all\r
-               - Want a proper module system\r
+               - Want a proper module system <<<<\r
 \r
        /* foo.onyx */\r
        foo :: proc (a i32) -> f32 {\r
@@ -21,4 +21,4 @@ Type checking at parse time:
 \r
        foo(5) would have a left node of SYMBOL:foo\r
        This will be resolved in a later stage between the parsing and semantic pass\r
-       \r
+       Type checking and resolving would have to occur afterwards\r
diff --git a/onyx b/onyx
index 9015a7d81bda3362b478f258095335967a094e0e..0786469358d3d498ecc20b1d390fc2338fbb24ae 100755 (executable)
Binary files a/onyx and b/onyx differ
diff --git a/onyx.c b/onyx.c
index d91dd70e492ac8af88798c48d7d274f49b2ab55b..27eeec059c56a1ba34163c0c6d20e43d837e7dd9 100644 (file)
--- a/onyx.c
+++ b/onyx.c
@@ -54,9 +54,8 @@ int main(int argc, char *argv[]) {
        if (onyx_message_has_errors(&msgs)) {
                onyx_message_print(&msgs);
        } else {
-               // TODO: Make a better AST printer so there aren't infinite loops
-               // onyx_ast_print(program);
-               bh_printf("No errors.\n");
+               onyx_ast_print(program, 0);
+               bh_printf("\nNo errors.\n");
        }
 
        bh_file_contents_delete(&fc);
index b7bf7a541f8efe56a92ce47fbe0ae0a4f0137d2b..a922f7c819b8128b30cb3c31e4f428dcbc60cdd0 100644 (file)
--- a/onyxlex.c
+++ b/onyxlex.c
@@ -230,7 +230,7 @@ OnyxToken* onyx_get_token(OnyxTokenizer* tokenizer) {
 
        // Number literal
        if (char_is_num(*tokenizer->curr)) {
-               u64 len = 0;
+               u32 len = 1;
                while (char_is_num(*(tokenizer->curr + 1)) || *(tokenizer->curr + 1) == '.') {
                        len++;
                        INCREMENT_CURR_TOKEN(tokenizer);
index 493527c51d63a5b087022e68e042ea73b6e9ea63..77bc0c47b83b2db099ef7c6ff2a8327b97f11f81 100644 (file)
@@ -223,6 +223,7 @@ static OnyxAstNode* parse_factor(OnyxParser* parser) {
                OnyxAstNode* lit_node = onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_LITERAL);
                lit_node->type = &builtin_types[ONYX_TYPE_INFO_KIND_INT64];
                lit_node->token = expect(parser, TOKEN_TYPE_LITERAL_NUMERIC);
+               lit_node->flags |= ONYX_AST_FLAG_COMPTIME;
                return lit_node;
        }
 
index 9b16c10b35c0d83d0188d6deda20fc4a05a9c6ef..dadf29f973f23f70fd5e31b7416d3f843fffa8d1 100644 (file)
@@ -108,9 +108,10 @@ extern OnyxTypeInfo builtin_types[];
 // only 32-bits of flags to play with
 typedef enum OnyxAstFlags {
        // Top-level flags
-       ONYX_AST_FLAG_EXPORTED   = BH_BIT(1),
-       ONYX_AST_FLAG_LVAL               = BH_BIT(2),
-       ONYX_AST_FLAG_CONST              = BH_BIT(3),
+       ONYX_AST_FLAG_EXPORTED          = BH_BIT(0),
+       ONYX_AST_FLAG_LVAL                      = BH_BIT(1),
+       ONYX_AST_FLAG_CONST                     = BH_BIT(2),
+       ONYX_AST_FLAG_COMPTIME          = BH_BIT(3),
 } OnyxAstFlags;
 
 struct OnyxAstNodeLocal {
index 5fdddcde8d9678b0fedfa4956fc927240456193d..2a07f2a9b65a53f4ce4d43648596047ad965d0e8 100644 (file)
@@ -1,5 +1,109 @@
 #include "onyxutils.h"
+#include "onyxlex.h"
+#include "onyxparser.h"
 
-void onyx_ast_print(OnyxAstNode* node) {
+#define print_indent { if (indent > 0) bh_printf("\n"); for (int i = 0; i < indent; i++) bh_printf("  "); }
+
+void onyx_ast_print(OnyxAstNode* node, i32 indent) {
        if (node == NULL) return;
+
+       print_indent;
+       bh_printf("(%d) %s ", node->flags, onyx_ast_node_kind_string(node->kind));
+
+       switch (node->kind) {
+       case ONYX_AST_NODE_KIND_PROGRAM: {
+               if (node->next)
+                       onyx_ast_print(node->next, indent + 1);
+
+               break;
+       }
+
+       case ONYX_AST_NODE_KIND_FUNCDEF: {
+               bh_printf("(%b) ", node->token->token, node->token->length);
+               OnyxAstNodeFuncDef* fd = &node->as_funcdef;
+
+               print_indent;
+               bh_printf("Params ");
+               if (fd->params)
+                       onyx_ast_print((OnyxAstNode *) fd->params, 0);
+
+               print_indent;
+               bh_printf("Returns %s", fd->return_type->name);
+
+               print_indent;
+               bh_printf("Body");
+               if (fd->body)
+                       onyx_ast_print((OnyxAstNode *) fd->body, indent + 1);
+
+               if (fd->next)
+                       onyx_ast_print((OnyxAstNode *) fd->next, indent);
+
+               break;
+       }
+
+       case ONYX_AST_NODE_KIND_PARAM: {
+               OnyxAstNodeParam* param = &node->as_param;
+               bh_printf("%b %s", param->token->token, param->token->length, param->type->name);
+               if (param->next && indent == 0) {
+                       bh_printf(", ");
+                       onyx_ast_print((OnyxAstNode *) param->next, 0);
+               }
+
+               break;
+       }
+
+       case ONYX_AST_NODE_KIND_BLOCK: {
+               OnyxAstNodeBlock* block = &node->as_block;
+               if (block->scope) {
+                       onyx_ast_print((OnyxAstNode *) block->scope, indent + 1);
+               }
+
+               if (block->body) {
+                       onyx_ast_print((OnyxAstNode *) block->body, indent + 1);
+               }
+
+               break;
+       }
+
+       case ONYX_AST_NODE_KIND_SCOPE: {
+               OnyxAstNodeScope* scope = &node->as_scope;
+               if (scope->last_local) {
+                       onyx_ast_print((OnyxAstNode *) scope->last_local, 0);
+               }
+
+               break;
+       }
+
+       case ONYX_AST_NODE_KIND_LOCAL: {
+               OnyxAstNodeLocal* local = &node->as_local;
+               bh_printf("%b %s", local->token->token, local->token->length, local->type->name);
+               if (local->prev_local && indent == 0) {
+                       bh_printf(", ");
+                       onyx_ast_print((OnyxAstNode *) local->prev_local, 0);
+               }
+               break;
+       }
+
+       case ONYX_AST_NODE_KIND_RETURN: {
+               if (node->next) {
+                       onyx_ast_print(node->next, indent + 1);
+               }
+
+               break;
+       }
+
+       case ONYX_AST_NODE_KIND_LITERAL: {
+               bh_printf("%b", node->token->token, node->token->length);
+               break;
+       }
+
+       default: {
+               onyx_ast_print(node->left, indent + 1);
+               onyx_ast_print(node->right, indent + 1);
+               if (node->next) {
+                       onyx_ast_print(node->next, indent);
+               }
+               break;
+       }
+       }
 }
index 4da320b561560d2ba8955b6dd09d685396108042..63b6f6015459237aa41d8bfb473ac202f7219e32 100644 (file)
@@ -2,4 +2,4 @@
 
 #include "onyxparser.h"
 
-void onyx_ast_print(OnyxAstNode* program);
+void onyx_ast_print(OnyxAstNode* program, i32 indent);
index ee2e65161e1093c15337564d2d5b391e96b6ca5b..19173392705698e4bb56eca34ce31d7df385cc11 100644 (file)
@@ -19,3 +19,18 @@ export mul :: proc (a i32, b i32) -> i32 {
 
        return c * d;
 }
+
+very_long_function_name :: proc () -> void {
+       a:i32;
+       b:i32;
+       c:i32;
+       d:i32;
+       e:i32;
+       f:i32;
+       g:i32;
+       h:i32;
+       i:i32 = 0 + 2;
+       j:i32;
+       k:i32;
+       l:i32;
+}