Skip to content

Commit

Permalink
change syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 8, 2023
1 parent aa5273e commit c1434f3
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> { f: T; }` => `Struct:<T>(f: @default(T))`.
- [ ] Lambdas + Closures: `sum := |a: int32, b: int32| [my_inherited_var] a + b;`.
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/process.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/ast/Decl.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
" "
Expand Down
6 changes: 3 additions & 3 deletions lib/rivet/src/ast/Sym.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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 {};
Expand Down
36 changes: 18 additions & 18 deletions lib/rivet/src/ast/Table.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
])
];
Expand Down Expand Up @@ -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)),
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/checker/match_expr.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/codegen/decls.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/lib.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
7 changes: 4 additions & 3 deletions lib/rivet/src/parser/exprs.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/prefs/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/resolver/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/src/process/mod.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions lib/std/src/semver/range.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)]);
}
2 changes: 1 addition & 1 deletion rivetc/src/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "!"
Expand Down
2 changes: 1 addition & 1 deletion rivetc/src/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions rivetc/src/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down

0 comments on commit c1434f3

Please sign in to comment.