-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fire: Add handling of builtin arithmetic operations
- Loading branch information
1 parent
6917994
commit 9fc5b0a
Showing
3 changed files
with
130 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//! This module takes care of performing calls to extern functions. Extern functions will | ||
//! either be builtins, or C functions defined in a dynamic library loaded with the `link_with` | ||
//! builtin. | ||
use fir::{Node, RefIdx}; | ||
use flatten::FlattenData; | ||
|
||
use crate::instance::Instance; | ||
use crate::GarbaJKollector; | ||
|
||
// TODO: Factor this with `ast_builtins`? | ||
static ARITHMETIC_BUILTINS: &[&'static str] = &["+", "-", "*", "/"]; | ||
Check failure on line 12 in fire/src/outside.rs GitHub Actions / clippystatics have by default a `'static` lifetime
|
||
|
||
enum Arithmetic { | ||
Add, | ||
Sub, | ||
Mul, | ||
Div, | ||
} | ||
|
||
impl From<&str> for Arithmetic { | ||
fn from(s: &str) -> Self { | ||
match s { | ||
"+" => Arithmetic::Add, | ||
"-" => Arithmetic::Sub, | ||
"*" => Arithmetic::Mul, | ||
"/" => Arithmetic::Div, | ||
_ => unreachable!("this is an interpreter error."), | ||
} | ||
} | ||
} | ||
|
||
pub fn perform_call( | ||
// FIXME: This should probably take a context, correct? &self? | ||
gc: &GarbaJKollector, | ||
node: &Node<FlattenData<'_>>, | ||
args: &[RefIdx], | ||
) -> Option<Instance> { | ||
let ast = node.data.ast.node(); | ||
let name = match &ast.node { | ||
ast::Node::Function { | ||
decl: ast::Declaration { name, .. }, | ||
.. | ||
} => name, | ||
other => { | ||
dbg!(other); | ||
unreachable!() | ||
} | ||
}; | ||
|
||
if ARITHMETIC_BUILTINS.contains(&name.access()) { | ||
return Some(arithmetic_builtin_call( | ||
gc, | ||
args, | ||
Arithmetic::from(name.access()), | ||
)); | ||
} | ||
|
||
if name.access() == "println" { | ||
args.iter().for_each(|arg| { | ||
let value = gc.lookup(&arg.expect_resolved()).unwrap(); | ||
if let Instance::String(s) = value { | ||
println!("{s}"); | ||
} else { | ||
unreachable!("typecheck didn't catch this error. this is an interpreter bug."); | ||
} | ||
}) | ||
} | ||
|
||
None | ||
} | ||
|
||
fn int_op(lhs: &i64, rhs: &i64, op: Arithmetic) -> Instance { | ||
let res = match op { | ||
Arithmetic::Add => lhs + rhs, | ||
Arithmetic::Sub => lhs - rhs, | ||
Arithmetic::Mul => lhs * rhs, | ||
Arithmetic::Div => lhs / rhs, | ||
}; | ||
|
||
Instance::Int(res) | ||
} | ||
|
||
fn float_op(lhs: &f64, rhs: &f64, op: Arithmetic) -> Instance { | ||
let res = match op { | ||
Arithmetic::Add => lhs + rhs, | ||
Arithmetic::Sub => lhs - rhs, | ||
Arithmetic::Mul => lhs * rhs, | ||
Arithmetic::Div => lhs / rhs, | ||
}; | ||
|
||
Instance::Float(res) | ||
} | ||
|
||
fn arithmetic_builtin_call(gc: &GarbaJKollector, args: &[RefIdx], op: Arithmetic) -> Instance { | ||
let lhs = gc.lookup(&args[0].expect_resolved()).unwrap(); | ||
let rhs = gc.lookup(&args[1].expect_resolved()).unwrap(); | ||
|
||
match (lhs, rhs) { | ||
(Instance::Int(l), Instance::Int(r)) => int_op(l, r, op), | ||
(Instance::Float(l), Instance::Float(r)) => float_op(l, r, op), | ||
_ => unreachable!("fuck me"), | ||
} | ||
} |