Skip to content

Commit

Permalink
refact(rivetc+rivet): use := for arguments with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 5, 2023
1 parent 282db58 commit a1315bf
Show file tree
Hide file tree
Showing 25 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion lib/c/src/errno.ri
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public func errno() -> int32 {
}

#[inline]
public func errno_msg(code: int32 = errno()) -> string {
public func errno_msg(code: int32 := errno()) -> string {
unsafe {
return if s := libc.strerror(code) {
string.from_raw(s)
Expand Down
6 changes: 3 additions & 3 deletions lib/core/src/StringBuilder.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct StringBuilder < Stringable {

/// Returns a string builder with capacity `cap`.
#[inline]
public func new(cap: usize = 0) -> Self {
public func new(cap: usize := 0) -> Self {
return Self(unsafe { Vector.new(@size_of(uint8), cap) });
}

Expand Down Expand Up @@ -54,7 +54,7 @@ public struct StringBuilder < Stringable {
}
}

public func writeln(mut self, s: string = "") {
public func writeln(mut self, s: string := "") {
if s.len > 0 {
unsafe {
self.write_raw_with_len(s.ptr, s.len);
Expand All @@ -63,7 +63,7 @@ public struct StringBuilder < Stringable {
self.write_byte(b'\n');
}

public func write_join(mut self, ss: []string, sep: string = "") {
public func write_join(mut self, ss: []string, sep: string := "") {
if ss.len == 1 {
self.write_string(ss[0]);
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Vector.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct Vector {
/// 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.
public func delete(mut self, i: usize, size: usize = 1, no_slices: bool = false) {
public func delete(mut self, i: usize, size: usize := 1, no_slices: bool := false) {
if i + size > self.len {
end_idx := if size == 1 { "..{}".fmt(i + size) } else { "" };
process_panic(
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/backtrace.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func bt_error_handler(data: mut_anyptr, msg_ptr: ?[&]mut uint8, errnum: int32) {
}
#endif

public func bt_print(frames_to_skip: int32 = 0) {
public func bt_print(frames_to_skip: int32 := 0) {
#if _RELEASE_
_ = frames_to_skip;
#else
Expand Down
7 changes: 3 additions & 4 deletions lib/core/src/console.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public func console_print(s: string, args: ...Stringable) {

/// Prints a message with a line end to stdout. stdout is flushed.
#[inline]
public func console_println(s: string = "", args: ...Stringable) {
public func console_println(s: string := "", args: ...Stringable) {
unsafe {
writeln_to_fd(1, s.fmt(args));
}
Expand All @@ -34,7 +34,7 @@ public func console_eprint(s: string, args: ...Stringable) {

/// Prints a message with a line end to stderr. Both stderr and stdout are
/// flushed.
public func console_eprintln(s: string = "", args: ...Stringable) {
public func console_eprintln(s: string := "", args: ...Stringable) {
unsafe {
_ = libc.fflush(libc.stdout);
_ = libc.fflush(libc.stderr);
Expand All @@ -51,9 +51,8 @@ public func console_is_atty(fd: int32) -> bool {
}

#[unsafe]
func write_buf_to_fd(fd: int32, buf_: [&]uint8, len: usize) {
func write_buf_to_fd(fd: int32, buf: [&]uint8, len: usize) {
unsafe {
buf := buf_;
mut x: isize := 0;
mut remaining_bytes := @as(isize, len);
while remaining_bytes > 0 {
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 @@ -45,7 +45,7 @@ public func process_id() -> uint32 {

/// Terminates current thread execution immediately after displaying a
/// message, followed by a backtrace.
public func process_panic(s: string = "", args: ...Stringable) -> never {
public func process_panic(s: string := "", args: ...Stringable) -> never {
console_eprintln("panic: {}", s.fmt(args));
bt_print(2);
process_exit(101);
Expand All @@ -71,7 +71,7 @@ public func process_abort() -> never {
/// This function will never return and will immediately terminate the
/// current process. The exit code is passed through to the underlying
/// OS and will be available for consumption by another process.
public func process_exit(code: int32 = 0) -> never {
public func process_exit(code: int32 := 0) -> never {
unsafe {
drop_globals();
libc.exit(code);
Expand Down
16 changes: 8 additions & 8 deletions lib/core/src/string.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct string < Stringable, Hashable, Throwable {
is_ref: bool;
#[unsafe]
public func from_raw(ptr: ?[&]uint8, is_ref: bool = false) -> Self {
public func from_raw(ptr: ?[&]uint8, is_ref: bool := false) -> Self {
if safe_ptr := ptr {
return unsafe {
Self.from_raw_with_len(safe_ptr, libc.strlen(ptr), is_ref)
Expand All @@ -23,7 +23,7 @@ public struct string < Stringable, Hashable, Throwable {
}
#[unsafe]
public func from_raw_with_len(ptr: ?[&]uint8, len: usize, is_ref: bool = false) -> Self {
public func from_raw_with_len(ptr: ?[&]uint8, len: usize, is_ref: bool := false) -> Self {
if len == 0 {
return emptyString;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public struct string < Stringable, Hashable, Throwable {
}
/// Wraps the given string within `width` in characters.
public func wrap(self, width: usize = 60, end: Self = "\n") -> Self {
public func wrap(self, width: usize := 60, end: Self := "\n") -> Self {
words := self.fields();
if words.len == 0 {
return emptyString;
Expand Down Expand Up @@ -410,7 +410,7 @@ public struct string < Stringable, Hashable, Throwable {
}

/// Replaces all occurences of `rep` with the string passed in `with_`.
public func replace(self, rep: Self, with_: Self) -> Self {
public func replace(self, rep: Self, with: Self) -> Self {
if self.len == 0 or rep.len == 0 or rep.len > self.len {
return self;
} else if !self.contains(rep) {
Expand All @@ -426,7 +426,7 @@ public struct string < Stringable, Hashable, Throwable {
if idxs.len == 0 {
return self;
}
new_len := self.len + idxs.len * (with_.len - rep.len);
new_len := self.len + idxs.len * (with.len - rep.len);
b := unsafe { @as([&]mut uint8, internal_alloc(new_len)) };
(mut b_i: usize, mut s_idx: usize) := (0, 0);
unsafe {
Expand All @@ -438,8 +438,8 @@ public struct string < Stringable, Hashable, Throwable {
}
s_idx = rep_pos + rep.len;
i = 0;
while i < with_.len : i += 1 {
b[b_i] = with_.ptr[i];
while i < with.len : i += 1 {
b[b_i] = with.ptr[i];
b_i += 1;
}
}
Expand Down Expand Up @@ -496,7 +496,7 @@ public struct string < Stringable, Hashable, Throwable {
/// first `nth` parts. When `nth` == 0, return all the splits.
/// The last returned element has the remainder of the string, even if the
/// remainder contains more `delim` substrings.
public func split(self, delim: Self, nth: usize = 0) -> []Self {
public func split(self, delim: Self, nth: usize := 0) -> []Self {
mut i: usize := 0;
mut res := @vec(Self);
match delim.len {
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/wyhash.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ func wyhash64(a: uint64, b: uint64) -> uint64 {
}

#[inline]
public func sum64(key: []uint8, seed: uint64 = 0) -> uint64 {
public func sum64(key: []uint8, seed: uint64 := 0) -> uint64 {
return unsafe { wyhash(&key[0], @as(uint64, key.len), seed) };
}

#[inline]
public func sum64_string(key: string, seed: uint64 = 0) -> uint64 {
public func sum64_string(key: string, seed: uint64 := 0) -> uint64 {
return unsafe { wyhash(key.ptr, @as(uint64, key.len), seed) };
}
4 changes: 2 additions & 2 deletions lib/rivet/src/ast/Table.ri
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public struct Table {

/// Returns the size and alignment (in bytes) of `typ`, similarly to
/// C's `sizeof(T)` and `_Alignof(T)`.
public func type_size(mut self, type: Type, is_raw: bool = false) -> (usize, usize) {
public func type_size(mut self, type: Type, is_raw: bool := false) -> (usize, usize) {
return match type is {
.Result, .Option => self.type_symbol_size(self.throwable_sym, is_raw),
.Anyptr, .Pointer, .Func => (self.pointer_size, self.pointer_size),
Expand All @@ -299,7 +299,7 @@ public struct Table {
};
}

public func type_symbol_size(mut self, mut type_sym: TypeSym, is_raw: bool = false) -> (usize, usize) {
public func type_symbol_size(mut self, mut type_sym: TypeSym, is_raw: bool := false) -> (usize, usize) {
return if type_sym.size != 0 {
(type_sym.size, type_sym.align)
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/ast/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct ImportedSymbols {
public mut syms: []ImportedSymbol;

#[inline]
public func add(mut self, name: string, sym: Sym, pos: token.Pos, is_used: bool = false) {
public func add(mut self, name: string, sym: Sym, pos: token.Pos, is_used: bool := false) {
self.syms.push(ImportedSymbol(name, sym, pos, is_used));
}

Expand Down
7 changes: 4 additions & 3 deletions lib/rivet/src/checker/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ public struct Checker {
}

func check_expr_is_mut(
self, expr: ast.Expr, from_assign: bool = false, from_selector: bool = false
self, expr: ast.Expr, from_assign: bool := false,
from_selector: bool := false
) {
match expr is {
.Paren as paren => self.check_expr_is_mut(paren.expr),
Expand Down Expand Up @@ -396,7 +397,7 @@ public struct Checker {
}
}

func has_return(self, stmts: []ast.Stmt, allow_throw: bool = false) -> bool {
func has_return(self, stmts: []ast.Stmt, allow_throw: bool := false) -> bool {
for stmt in stmts {
if stmt is .Expr as expr and self.expr_has_return(expr, allow_throw) {
return true;
Expand All @@ -405,7 +406,7 @@ public struct Checker {
return false;
}

func expr_has_return(self, expr: ast.Expr, allow_throw: bool = false) -> bool {
func expr_has_return(self, expr: ast.Expr, allow_throw: bool := false) -> bool {
return match expr is {
.Match as match_expr => {
for i, branch in match_expr.branches {
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/codegen/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public struct Codegen {
};
}

func mangle_symbol(self, mut sym: ast.Sym, insert_r: bool = true) -> string {
func mangle_symbol(self, mut sym: ast.Sym, insert_r: bool := true) -> string {
if sym.mangled_name.len > 0 {
return sym.mangled_name;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rivet/src/parser/decls.ri
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extend Parser {
}

public func parse_attributes(
mut self, parse_mod_attributes: bool = false
mut self, parse_mod_attributes: bool := false
) -> ast.Attributes {
mut attributes := ast.Attributes();
while self.accept(.Hash) {
Expand Down Expand Up @@ -583,7 +583,7 @@ extend Parser {
self.expect(.Colon);
arg_type := self.parse_type();
is_variadic = arg_type is .Variadic;
arg_expr := if self.accept(.Assign) {
arg_expr := if self.accept(.DeclAssign) {
has_named_args = true;
self.parse_expr()
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/parser/exprs.ri
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ extend Parser {
);
}

func parse_ident(mut self, is_comptime: bool = false) -> ast.Expr {
func parse_ident(mut self, is_comptime: bool := false) -> ast.Expr {
pos := if is_comptime {
self.prev_tok.pos + self.tok.pos
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/rivet/src/parser/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public struct Parser {
}

func parse_var_decl(
mut self, inside_global: bool = false, support_type: bool = true,
support_ref: bool = false, support_mut: bool = true
mut self, inside_global: bool := false, support_type: bool := true,
support_ref: bool := false, support_mut: bool := true
) -> ast.ObjectData {
pos := self.tok.pos;
is_mut := support_mut and self.accept(.KwMut);
Expand Down
14 changes: 7 additions & 7 deletions lib/rivet/src/tokenizer/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public struct Tokenizer {
}

#[inline]
func new_token(self, kind: token.Kind, lit: string = "", len: usize = 1) -> token.Token {
func new_token(self, kind: token.Kind, lit: string := "", len: usize := 1) -> token.Token {
return token.Token(lit, kind, len, token.Pos(
file: self.file,
line: self.line,
Expand All @@ -75,8 +75,8 @@ public struct Tokenizer {

#[inline]
func new_multiline_token(
self, kind: token.Kind, lit: string = "", len: usize = 1,
start_line: usize = 0, end_line: usize = 0, start_col: usize = 0
self, kind: token.Kind, lit: string := "", len: usize := 1,
start_line: usize := 0, end_line: usize := 0, start_col: usize := 0
) -> token.Token {
return token.Token(lit, kind, len, token.Pos(
file: self.file,
Expand Down Expand Up @@ -225,22 +225,22 @@ public struct Tokenizer {
}

#[inline]
func error(self, msg: string, pos: token.Pos = self.current_pos()) {
func error(self, msg: string, pos: token.Pos := self.current_pos()) {
report.error(msg, pos);
}

#[inline]
func warn(self, msg: string, pos: token.Pos = self.current_pos()) {
func warn(self, msg: string, pos: token.Pos := self.current_pos()) {
report.warn(msg, pos);
}

#[inline]
func error_builder(self, msg: string, pos: token.Pos = self.current_pos()) -> report.ReportBuilder {
func error_builder(self, msg: string, pos: token.Pos := self.current_pos()) -> report.ReportBuilder {
return report.error_builder(msg, pos);
}

#[inline]
func warn_builder(self, msg: string, pos: token.Pos = self.current_pos()) -> report.ReportBuilder {
func warn_builder(self, msg: string, pos: token.Pos := self.current_pos()) -> report.ReportBuilder {
return report.warn_builder(msg, pos);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/utils/smart_quote.ri
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const DOUBLE_QUOTE: uint8 := 34;
static doubleEscape := "\\\\";
static invalidEscapes := b"({`.";

public func smart_quote(s: string, raw: bool = false) -> string {
public func smart_quote(s: string, raw: bool := false) -> string {
if s.len == 0 {
return "";
} else if s.len < 256 {
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/utils/version.ri
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import std/process;

public static version := "0.1.0b";
public static version := "0.1.0a";

public func last_commit_hash() -> string {
if result := process.execute("git log -n 1 --pretty=format:%h") {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/src/console/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public alias is_atty = core.console_is_atty;

/// Returns a one-line string from stdin, after printing a prompt. In
/// the event of error (end of input) or a empty string, returns `none`.
public func readln(prompt: string = "") -> ?string {
public func readln(prompt: string := "") -> ?string {
if prompt.len > 0 {
print(prompt);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/src/env/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public func get(name: string) -> ?string {
}

/// Sets the value of an environment variable with `name` to `value`.
public func set(name: string, value: string, overwrite: bool = true) -> ! {
public func set(name: string, value: string, overwrite: bool := true) -> ! {
if unsafe { libc.setenv(name.ptr, value.ptr, overwrite) == -1 } {
throw core.last_errno_error();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/src/fs/Directory.ri
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct EmptyStringError < Throwable {
}

public struct Directory {
public func make(path_: string, mode: usize = 0o777) -> ! {
public func make(path_: string, mode: usize := 0o777) -> ! {
if path_ == "." {
return;
}
Expand All @@ -61,7 +61,7 @@ public struct Directory {
}
}

public func walk(path_: string, ext: string = "") -> ![]string {
public func walk(path_: string, ext: string := "") -> ![]string {
if path_.is_empty() {
throw EmptyStringError("`Directory.walk` expects a folder, not an empty string");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/src/fs/File.ri
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct File {
f: &mut libc.FILE;
mut is_opened: bool;

public func open(path: string, mode: string = "r") -> !File {
public func open(path: string, mode: string := "r") -> !File {
unsafe {
if f := libc.fopen(path.ptr, mode.ptr) {
return Self(f, true);
Expand Down
Loading

0 comments on commit a1315bf

Please sign in to comment.