Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 4, 2023
2 parents 9aee1be + caba9ad commit dc2bd05
Show file tree
Hide file tree
Showing 67 changed files with 545 additions and 505 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ The compiler comes with a number of libraries that are located in `lib/`:

* `c/`: Contains wrappers to the standard C library for both Linux and Windows.
* `core/`: This is the heart of Rivet, it contains the code that gives life to
the strings, vectors, the backtrace, etc.
the strings, dynamic arrays, the backtrace, etc.
* `std/`: The standard Rivet library, this module contains several submodules
with functions and types useful for development.
* `rivet/`: The self-hosted compiler code.
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- [ ] Constant-folding: `x := 2 * 2;` => `x := 4;`.
- [ ] Optimize code, inline functions/methods annotated with `inline`,
delete unused code, etc.
- [ ] (Atomic) Reference-Counting for traits, boxed enums, strings, vectors and structs.
- [ ] (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`).
Expand Down
14 changes: 7 additions & 7 deletions lib/core/src/Vector.ri → lib/core/src/DynArray.ri
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ struct DynArray {

func get(self, idx: uint) -> rawptr {
if idx >= self.len {
runtime_error("vector index out of range (index: {}, len: {})", idx, self.len);
runtime_error("dynamic array index out of range (index: {}, len: {})", idx, self.len);
}
return unsafe { @ptr_add(@as([&]mut uint8, self.ptr), idx * self.elem_size) };
}

func set(self, idx: uint, val: rawptr) {
if idx >= self.len {
runtime_error("vector index out of range (index: {}, len: {})", idx, self.len);
runtime_error("dynamic array index out of range (index: {}, len: {})", idx, self.len);
}
unsafe {
mem_copy(
Expand All @@ -66,11 +66,11 @@ struct DynArray {
self.len += 1;
}

/// Returns the last element of the vector, and removes it. If the
/// vector is empty, this will panic.
/// Returns the last element of the dynamic array, and removes it. If the
/// dynamic array is empty, this will panic.
func pop(mut self) -> rawptr {
if self.len == 0 {
runtime_error("Vec.pop: vector is empty");
runtime_error("DynArray.pop: dynamic array is empty");
}
new_len := self.len - 1;
self.len = new_len;
Expand All @@ -79,8 +79,8 @@ struct DynArray {

/// Deletes `size` elements beginning with index `i`.
/// NOTE: This function does NOT operate in-place. Internally, it creates a copy
/// of the vector, skipping over `size` elements starting at `i`, and then points
/// the original variable to the new memory location.
/// of the dynamic array, skipping over `size` elements starting at `i`, and then
/// points the original variable to the new memory location.
pub func delete(mut self, i: uint, size: uint := 1, no_slices: bool := false) {
if i + size > self.len {
end_idx := if size == 1 { "..{}".fmt(i + size) } else { "" };
Expand Down
14 changes: 7 additions & 7 deletions lib/core/src/StringFormatter.ri
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct StringFormatter {
args_idx += 1;
if arg_idx >= args.len {
runtime_error(
"string.fmt(): argument index out of range (argument index: {}, len: {}) at index {}",
"string.fmt: argument index out of range (argument index: {}, len: {}) at index {}",
arg_idx, args.len, self.i
);
}
Expand Down Expand Up @@ -60,15 +60,15 @@ pub struct StringFormatter {
self.i += 1;
if self.i >= self.buf.len {
runtime_error(
"string.fmt(): incomplete format string at index {}",
"string.fmt: incomplete format string at index {}",
start
);
}
}
index := buf.as_uint64();
if index >= args.len {
runtime_error(
"string.fmt(): argument index out of range (index: {}, len: {})",
"string.fmt: argument index out of range (index: {}, len: {})",
index, args.len
);
}
Expand All @@ -90,7 +90,7 @@ pub struct StringFormatter {
}
} else {
runtime_error(
"string.fmt(): expecting closing `}}` in format string at index {}",
"string.fmt: expecting closing `}}` in format string at index {}",
self.i
);
}
Expand All @@ -101,7 +101,7 @@ pub struct StringFormatter {
self.i += 1;
} else {
runtime_error(
"string.fmt(): single `}}` encountered in format string at index {}",
"string.fmt: single `}}` encountered in format string at index {}",
self.i
);
}
Expand All @@ -123,13 +123,13 @@ pub struct StringFormatter {
buf.push(unsafe { self.buf.ptr[self.i] });
self.i += 1;
if self.i >= self.buf.len {
runtime_error("string.fmt(): incomplete format string (index: {})", start);
runtime_error("string.fmt: incomplete format string (index: {})", start);
}
}
fwidth := buf.as_int();
if fwidth == 0 {
runtime_error(
"string.fmt(): invalid width value (cannot be 0 and cannot be omitted) -> (index: {})",
"string.fmt: invalid width value (cannot be 0 and cannot be omitted) -> (index: {})",
start
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/entry_point.ri
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern (Rivet) {

func init_args(_argc: uint, _argv: [&][&]uint8) {
unsafe {
ARGS = @vec(string, _argc);
ARGS = @dyn_array(string, _argc);
mut i: uint := 0;
while i < _argc : i += 1 {
ARGS.push(string.from_raw(_argv[i]));
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/rune.ri
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extend rune < Stringable {
}

pub func as_bytes(self) -> []uint8 {
res := @vec(uint8, 5);
res := @dyn_array(uint8, 5);
res_v := @as(DynArray, res);
res_v.len = utf32_decode_to_buffer(self, unsafe { @as([&]mut uint8, res_v.ptr) });
return res;
Expand Down Expand Up @@ -148,4 +148,4 @@ func utf8_to_utf32(bytes: []uint8) -> !rune {
shift = 6;
}
return res;
}
}
12 changes: 6 additions & 6 deletions lib/core/src/string.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub struct string < Stringable, Hashable, Throwable {
/// Returns a string array of the string split by '\t' and ' '.
pub func fields(self) -> []Self {
mut res := @vec(Self);
mut res := @dyn_array(Self);
mut word_start: uint := 0;
mut word_len: uint := 0;
mut is_in_word := false;
Expand Down Expand Up @@ -458,7 +458,7 @@ pub struct string < Stringable, Hashable, Throwable {
return self;
}
mut idx: uint := 0;
mut idxs := @vec(uint);
mut idxs := @dyn_array(uint);
while {
idx = self.index_after_of(rep, idx) ?? break;
idxs.push(idx);
Expand Down Expand Up @@ -508,7 +508,7 @@ pub struct string < Stringable, Hashable, Throwable {
/// Returns an array of all the UTF8 runes in the string `self` which is useful
/// if you want random access to them.
pub func as_runes(self) -> []rune {
mut runes := @vec(rune, self.runes_count());
mut runes := @dyn_array(rune, self.runes_count());
mut i: uint := 0;
while i < self.len : i += 1 {
char_len := unsafe { self.ptr[i] }.len_utf8();
Expand Down Expand Up @@ -541,7 +541,7 @@ pub struct string < Stringable, Hashable, Throwable {
/// remainder contains more `delim` substrings.
pub func split(self, delim: Self, nth: uint := 0) -> []Self {
mut i: uint := 0;
mut res := @vec(Self);
mut res := @dyn_array(Self);
match delim.len {
0 -> {
i = 1;
Expand Down Expand Up @@ -602,7 +602,7 @@ pub struct string < Stringable, Hashable, Throwable {
/// If the delimiter string is empty then `.split()` is used.
pub func split_any(self, delim: Self) -> []Self {
mut i: uint := 0;
mut res := @vec(Self);
mut res := @dyn_array(Self);
// check empty source string
if self.len > 0 {
// if empty delimiter string using default split
Expand Down Expand Up @@ -630,7 +630,7 @@ pub struct string < Stringable, Hashable, Throwable {
/// NOTE: algorithm is "greedy", consuming '\r\n' as a single line ending with higher
/// priority than '\r' and '\n' as multiple endings
pub func split_into_lines(self) -> []Self {
mut res := @vec(Self);
mut res := @dyn_array(Self);
if self.len == 0 {
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/tests/string_test.ri
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test "string.runes_count()" {

test "string.tokenize()" {
mut iterator := " abc def ghi ".tokenize(b' ');
mut res := @vec(string, 3);
mut res := @dyn_array(string, 3);
while w := iterator.next() {
res.push(w);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/core/tests/vector_test.ri
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test "vectors: push and pop" {
test "dynamic arrays: push and pop" {
mut vec := ["A", "B"];
@assert(vec.len == 2);

Expand All @@ -11,7 +11,7 @@ test "vectors: push and pop" {
@assert(vec.len == 2);
}

test "vectors: clear" {
test "dynamic arrays: clear" {
mut vec := ["A", "B"];
@assert(vec.len == 2);
@assert(vec[0] == "A");
Expand Down
4 changes: 2 additions & 2 deletions lib/rivet/src/ast/CHeader.ri
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extend Table {
mut name := tokens.next()?;
define_value := tokens.rest();
has_value := define_value.len > 0;
mut args := @vec(string);
mut args := @dyn_array(string);
mut is_macro := true;
if has_value {
if paren_idx := name.index_of_byte(b'(') {
Expand Down Expand Up @@ -157,7 +157,7 @@ extend Table {
lines := result.output.find_between(
"#include <...> search starts here:", "End of search list."
).split_into_lines();
mut paths := @vec(string);
mut paths := @dyn_array(string);
for line in lines {
if line.is_empty() {
continue;
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 @@ -210,7 +210,7 @@ pub struct ImportedMod {
}

pub func filter_field_decl(decls: []Decl) -> []Decl {
mut fields := @vec(Decl);
mut fields := @dyn_array(Decl);
for decl in decls {
if decl is .Field {
fields.push(decl);
Expand Down
12 changes: 6 additions & 6 deletions lib/rivet/src/ast/Expr.ri
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub enum Expr < traits.Stringable {
BuiltinCall {
name: string;
args: []CallArg;
vec_is_mut: bool;
dyn_array_is_mut: bool;
pos: token.Pos;
mut builtin: Builtin := .Invalid();
mut type: Type;
Expand Down Expand Up @@ -377,16 +377,16 @@ pub enum Expr < traits.Stringable {
sb.write_byte(b')');
sb.to_string()
},
.DynArrayLiteral as vector_lit -> {
.DynArrayLiteral as dyn_array_lit -> {
mut sb := strings.Builder.from_string("[");
for i, value in vector_lit.values {
for i, value in dyn_array_lit.values {
sb.write_string(value.to_string());
if i < vector_lit.values.len - 1 {
if i < dyn_array_lit.values.len - 1 {
sb.write_string(", ");
}
}
sb.write_byte(b']');
if vector_lit.is_arr {
if dyn_array_lit.is_arr {
sb.write_byte(b'!');
}
sb.to_string()
Expand Down Expand Up @@ -602,4 +602,4 @@ pub struct MatchBranch {
pub is_else: bool;
pub pos: token.Pos;
pub mut type: Type;
}
}
16 changes: 8 additions & 8 deletions lib/rivet/src/ast/Sym.ri
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub struct Module < Sym {
return type_sym;
}

pub func add_or_get_vec(mut self, elem_type: Type, is_mut: bool) -> TypeSym {
pub func add_or_get_dyn_array(mut self, elem_type: Type, is_mut: bool) -> TypeSym {
unique_name := if is_mut { "[]mut " } else { "[]" }.concat(elem_type.to_qualstring());
if type_sym := self.scope.find(unique_name) {
return @as(TypeSym, type_sym);
Expand Down Expand Up @@ -221,11 +221,11 @@ pub struct Module < Sym {
ret_type: .Basic(type_sym),
has_body: true
)) catch {};
if vector_sym := self.scope.find("core")?.scope.find("DynArray") {
type_sym.scope.add(vector_sym.scope.find("is_empty")?) catch {};
type_sym.scope.add(vector_sym.scope.find("delete")?) catch {};
type_sym.scope.add(vector_sym.scope.find("trim")?) catch {};
type_sym.scope.add(vector_sym.scope.find("clear")?) catch {};
if dyn_array_sym := self.scope.find("core")?.scope.find("DynArray") {
type_sym.scope.add(dyn_array_sym.scope.find("is_empty")?) catch {};
type_sym.scope.add(dyn_array_sym.scope.find("delete")?) catch {};
type_sym.scope.add(dyn_array_sym.scope.find("trim")?) catch {};
type_sym.scope.add(dyn_array_sym.scope.find("clear")?) catch {};
}
self.scope.add(type_sym) catch {};
return type_sym;
Expand All @@ -246,7 +246,7 @@ pub struct Module < Sym {
if type_sym := self.scope.find(unique_name) {
return @as(TypeSym, type_sym);
}
mut fields := @vec(Field);
mut fields := @dyn_array(Field);
for i, type in types {
fields.push(Field(name: i.to_string(), is_public: true, type: type));
}
Expand Down Expand Up @@ -398,4 +398,4 @@ pub struct Func < Sym {
pos: token.noPos
);
}
}
}
14 changes: 7 additions & 7 deletions lib/rivet/src/ast/Table.ri
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Table {
pub mut float64_sym: TypeSym;
pub mut string_sym: TypeSym;
pub mut throwable_sym: TypeSym;
pub mut vector_sym: TypeSym; // from `core` module
pub mut dyn_array_sym: TypeSym; // from `core` module

/// Primitive types.
pub mut bool_t: Type;
Expand Down Expand Up @@ -171,7 +171,7 @@ pub struct Table {
BuiltinArg("msg", type: self.string_t, is_optional: true)
]),

.Func("vec", [
.Func("dyn_array", [
BuiltinArg("type", is_any: true),
BuiltinArg("cap", type: self.uint_t, is_optional: true)
], checks: [
Expand Down Expand Up @@ -342,7 +342,7 @@ pub struct Table {
} else {
self.type_size(enum_info.underlying_type)
},
.DynArray -> self.type_symbol_size(self.vector_sym, is_raw),
.DynArray -> self.type_symbol_size(self.dyn_array_sym, is_raw),
.Array as array_info -> {
(elem_size, elem_align) := self.type_size(array_info.elem_type);
(array_info.size * elem_size, elem_align)
Expand All @@ -353,7 +353,7 @@ pub struct Table {
types := if type_sym.info is .Tuple as tuple_lit {
tuple_lit.types
} else {
mut tmp := @vec(Type);
mut tmp := @dyn_array(Type);
for field in type_sym.full_fields() {
tmp.push(field.type);
}
Expand Down Expand Up @@ -388,15 +388,15 @@ pub struct Table {
}

pub func filter_files(self, inputs: []string) -> []string {
mut new_inputs := @vec(string, inputs.len);
mut new_inputs := @dyn_array(string, inputs.len);
for input in inputs {
base_name_input := Path.base_name(input);
if base_name_input.count(".") == 1 {
new_inputs.push(input);
continue;
}
exts := base_name_input[..base_name_input.len - 3].split(".")[1..];
mut already_exts := @vec(string, exts.len);
mut already_exts := @dyn_array(string, exts.len);
mut should_compile := false;
for ext in exts {
if ext in already_exts {
Expand Down Expand Up @@ -577,4 +577,4 @@ enum BuiltinFuncCheck {
arg_idx: uint;
type: BuiltinFuncType;
}
}
}
Loading

0 comments on commit dc2bd05

Please sign in to comment.