1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-09-28 07:27:35 +00:00

feat: some comment in compiled file

This commit is contained in:
Natapat Samutpong 2022-02-06 14:30:51 +07:00
parent 4ce5e43c78
commit 8be3cb5a55
3 changed files with 20 additions and 5 deletions

View file

@ -26,7 +26,6 @@ impl Compiler {
pub fn compile(&mut self, src: Sexpr) -> Result<Vec<Instr>, String> { pub fn compile(&mut self, src: Sexpr) -> Result<Vec<Instr>, String> {
let mut result = Vec::new(); let mut result = Vec::new();
let comp = src.clone(); // Used for commenting
'tco: loop { 'tco: loop {
match src { match src {
@ -40,7 +39,6 @@ impl Compiler {
} }
}, },
"fun" => { "fun" => {
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])),
@ -180,7 +178,7 @@ impl Compiler {
result.push(Instr::Not); result.push(Instr::Not);
}, },
_ => { _ => {
result.push(Instr::Comment { text: format!("{} function", intrinsic) }); result.push(Instr::Comment { text: format!("`{}` function", intrinsic) });
result.push(Instr::JumpLabel { to: format!("function_{}", intrinsic), }); result.push(Instr::JumpLabel { to: format!("function_{}", intrinsic), });
} }
} }
@ -209,6 +207,7 @@ impl Compiler {
Some((_, pointer)) => *pointer, Some((_, pointer)) => *pointer,
None => return Err(format!("Undefined variable {}", s)), None => return Err(format!("Undefined variable {}", s)),
}; };
result.push(Instr::Comment { text: format!("`{}` variable", s) });
result.push(Instr::Load { address: var_pointer }); result.push(Instr::Load { address: var_pointer });
}, },
_ => { result.append(&mut self.compile(atom.clone())?); } _ => { result.append(&mut self.compile(atom.clone())?); }

16
var.bsm Normal file
View file

@ -0,0 +1,16 @@
.function_return_true:
PUSH true
RET
.function_main:
PUSH "John"
STORE %1
; `return_true` function
JMPL function_return_true
JMPF 3
; `name` variable
LOAD %1
CALL print
JMP 2
PUSH "no"
CALL print
RET