From c1434f3433590ff7261099f252e9e9b431d2494d Mon Sep 17 00:00:00 2001 From: Jose Mendoza <56417208+StunxFS@users.noreply.github.com> Date: Fri, 8 Dec 2023 15:23:41 +0000 Subject: [PATCH] change syntax --- ROADMAP.md | 2 +- lib/core/src/process.c.ri | 4 ++-- lib/rivet/src/ast/Decl.ri | 2 +- lib/rivet/src/ast/Sym.ri | 6 ++--- lib/rivet/src/ast/Table.ri | 36 ++++++++++++++--------------- lib/rivet/src/checker/match_expr.ri | 2 +- lib/rivet/src/codegen/decls.ri | 2 +- lib/rivet/src/lib.ri | 2 +- lib/rivet/src/parser/exprs.ri | 7 +++--- lib/rivet/src/prefs/mod.ri | 2 +- lib/rivet/src/resolver/mod.ri | 2 +- lib/std/src/process/mod.c.ri | 2 +- lib/std/src/semver/range.ri | 6 ++--- rivetc/src/ast.py | 2 +- rivetc/src/checker.py | 2 +- rivetc/src/parser.py | 4 ++-- 16 files changed, 42 insertions(+), 41 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 41024db0f..d33c2e6cc 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -23,7 +23,7 @@ - [ ] (Atomic) Reference-Counting for traits, boxed enums, strings, dynamic arrays and structs. - [ ] Better support for embedded structs. - [ ] `undefined` for uninitialized variables: `x: [5]uint8 := undefined;`. -- [ ] Disallow empty array literal (`x := []!; -> ERROR`). +- [ ] Disallow empty array literal (`x := []; -> ERROR`). - [ ] Add `@is_flag_defined()` builtin function. - [ ] Generic support: `Struct { f: T; }` => `Struct:(f: @default(T))`. - [ ] Lambdas + Closures: `sum := |a: int32, b: int32| [my_inherited_var] a + b;`. diff --git a/lib/core/src/process.c.ri b/lib/core/src/process.c.ri index 764d68cb0..35b1883ae 100644 --- a/lib/core/src/process.c.ri +++ b/lib/core/src/process.c.ri @@ -6,7 +6,7 @@ import c/libc; /// Returns the path name of the executable that started the current process. pub func process_executable() -> !string { - res: [libc.MAX_PATH_LEN]uint8 := []!; + res: [libc.MAX_PATH_LEN]uint8 := []; count := unsafe { libc.readlink(c"/proc/self/exe", &res[0], libc.MAX_PATH_LEN) }; @@ -28,7 +28,7 @@ pub func process_set_cwd(path: string) -> ! { /// Returns the absolute path of the current working directory. pub func process_get_cwd() -> !string { unsafe { - buf: [libc.MAX_PATH_LEN]mut uint8 := []!; + buf: [libc.MAX_PATH_LEN]mut uint8 := []; if _ := libc.getcwd(&mut buf[0], libc.MAX_PATH_LEN) { return string.from_raw(&buf[0]).clone(); } diff --git a/lib/rivet/src/ast/Decl.ri b/lib/rivet/src/ast/Decl.ri index c48514210..3528a8d08 100644 --- a/lib/rivet/src/ast/Decl.ri +++ b/lib/rivet/src/ast/Decl.ri @@ -186,7 +186,7 @@ pub struct DocComment { func merge(self) -> string { mut res := strings.Builder.new(); for line in self.lines { - res.write_join([line, if line.is_empty() or line.ends_with(".") { + res.write_join(+[line, if line.is_empty() or line.ends_with(".") { "\n" } else { " " diff --git a/lib/rivet/src/ast/Sym.ri b/lib/rivet/src/ast/Sym.ri index 177f4475b..9bb155011 100644 --- a/lib/rivet/src/ast/Sym.ri +++ b/lib/rivet/src/ast/Sym.ri @@ -163,7 +163,7 @@ pub struct Module < Sym { is_public: true, name: unique_name, info: .Array(elem_typ, size, is_mut), - fields: [ + fields: +[ Field( name: "len", is_public: true, type: .Basic(self.scope.find_type_symbol_by_index_or_panic(11)) @@ -183,7 +183,7 @@ pub struct Module < Sym { is_public: true, name: unique_name, info: .DynArray(elem_type, is_mut), - fields: [ + fields: +[ Field( name: "len", is_public: true, type: .Basic(self.scope.find_type_symbol_by_index_or_panic(11)) @@ -200,7 +200,7 @@ pub struct Module < Sym { is_method: true, self_type: .Basic(type_sym), self_is_mut: true, - args: [Arg("value", type: elem_type, pos: token.noPos)], + args: +[Arg("value", type: elem_type, pos: token.noPos)], ret_type: .Void(), has_body: true )) catch {}; diff --git a/lib/rivet/src/ast/Table.ri b/lib/rivet/src/ast/Table.ri index 6d84da43b..df001920a 100644 --- a/lib/rivet/src/ast/Table.ri +++ b/lib/rivet/src/ast/Table.ri @@ -128,7 +128,7 @@ pub struct Table { } func setup_builtins(mut self) { - self.builtins = [ + self.builtins = +[ // variables .Const("_FILE_", self.string_t), .Const("_LINE_", self.uint_t), @@ -138,56 +138,56 @@ pub struct Table { .Const("_RIVET_COMMIT_", self.string_t), // functions - .Func("size_of", [BuiltinArg("value", is_any: true)], self.uint_t), - .Func("align_of", [BuiltinArg("value", is_any: true)], self.uint_t), + .Func("size_of", +[BuiltinArg("value", is_any: true)], self.uint_t), + .Func("align_of", +[BuiltinArg("value", is_any: true)], self.uint_t), - .Func("ptr_add", [ + .Func("ptr_add", +[ BuiltinArg("pointer", is_any: true), BuiltinArg("value", is_any: true) - ], is_unsafe: true, checks: [ + ], is_unsafe: true, checks: +[ .ArgumentTypeShouldBe(0, .Pointer), .ArgumentTypeShouldBe(1, .Integer), .ReturnTypeEqualToArgumentType(0) ]), - .Func("ptr_diff", [ + .Func("ptr_diff", +[ BuiltinArg("pointer", is_any: true), BuiltinArg("pointer2", is_any: true) - ], self.uint_t, is_unsafe: true, checks: [ + ], self.uint_t, is_unsafe: true, checks: +[ .ArgumentTypeShouldBe(0, .Pointer), .ArgumentTypeEqualToArgumentType(1, 0) ]), - .Func("as", [ + .Func("as", +[ BuiltinArg("type", is_any: true), BuiltinArg("value", is_any: true) - ], checks: [ + ], checks: +[ .ReturnTypeEqualToArgumentType(0) ]), - .Func("unreachable", [], .Never(token.noPos)), - .Func("breakpoint", []), - .Func("assert", [ + .Func("unreachable", +[], .Never(token.noPos)), + .Func("breakpoint", +[]), + .Func("assert", +[ BuiltinArg("cond", type: self.bool_t), BuiltinArg("msg", type: self.string_t, is_optional: true) ]), - .Func("dyn_array", [ + .Func("dyn_array", +[ BuiltinArg("type", is_any: true), BuiltinArg("cap", type: self.uint_t, is_optional: true) - ], checks: [ + ], checks: +[ .ReturnTypeEqualToArgumentType(0) ]), - .Func("set_enum_ref_value", [ + .Func("set_enum_ref_value", +[ BuiltinArg("enum_value", is_any: true, is_mut: true), BuiltinArg("new_value", is_any: true) - ], is_unsafe: true, checks: [ + ], is_unsafe: true, checks: +[ .ArgumentTypeShouldBe(0, .Enum), .ArgumentTypeEqualToArgumentType(1, 0) ]), // TODO: rename to `ignore_warn`: `@ignore_warn("not_mutated", expr)`. - .Func("ignore_not_mutated_warn", [ + .Func("ignore_not_mutated_warn", +[ BuiltinArg("expr", is_any: true, is_mut: true) ]) ]; @@ -497,7 +497,7 @@ pub struct Table { pub func universe() -> Module { return Module( name: "universe", - scope: Scope(syms: [ + scope: Scope(syms: +[ TypeSym(name: "bool", info: .Bool()), TypeSym(name: "rune", info: .Rune()), TypeSym(name: "int8", info: .SizedInt(8)), diff --git a/lib/rivet/src/checker/match_expr.ri b/lib/rivet/src/checker/match_expr.ri index 683b693b8..69439332f 100644 --- a/lib/rivet/src/checker/match_expr.ri +++ b/lib/rivet/src/checker/match_expr.ri @@ -168,7 +168,7 @@ extend Checker { match_expr.is_exhaustive = true; mut unhandled := @dyn_array(string); if expr_type == self.table.bool_t and match_expr.branches.len == 1 { - for v in ["true", "false"]! { + for v in ["true", "false"] { if !branch_exprs.contains(v) { match_expr.is_exhaustive = false; unhandled.push("`{}`".fmt(v)); diff --git a/lib/rivet/src/codegen/decls.ri b/lib/rivet/src/codegen/decls.ri index 864e3afe7..1df0b5dc3 100644 --- a/lib/rivet/src/codegen/decls.ri +++ b/lib/rivet/src/codegen/decls.ri @@ -57,7 +57,7 @@ extend Codegen { self.cur_func.add_block(self.cur_func.cur_block); self.gen_stmts(func_decl.stmts); self.cur_func.add_block( - mir.Block(stmts: [ + mir.Block(stmts: +[ if ret_type_is_void { .Return() } else { diff --git a/lib/rivet/src/lib.ri b/lib/rivet/src/lib.ri index c41c67885..d5f5b3aeb 100644 --- a/lib/rivet/src/lib.ri +++ b/lib/rivet/src/lib.ri @@ -103,7 +103,7 @@ pub struct Compiler { } filtered_files } else { - [self.prefs.input] + +[self.prefs.input] }; if files.is_empty() { utils.error("no input received"); diff --git a/lib/rivet/src/parser/exprs.ri b/lib/rivet/src/parser/exprs.ri index c81f6a56c..ee81c5510 100644 --- a/lib/rivet/src/parser/exprs.ri +++ b/lib/rivet/src/parser/exprs.ri @@ -291,7 +291,7 @@ extend Parser { self.next(); e := self.parse_expr(); if self.accept(.Comma) { // tuple - mut exprs: []mut ast.Expr := [e]; + mut exprs: []mut ast.Expr := +[e]; while { exprs.push(self.parse_expr()); if !self.accept(.Comma) { @@ -309,7 +309,8 @@ extend Parser { .Paren(e, pos + e.position() + self.prev_tok.pos) } }, - self.tok.kind == .Lbracket -> { + self.tok.kind in [.Lbracket, .Plus] -> { + is_dyn := self.accept(.Plus); mut elems := @dyn_array(mut ast.Expr); mut pos := self.tok.pos; self.next(); @@ -323,7 +324,7 @@ extend Parser { } pos += self.tok.pos; self.expect(.Rbracket); - .ArrayLiteral(elems, !self.accept(.Bang), pos) + .ArrayLiteral(elems, is_dyn, pos) }, self.tok.kind == .Name -> if self.peek_tok.kind == .Char { if self.tok.lit == "b" { diff --git a/lib/rivet/src/prefs/mod.ri b/lib/rivet/src/prefs/mod.ri index 413f919d2..153d375b2 100644 --- a/lib/rivet/src/prefs/mod.ri +++ b/lib/rivet/src/prefs/mod.ri @@ -59,7 +59,7 @@ pub struct Prefs { pub mut mod_dir: string; pub mut mod_output: string; - pub mut library_path: []string := [rivetcLibDir, libDir]; + pub mut library_path: []string := +[rivetcLibDir, libDir]; pub mut libraries_to_link: []string; pub mut objects_to_link: []string; diff --git a/lib/rivet/src/resolver/mod.ri b/lib/rivet/src/resolver/mod.ri index 26eadb575..d9c57801a 100644 --- a/lib/rivet/src/resolver/mod.ri +++ b/lib/rivet/src/resolver/mod.ri @@ -34,7 +34,7 @@ pub struct Resolver { if report.total_errors() > 0 { return; } - self.preludes = [ + self.preludes = +[ Prelude("Throwable", self.table.throwable_sym) ]; for sf in source_files { diff --git a/lib/std/src/process/mod.c.ri b/lib/std/src/process/mod.c.ri index 137b26be5..bc442717d 100644 --- a/lib/std/src/process/mod.c.ri +++ b/lib/std/src/process/mod.c.ri @@ -56,7 +56,7 @@ pub func execute(cmd: string) -> !Result { unsafe { if f := libc.popen(pcmd.ptr, c"r") { fd := libc.fileno(f); - buf: [4096]mut uint8 := []!; + buf: [4096]mut uint8 := []; pbuf: [&]mut uint8 := &mut buf[0]; mut output := Builder.new(1024); while { diff --git a/lib/std/src/semver/range.ri b/lib/std/src/semver/range.ri index 989a52026..b45b378be 100644 --- a/lib/std/src/semver/range.ri +++ b/lib/std/src/semver/range.ri @@ -156,7 +156,7 @@ struct Range { func expand_x(raw_range: string) -> ?ComparatorSet { min_ver := Range.parse_x(raw_range) ?? return none; if min_ver.major == 0 { - return ComparatorSet([Comparator(min_ver, .Ge)]); + return ComparatorSet(+[Comparator(min_ver, .Ge)]); } mut max_ver := min_ver; max_ver = if min_ver.minor == 0 { @@ -224,10 +224,10 @@ func expand_hyphen(raw_range: string) -> ?ComparatorSet { #[inline] func make_comparator_set_ge_lt(min: Version, max: Version) -> ComparatorSet { - return ComparatorSet([Comparator(min, .Ge), Comparator(max, .Lt)]); + return ComparatorSet(+[Comparator(min, .Ge), Comparator(max, .Lt)]); } #[inline] func make_comparator_set_ge_le(min: Version, max: Version) -> ComparatorSet { - return ComparatorSet([Comparator(min, .Ge), Comparator(max, .Le)]); + return ComparatorSet(+[Comparator(min, .Ge), Comparator(max, .Le)]); } diff --git a/rivetc/src/ast.py b/rivetc/src/ast.py index 9edbf8ab5..afdd3c4d4 100644 --- a/rivetc/src/ast.py +++ b/rivetc/src/ast.py @@ -556,7 +556,7 @@ def __repr__(self): if len(self.elems) == 0: if self.is_dyn: return "[]" - return "[]!" + return "[]" res = f"[{', '.join([str(e) for e in self.elems])}]" if not self.is_dyn: res += "!" diff --git a/rivetc/src/checker.py b/rivetc/src/checker.py index aaec77484..da1f7cd28 100644 --- a/rivetc/src/checker.py +++ b/rivetc/src/checker.py @@ -148,7 +148,7 @@ def check_decl(self, decl): field_typ = self.check_expr(decl.def_expr) self.expected_type = old_expected_type try: - self.check_compatible_types(field_typ, decl.typ) + self.check_types(field_typ, decl.typ) except utils.CompilerError as e: report.error(e.args[0], decl.pos) elif isinstance(decl, ast.ExtendDecl): diff --git a/rivetc/src/parser.py b/rivetc/src/parser.py index 234fdb648..8188fcc28 100644 --- a/rivetc/src/parser.py +++ b/rivetc/src/parser.py @@ -869,7 +869,8 @@ def parse_primary_expr(self): expr = ast.ParExpr(e, e.pos) elif self.tok.kind in (Kind.KwUnsafe, Kind.Lbrace): expr = self.parse_block_expr() - elif self.tok.kind == Kind.Lbracket: + elif self.tok.kind in (Kind.Lbracket, Kind.Plus): + is_dyn = self.accept(Kind.Plus) elems = [] pos = self.tok.pos self.next() @@ -879,7 +880,6 @@ def parse_primary_expr(self): if not self.accept(Kind.Comma): break self.expect(Kind.Rbracket) - is_dyn = not self.accept(Kind.Bang) expr = ast.ArrayLiteral(elems, is_dyn, pos) elif self.tok.kind == Kind.Name: if self.peek_tok.kind == Kind.Char: