From: Brendan Hansen Date: Fri, 12 Nov 2021 22:10:36 +0000 (-0600) Subject: updated syntax definitions X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=672e5da77f5f7ed25554963a45d5127489632190;p=onyx.git updated syntax definitions --- diff --git a/core/container/iter.onyx b/core/container/iter.onyx index f09f2e2c..dc04d67f 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -2,6 +2,12 @@ package core.iter use package core.intrinsics.onyx { __zero_value } +as_iterator :: #match {} + +Iterable :: interface (T: type_expr) { + as_iterator(T); +} + close :: (it: Iterator($T)) { it.close(it.data); } @@ -274,6 +280,24 @@ enumerate :: (it: Iterator($T), start_index: i32 = 0) -> Iterator(Enumeration_Va }; } +fold :: (it: Iterator($T), initial_value: R, combine: (T, $R) -> R) -> R { + for value: it { + initial_value = combine(value, initial_value); + } + + return initial_value; +} + +to_array :: (it: Iterator($T), allocator := context.allocator) -> [..] T { + array :: package core.array + + arr := array.make(T, allocator=allocator); + for v: it do array.push(^arr, v); + + return arr; +} + +#match as_iterator from_array from_array :: (arr: [] $T) -> Iterator(^T) { Context :: struct (T: type_expr) { data: ^T; @@ -307,19 +331,32 @@ from_array :: (arr: [] $T) -> Iterator(^T) { }; } -fold :: (it: Iterator($T), initial_value: R, combine: (T, $R) -> R) -> R { - for value: it { - initial_value = combine(value, initial_value); +#match as_iterator (r: range) -> Iterator(i32) { + Context :: struct { + r: range; + v: i32; } - - return initial_value; -} -to_array :: (it: Iterator($T), allocator := context.allocator) -> [..] T { - array :: package core.array + next :: (use c: ^Context) -> (i32, bool) { + if v > r.high { + return 0, false; + } else { + defer v += r.step; + return v, true; + } + } - arr := array.make(T, allocator=allocator); - for v: it do array.push(^arr, v); + close :: (c: ^Context) { + cfree(c); + } - return arr; + c := new(Context); + c.r = r; + c.v = r.low; + + return .{ + data = c, + next = next, + close = close, + }; } diff --git a/misc/onyx.vim b/misc/onyx.vim index 7f750f83..ad510de2 100644 --- a/misc/onyx.vim +++ b/misc/onyx.vim @@ -11,7 +11,7 @@ let s:cpo_save = &cpo set cpo&vim syn keyword onyxKeyword package struct enum proc use global macro -syn keyword onyxKeyword if elseif else +syn keyword onyxKeyword if elseif else where interface syn keyword onyxKeyword for while do syn keyword onyxKeyword switch case syn keyword onyxKeyword break continue return defer fallthrough diff --git a/misc/vscode/onyx-0.0.1.vsix b/misc/vscode/onyx-0.0.1.vsix index 420a7b78..17a8a2e0 100644 Binary files a/misc/vscode/onyx-0.0.1.vsix and b/misc/vscode/onyx-0.0.1.vsix differ diff --git a/misc/vscode/syntaxes/onyx.tmLanguage b/misc/vscode/syntaxes/onyx.tmLanguage index 2f651c03..1ea8cb72 100644 --- a/misc/vscode/syntaxes/onyx.tmLanguage +++ b/misc/vscode/syntaxes/onyx.tmLanguage @@ -164,7 +164,7 @@ match - \b(use|macro|package)\b + \b(use|macro|package|where)\b name keyword.control.onyx @@ -200,7 +200,7 @@ match - \b(struct|enum)\b + \b(struct|enum|interface)\b name storage.type.onyx @@ -230,7 +230,7 @@ match - \b(struct|enum)\b(?:(\{)(\}))? + \b(struct|enum|interface)\b(?:(\{)(\}))? name storage.type.onyx @@ -315,7 +315,7 @@ match - \b(\b[[:alpha:]_]+[[:alnum:]_]*\b)\s*[:]\s*[:]\s*(struct|enum) + \b(\b[[:alpha:]_]+[[:alnum:]_]*\b)\s*[:]\s*[:]\s*(struct|enum|interface) captures 1