updated syntax definitions
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 12 Nov 2021 22:10:36 +0000 (16:10 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 12 Nov 2021 22:10:36 +0000 (16:10 -0600)
core/container/iter.onyx
misc/onyx.vim
misc/vscode/onyx-0.0.1.vsix
misc/vscode/syntaxes/onyx.tmLanguage

index f09f2e2c146d31a04bd3bbd8c04c2fc212b55030..dc04d67fae8d053a85778e79068d2e95b079216f 100644 (file)
@@ -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,
+    };
 }
index 7f750f832855eb81962a0a2f12018b63116b8814..ad510de219e7599290ad11bb77578487ee8866c9 100644 (file)
@@ -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
index 420a7b7850a946bcd1b0b121ffd57b194b5629e0..17a8a2e04d625beb495cc9bd35776b460c5a213f 100644 (file)
Binary files a/misc/vscode/onyx-0.0.1.vsix and b/misc/vscode/onyx-0.0.1.vsix differ
index 2f651c031b71462a7a7f0e30a8e0913626474e1d..1ea8cb7232e775a390e17b1017055d69cdbfbbe2 100644 (file)
                                </dict>
                                <dict>
                                        <key>match</key>
-                    <string>\b(use|macro|package)\b</string>
+                    <string>\b(use|macro|package|where)\b</string>
                                        <key>name</key>
                                        <string>keyword.control.onyx</string>
                                </dict>
                                </dict>
                                <dict>
                                        <key>match</key>
-                                       <string>\b(struct|enum)\b</string>
+                                       <string>\b(struct|enum|interface)\b</string>
                                        <key>name</key>
                                        <string>storage.type.onyx</string>
                                </dict>
                        <array>
                                <dict>
                                        <key>match</key>
-                                       <string>\b(struct|enum)\b(?:(\{)(\}))?</string>
+                                       <string>\b(struct|enum|interface)\b(?:(\{)(\}))?</string>
                                        <key>name</key>
                                        <string>storage.type.onyx</string>
                                </dict>
                                </dict>
                                <dict>
                                        <key>match</key>
-                                       <string>\b(\b[[:alpha:]_]+[[:alnum:]_]*\b)\s*[:]\s*[:]\s*(struct|enum)</string>
+                                       <string>\b(\b[[:alpha:]_]+[[:alnum:]_]*\b)\s*[:]\s*[:]\s*(struct|enum|interface)</string>
                                        <key>captures</key>
                                        <dict>
                                                <key>1</key>