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

feat: call with stack instead of registers

This commit is contained in:
Natapat Samutpong 2022-01-31 16:06:53 +07:00
parent 5cc43a2503
commit 560ac69863
4 changed files with 11 additions and 25 deletions

View file

@ -38,7 +38,7 @@ impl Compiler {
} }
}, },
"fun" => { "fun" => {
result.push(Instr::Comment { text: format!("--- {}", comp) }); result.push(Instr::Comment { text: format!("function {}", comp) });
let function_name = match &cdr[0] { let function_name = match &cdr[0] {
Symbol(ref name) => format!("function_{}", name.clone()), Symbol(ref name) => format!("function_{}", name.clone()),
_ => return Err(format!("Expected function name, got {}", cdr[0])), _ => return Err(format!("Expected function name, got {}", cdr[0])),
@ -75,15 +75,8 @@ impl Compiler {
let to = self.next_register(); let to = self.next_register();
let call_register = self.next_register(); let call_register = self.next_register();
result.push(Instr::Pop { address: to }); result.push(Instr::Push { value: Type::Int(1) });
result.push(Instr::Store { result.push(Instr::Call);
address: call_register,
value: Type::Int(1),
});
result.push(Instr::Call {
address: call_register,
args: to,
});
}, },
"add" | "+" => { "add" | "+" => {
let mut lhs = self.compile_atom(&args[0])?; let mut lhs = self.compile_atom(&args[0])?;
@ -135,23 +128,19 @@ impl Compiler {
match atom { match atom {
Int(i) => { Int(i) => {
result.push(Instr::Comment { text: format!("----- {}", comp) });
result.push(Instr::Push { value: Type::Int(*i) }); result.push(Instr::Push { value: Type::Int(*i) });
}, },
Float(f) => { Float(f) => {
result.push(Instr::Comment { text: format!("----- {}", comp) });
result.push(Instr::Push { value: Type::Float(*f) }); result.push(Instr::Push { value: Type::Float(*f) });
}, },
Str(s) => { Str(s) => {
result.push(Instr::Comment { text: format!("----- {}", comp) });
result.push(Instr::Push { value: Type::String(s.to_string()) }); result.push(Instr::Push { value: Type::String(s.to_string()) });
}, },
Boolean(b) => { Boolean(b) => {
result.push(Instr::Comment { text: format!("----- {}", comp) });
result.push(Instr::Push { value: Type::Boolean(*b) }); result.push(Instr::Push { value: Type::Boolean(*b) });
}, },
Symbol(s) => { Symbol(s) => {
result.push(Instr::Comment { text: format!("----- {} variable", comp) }); result.push(Instr::Comment { text: format!("{} variable", comp) });
result.push(Instr::Jump { to: format!("function_{}", s), }); result.push(Instr::Jump { to: format!("function_{}", s), });
}, },
_ => { result.append(&mut self.compile(atom.clone())?); } _ => { result.append(&mut self.compile(atom.clone())?); }

View file

@ -183,7 +183,7 @@ pub enum Instr {
// Store a literal value into a register. // Store a literal value into a register.
Store { address: Register, value: Type }, Store { address: Register, value: Type },
// Call intrinsic function. // Call intrinsic function.
Call { address: Register, args: Register }, Call,
// Stack operations. // Stack operations.
Push { value: Type }, Pop { address: Register }, Swap, Push { value: Type }, Pop { address: Register }, Swap,
// Stack arithmetic. // Stack arithmetic.
@ -204,7 +204,7 @@ impl Display for Instr {
Instr::Label { name } => write!(f, ".{}:", name), Instr::Label { name } => write!(f, ".{}:", name),
Instr::Comment { text } => write!(f, ";{}", text), Instr::Comment { text } => write!(f, ";{}", text),
Instr::Store { address, value } => write!(f, " STORE {} {}", address, value), Instr::Store { address, value } => write!(f, " STORE {} {}", address, value),
Instr::Call { address, args } => write!(f, " CALL {} {}", address, args), Instr::Call => write!(f, " CALL"),
Instr::Push { value } => write!(f, " PUSH {}", value), Instr::Push { value } => write!(f, " PUSH {}", value),
Instr::Pop { address } => write!(f, " POP {}", address), Instr::Pop { address } => write!(f, " POP {}", address),
Instr::Swap => write!(f, " SWAP"), Instr::Swap => write!(f, " SWAP"),

View file

@ -20,10 +20,7 @@ pub fn parse_instr(src: &str) -> Vec<Instr> {
address: register!(tokens[1]), address: register!(tokens[1]),
value: value!(tokens[2]), value: value!(tokens[2]),
}); }, }); },
"CALL" => { result.push(Instr::Call { "CALL" => { result.push(Instr::Call); },
address: register!(tokens[1]),
args: register!(tokens[2]),
}); },
"PUSH" => { result.push(Instr::Push { value: value!(tokens[1]) }); }, "PUSH" => { result.push(Instr::Push { value: value!(tokens[1]) }); },
"POP" => { result.push(Instr::Pop { address: register!(tokens[1]) }); }, "POP" => { result.push(Instr::Pop { address: register!(tokens[1]) }); },
"SWAP" => { result.push(Instr::Swap); }, "SWAP" => { result.push(Instr::Swap); },

View file

@ -73,10 +73,10 @@ impl VM {
self.store(&address, &value)?; self.store(&address, &value)?;
continue 'tco; continue 'tco;
}, },
Call { address, args, .. } => { Call => {
let args = &self.registers[args.value()]; let index = &self.stack.pop().unwrap();
let address = &self.registers[address.value()]; let args = &self.stack.pop().unwrap();
call(address, args, self.instr_pointer)?; call(index, args, self.instr_pointer)?;
continue 'tco; continue 'tco;
}, },
Push { value } => { Push { value } => {