From: Brendan Hansen Date: Thu, 17 Jun 2021 19:15:10 +0000 (-0500) Subject: added warning for using 'proc' keyword; got rid of proc from core libraries X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c739600971a66c59ba1775541fc67d847219915b;p=onyx.git added warning for using 'proc' keyword; got rid of proc from core libraries --- diff --git a/bin/onyx b/bin/onyx index a477f85f..828b2c92 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/bin/test b/bin/test index 4b485717..c7766037 100755 --- a/bin/test +++ b/bin/test @@ -8,6 +8,12 @@ print_check() { fi } +if command -v wasmer >/dev/null; then + printf "Using $(wasmer --version)\n" +else + printf "Using $(node --verison)\n" +fi + failed=0 for test_file in $(find tests/ -name '*.onyx'); do filename=$(basename -- "$test_file") @@ -16,16 +22,30 @@ for test_file in $(find tests/ -name '*.onyx'); do print_check "$name" - if ! ./bin/onyx -r js --use-post-mvp-features "$test_file" -o "./tests/$name.wasm" >/dev/null; then - printf "\n❌ Failed to compile $name.onyx.\n" - failed=1 - continue - fi - - if ! ./bin/onyx-js "./tests/$name.wasm" > ./tmpoutput; then - printf "\n❌ Failed to run $name.onyx.\n" - failed=1 - continue + if command -v wasmer >/dev/null; then + if ! ./bin/onyx --use-post-mvp-features "$test_file" -o "./tests/$name.wasm" >/dev/null; then + printf "\n❌ Failed to compile $name.onyx.\n" + failed=1 + continue + fi + + if ! wasmer "./tests/$name.wasm" > ./tmpoutput; then + printf "\n❌ Failed to run $name.onyx.\n" + failed=1 + continue + fi + else + if ! ./bin/onyx -r js --use-post-mvp-features "$test_file" -o "./tests/$name.wasm" >/dev/null; then + printf "\n❌ Failed to compile $name.onyx.\n" + failed=1 + continue + fi + + if ! ./bin/onyx-js "./tests/$name.wasm" > ./tmpoutput; then + printf "\n❌ Failed to run $name.onyx.\n" + failed=1 + continue + fi fi if ! diff ./tmpoutput "$dirname/$name" >/dev/null; then diff --git a/core/builtin.onyx b/core/builtin.onyx index b7bde249..f43a1bb7 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -22,7 +22,7 @@ vararg :: #type ^struct { count: i32; } -vararg_get :: proc { +vararg_get :: #match { (va: vararg, ret: ^$T) -> bool { if va.count <= 0 do return false; *ret = *cast(^T) va.data; diff --git a/core/container/array.onyx b/core/container/array.onyx index bfc16120..bae2d242 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -159,7 +159,7 @@ concat :: (arr: ^[..] $T, other: [..] T) { } // Uses '==' to compare for equality. -contains :: proc { +contains :: #match { (arr: ^[..] $T, x: T) -> bool { for it: *arr do if it == x do return true; return false; @@ -172,7 +172,7 @@ contains :: proc { } // Uses '+' to sum. -sum :: proc { +sum :: #match { (arr: ^[..] $T, start: T = 0) -> T { sum := start; for it: *arr do sum += it; @@ -186,7 +186,7 @@ sum :: proc { } } -product :: proc { +product :: #match { (arr: ^[..] $T, start: T = 1) -> T { sum := start; for it: *arr do sum *= it; @@ -215,7 +215,7 @@ to_slice :: (arr: ^[..] $T) -> [] T { ** Simple insertion sort ** cmp should return >0 if left > right */ -sort :: proc { +sort :: #match { (arr: ^[..] $T, cmp: (T, T) -> i32) { for i: 1 .. arr.count { x := arr.data[i]; @@ -251,7 +251,7 @@ sort :: proc { } } -fold :: proc { +fold :: #match { (arr: ^[..] $T, init: $R, f: (T, R) -> R) -> R { val := init; for it: *arr do val = f(it, val); @@ -265,7 +265,7 @@ fold :: proc { } } -map :: proc { +map :: #match { (arr: ^[..] $T, f: (^T) -> void) do for ^it: *arr do f(it);, (arr: ^[..] $T, f: (T) -> T) do for ^it: *arr do *it = f(*it);, (arr: ^[..] $T, data: $R, f: (^T, R) -> void) do for ^it: *arr do f(it, data);, @@ -330,7 +330,7 @@ find_ptr :: (arr: ^[..] $T, value: T) -> ^T { return null; } -count_where :: proc { +count_where :: #match { (arr: ^[..] $T, predicate: (T) -> bool) -> u32 { count: u32 = 0; for ^it: *arr do if predicate(*it) do count += 1; @@ -362,13 +362,13 @@ fold_idx_elem :: (arr: ^$T, count: i32, cmp: (T, T) -> bool) -> (i32, T) { #private_file cmp_greater :: (x: $T, y: T) -> bool do return x > y; #private_file cmp_less :: (x: $T, y: T) -> bool do return x < y; -greatest :: proc { +greatest :: #match { (arr: [..] $T) -> (i32, T) { return fold_idx_elem(arr.data, arr.count, cmp_greater); }, (arr: [] $T) -> (i32, T) { return fold_idx_elem(arr.data, arr.count, cmp_greater); }, (arr: [$N] $T) -> (i32, T) { return fold_idx_elem(cast(^T) arr, N, cmp_greater); }, } -least :: proc { +least :: #match { (arr: [..] $T) -> (i32, T) { return fold_idx_elem(arr.data, arr.count, cmp_less); }, (arr: [] $T) -> (i32, T) { return fold_idx_elem(arr.data, arr.count, cmp_less); }, (arr: [$N] $T) -> (i32, T) { return fold_idx_elem(cast(^T) arr, N, cmp_less); }, diff --git a/core/container/list.onyx b/core/container/list.onyx index a831b2a2..4521c787 100644 --- a/core/container/list.onyx +++ b/core/container/list.onyx @@ -85,10 +85,10 @@ fold :: (list: ^List($T), init: $R, f: (T, R) -> R) -> R { return val; } -map :: proc { - (list: ^List($T), f: (^T) -> void) { - } -} +// map :: #match { +// (list: ^List($T), f: (^T) -> void) { +// } +// } get_iterator :: (list: ^List($T)) -> Iterator(T) { iterator_next :: ($T: type_expr, data: rawptr) -> (T, bool) { diff --git a/core/hash.onyx b/core/hash.onyx index bf3b5028..19d7959e 100644 --- a/core/hash.onyx +++ b/core/hash.onyx @@ -1,6 +1,6 @@ package core.hash -to_u32 :: proc { +to_u32 :: #match { (key: rawptr) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; }, (key: i32) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; }, (key: i64) -> u32 { return cast(u32) (cast(u64) 0xcbf29ce7 ^ cast(u64) key); }, diff --git a/core/io/file.onyx b/core/io/file.onyx index 454e9f5a..c13f99a5 100644 --- a/core/io/file.onyx +++ b/core/io/file.onyx @@ -149,7 +149,7 @@ get_contents_from_file :: (file: File) -> str { return data[0 .. size]; } -get_contents :: proc { +get_contents :: #match { get_contents_from_file, (path: str) -> str { diff --git a/core/io/writer.onyx b/core/io/writer.onyx index 53dc88e5..27023c79 100644 --- a/core/io/writer.onyx +++ b/core/io/writer.onyx @@ -77,7 +77,7 @@ write_format :: (use writer: ^Writer, format: str, va: ...) { write_str(writer, conv.str_format_va(format, buffer[0 .. 2048], va)); } -write :: proc { +write :: #match { write_str, write_cstr, write_i32, write_f32, write_i64, write_f64, diff --git a/core/math.onyx b/core/math.onyx index c330fc97..11e6610e 100644 --- a/core/math.onyx +++ b/core/math.onyx @@ -143,7 +143,7 @@ atanh :: (t: $T) -> T { // which if it is an integer, will be 2! This should always return a floating point number! exp :: (p: $T) -> T do return pow(base = cast(T) E, p = p); -pow :: proc { +pow :: #match { // Fast implementation of power when raising to an integer power. (base: $T, p: i32) -> T { if base == 0 do return 0; @@ -230,13 +230,13 @@ log :: (a: $T, base: $R) -> T { // they would be however, the fact that these overloads are intrinsic means they cannot // be reference from the element section and therefore cannot be passed around or used // as values. -max :: proc { wasm.max_f32, wasm.max_f64, max_poly } +max :: #match { wasm.max_f32, wasm.max_f64, max_poly } max_poly :: (a: $T, b: T) -> T { if a >= b do return a; else do return b; } -min :: proc { wasm.min_f32, wasm.min_f64, min_poly } +min :: #match { wasm.min_f32, wasm.min_f64, min_poly } min_poly :: (a: $T, b: T) -> T { if a <= b do return a; else do return b; @@ -248,12 +248,12 @@ clamp :: (v: $T, lo: T, hi: T) -> T { return v; } -sqrt :: proc { wasm.sqrt_f32, wasm.sqrt_f64, sqrt_poly } +sqrt :: #match { wasm.sqrt_f32, wasm.sqrt_f64, sqrt_poly } sqrt_poly :: (x: $T) -> T { return ~~ wasm.sqrt_f64(~~ x); } -abs :: proc { wasm.abs_f32, wasm.abs_f64, abs_poly } +abs :: #match { wasm.abs_f32, wasm.abs_f64, abs_poly } abs_poly :: (x: $T) -> T { if x >= 0 do return x; else do return -x; @@ -265,7 +265,7 @@ sign :: (x: $T) -> T { return 0; } -copysign :: proc { wasm.copysign_f32, wasm.copysign_f64, copysign_poly } +copysign :: #match { wasm.copysign_f32, wasm.copysign_f64, copysign_poly } copysign_poly :: (x: $T, y: T) -> T { return abs(x) * sign(y); } @@ -277,10 +277,10 @@ copysign_poly :: (x: $T, y: T) -> T { // Floating point rounding // -ceil :: proc { wasm.ceil_f32, wasm.ceil_f64 } -floor :: proc { wasm.floor_f32, wasm.floor_f64 } -trunc :: proc { wasm.trunc_f32, wasm.trunc_f64 } -nearest :: proc { wasm.nearest_f32, wasm.nearest_f64 } +ceil :: #match { wasm.ceil_f32, wasm.ceil_f64 } +floor :: #match { wasm.floor_f32, wasm.floor_f64 } +trunc :: #match { wasm.trunc_f32, wasm.trunc_f64 } +nearest :: #match { wasm.nearest_f32, wasm.nearest_f64 } @@ -288,11 +288,11 @@ nearest :: proc { wasm.nearest_f32, wasm.nearest_f64 } // Integer operations // -clz :: proc { wasm.clz_i32, wasm.clz_i64 } -ctz :: proc { wasm.ctz_i32, wasm.ctz_i64 } -popcnt :: proc { wasm.popcnt_i32, wasm.popcnt_i64 } -rotate_left :: proc { wasm.rotl_i32, wasm.rotl_i64 } -rotate_right :: proc { wasm.rotr_i32, wasm.rotr_i64 } +clz :: #match { wasm.clz_i32, wasm.clz_i64 } +ctz :: #match { wasm.ctz_i32, wasm.ctz_i64 } +popcnt :: #match { wasm.popcnt_i32, wasm.popcnt_i64 } +rotate_left :: #match { wasm.rotl_i32, wasm.rotl_i64 } +rotate_right :: #match { wasm.rotr_i32, wasm.rotr_i64 } diff --git a/core/memory.onyx b/core/memory.onyx index 3209b438..ec57fae9 100644 --- a/core/memory.onyx +++ b/core/memory.onyx @@ -44,7 +44,7 @@ make_slice :: ($T: type_expr, count: i32, allocator := context.allocator) -> [] }; } -align :: proc { +align :: #match { (size: ^u64, align: u64) { if *size % align != 0 { *size += align - (*size % align); diff --git a/core/stdio.onyx b/core/stdio.onyx index a81f1a4f..1724fef7 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -14,7 +14,7 @@ package core auto_flush_stdio := true -print :: proc { +print :: #match { (x: str) { io.write(^print_writer, x); if x[x.count - 1] == #char "\n" && auto_flush_stdio do __flush_stdio(); @@ -35,7 +35,7 @@ printf :: (format: str, va: ...) { } // This works on both slices and arrays -print_array :: proc { +print_array :: #match { (arr: [$N] $T, sep := " ") { for i: 0 .. N { print(arr[i]); diff --git a/core/string.onyx b/core/string.onyx index 9bd6e4d1..422ac6ec 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -24,7 +24,7 @@ from_cstr :: (s: cstr) -> str { free :: (s: str, allocator := context.allocator) do raw_free(allocator, s.data); -length :: proc { +length :: #match { (s: str) -> u32 { return s.count; }, @@ -41,7 +41,7 @@ length :: proc { }, } -concat :: proc { +concat :: #match { (s1: str, s2: str, allocator := context.allocator) -> str { len1 := length(s1); len2 := length(s2); @@ -103,7 +103,7 @@ split :: (s: str, delim: u8, allocator := context.allocator) -> []str { return strarr[0 .. delim_count + 1]; } -contains :: proc { +contains :: #match { (s: str, c: u8) -> bool { for ch: s do if ch == c do return true; return false; @@ -170,7 +170,7 @@ ends_with :: (s: str, suffix: str) -> bool { } -strip_leading_whitespace :: proc { +strip_leading_whitespace :: #match { (s: ^str) { while true do switch s.data[0] { case #char " ", #char "\t", #char "\n", #char "\r" { @@ -189,7 +189,7 @@ strip_leading_whitespace :: proc { }, } -strip_trailing_whitespace :: proc { +strip_trailing_whitespace :: #match { (s: ^str) { while true do switch s.data[s.count - 1] { case #char " ", #char "\t", #char "\n", #char "\r" { @@ -207,7 +207,7 @@ strip_trailing_whitespace :: proc { }, } -trim_start :: proc { +trim_start :: #match { (s: ^str, char: u8) { while s.data[0] == char { s.data += 1; @@ -222,7 +222,7 @@ trim_start :: proc { } } -trim_end :: proc { +trim_end :: #match { (s: ^str, char: u8) { while s.data[s.count - 1] == char { s.count -= 1; @@ -236,7 +236,7 @@ trim_end :: proc { } } -advance :: proc { +advance :: #match { (s: ^str, chars := 1) { chars = math.min(chars, s.count); diff --git a/core/string/builder.onyx b/core/string/builder.onyx index afbfc8ec..b5e7f635 100644 --- a/core/string/builder.onyx +++ b/core/string/builder.onyx @@ -13,19 +13,19 @@ Builder :: struct { data : [..] u8; } -make :: proc (initial_cap := 4, alloc := context.allocator) -> Builder { +make :: (initial_cap := 4, alloc := context.allocator) -> Builder { builder : Builder; array.init(^builder.data, initial_cap, allocator = alloc); return builder; } -clear :: proc (use sb: ^Builder) -> ^Builder { +clear :: (use sb: ^Builder) -> ^Builder { data.count = 0; return sb; } -add_str :: proc (use sb: ^Builder, s: str) -> ^Builder { +add_str :: (use sb: ^Builder, s: str) -> ^Builder { len_total := data.count + s.count; if data.capacity < len_total { @@ -37,31 +37,31 @@ add_str :: proc (use sb: ^Builder, s: str) -> ^Builder { return sb; } -add_cstr :: proc (use sb: ^Builder, cstring: cstr) -> ^Builder { +add_cstr :: (use sb: ^Builder, cstring: cstr) -> ^Builder { s := string.from_cstr(cstring); return add_str(sb, s); } -add_i64 :: proc (use sb: ^Builder, n: i64, base: u64 = 10) -> ^Builder { +add_i64 :: (use sb: ^Builder, n: i64, base: u64 = 10) -> ^Builder { buf : [256] u8; s := conv.i64_to_str(n, base, buf[0 .. 256]); return add_str(sb, s); } -add_f64 :: proc (use sb: ^Builder, f: f64) -> ^Builder { +add_f64 :: (use sb: ^Builder, f: f64) -> ^Builder { buf : [256] u8; s := conv.f64_to_str(f, buf[0 .. 256]); return add_str(sb, s); } -add_bool :: proc (use sb: ^Builder, b: bool) -> ^Builder { +add_bool :: (use sb: ^Builder, b: bool) -> ^Builder { if b do return add_str(sb, "true"); else do return add_str(sb, "false"); return sb; } -append :: proc { +append :: #match { add_str, add_cstr, add_i64, @@ -69,7 +69,7 @@ append :: proc { add_bool, } -to_str :: proc (use sb: ^Builder) -> str { +to_str :: (use sb: ^Builder) -> str { return str.{ data.data, data.count }; } diff --git a/core/string/reader.onyx b/core/string/reader.onyx index ed690798..0cea068a 100644 --- a/core/string/reader.onyx +++ b/core/string/reader.onyx @@ -9,26 +9,26 @@ String_Reader :: struct { original_str : str; } -make :: proc (s: str) -> String_Reader { +make :: (s: str) -> String_Reader { reader : String_Reader; init(^reader, s); return reader; } -init :: proc (use reader: ^String_Reader, orig_str: str) { +init :: (use reader: ^String_Reader, orig_str: str) { original_str = orig_str; data = original_str.data; count = original_str.count; } -reset :: proc (use reader: ^String_Reader) { +reset :: (use reader: ^String_Reader) { data = original_str.data; count = original_str.count; } -empty :: proc (use reader: ^String_Reader) -> bool do return count == 0; +empty :: (use reader: ^String_Reader) -> bool do return count == 0; -read_u32 :: proc (use reader: ^String_Reader) -> u32 { +read_u32 :: (use reader: ^String_Reader) -> u32 { n: u32 = 0; skip_whitespace(reader); @@ -44,7 +44,7 @@ read_u32 :: proc (use reader: ^String_Reader) -> u32 { return n; } -read_u64 :: proc (use reader: ^String_Reader) -> u64 { +read_u64 :: (use reader: ^String_Reader) -> u64 { n: u64 = 0; skip_whitespace(reader); @@ -60,7 +60,7 @@ read_u64 :: proc (use reader: ^String_Reader) -> u64 { return n; } -read_byte :: proc (use reader: ^String_Reader) -> u8 { +read_byte :: (use reader: ^String_Reader) -> u8 { if count == 0 do return #char "\0"; defer { @@ -71,7 +71,7 @@ read_byte :: proc (use reader: ^String_Reader) -> u8 { return data[0]; } -read_bytes :: proc (use reader: ^String_Reader, byte_count := 1) -> str { +read_bytes :: (use reader: ^String_Reader, byte_count := 1) -> str { bc := byte_count; if count < bc do bc = count; @@ -83,7 +83,7 @@ read_bytes :: proc (use reader: ^String_Reader, byte_count := 1) -> str { return str.{ data, bc }; } -read_line :: proc (use reader: ^String_Reader) -> str { +read_line :: (use reader: ^String_Reader) -> str { out : str; out.data = data; out.count = 0; @@ -104,7 +104,7 @@ read_line :: proc (use reader: ^String_Reader) -> str { return out; } -read_until :: proc (use reader: ^String_Reader, skip: u32, uptos: ..u8) -> str { +read_until :: (use reader: ^String_Reader, skip: u32, uptos: ..u8) -> str { if count == 0 do return str.{ null, 0 }; out : str; @@ -131,7 +131,7 @@ read_until :: proc (use reader: ^String_Reader, skip: u32, uptos: ..u8) -> str { } // Reads a continuous string of alphabetic chars along with underscores '_' -read_word :: proc (use reader: ^String_Reader) -> str { +read_word :: (use reader: ^String_Reader) -> str { if count == 0 do return str.{ null, 0 }; out := str.{ data, 0 }; @@ -150,7 +150,7 @@ read_word :: proc (use reader: ^String_Reader) -> str { return out; } -advance_line :: proc (use reader: ^String_Reader) { +advance_line :: (use reader: ^String_Reader) { if count == 0 do return; adv := 0; @@ -161,7 +161,7 @@ advance_line :: proc (use reader: ^String_Reader) { } -skip_whitespace :: proc (use reader: ^String_Reader) { +skip_whitespace :: (use reader: ^String_Reader) { while count > 0 do switch data[0] { case #char " ", #char "\t", #char "\n", #char "\r" { data += 1; @@ -172,7 +172,7 @@ skip_whitespace :: proc (use reader: ^String_Reader) { } } -skip_bytes :: proc (use reader: ^String_Reader, byte_count := 1) { +skip_bytes :: (use reader: ^String_Reader, byte_count := 1) { bc := byte_count; if count < bc do bc = count; @@ -182,7 +182,7 @@ skip_bytes :: proc (use reader: ^String_Reader, byte_count := 1) { -starts_with :: proc (use reader: ^String_Reader, s: str) -> bool { +starts_with :: (use reader: ^String_Reader, s: str) -> bool { if count < s.count do return false; while i := 0; i < s.count { if data[i] != s[i] do return false; diff --git a/core/wasi/clock.onyx b/core/wasi/clock.onyx index 18b19b59..0760f2d5 100644 --- a/core/wasi/clock.onyx +++ b/core/wasi/clock.onyx @@ -18,7 +18,7 @@ time_ns :: () -> u64 { return ~~output_time; } -sleep :: proc { +sleep :: #match { (seconds: u64) { sleep(nanoseconds=seconds * 1000000000); }, (milliseconds: u64) { sleep(nanoseconds=milliseconds * 1000000); }, diff --git a/include/onyxerrors.h b/include/onyxerrors.h index 58b39650..993f4f70 100644 --- a/include/onyxerrors.h +++ b/include/onyxerrors.h @@ -25,6 +25,7 @@ extern OnyxErrors msgs; void onyx_errors_init(bh_arr(bh_file_contents)* files); void onyx_report_error(OnyxFilePos pos, char * format, ...); +void onyx_report_warning(OnyxFilePos pos, char* format, ...); void onyx_errors_print(); b32 onyx_has_errors(); void onyx_clear_errors(); diff --git a/misc/onyx.sublime-syntax b/misc/onyx.sublime-syntax index 950dddea..0e95db8a 100644 --- a/misc/onyx.sublime-syntax +++ b/misc/onyx.sublime-syntax @@ -26,7 +26,7 @@ contexts: # strings in YAML. When using single quoted strings, only single quotes # need to be escaped: this is done by using two single quotes next to each # other. - - match: '\b(package|struct|proc|use|global|enum|if|elseif|else|for|while|do|break|continue|fallthrough|return|as|cast|sizeof|alignof|defer|switch|case)\b' + - match: '\b(package|struct|use|global|enum|if|elseif|else|for|while|do|break|continue|fallthrough|return|as|cast|sizeof|alignof|defer|switch|case)\b' scope: keyword.control.onyx - match: '\b(bool|void|i8|u8|i16|u16|i32|u32|i64|u64|f32|f64|rawptr|str|cstr|range|type_expr)\b' @@ -54,10 +54,10 @@ contexts: - match: '\$[a-zA-Z0-9_]+' scope: constant.other.onyx - - match: '([a-zA-Z_][a-zA-Z0-9_]*)\s*::\s*(proc)' + - match: '([a-zA-Z_][a-zA-Z0-9_]*)\s*::\s*(#match)' captures: 1: entity.name.function - 2: keyword.control.onyx + 2: keyword.other.onyx - match: '([a-zA-Z_][a-zA-Z0-9_]*)\s*::\s*\(' captures: diff --git a/modules/immediate_mode/immediate_renderer.onyx b/modules/immediate_mode/immediate_renderer.onyx index 40c946e4..302f6482 100644 --- a/modules/immediate_mode/immediate_renderer.onyx +++ b/modules/immediate_mode/immediate_renderer.onyx @@ -128,7 +128,7 @@ Immediate_Renderer :: struct { vertex_count = 0; } - push_vertex :: proc { + push_vertex :: #match { // If a color is not provided, the previous color is used. (use ir: ^Immediate_Renderer, position: Vector2) { push_vertex(ir, position, color = verticies[vertex_count - 1].color); @@ -287,7 +287,7 @@ immediate_renderer_free :: () { Immediate_Renderer.free(^immediate_renderer); } -vertex :: proc { +vertex :: #match { (position: Vector2) { immediate_renderer->push_vertex(position); }, (position: Vector2, color: Color4) { immediate_renderer->push_vertex(position, color); }, } diff --git a/modules/json/types.onyx b/modules/json/types.onyx index fced87e0..74546d82 100644 --- a/modules/json/types.onyx +++ b/modules/json/types.onyx @@ -133,7 +133,7 @@ get_idx :: (v: ^Value, idx: i32) -> ^Value { return v_arr.array_[idx]; } -free :: proc { +free :: #match { (v: ^Value, allocator: Allocator) do switch v.type { case .String { v_str := cast(^Value_String) v; diff --git a/modules/ui/components/slider.onyx b/modules/ui/components/slider.onyx index 3a8bebfe..04859b48 100644 --- a/modules/ui/components/slider.onyx +++ b/modules/ui/components/slider.onyx @@ -73,7 +73,7 @@ slider :: (use r: Rectangle, value: ^$T, min_value: T, max_value: T, text: str, } #private_file -adjust_slider_value :: proc { +adjust_slider_value :: #match { @Incomplete // the step parameter is ignored. // Integers need to be (value: ^i32, x: f32, width: f32, min_value: i32, max_value: i32) { diff --git a/modules/ui/flow.onyx b/modules/ui/flow.onyx index 3079298a..03558ba9 100644 --- a/modules/ui/flow.onyx +++ b/modules/ui/flow.onyx @@ -3,7 +3,7 @@ package ui // UI Flow Flow :: struct { - split_vertical :: proc { + split_vertical :: #match { (r: Rectangle, left_percent: f32, padding := 0.0f) -> (left: Rectangle, right: Rectangle) { return split_vertical(r, left_width=left_percent * Rectangle.width(r), padding=padding); }, @@ -30,7 +30,7 @@ Flow :: struct { } - split_horizontal :: proc { + split_horizontal :: #match { (r: Rectangle, top_percent: f32, padding := 0.0f) -> (top: Rectangle, bottom: Rectangle) { return split_horizontal(r, top_height=top_percent * Rectangle.height(r), padding=padding); }, diff --git a/modules/ui/ui.onyx b/modules/ui/ui.onyx index 526c2ca1..197ffce9 100644 --- a/modules/ui/ui.onyx +++ b/modules/ui/ui.onyx @@ -176,7 +176,7 @@ draw_text_raw :: (text: str, x: f32, y: f32, size := DEFAULT_TEXT_SIZE, color := gl.bindTexture(gl.TEXTURE_2D, -1); } -draw_rect :: proc { +draw_rect :: #match { (use r: Rectangle, color := gfx.Color4.{1,1,1}) { gfx.set_texture(); diff --git a/modules/vecmath/vector2.onyx b/modules/vecmath/vector2.onyx index 1363907a..d3b5313e 100644 --- a/modules/vecmath/vector2.onyx +++ b/modules/vecmath/vector2.onyx @@ -43,7 +43,7 @@ vector2_equal :: (a: Vector2($T), b: Vector2(T)) -> bool { return a.x == b.x && a.y == b.y; } -#add_overload io.write, vector2_write +#add_match io.write, vector2_write vector2_write :: (writer: ^io.Writer, v: Vector2($T)) { io.write(writer, "Vector2("); io.write(writer, v.x); diff --git a/modules/webgl2/webgl2.onyx b/modules/webgl2/webgl2.onyx index d59501b6..0b20cfb2 100644 --- a/modules/webgl2/webgl2.onyx +++ b/modules/webgl2/webgl2.onyx @@ -744,7 +744,7 @@ blendFuncSeparate :: (srcRGB: GLenum, dstRGB: GLenum, srcAlpha: GLe blitFramebuffer :: (sx0: GLint, sy0: GLint, sx1: GLint, sy1: GLint, dx0: GLint, dy0: GLint, dx1: GLint, dy1: GLint, mask: GLbitfield, filter: GLenum) -> void #foreign "gl" "blitFramebuffer" --- bufferDataWithData :: (target: GLenum, buffer: [] void, usage: GLenum) -> void #foreign "gl" "bufferDataWithData" --- bufferDataNoData :: (target: GLenum, size: GLsizei, usage: GLenum) -> void #foreign "gl" "bufferDataNoData" --- -bufferData :: proc { bufferDataWithData, bufferDataNoData } +bufferData :: #match { bufferDataWithData, bufferDataNoData } bufferSubData :: (target: GLenum, offset: GLsizei, data: [] void) -> void #foreign "gl" "bufferSubData" --- canvasSize :: (width: GLsizei, height: GLsizei) -> void #foreign "gl" "canvasSize" --- checkFrameBufferStatus :: (target: GLenum) -> GLenum #foreign "gl" "checkFrameBufferStatus" --- diff --git a/src/onyxerrors.c b/src/onyxerrors.c index 81f009de..81a3cbab 100644 --- a/src/onyxerrors.c +++ b/src/onyxerrors.c @@ -12,21 +12,6 @@ void onyx_errors_init(bh_arr(bh_file_contents)* files) { bh_arr_new(global_heap_allocator, errors.errors, 4); } -void onyx_report_error(OnyxFilePos pos, char * format, ...) { - - va_list vargs; - va_start(vargs, format); - char* msg = bh_bprintf_va(format, vargs); - va_end(vargs); - - OnyxError err = { - .pos = pos, - .text = bh_strdup(errors.msg_alloc, msg), - }; - - bh_arr_push(errors.errors, err); -} - static void print_detailed_message(OnyxError* err, bh_file_contents* fc) { bh_printf("(%s:%l,%l) %s\n", err->pos.filename, err->pos.line, err->pos.column, err->text); @@ -79,4 +64,41 @@ b32 onyx_has_errors() { void onyx_clear_errors() { bh_printf("ERRORS WERE CLEARED!!!\n"); bh_arr_set_length(errors.errors, 0); -} \ No newline at end of file +} + +void onyx_report_error(OnyxFilePos pos, char * format, ...) { + + va_list vargs; + va_start(vargs, format); + char* msg = bh_bprintf_va(format, vargs); + va_end(vargs); + + OnyxError err = { + .pos = pos, + .text = bh_strdup(errors.msg_alloc, msg), + }; + + bh_arr_push(errors.errors, err); +} + +void onyx_report_warning(OnyxFilePos pos, char* format, ...) { + va_list vargs; + va_start(vargs, format); + char* msg = bh_bprintf_va(format, vargs); + va_end(vargs); + + OnyxError err = { + .pos = pos, + .text = msg, + }; + + bh_file_contents file_contents = { 0 }; + bh_arr_each(bh_file_contents, fc, *errors.file_contents) { + if (!strcmp(fc->filename, pos.filename)) { + file_contents = *fc; + break; + } + } + + print_detailed_message(&err, &file_contents); +} diff --git a/src/onyxparser.c b/src/onyxparser.c index 44347d15..fd805896 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -471,6 +471,7 @@ static AstTyped* parse_factor(OnyxParser* parser) { case Token_Type_Keyword_Proc: { OnyxToken* proc_token = expect_token(parser, Token_Type_Keyword_Proc); + onyx_report_warning(proc_token->pos, "Warning: 'proc' is a deprecated keyword."); retval = (AstTyped *) parse_function_definition(parser, proc_token); break; } @@ -1555,6 +1556,7 @@ static AstType* parse_type(OnyxParser* parser) { case Token_Type_Keyword_Proc: { OnyxToken* proc_token = expect_token(parser, Token_Type_Keyword_Proc); + onyx_report_warning(proc_token->pos, "Warning: 'proc' is a deprecated keyword."); *next_insertion = parse_function_type(parser, proc_token); next_insertion = NULL; break; @@ -1909,27 +1911,34 @@ static void parse_function_params(OnyxParser* parser, AstFunction* func) { return; } -static AstFunction* parse_function_definition(OnyxParser* parser, OnyxToken* token) { - if (consume_token_if_next(parser, '{')) { - AstOverloadedFunction* ofunc = make_node(AstOverloadedFunction, Ast_Kind_Overloaded_Function); - ofunc->token = token; - ofunc->flags |= Ast_Flag_Comptime; +static AstOverloadedFunction* parse_overloaded_function(OnyxParser* parser, OnyxToken* token) { + expect_token(parser, '{'); - bh_arr_new(global_heap_allocator, ofunc->overloads, 4); + AstOverloadedFunction* ofunc = make_node(AstOverloadedFunction, Ast_Kind_Overloaded_Function); + ofunc->token = token; + ofunc->flags |= Ast_Flag_Comptime; - while (!consume_token_if_next(parser, '}')) { - if (parser->hit_unexpected_token) return (AstFunction *) ofunc; + bh_arr_new(global_heap_allocator, ofunc->overloads, 4); + + while (!consume_token_if_next(parser, '}')) { + if (parser->hit_unexpected_token) return ofunc; - AstTyped* o_node = parse_expression(parser, 0); + AstTyped* o_node = parse_expression(parser, 0); - bh_arr_push(ofunc->overloads, o_node); + bh_arr_push(ofunc->overloads, o_node); - if (parser->curr->type != '}') - expect_token(parser, ','); - } - - ENTITY_SUBMIT(ofunc); - return (AstFunction *) ofunc; + if (parser->curr->type != '}') + expect_token(parser, ','); + } + + ENTITY_SUBMIT(ofunc); + return ofunc; +} + +static AstFunction* parse_function_definition(OnyxParser* parser, OnyxToken* token) { + // :TemporaryForProcRemoval + if (parser->curr->type == '{') { + return (AstFunction *) parse_overloaded_function(parser, token); } AstFunction* func_def = make_node(AstFunction, Ast_Kind_Function); @@ -1998,6 +2007,7 @@ static AstFunction* parse_function_definition(OnyxParser* parser, OnyxToken* tok static b32 parse_possible_function_definition(OnyxParser* parser, AstTyped** ret) { if (parser->curr->type == Token_Type_Keyword_Proc) { OnyxToken* proc_token = expect_token(parser, Token_Type_Keyword_Proc); + onyx_report_warning(proc_token->pos, "Warning: 'proc' is a deprecated keyword."); AstFunction* func_node = parse_function_definition(parser, proc_token); *ret = (AstTyped *) func_node; return 1; @@ -2162,6 +2172,7 @@ static AstIf* parse_static_if_stmt(OnyxParser* parser, b32 parse_block_as_statem static AstTyped* parse_top_level_expression(OnyxParser* parser) { if (parser->curr->type == Token_Type_Keyword_Proc) { OnyxToken* proc_token = expect_token(parser, Token_Type_Keyword_Proc); + onyx_report_warning(proc_token->pos, "Warning: 'proc' is a deprecated keyword."); AstFunction* func_node = parse_function_definition(parser, proc_token); return (AstTyped *) func_node; @@ -2176,6 +2187,13 @@ static AstTyped* parse_top_level_expression(OnyxParser* parser) { alias->to = parse_type(parser); return (AstTyped *) alias; } + + if (parse_possible_directive(parser, "match")) { + // :LinearTokenDependent + OnyxToken* directive_token = parser->curr - 2; + AstOverloadedFunction* ofunc = parse_overloaded_function(parser, directive_token); + return (AstTyped *) ofunc; + } return parse_expression(parser, 1); } @@ -2248,9 +2266,10 @@ static void parse_top_level_statement(OnyxParser* parser) { return; } - case Token_Type_Keyword_Proc: - parse_top_level_expression(parser); - return; + // case Token_Type_Keyword_Proc: + // onyx_report_warning(parser->curr->pos, "Warning: 'proc' is a deprecated keyword."); + // parse_top_level_expression(parser); + // return; case Token_Type_Symbol: { OnyxToken* symbol = expect_token(parser, Token_Type_Symbol); @@ -2347,7 +2366,7 @@ static void parse_top_level_statement(OnyxParser* parser) { ENTITY_SUBMIT(operator); return; } - else if (parse_possible_directive(parser, "add_overload")) { + else if (parse_possible_directive(parser, "add_overload") || parse_possible_directive(parser, "add_match")) { AstDirectiveAddOverload *add_overload = make_node(AstDirectiveAddOverload, Ast_Kind_Directive_Add_Overload); add_overload->token = dir_token; add_overload->overloaded_function = (AstNode *) parse_expression(parser, 0); diff --git a/tests/aoc-2020/day17.onyx b/tests/aoc-2020/day17.onyx index cfef3b25..9839aa0c 100644 --- a/tests/aoc-2020/day17.onyx +++ b/tests/aoc-2020/day17.onyx @@ -12,7 +12,7 @@ CubeState :: struct { next := false; } -#add_overload hash.to_u32, (c: CubePos) -> u32 { +#add_match hash.to_u32, (c: CubePos) -> u32 { return 17 * c.x + 13 * c.y + 11 * c.z + 19 * c.w; } diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index a2182c57..4979884a 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -7,7 +7,7 @@ Vec2 :: struct { y: i32 = 0; } -#add_overload hash.to_u32, (v: Vec2) -> u32 { +#add_match hash.to_u32, (v: Vec2) -> u32 { return v.x * 11 + v.y * 17; } diff --git a/tests/array_struct_robustness.onyx b/tests/array_struct_robustness.onyx index e201ae36..1668b9cf 100644 --- a/tests/array_struct_robustness.onyx +++ b/tests/array_struct_robustness.onyx @@ -5,7 +5,7 @@ use package core Vec2 :: struct { x: i32; y: i32; } // Overload print() to print Vec2's. -#add_overload io.write, (use writer: ^io.Writer, use v: Vec2) { +#add_match io.write, (use writer: ^io.Writer, use v: Vec2) { io.write_format(writer, "Vec2(%i, %i)", x, y); } diff --git a/tests/named_arguments_test.onyx b/tests/named_arguments_test.onyx index d369d294..4f993236 100644 --- a/tests/named_arguments_test.onyx +++ b/tests/named_arguments_test.onyx @@ -25,7 +25,7 @@ main :: (args: [] cstr) { println("\n\n========================="); - poly_overloaded :: proc { + poly_overloaded :: #match { (y: [$N] $T) { print("MATCHED Y: "); print_array(y); }, (x: $T) { print("MATCHED X: "); println(x); }, (z: $T) { print("MATCHED Z: "); println(z); }, diff --git a/tests/operator_overload.onyx b/tests/operator_overload.onyx index 4041ffa8..d3c0eefb 100644 --- a/tests/operator_overload.onyx +++ b/tests/operator_overload.onyx @@ -99,7 +99,7 @@ main :: (args: [] cstr) { println(test_overload("World!", "Hello!")); } -test_overload :: proc { +test_overload :: #match { (x: $T, y: T) -> T { return x; }, (x: $T, y: $R) -> R { return y; }, } diff --git a/tests/overload_with_autocast.onyx b/tests/overload_with_autocast.onyx index 1dcef3d3..2328fdb2 100644 --- a/tests/overload_with_autocast.onyx +++ b/tests/overload_with_autocast.onyx @@ -2,7 +2,7 @@ use package core -overloaded :: proc { +overloaded :: #match { (x: str, y: i32) do println("Called str, i32"); , (x: f32, y: str) do println("Called f32, str"); , (x: i32, y: i32) do println("Called i32, i32"); ,