Skip to content

Commit

Permalink
Add {I32,I8}.abs
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Jan 22, 2025
1 parent 9c58a49 commit 33c03d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Num.fir
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl I8:
prim asU8(self): U8
prim asI32(self): I32
prim asU32(self): U32
prim abs(self): I8

impl U32:
prim asI8(self): I8
Expand All @@ -181,3 +182,4 @@ impl I32:
prim asI8(self): I8
prim asU8(self): U8
prim asU32(self): U32
prim abs(self): I32
6 changes: 6 additions & 0 deletions src/interpreter/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub enum BuiltinFun {
I32AsU8,
I32AsU32,
I32Neg,
I32Abs,

// U32
U32Add,
Expand Down Expand Up @@ -89,6 +90,7 @@ pub enum BuiltinFun {
I8AsI32,
I8AsU32,
I8Neg,
I8Abs,

// U8
U8Add,
Expand Down Expand Up @@ -717,6 +719,10 @@ pub fn call_builtin_fun<W: Write>(

BuiltinFun::I32Neg => i32_as_val(val_as_i32(args[0]).neg()),

BuiltinFun::I8Abs => i8_as_val(val_as_i8(args[0]).abs()),

BuiltinFun::I32Abs => i32_as_val(val_as_i32(args[0]).abs()),

BuiltinFun::Try => {
return try_(w, pgm, heap, args, loc, "Ptr");
}
Expand Down
2 changes: 2 additions & 0 deletions src/interpreter/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pub fn collect_funs(pgm: Vec<L<ast::TopDecl>>) -> (Map<Id, Fun>, Map<Id, Map<Id,
"asU8" => BuiltinFun::I32AsU8,
"asU32" => BuiltinFun::I32AsU32,
"__neg" => BuiltinFun::I32Neg,
"abs" => BuiltinFun::I32Abs,
},
"U32" => {
"__add" => BuiltinFun::U32Add,
Expand Down Expand Up @@ -233,6 +234,7 @@ pub fn collect_funs(pgm: Vec<L<ast::TopDecl>>) -> (Map<Id, Fun>, Map<Id, Map<Id,
"asI32" => BuiltinFun::I8AsI32,
"asU32" => BuiltinFun::I8AsU32,
"__neg" => BuiltinFun::I8Neg,
"abs" => BuiltinFun::I8Abs,
},
"U8" => {
"__add" => BuiltinFun::U8Add,
Expand Down

0 comments on commit 33c03d1

Please sign in to comment.