Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(all): change typematching cast syntax #49

Merged
merged 9 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/core/tests/string_test.ri
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test "string.parse_int()" {
test "string.substr()" {
str := "hello world!";
@assert(str.substr() == str);
@assert(str.substr(5) == "world!");
@assert(str.substr(end: 4) == "hello");
@assert(str.substr(1, 8) == "llo wor");
@assert(str.substr(6) == "world!");
@assert(str.substr(end: 5) == "hello");
@assert(str.substr(2, 9) == "llo wor");
}
4 changes: 2 additions & 2 deletions lib/rivet/src/ast/CHeader.ri
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ enum CImportValue {

#[inline]
pub func to_bool(self) -> bool {
return (self is .Int as int and int == 1)
or (self is .Uint as uint and uint == 1);
return (self is .Int(int) and int == 1)
or (self is .Uint(uint) and uint == 1);
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/rivet/src/ast/Decl.ri
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ pub enum Decl {

pub func decls(self) -> ?[]Self {
return match self is {
.Extern as extern_decl -> extern_decl.decls,
.Trait as trait_decl -> trait_decl.decls,
.Enum as enum_decl -> enum_decl.decls,
.Struct as struct_decl -> struct_decl.decls,
.Extend as extend_decl -> extend_decl.decls,
.Extern(extern_decl) -> extern_decl.decls,
.Trait(trait_decl) -> trait_decl.decls,
.Enum(enum_decl) -> enum_decl.decls,
.Struct(struct_decl) -> struct_decl.decls,
.Extend(extend_decl) -> extend_decl.decls,
else -> none
};
}
Expand Down
122 changes: 61 additions & 61 deletions lib/rivet/src/ast/Expr.ri
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pub enum Expr < traits.Stringable {

pub func clean_paren(self) -> Self {
mut res := self;
while res is .Paren as paren {
while res is .Paren(paren) {
res = paren.expr;
}
return res;
Expand All @@ -287,65 +287,65 @@ pub enum Expr < traits.Stringable {
#[inline]
pub func position(self) -> token.Pos {
return match self is {
.Empty as empty_pos -> empty_pos,
.Paren as paren -> paren.pos,
.Type as type -> type.position(),
.Assign as assign -> assign.pos,
.Ident as ident -> ident.pos,
.SelfTy as self_ty -> self_ty.pos,
.SelfLiteral as self_lit -> self_lit.pos,
.NoneLiteral as none_lit -> none_lit.pos,
.BoolLiteral as bool_lit -> bool_lit.pos,
.CharLiteral as char_lit -> char_lit.pos,
.IntegerLiteral as int_lit -> int_lit.pos,
.FloatLiteral as float_lit -> float_lit.pos,
.StringLiteral as string_lit -> string_lit.pos,
.EnumLiteral as enum_lit -> enum_lit.pos,
.TupleLiteral as tuple_lit -> tuple_lit.pos,
.DynArrayLiteral as dyn_array_lit -> dyn_array_lit.pos,
.Index as index -> index.pos,
.Selector as selector -> selector.pos,
.Indirect as indirect -> indirect.pos,
.OptionCheck as option_check -> option_check.pos,
.Branch as branch -> branch.pos,
.Range as range -> range.pos,
.Call as call -> call.pos,
.BuiltinCall as builtin_call -> builtin_call.pos,
.Unary as unary -> unary.pos,
.Binary as binary -> binary.pos,
.Return as return_expr -> return_expr.pos,
.Throw as throw_expr -> throw_expr.pos,
.Block as block -> block.pos,
.If as if_expr -> if_expr.pos,
.Match as match_expr -> match_expr.pos,
.Guard as guard -> guard.pos
.Empty(empty_pos) -> empty_pos,
.Paren(paren) -> paren.pos,
.Type(type) -> type.position(),
.Assign(assign) -> assign.pos,
.Ident(ident) -> ident.pos,
.SelfTy(self_ty) -> self_ty.pos,
.SelfLiteral(self_lit) -> self_lit.pos,
.NoneLiteral(none_lit) -> none_lit.pos,
.BoolLiteral(bool_lit) -> bool_lit.pos,
.CharLiteral(char_lit) -> char_lit.pos,
.IntegerLiteral(int_lit) -> int_lit.pos,
.FloatLiteral(float_lit) -> float_lit.pos,
.StringLiteral(string_lit) -> string_lit.pos,
.EnumLiteral(enum_lit) -> enum_lit.pos,
.TupleLiteral(tuple_lit) -> tuple_lit.pos,
.DynArrayLiteral(dyn_array_lit) -> dyn_array_lit.pos,
.Index(index) -> index.pos,
.Selector(selector) -> selector.pos,
.Indirect(indirect) -> indirect.pos,
.OptionCheck(option_check) -> option_check.pos,
.Branch(branch) -> branch.pos,
.Range(range) -> range.pos,
.Call(call) -> call.pos,
.BuiltinCall(builtin_call) -> builtin_call.pos,
.Unary(unary) -> unary.pos,
.Binary(binary) -> binary.pos,
.Return(return_expr) -> return_expr.pos,
.Throw(throw_expr) -> throw_expr.pos,
.Block(block) -> block.pos,
.If(if_expr) -> if_expr.pos,
.Match(match_expr) -> match_expr.pos,
.Guard(guard) -> guard.pos
};
}

#[inline]
pub func to_string(self) -> string {
return match self is {
.Empty -> "<empty-expression>",
.Paren as paren -> "({})".fmt(paren.expr),
.Type as type -> type.to_string(),
.Assign as assign -> "{} {} {}".fmt(assign.left, assign.op, assign.right),
.Ident as ident -> if ident.is_comptime {
.Paren(paren) -> "({})".fmt(paren.expr),
.Type(type) -> type.to_string(),
.Assign(assign) -> "{} {} {}".fmt(assign.left, assign.op, assign.right),
.Ident(ident) -> if ident.is_comptime {
"@".concat(ident.name)
} else {
ident.name
},
.SelfTy -> "Self",
.SelfLiteral -> "self",
.NoneLiteral -> "none",
.BoolLiteral as bool_lit -> bool_lit.value.to_string(),
.CharLiteral as char_lit -> if char_lit.is_byte {
.BoolLiteral(bool_lit) -> bool_lit.value.to_string(),
.CharLiteral(char_lit) -> if char_lit.is_byte {
"b'{}'".fmt(char_lit.value)
} else {
"'{}'".fmt(char_lit.value)
},
.IntegerLiteral as int_lit -> int_lit.value,
.FloatLiteral as float_lit -> float_lit.value,
.StringLiteral as string_lit -> if string_lit.is_bytestr {
.IntegerLiteral(int_lit) -> int_lit.value,
.FloatLiteral(float_lit) -> float_lit.value,
.StringLiteral(string_lit) -> if string_lit.is_bytestr {
"b\"{}\"".fmt(string_lit.value)
} else if string_lit.is_raw {
"r\"{}\"".fmt(string_lit.value)
Expand All @@ -354,8 +354,8 @@ pub enum Expr < traits.Stringable {
} else {
"\"{}\"".fmt(string_lit.value)
},
.EnumLiteral as enum_lit -> ".".concat(enum_lit.value),
.TupleLiteral as tuple_lit -> {
.EnumLiteral(enum_lit) -> ".".concat(enum_lit.value),
.TupleLiteral(tuple_lit) -> {
mut sb := strings.Builder.from_string("(");
for i, value in tuple_lit.values {
sb.write_string(value.to_string());
Expand All @@ -366,7 +366,7 @@ pub enum Expr < traits.Stringable {
sb.write_byte(b')');
sb.to_string()
},
.DynArrayLiteral as dyn_array_lit -> {
.DynArrayLiteral(dyn_array_lit) -> {
mut sb := strings.Builder.from_string("[");
for i, value in dyn_array_lit.values {
sb.write_string(value.to_string());
Expand All @@ -380,11 +380,11 @@ pub enum Expr < traits.Stringable {
}
sb.to_string()
},
.Selector as selector -> "{}.{}".fmt(selector.left, selector.field_name),
.Indirect as indirect -> "{}.*".fmt(indirect.left),
.OptionCheck as option_check -> "{}?".fmt(option_check.left),
.Branch as branch_expr -> branch_expr.op.to_string(),
.Range as range -> {
.Selector(selector) -> "{}.{}".fmt(selector.left, selector.field_name),
.Indirect(indirect) -> "{}.*".fmt(indirect.left),
.OptionCheck(option_check) -> "{}?".fmt(option_check.left),
.Branch(branch_expr) -> branch_expr.op.to_string(),
.Range(range) -> {
mut sb := strings.Builder.new();
if range.has_start {
sb.write_string(range.start.to_string());
Expand All @@ -399,8 +399,8 @@ pub enum Expr < traits.Stringable {
}
sb.to_string()
},
.Index as index -> "{}[{}]".fmt(index.left, index.index),
.Call as call -> {
.Index(index) -> "{}[{}]".fmt(index.left, index.index),
.Call(call) -> {
mut sb := strings.Builder.new();
sb.write_string(call.left.to_string());
sb.write_byte(b'(');
Expand Down Expand Up @@ -433,7 +433,7 @@ pub enum Expr < traits.Stringable {
}
sb.to_string()
},
.BuiltinCall as builtin_call -> {
.BuiltinCall(builtin_call) -> {
mut sb := strings.Builder.from_string("@");
sb.write_string(builtin_call.name);
sb.write_byte(b'(');
Expand All @@ -446,15 +446,15 @@ pub enum Expr < traits.Stringable {
sb.write_byte(b')');
sb.to_string()
},
.Unary as unary -> "{}{}".fmt(unary.op, unary.right),
.Binary as binary -> "{} {} {}".fmt(binary.left, binary.op, binary.right),
.Return as return_expr -> if return_expr.has_expr {
.Unary(unary) -> "{}{}".fmt(unary.op, unary.right),
.Binary(binary) -> "{} {} {}".fmt(binary.left, binary.op, binary.right),
.Return(return_expr) -> if return_expr.has_expr {
"return {}".fmt(return_expr.expr)
} else {
"return"
},
.Throw as throw_expr -> "throw {}".fmt(throw_expr.expr),
.Block as block -> {
.Throw(throw_expr) -> "throw {}".fmt(throw_expr.expr),
.Block(block) -> {
mut sb := strings.Builder.new();
if block.is_unsafe {
sb.write_string("unsafe ");
Expand All @@ -468,7 +468,7 @@ pub enum Expr < traits.Stringable {
sb.write_string(" }");
sb.to_string()
},
.If as if_expr -> {
.If(if_expr) -> {
mut sb := strings.Builder.new();
for branch in if_expr.branches {
if branch.is_else {
Expand All @@ -484,7 +484,7 @@ pub enum Expr < traits.Stringable {
}
sb.to_string()
},
.Match as match_expr -> {
.Match(match_expr) -> {
mut sb := strings.Builder.new();
sb.write_fmt("match {} ", match_expr.expr);
if match_expr.is_typematch {
Expand Down Expand Up @@ -519,7 +519,7 @@ pub enum Expr < traits.Stringable {
sb.write_string("} ");
sb.to_string()
},
.Guard as guard -> {
.Guard(guard) -> {
mut sb := strings.Builder.new();
if guard.vars.len > 0 {
sb.write_string("(");
Expand Down
8 changes: 4 additions & 4 deletions lib/rivet/src/ast/Scope.ri
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Scope {

pub func add_or_get(mut self, sym: Sym) -> !Sym {
if old_sym := self.find(sym.name) {
if sym is TypeSym as type_sym and old_sym is TypeSym as mut old_type_sym {
if sym is TypeSym(type_sym) and old_sym is TypeSym(mut old_type_sym) {
if old_type_sym.update(type_sym)! {
return old_sym;
}
Expand Down Expand Up @@ -117,23 +117,23 @@ pub struct Scope {

pub func update_type(self, name: string, type: Type) {
if sym := self.lookup(name) {
if sym is Var as var_info {
if sym is Var(var_info) {
var_info.type = type;
}
}
}

pub func update_is_hidden_ref(self, name: string, is_hidden_ref: bool) {
if sym := self.lookup(name) {
if sym is Var as var_info {
if sym is Var(var_info) {
var_info.is_hidden_ref = is_hidden_ref;
}
}
}

pub func update_is_used(self, name: string, is_used: bool) {
if sym := self.lookup(name) {
if sym is Var as var_info {
if sym is Var(var_info) {
var_info.is_used = is_used;
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/rivet/src/ast/Stmt.ri
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ pub enum Stmt {

pub func position(self) -> token.Pos {
return match self is {
.Empty as empty_pos -> empty_pos,
.Expr as expr -> expr.position(),
.VarDecl as var_decl -> var_decl.pos,
.While as while_stmt -> while_stmt.pos,
.For as for_stmt -> for_stmt.pos,
.Defer as defer_stmt -> defer_stmt.pos
.Empty(empty_pos) -> empty_pos,
.Expr(expr) -> expr.position(),
.VarDecl(var_decl) -> var_decl.pos,
.While(while_stmt) -> while_stmt.pos,
.For(for_stmt) -> for_stmt.pos,
.Defer(defer_stmt) -> defer_stmt.pos
};
}
}
10 changes: 5 additions & 5 deletions lib/rivet/src/ast/Sym.ri
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub trait Sym {
func module(self) -> ?Module {
mut p := self;
while {
if p is Module as mod {
if p is Module(mod) {
return mod;
} else if p_ := p.parent {
p = p_;
Expand All @@ -96,23 +96,23 @@ pub trait Sym {

func type_of(self) -> string {
return match self is {
SymRef as sym_ref -> if sym_ref.ref_resolved {
SymRef(sym_ref) -> if sym_ref.ref_resolved {
sym_ref.ref.type_of()
} else {
"alias"
},
Module -> "module",
Const -> "constant",
Var as obj -> match obj.level {
Var(obj) -> match obj.level {
.Receiver, .Argument -> "argument",
else -> "variable"
},
TypeSym as type_sym -> if type_sym.info.is_compound() {
TypeSym(type_sym) -> if type_sym.info.is_compound() {
type_sym.info.to_string()
} else {
"type"
},
Func as func_info -> func_info.kind(),
Func(func_info) -> func_info.kind(),
else -> "unknown symbol kind"
};
}
Expand Down
Loading