1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05: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,14 +39,13 @@ 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])),
}; };
let body = &cdr[1]; let body = &cdr[1];
result.push(Instr::Label{ name: function_name }); result.push(Instr::Label { name: function_name });
result.append(&mut self.compile(body.clone())?); result.append(&mut self.compile(body.clone())?);
result.push(Instr::Return); result.push(Instr::Return);
}, },
@ -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())?); }

View file

@ -56,7 +56,7 @@ impl Display for Instr {
// --4-- Padding // --4-- Padding
// ----------20--------- Parameter start // ----------20--------- Parameter start
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::Load { address } => write!(f, " LOAD {}", address), Instr::Load { address } => write!(f, " LOAD {}", address),
Instr::Store { address } => write!(f, " STORE {}", address), Instr::Store { address } => write!(f, " STORE {}", address),

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