From: Brendan Hansen Date: Wed, 12 Aug 2020 17:29:30 +0000 (-0500) Subject: added 'builtin' package for symbols that are built into the language X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=d6ca8cb5c76df77dfe004f8dfde07c1320247f75;p=onyx.git added 'builtin' package for symbols that are built into the language --- diff --git a/core/alloc.onyx b/core/alloc.onyx index fda4cdb4..fe975ed3 100644 --- a/core/alloc.onyx +++ b/core/alloc.onyx @@ -3,6 +3,7 @@ package memory #include_file "memory" #include_file "intrinsics" +use package builtin { __heap_start } use package intrinsics { memory_size, memory_grow } // Need to define this somewhere diff --git a/docs/plan b/docs/plan index fe7948b1..316d6f7d 100644 --- a/docs/plan +++ b/docs/plan @@ -162,10 +162,9 @@ HOW: - structs can still be passed by value however - removed the implicit splatting feature - [ ] package builtin + [X] package builtin - Place to store builtin types and values __heap_start - __stack_base __stack_top etc diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index ebb99d5e..8af55706 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -487,6 +487,7 @@ extern AstNumLit builtin_heap_start; extern AstGlobal builtin_stack_top; typedef struct BuiltinSymbol { + char* package; char* sym; AstNode* node; } BuiltinSymbol; diff --git a/onyx b/onyx index efe6385a..3c14e98c 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/fcf.onyx b/progs/fcf.onyx index b16687ad..12500c0b 100644 --- a/progs/fcf.onyx +++ b/progs/fcf.onyx @@ -1,5 +1,7 @@ -use "progs/print_funcs" -use "progs/intrinsics" +#include_folder "./core" + +#include_file "printing" +#include_file "intrinsics" use package printing @@ -39,13 +41,13 @@ I32Array :: struct { } array_map :: proc (arr: I32Array, map: proc (i32) -> i32) { - for i: 0, arr.length arr.data[i] = map(arr.data[i]); + for i: 0, arr.length do arr.data[i] = map(arr.data[i]); } minus_one :: proc (n: i32) -> i32 { return n - 1; } double :: proc (n: i32) -> i32 { return n << 1; } -proc #export "main" { +proc #export "start" { call_me(echo, 10); print(cast(my_int) add); @@ -56,7 +58,7 @@ proc #export "main" { funcs[3] = div; funcs[4] = mod; - for i: 0, 5 print(funcs[i](10, 3)); + for i: 0, 5 do print(funcs[i](10, 3)); dc := cast(^DeferredCall) __heap_start; dc.func = add; @@ -67,12 +69,15 @@ proc #export "main" { len :: 10; data := cast(^i32) (cast(i32) __heap_start + sizeof DeferredCall); - for i: 0, len data[i] = i; + for i: 0, len do data[i] = i; print(cast([] i32) data, len); add_one :: proc (n: i32) -> i32 { return n + 1; }; - array_map(len, data, add_one); + arr : I32Array; + arr.length = len; + arr.data = data; + array_map(arr, add_one); print(cast([] i32) data, len); cheese := Cheeses.Cheddar; diff --git a/progs/stack_based.onyx b/progs/stack_based.onyx index df4699c7..b0ac4c35 100644 --- a/progs/stack_based.onyx +++ b/progs/stack_based.onyx @@ -88,8 +88,6 @@ start :: proc #export { print("Hello, World!"); print_hex(cast(u64) some_value); print("Hello, World!"); - print_ptr(__heap_start); - print_ptr(__stack_top); print_bin(42l); print_hex(42l); @@ -143,4 +141,10 @@ start :: proc #export { |> print(); stupid_idea(1234)() |> print(); + + v : [5] Vec3; + v[2].x = 4; + v[2].y = 5; + v[2].z = 6; + mag_squared(v[2]) |> print(); } \ No newline at end of file diff --git a/src/onyxbuiltins.c b/src/onyxbuiltins.c index 1a8c7bcc..e4a8d423 100644 --- a/src/onyxbuiltins.c +++ b/src/onyxbuiltins.c @@ -21,24 +21,24 @@ AstNumLit builtin_heap_start = { Ast_Kind_NumLit, Ast_Flag_Const, &builtin_heap_ AstGlobal builtin_stack_top = { Ast_Kind_Global, Ast_Flag_Const | Ast_Flag_Global_Stack_Top, &builtin_stack_top_token, NULL, (AstType *) &basic_type_rawptr, NULL }; const BuiltinSymbol builtin_symbols[] = { - { "void", (AstNode *) &basic_type_void }, - { "bool", (AstNode *) &basic_type_bool }, - { "i8", (AstNode *) &basic_type_i8 }, - { "u8", (AstNode *) &basic_type_u8 }, - { "i16", (AstNode *) &basic_type_i16 }, - { "u16", (AstNode *) &basic_type_u16 }, - { "i32", (AstNode *) &basic_type_i32 }, - { "u32", (AstNode *) &basic_type_u32 }, - { "i64", (AstNode *) &basic_type_i64 }, - { "u64", (AstNode *) &basic_type_u64 }, - { "f32", (AstNode *) &basic_type_f32 }, - { "f64", (AstNode *) &basic_type_f64 }, - { "rawptr", (AstNode *) &basic_type_rawptr }, + { NULL, "void", (AstNode *) &basic_type_void }, + { NULL, "bool", (AstNode *) &basic_type_bool }, + { NULL, "i8", (AstNode *) &basic_type_i8 }, + { NULL, "u8", (AstNode *) &basic_type_u8 }, + { NULL, "i16", (AstNode *) &basic_type_i16 }, + { NULL, "u16", (AstNode *) &basic_type_u16 }, + { NULL, "i32", (AstNode *) &basic_type_i32 }, + { NULL, "u32", (AstNode *) &basic_type_u32 }, + { NULL, "i64", (AstNode *) &basic_type_i64 }, + { NULL, "u64", (AstNode *) &basic_type_u64 }, + { NULL, "f32", (AstNode *) &basic_type_f32 }, + { NULL, "f64", (AstNode *) &basic_type_f64 }, + { NULL, "rawptr", (AstNode *) &basic_type_rawptr }, - { "__heap_start", (AstNode *) &builtin_heap_start }, - { "__stack_top", (AstNode *) &builtin_stack_top }, + { "builtin", "__heap_start", (AstNode *) &builtin_heap_start }, + { "builtin", "__stack_top", (AstNode *) &builtin_stack_top }, - { NULL, NULL }, + { NULL, NULL, NULL }, }; void initialize_builtins() { diff --git a/src/onyxsymres.c b/src/onyxsymres.c index a4a855ae..cf9e0a3a 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -523,7 +523,18 @@ void onyx_resolve_symbols() { // NOTE: Add types to global scope BuiltinSymbol* bsym = (BuiltinSymbol *) &builtin_symbols[0]; while (bsym->sym != NULL) { - symbol_builtin_introduce(semstate.curr_scope, bsym->sym, bsym->node); + if (bsym->package == NULL) + symbol_builtin_introduce(semstate.curr_scope, bsym->sym, bsym->node); + else { + Package* p = program_info_package_lookup_or_create( + semstate.program, + bsym->package, + semstate.curr_scope, + semstate.node_allocator); + assert(p); + + symbol_builtin_introduce(p->scope, bsym->sym, bsym->node); + } bsym++; }