Skip to content

Commit c7cc429

Browse files
committed
refact(rivet): remove .prefs field from some structs, use Env.prefs instead
1 parent a650349 commit c7cc429

34 files changed

+817
-819
lines changed

cmd/src/tools/cmd_fmt.ri

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ pub func cmd_fmt(args: []string) -> ! {
3939
);
4040
prefs_.check_input(input);
4141
prefs_.load_module_info()!;
42-
table_ := ast.Table.new(prefs_);
43-
mut builder := rivet.Builder(table: table_, prefs: prefs_);
42+
env := ast.Env.new(prefs_);
43+
mut builder := rivet.Builder(env: env);
4444
builder.load_root_module()!;
45-
mut formatter := fmt.Formatter(prefs_, table_, write_to_file: write_to_file);
46-
for source_file in table_.source_files {
45+
mut formatter := fmt.Formatter(env, write_to_file: write_to_file);
46+
for source_file in env.source_files {
4747
output := formatter.format(source_file);
4848
console.writeln(output);
4949
}

lib/rivet/src/ast/Builtin.ri

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright (C) 2023 The Rivet Developers. All rights reserved.
2+
// Use of this source code is governed by an MIT license that can
3+
// be found in the LICENSE file.
4+
5+
import ../token;
6+
7+
pub enum Builtin {
8+
Invalid,
9+
Const {
10+
name: string;
11+
type: Type;
12+
},
13+
Func {
14+
name: string;
15+
args: []BuiltinArg;
16+
type: Type;
17+
is_unsafe: bool;
18+
checks: []BuiltinFuncCheck;
19+
20+
func args_len(self) -> uint {
21+
mut l: uint := 0;
22+
for arg in self.args {
23+
if arg.is_optional {
24+
break;
25+
}
26+
l += 1;
27+
}
28+
return l;
29+
}
30+
};
31+
32+
pub func type(self) -> Type {
33+
return match self {
34+
.Const(b_const) -> b_const.type,
35+
.Func(b_func) -> b_func.type,
36+
else -> .Void
37+
};
38+
}
39+
}
40+
41+
struct BuiltinArg {
42+
pub name: string;
43+
pub is_mut: bool;
44+
pub is_any: bool; // any type
45+
pub type: Type;
46+
pub is_optional: bool;
47+
}
48+
49+
enum BuiltinFuncType {
50+
Pointer,
51+
Enum,
52+
Integer
53+
}
54+
55+
enum BuiltinFuncCheck {
56+
ReturnTypeEqualToArgumentType(uint),
57+
ArgumentTypeEqualToArgumentType {
58+
arg1_idx: uint;
59+
arg2_idx: uint;
60+
},
61+
ArgumentTypeShouldBe {
62+
arg_idx: uint;
63+
type: BuiltinFuncType;
64+
}
65+
}
66+
67+
extend Env {
68+
func setup_builtins(mut self) {
69+
self.builtins = +[
70+
// variables
71+
.Const("_FILE_", self.string_t),
72+
.Const("_LINE_", self.uint_t),
73+
.Const("_COLUMN_", self.uint_t),
74+
.Const("_FUNCTION_", self.string_t),
75+
.Const("_RIVET_VERSION_", self.string_t),
76+
.Const("_RIVET_COMMIT_", self.string_t),
77+
78+
// functions
79+
.Func("size_of", +[BuiltinArg("value", is_any: true)], self.uint_t),
80+
.Func("align_of", +[BuiltinArg("value", is_any: true)], self.uint_t),
81+
82+
.Func("ptr_add", +[
83+
BuiltinArg("pointer", is_any: true),
84+
BuiltinArg("value", is_any: true)
85+
], is_unsafe: true, checks: +[
86+
.ArgumentTypeShouldBe(0, .Pointer),
87+
.ArgumentTypeShouldBe(1, .Integer),
88+
.ReturnTypeEqualToArgumentType(0)
89+
]),
90+
.Func("ptr_diff", +[
91+
BuiltinArg("pointer", is_any: true),
92+
BuiltinArg("pointer2", is_any: true)
93+
], self.uint_t, is_unsafe: true, checks: +[
94+
.ArgumentTypeShouldBe(0, .Pointer),
95+
.ArgumentTypeEqualToArgumentType(1, 0)
96+
]),
97+
98+
.Func("as", +[
99+
BuiltinArg("type", is_any: true),
100+
BuiltinArg("value", is_any: true)
101+
], checks: +[
102+
.ReturnTypeEqualToArgumentType(0)
103+
]),
104+
105+
.Func("unreachable", +[], .Never(token.noPos)),
106+
.Func("breakpoint", +[]),
107+
.Func("assert", +[
108+
BuiltinArg("cond", type: self.bool_t),
109+
BuiltinArg("msg", type: self.string_t, is_optional: true)
110+
]),
111+
112+
.Func("set_enum_ref_value", +[
113+
BuiltinArg("enum_value", is_any: true, is_mut: true),
114+
BuiltinArg("new_value", is_any: true)
115+
], is_unsafe: true, checks: +[
116+
.ArgumentTypeShouldBe(0, .Enum),
117+
.ArgumentTypeEqualToArgumentType(1, 0)
118+
]),
119+
120+
// TODO: rename to `ignore_warn`: `@ignore_warn("not_mutated", expr)`.
121+
.Func("ignore_not_mutated_warn", +[
122+
BuiltinArg("expr", is_any: true, is_mut: true)
123+
])
124+
];
125+
}
126+
127+
pub func find_builtin(self, name: string) -> ?Builtin {
128+
for builtin in self.builtins {
129+
is_equal := match builtin {
130+
.Const(b_var) -> b_var.name == name,
131+
.Func(b_func) -> b_func.name == name,
132+
else -> false
133+
};
134+
if is_equal {
135+
return builtin;
136+
}
137+
}
138+
return none;
139+
}
140+
141+
pub func exist_builtin(self, name: string) -> bool {
142+
return self.find_builtin(name) != none;
143+
}
144+
}

lib/rivet/src/ast/CHeader.ri

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ enum CImportValue {
108108
}
109109
}
110110

111-
extend Table {
111+
extend Env {
112112
func setup_c_utils(mut self) {
113113
self.c_include_paths = self.get_include_paths();
114114
self.c_defines = self.get_global_defines();

0 commit comments

Comments
 (0)