mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
feat: more simpler math compiling
This commit is contained in:
parent
7c708b7c04
commit
8ec6546ed2
|
@ -111,34 +111,26 @@ impl Compiler {
|
|||
|
||||
match atom {
|
||||
Int(i) => {
|
||||
let r = self.next_register();
|
||||
result.push(Instr::Store {
|
||||
address: r,
|
||||
result.push(Instr::Push {
|
||||
value: Type::Int(*i),
|
||||
label: self.next_label(),
|
||||
});
|
||||
}
|
||||
Float(f) => {
|
||||
let r = self.next_register();
|
||||
result.push(Instr::Store {
|
||||
address: r,
|
||||
result.push(Instr::Push {
|
||||
value: Type::Float(*f),
|
||||
label: self.next_label(),
|
||||
});
|
||||
}
|
||||
Boolean(b) => {
|
||||
let r = self.next_register();
|
||||
result.push(Instr::Store {
|
||||
address: r,
|
||||
result.push(Instr::Push {
|
||||
value: Type::Boolean(*b),
|
||||
label: self.next_label(),
|
||||
});
|
||||
}
|
||||
Str(s) => {
|
||||
let r = self.next_register();
|
||||
result.push(Instr::Store {
|
||||
address: r,
|
||||
value: Type::String(s.to_string()),
|
||||
result.push(Instr::Push {
|
||||
value: Type::String(s.clone()),
|
||||
label: self.next_label(),
|
||||
});
|
||||
}
|
||||
|
@ -158,6 +150,10 @@ impl Compiler {
|
|||
let mut arg = self.compile_atom(&args[0], depth + 1)?;
|
||||
result.append(&mut arg);
|
||||
let arg_pointer = self.current_register();
|
||||
result.push(Instr::Pop {
|
||||
address: arg_pointer,
|
||||
label: self.next_label(),
|
||||
});
|
||||
|
||||
let call_register = self.next_register();
|
||||
result.push(Instr::Store {
|
||||
|
@ -174,71 +170,39 @@ impl Compiler {
|
|||
},
|
||||
"add" | "+" => {
|
||||
let mut lhs = self.compile_atom(&args[0], depth + 1)?;
|
||||
let lhs_pointer = self.current_register();
|
||||
result.append(&mut lhs);
|
||||
|
||||
let mut rhs = self.compile_atom(&args[1], depth + 1)?;
|
||||
let rhs_pointer = self.current_register();
|
||||
result.append(&mut rhs);
|
||||
|
||||
let result_register = self.next_register();
|
||||
result.push(Instr::IAdd {
|
||||
lhs: lhs_pointer,
|
||||
rhs: rhs_pointer,
|
||||
to: result_register,
|
||||
label: self.next_label(),
|
||||
});
|
||||
result.push(Instr::Add { label: self.next_label() });
|
||||
},
|
||||
"sub" | "-" => {
|
||||
let mut lhs = self.compile_atom(&args[0], depth + 1)?;
|
||||
let lhs_pointer = self.current_register();
|
||||
result.append(&mut lhs);
|
||||
|
||||
let mut rhs = self.compile_atom(&args[1], depth + 1)?;
|
||||
let rhs_pointer = self.current_register();
|
||||
result.append(&mut rhs);
|
||||
|
||||
let result_register = self.next_register();
|
||||
result.push(Instr::ISub {
|
||||
lhs: lhs_pointer,
|
||||
rhs: rhs_pointer,
|
||||
to: result_register,
|
||||
label: self.next_label(),
|
||||
});
|
||||
result.push(Instr::Sub { label: self.next_label() });
|
||||
},
|
||||
"mul" | "*" => {
|
||||
let mut lhs = self.compile_atom(&args[0], depth + 1)?;
|
||||
let lhs_pointer = self.current_register();
|
||||
result.append(&mut lhs);
|
||||
|
||||
let mut rhs = self.compile_atom(&args[1], depth + 1)?;
|
||||
let rhs_pointer = self.current_register();
|
||||
result.append(&mut rhs);
|
||||
|
||||
let result_register = self.next_register();
|
||||
result.push(Instr::IMul {
|
||||
lhs: lhs_pointer,
|
||||
rhs: rhs_pointer,
|
||||
to: result_register,
|
||||
label: self.next_label(),
|
||||
});
|
||||
result.push(Instr::Mul { label: self.next_label() });
|
||||
},
|
||||
"div" | "/" => {
|
||||
let mut lhs = self.compile_atom(&args[0], depth + 1)?;
|
||||
let lhs_pointer = self.current_register();
|
||||
result.append(&mut lhs);
|
||||
|
||||
let mut rhs = self.compile_atom(&args[1], depth + 1)?;
|
||||
let rhs_pointer = self.current_register();
|
||||
result.append(&mut rhs);
|
||||
|
||||
let result_register = self.next_register();
|
||||
result.push(Instr::IDiv {
|
||||
lhs: lhs_pointer,
|
||||
rhs: rhs_pointer,
|
||||
to: result_register,
|
||||
label: self.next_label(),
|
||||
});
|
||||
result.push(Instr::Div { label: self.next_label() });
|
||||
},
|
||||
_ => return Err(format!("Unknown intrinsic: {}", intrinsic)),
|
||||
}
|
||||
|
|
|
@ -76,6 +76,11 @@ pub enum Instr {
|
|||
Store { address: Register, value: Type, label: usize },
|
||||
// Call intrinsic function.
|
||||
Call { address: Register, args: Register, label: usize },
|
||||
// Stack operations.
|
||||
Push { value: Type, label: usize }, Pop { address: Register, label: usize },
|
||||
// Stack arithmetic.
|
||||
Add { label: usize }, Sub { label: usize },
|
||||
Mul { label: usize }, Div { label: usize },
|
||||
// Immediate arithmetic.
|
||||
IAdd { lhs: Register, rhs: Register, to: Register, label: usize },
|
||||
ISub { lhs: Register, rhs: Register, to: Register, label: usize },
|
||||
|
@ -94,6 +99,12 @@ impl Display for Instr {
|
|||
// Instr::Load { address, label } => write!(f, "{}: LOAD {}", label, address),
|
||||
Instr::Store { address, value , label} => write!(f, "{}: STORE {} {}", label, address, value),
|
||||
Instr::Call { address, args, label } => write!(f, "{}: CALL {} {}", label, address, args),
|
||||
Instr::Push { value, label } => write!(f, "{}: PUSH {}", label, value),
|
||||
Instr::Pop { address, label } => write!(f, "{}: POP {}", label, address),
|
||||
Instr::Add { label } => write!(f, "{}: ADD", label),
|
||||
Instr::Sub { label } => write!(f, "{}: SUB", label),
|
||||
Instr::Mul { label } => write!(f, "{}: MUL", label),
|
||||
Instr::Div { label } => write!(f, "{}: DIV", label),
|
||||
Instr::IAdd { lhs, rhs, to, label } => write!(f, "{}: IADD {} {} {}", label, lhs, rhs, to),
|
||||
Instr::ISub { lhs, rhs, to, label } => write!(f, "{}: ISUB {} {} {}", label, lhs, rhs, to),
|
||||
Instr::IMul { lhs, rhs, to, label } => write!(f, "{}: IMUL {} {} {}", label, lhs, rhs, to),
|
||||
|
|
Loading…
Reference in a new issue