1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

feat: math intrinsic

This commit is contained in:
Natapat Samutpong 2022-01-26 09:13:25 +07:00
parent 3d02755095
commit 5a65ee2fc7
5 changed files with 78 additions and 8 deletions

1
example/math.blsp Normal file
View file

@ -0,0 +1 @@
(print (/ (+ 345 345) 10))

7
math.bbb Normal file
View file

@ -0,0 +1,7 @@
STORE r1 $345
STORE r2 $345
IADD r1 r2 r3
STORE r4 $10
IDIV r3 r4 r5
STORE r6 $1
CALL r6 r5

View file

@ -85,7 +85,9 @@ impl Compiler {
value: Type::String(s.to_string()),
});
}
_ => return Err(format!("Expected atom, got {:?}", atom)),
_ => {
result.append(&mut self.compile(atom.clone())?);
}
}
Ok(result)
@ -110,7 +112,71 @@ impl Compiler {
address: call_register,
args: arg_pointer,
});
}
},
"add" | "+" => {
let mut lhs = self.compile_atom(&args[0])?;
let lhs_pointer = self.current_pointer();
result.append(&mut lhs);
let mut rhs = self.compile_atom(&args[1])?;
let rhs_pointer = self.current_pointer();
result.append(&mut rhs);
let result_register = self.next_register();
result.push(Instr::IAdd {
lhs: lhs_pointer,
rhs: rhs_pointer,
to: result_register,
});
},
"sub" | "-" => {
let mut lhs = self.compile_atom(&args[0])?;
let lhs_pointer = self.current_pointer();
result.append(&mut lhs);
let mut rhs = self.compile_atom(&args[1])?;
let rhs_pointer = self.current_pointer();
result.append(&mut rhs);
let result_register = self.next_register();
result.push(Instr::ISub {
lhs: lhs_pointer,
rhs: rhs_pointer,
to: result_register,
});
},
"mul" | "*" => {
let mut lhs = self.compile_atom(&args[0])?;
let lhs_pointer = self.current_pointer();
result.append(&mut lhs);
let mut rhs = self.compile_atom(&args[1])?;
let rhs_pointer = self.current_pointer();
result.append(&mut rhs);
let result_register = self.next_register();
result.push(Instr::IMul {
lhs: lhs_pointer,
rhs: rhs_pointer,
to: result_register,
});
},
"div" | "/" => {
let mut lhs = self.compile_atom(&args[0])?;
let lhs_pointer = self.current_pointer();
result.append(&mut lhs);
let mut rhs = self.compile_atom(&args[1])?;
let rhs_pointer = self.current_pointer();
result.append(&mut rhs);
let result_register = self.next_register();
result.push(Instr::IDiv {
lhs: lhs_pointer,
rhs: rhs_pointer,
to: result_register,
});
},
_ => return Err(format!("Unknown intrinsic: {}", intrinsic)),
}

View file

@ -38,8 +38,6 @@ pub enum Instr {
Store { address: Register, value: Type, },
// Call intrinsic function.
Call { address: Register, args: Register },
// Stack arithmetic.
Add, Sub, Mul, Div,
// Immediate arithmetic.
IAdd { lhs: Register, rhs: Register, to: Register, },
ISub { lhs: Register, rhs: Register, to: Register, },
@ -53,10 +51,6 @@ impl Display for Instr {
Instr::Load { address } => write!(f, "LOAD {}", address),
Instr::Store { address, value } => write!(f, "STORE {} {}", address, value),
Instr::Call { address, args } => write!(f, "CALL {} {}", address, args),
Instr::Add => write!(f, "ADD"),
Instr::Sub => write!(f, "SUB"),
Instr::Mul => write!(f, "MUL"),
Instr::Div => write!(f, "DIV"),
Instr::IAdd { lhs, rhs, to } => write!(f, "IADD {} {} {}", lhs, rhs, to),
Instr::ISub { lhs, rhs, to } => write!(f, "ISUB {} {} {}", lhs, rhs, to),
Instr::IMul { lhs, rhs, to } => write!(f, "IMUL {} {} {}", lhs, rhs, to),

View file

@ -1,2 +1,4 @@
/// Contains the definition of instructions and register.
pub mod lib;
/// Contains the compiler.
pub mod compile;