From 8be3cb5a5555a1df2ebf365fa8e1f07e9cfb2410 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sun, 6 Feb 2022 14:30:51 +0700 Subject: [PATCH] feat: some comment in compiled file --- blspc/src/compiler/compile.rs | 7 +++---- blspc/src/vm/instr.rs | 2 +- var.bsm | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 var.bsm diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index de8e917..ca8442d 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -26,7 +26,6 @@ impl Compiler { pub fn compile(&mut self, src: Sexpr) -> Result, String> { let mut result = Vec::new(); - let comp = src.clone(); // Used for commenting 'tco: loop { match src { @@ -40,14 +39,13 @@ impl Compiler { } }, "fun" => { - 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])), }; 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.push(Instr::Return); }, @@ -180,7 +178,7 @@ impl Compiler { 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), }); } } @@ -209,6 +207,7 @@ impl Compiler { Some((_, pointer)) => *pointer, None => return Err(format!("Undefined variable {}", s)), }; + result.push(Instr::Comment { text: format!("`{}` variable", s) }); result.push(Instr::Load { address: var_pointer }); }, _ => { result.append(&mut self.compile(atom.clone())?); } diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index b447a97..9dacec4 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -56,7 +56,7 @@ impl Display for Instr { // --4-- Padding // ----------20--------- Parameter start 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::Store { address } => write!(f, " STORE {}", address), diff --git a/var.bsm b/var.bsm new file mode 100644 index 0000000..f61081e --- /dev/null +++ b/var.bsm @@ -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