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:
parent
5cc43a2503
commit
560ac69863
|
@ -38,7 +38,7 @@ impl Compiler {
|
|||
}
|
||||
},
|
||||
"fun" => {
|
||||
result.push(Instr::Comment { text: format!("--- {}", comp) });
|
||||
result.push(Instr::Comment { text: format!("function {}", comp) });
|
||||
let function_name = match &cdr[0] {
|
||||
Symbol(ref name) => format!("function_{}", name.clone()),
|
||||
_ => return Err(format!("Expected function name, got {}", cdr[0])),
|
||||
|
@ -75,15 +75,8 @@ impl Compiler {
|
|||
let to = self.next_register();
|
||||
let call_register = self.next_register();
|
||||
|
||||
result.push(Instr::Pop { address: to });
|
||||
result.push(Instr::Store {
|
||||
address: call_register,
|
||||
value: Type::Int(1),
|
||||
});
|
||||
result.push(Instr::Call {
|
||||
address: call_register,
|
||||
args: to,
|
||||
});
|
||||
result.push(Instr::Push { value: Type::Int(1) });
|
||||
result.push(Instr::Call);
|
||||
},
|
||||
"add" | "+" => {
|
||||
let mut lhs = self.compile_atom(&args[0])?;
|
||||
|
@ -135,23 +128,19 @@ impl Compiler {
|
|||
|
||||
match atom {
|
||||
Int(i) => {
|
||||
result.push(Instr::Comment { text: format!("----- {}", comp) });
|
||||
result.push(Instr::Push { value: Type::Int(*i) });
|
||||
},
|
||||
Float(f) => {
|
||||
result.push(Instr::Comment { text: format!("----- {}", comp) });
|
||||
result.push(Instr::Push { value: Type::Float(*f) });
|
||||
},
|
||||
Str(s) => {
|
||||
result.push(Instr::Comment { text: format!("----- {}", comp) });
|
||||
result.push(Instr::Push { value: Type::String(s.to_string()) });
|
||||
},
|
||||
Boolean(b) => {
|
||||
result.push(Instr::Comment { text: format!("----- {}", comp) });
|
||||
result.push(Instr::Push { value: Type::Boolean(*b) });
|
||||
},
|
||||
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.append(&mut self.compile(atom.clone())?); }
|
||||
|
|
|
@ -183,7 +183,7 @@ pub enum Instr {
|
|||
// Store a literal value into a register.
|
||||
Store { address: Register, value: Type },
|
||||
// Call intrinsic function.
|
||||
Call { address: Register, args: Register },
|
||||
Call,
|
||||
// Stack operations.
|
||||
Push { value: Type }, Pop { address: Register }, Swap,
|
||||
// Stack arithmetic.
|
||||
|
@ -204,7 +204,7 @@ impl Display for Instr {
|
|||
Instr::Label { name } => write!(f, ".{}:", name),
|
||||
Instr::Comment { text } => write!(f, ";{}", text),
|
||||
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::Pop { address } => write!(f, " POP {}", address),
|
||||
Instr::Swap => write!(f, " SWAP"),
|
||||
|
|
|
@ -20,10 +20,7 @@ pub fn parse_instr(src: &str) -> Vec<Instr> {
|
|||
address: register!(tokens[1]),
|
||||
value: value!(tokens[2]),
|
||||
}); },
|
||||
"CALL" => { result.push(Instr::Call {
|
||||
address: register!(tokens[1]),
|
||||
args: register!(tokens[2]),
|
||||
}); },
|
||||
"CALL" => { result.push(Instr::Call); },
|
||||
"PUSH" => { result.push(Instr::Push { value: value!(tokens[1]) }); },
|
||||
"POP" => { result.push(Instr::Pop { address: register!(tokens[1]) }); },
|
||||
"SWAP" => { result.push(Instr::Swap); },
|
||||
|
|
|
@ -73,10 +73,10 @@ impl VM {
|
|||
self.store(&address, &value)?;
|
||||
continue 'tco;
|
||||
},
|
||||
Call { address, args, .. } => {
|
||||
let args = &self.registers[args.value()];
|
||||
let address = &self.registers[address.value()];
|
||||
call(address, args, self.instr_pointer)?;
|
||||
Call => {
|
||||
let index = &self.stack.pop().unwrap();
|
||||
let args = &self.stack.pop().unwrap();
|
||||
call(index, args, self.instr_pointer)?;
|
||||
continue 'tco;
|
||||
},
|
||||
Push { value } => {
|
||||
|
|
Loading…
Reference in a new issue