Skip to content

Commit

Permalink
use slices everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 25, 2023
1 parent b710392 commit 0f731f0
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 53 deletions.
6 changes: 3 additions & 3 deletions cmd/src/tools/cmd_fmt.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import std/flag;
Expand All @@ -15,7 +15,7 @@ import rivet/parser;
var fmt_desc := "Formats the given Rivet source files or recursively formats all files in the
directory, then prints their formatted source to stdout.";

pub func cmd_fmt(args: []string) -> ! {
pub func cmd_fmt(args: [:]string) -> ! {
mut fp := flag.FlagParser.new(args);
fp.set_application_name("rivet fmt");
fp.set_arguments_description("[FILES|DIRECTORIES]");
Expand Down
2 changes: 1 addition & 1 deletion cmd/src/tools/cmd_new.ri
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var available_templates := "Available templates:
bin A simple binary project (default).
lib A simple library project.";

pub func cmd_new(args: []string, is_init: bool) -> ! {
pub func cmd_new(args: [:]string, is_init: bool) -> ! {
mut template := "";
mut fp := flag.FlagParser.new(args);
if is_init {
Expand Down
71 changes: 71 additions & 0 deletions lib/core/src/Slice.ri
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

struct Slice {
ptr: rawptr;
elem_size: uint;
len: uint;

func get(&self, idx: uint) -> rawptr {
if 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("dynamic array index out of range (index: {}, len: {})", idx, self.len);
}
unsafe {
mem_copy(
@ptr_add(@as([&]mut uint8, self.ptr), self.elem_size * idx),
val, self.elem_size
);
}
}

#[inline]
func is_empty(&self) -> bool {
return self.len == 0;
}

func slice(self, start: uint, end: uint) -> Self {
if start > end || end > self.len {
runtime_error(
"slice index [{}:{}] out of range (len: {})", start, end, self.len
);
}
len := end - start;
if len == self.len {
return Self(self.ptr, self.elem_size, self.len);
}
return Self(
unsafe { @ptr_add(@as([&]mut uint8, self.ptr), start * self.elem_size) },
self.elem_size, len
);
}

#[inline]
func slice_from(self, start: uint) -> Self {
return self.slice(start, self.len);
}

#[inline]
func to_dynamic_array(&self) -> DynArray {
return unsafe { DynArray.from_array(self.ptr, self.elem_size, self.len) };
}

func ==(&self, rhs: &Self) -> bool {
if self.len != rhs.len {
return false;
}
return mem_cmp(self.ptr, rhs.ptr, self.len) == 0;
}

#[inline]
func !=(&self, rhs: &Self) -> bool {
return !(self == rhs);
}
}
6 changes: 3 additions & 3 deletions lib/core/src/StringBuilder.c.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import c/libc;
Expand Down Expand Up @@ -65,7 +65,7 @@ pub struct StringBuilder < Stringable {
self.write_byte('\n');
}

pub func write_join(mut self, ss: []string, sep: string := "") {
pub func write_join(mut self, ss: [:]string, sep: string := "") {
if ss.len == 1 {
self.write_string(ss[0]);
} else {
Expand Down
1 change: 0 additions & 1 deletion lib/rivet/src/ast/Env.ri
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import std/sys;
import std/fs.Path;

import ../token;
import ../prefs;
import ../report;
import ../utils;
Expand Down
4 changes: 2 additions & 2 deletions lib/rivet/src/builder/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ pub struct Builder {
"/"
);
full_name = if "src" in names {
src_idx := utils.index_of(names, "src");
src_idx := utils.index_of(names[:], "src");
utils.join(names[:src_idx], ".").concat(
".", utils.join(names[src_idx + 1:], ".")
)
} else {
utils.join(names, ".")
utils.join(names[:], ".")
};
}
if found {
Expand Down
6 changes: 3 additions & 3 deletions lib/rivet/src/checker/call_expr.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ../ast;
Expand Down Expand Up @@ -422,7 +422,7 @@ extend Checker {
// check default expressions
if func_.has_named_args {
args_len := call_expr.pure_args_count();
mut args := call_expr.args[:args_len];
mut args := call_expr.args[:args_len].to_dynamic_array();
mut i: uint := args_len;
while i < func_args_len : i += 1 {
arg := func_.args[i];
Expand Down
4 changes: 2 additions & 2 deletions lib/rivet/src/checker/exprs.ri
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ extend Checker {
}
self.inside_unsafe_block = false;
}
block.defer_stmts = self.defer_stmts[self.defer_stmts_start:];
self.defer_stmts = self.defer_stmts[:self.defer_stmts_start];
block.defer_stmts = self.defer_stmts[self.defer_stmts_start:].to_dynamic_array();
self.defer_stmts = self.defer_stmts[:self.defer_stmts_start].to_dynamic_array();
block.type
},
.Unary(mut unary) -> self.check_unary(unary),
Expand Down
6 changes: 3 additions & 3 deletions lib/rivet/src/checker/match_expr.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ../ast;
Expand Down Expand Up @@ -198,7 +198,7 @@ extend Checker {
if unhandled.len > 0 {
mut err_details := "add `match` branches for: ";
if unhandled.len < MATCH_EXHAUSTIVE_CUTOFF_LIMIT {
err_details = err_details.concat(utils.join(unhandled, ", "));
err_details = err_details.concat(utils.join(unhandled[:], ", "));
} else {
err_details = err_details.concat(
utils.join(unhandled[:MATCH_EXHAUSTIVE_CUTOFF_LIMIT], ", ")
Expand Down
6 changes: 3 additions & 3 deletions lib/rivet/src/depgraph/mod.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import std/strings;
Expand Down Expand Up @@ -89,7 +89,7 @@ pub struct DepGraph {
}
(seen, cycle_names) = nn.is_part_of_cycle(k.key, cycle_names);
if seen {
out.writeln_fmt(" * `{}`", utils.join(cycle_names, "` -> `"));
out.writeln_fmt(" * `{}`", utils.join(cycle_names[:], "` -> `"));
nn.is_cycle = maps.MapStringBool();
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rivet/src/prefs/mod.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import std/env;
Expand Down Expand Up @@ -82,7 +82,7 @@ pub struct Prefs {
pub mut run_output: bool;
pub mut run_output_args: []string;

pub func from_args(args: []string, is_test: bool, run_output: bool, is_check: bool) -> !Self {
pub func from_args(args: [:]string, is_test: bool, run_output: bool, is_check: bool) -> !Self {
mut prefs := Prefs(is_test: is_test, run_output: run_output, check: is_check);
mut fp := flag.FlagParser.new(args);
fp.set_application_name(if is_test {
Expand Down
6 changes: 3 additions & 3 deletions lib/rivet/src/tokenizer/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub struct Tokenizer {
s.substr(end_idx)
});
}
return utils.join(ss, "");
return utils.join(ss[:], "");
}

#[inline]
Expand Down Expand Up @@ -298,7 +298,7 @@ func decode_o_escapes(s: string, start: uint, escapes_pos: []uint) -> string {
s.substr(end_idx)
});
}
return utils.join(ss, "");
return utils.join(ss[:], "");
}

func decode_u_escape_single(str: string, idx: uint) -> (uint, string) {
Expand All @@ -316,7 +316,7 @@ func decode_unicode_escaped_rune(str: string) -> string {
mut ss := []string(cap: 2);
ss.push(segment);
ss.push(str.substr(end_idx));
utils.join(ss, "")
utils.join(ss[:], "")
};
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/tokenizer/next.ri
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ extend Tokenizer {
if segment_idx < lit.len {
str_segments.push(lit.substr(segment_idx));
}
lit = utils.join(str_segments, "");
lit = utils.join(str_segments[:], "");
}
if n_cr_chars > 0 {
lit = lit.replace("\r", "");
Expand Down
14 changes: 7 additions & 7 deletions lib/rivet/src/utils/array.ri
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import std/strings;

pub func join(vec: []string, s: string) -> string {
return if vec.is_empty() {
pub func join(slice: [:]string, s: string) -> string {
return if slice.is_empty() {
""
} else {
mut sb := strings.Builder.new();
sb.write_join(vec, s);
sb.write_join(slice, s);
sb.to_string()
};
}

pub func index_of(vec: []string, value: string) -> uint {
for i, val in vec {
pub func index_of(slice: [:]string, value: string) -> uint {
for i, val in slice {
if val == value {
return i;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rivet/src/utils/file.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import std/env;
Expand Down Expand Up @@ -57,7 +57,7 @@ struct SourceCache {
};
}

pub func find_lines_between(mut self, path_: string, line: uint, end_line: uint) -> ?[]string {
pub func find_lines_between(mut self, path_: string, line: uint, end_line: uint) -> ?[:]string {
return if lines := self.find_lines(path_) {
if lines.len > 0 && lines.len >= end_line {
lines[line:end_line]
Expand Down
21 changes: 10 additions & 11 deletions lib/std/src/flag/mod.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ../traits;
Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct Flag < traits.Stringable {
#[boxed]
pub struct FlagParser {
/// The original arguments to be parsed.
original_args: []string;
original_args: [:]string;
/// The index of a `--`, `none` if there is not any.
idx_dashdash: ?uint;
/// All options after `--` are ignored, and will be passed to the application unmodified.
Expand Down Expand Up @@ -71,19 +71,18 @@ pub struct FlagParser {
mut footers: []string;

/// Create a new flag parser for the given args.
pub func new(args: []string) -> Self {
original_args := args.clone();
mut all_before_dashdash := args.clone();
pub func new(args: [:]string) -> Self {
mut all_before_dashdash := args.to_dynamic_array();
mut all_after_dashdash := []string();
idx_dashdash := index_of(args, "--");
if v := idx_dashdash {
all_before_dashdash.trim(v);
if v < original_args.len {
all_after_dashdash = original_args[v + 1:];
if v < args.len {
all_after_dashdash = args[v + 1:].to_dynamic_array();
}
}
return Self(
original_args: original_args,
original_args: args,
idx_dashdash: idx_dashdash,
all_after_dashdash: all_after_dashdash,
args: all_before_dashdash,
Expand Down Expand Up @@ -487,8 +486,8 @@ pub struct FlagParser {
}
}

pub func index_of(vec: []string, value: string) -> ?uint {
for i, val in vec {
pub func index_of(slice: [:]string, value: string) -> ?uint {
for i, val in slice {
if val == value {
return i;
}
Expand Down
8 changes: 5 additions & 3 deletions rivetc/src/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,18 +1630,20 @@ def gen_expr(self, expr, custom_tmp = None):
else:
end = None
tmp = self.cur_func.local_name()
if s.kind == TypeKind.DynArray:
if s.kind in (TypeKind.DynArray, TypeKind.Slice):
if end == None:
method_name = "_R4core5Slice10slice_fromM" if s.kind==TypeKind.Slice else "_R4core8DynArray10slice_fromM"
inst = ir.Inst(
ir.InstKind.Call, [
ir.Name("_R4core8DynArray10slice_fromM"), left,
ir.Name(method_name), left,
start
]
)
else:
method_name = "_R4core5Slice5sliceM" if s.kind==TypeKind.Slice else "_R4core8DynArray5sliceM"
inst = ir.Inst(
ir.InstKind.Call, [
ir.Name("_R4core8DynArray5sliceM"), left, start,
ir.Name(method_name), left, start,
end
]
)
Expand Down
2 changes: 1 addition & 1 deletion rivetc/src/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def qualstr(self):
return f"[:]{self.typ.qualstr()}"

def __eq__(self, other):
if not isinstance(other, DynArray):
if not isinstance(other, Slice):
return False
return self.typ == other.typ and self.is_mut == other.is_mut

Expand Down

0 comments on commit 0f731f0

Please sign in to comment.