From a7a61acd6ef532a6fde575f1d20abc0e48aa5460 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sat, 5 Feb 2022 13:31:39 +0700 Subject: [PATCH] refactor: `CALL` use string --- blspc/src/compiler/compile.rs | 15 +++--------- blspc/src/vm/instr.rs | 4 ++-- blspc/src/vm/parser.rs | 2 +- blspc/src/vm/vm.rs | 45 +++++++++++++++-------------------- 4 files changed, 25 insertions(+), 41 deletions(-) diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index 6fd5d37..4eb946e 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -23,10 +23,6 @@ impl Compiler { r } - fn current_register(&self) -> Register { - Register { value: self.register_pointer - 1 } - } - pub fn compile(&mut self, src: Sexpr) -> Result, String> { let mut result = Vec::new(); let comp = src.clone(); // Used for commenting @@ -121,15 +117,10 @@ impl Compiler { match intrinsic.as_str() { "print" => { result.append(&mut self.compile(args[0].clone())?); - - result.push(Instr::Push { value: Type::Int(1) }); - result.push(Instr::Call); + result.push(Instr::Call { function: "print".to_string() }); }, - "read" => { - result.push(Instr::Push { value: Type::Int(0) }); // read doesn't need an argument - result.push(Instr::Push { value: Type::Int(2) }); - result.push(Instr::Call); - } + "read" => { result.push(Instr::Call { function: "read".to_string() }); } + "add" | "+" => { let mut lhs = self.compile_atom(&args[0])?; result.append(&mut lhs); diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index 966e2dd..8a53e94 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -176,7 +176,7 @@ pub enum Instr { // Variable declaration Load { address: Register }, Store { address: Register }, // Call intrinsic function. - Call, + Call { function: String }, // Stack operations. Push { value: Type }, Pop { address: Register }, Swap, // Stack arithmetic operations. @@ -204,7 +204,7 @@ impl Display for Instr { Instr::Load { address } => write!(f, " LOAD {}", address), Instr::Store { address } => write!(f, " STORE {}", address), - Instr::Call => write!(f, " CALL"), + Instr::Call { function } => write!(f, " CALL {}", function), Instr::Push { value } => write!(f, " PUSH {}", value), Instr::Pop { address } => write!(f, " POP {}", address), diff --git a/blspc/src/vm/parser.rs b/blspc/src/vm/parser.rs index 0eba2db..17cd029 100644 --- a/blspc/src/vm/parser.rs +++ b/blspc/src/vm/parser.rs @@ -19,7 +19,7 @@ pub fn parse_instr(src: &str) -> Vec { "LOAD" => { result.push(Instr::Load { address: register!(tokens[1].to_string()) }); }, "STORE" => { result.push(Instr::Store { address: register!(tokens[1].to_string()) }); }, - "CALL" => { result.push(Instr::Call); }, + "CALL" => { result.push(Instr::Call { function: tokens[1].to_string() }); }, "PUSH" => { result.push(Instr::Push { value: value!(tokens[1]) }); }, "POP" => { result.push(Instr::Pop { address: register!(tokens[1]) }); }, diff --git a/blspc/src/vm/vm.rs b/blspc/src/vm/vm.rs index 8f6c3d9..d89f16b 100644 --- a/blspc/src/vm/vm.rs +++ b/blspc/src/vm/vm.rs @@ -6,7 +6,7 @@ pub enum Error { NoMainFunction, StackOverflow, UnknownFunction(String), - UnknownFunctionCall(isize, isize), + UnknownFunctionCall(String), InvalidAriphmeticOperation, } @@ -16,7 +16,7 @@ impl Display for Error { Error::NoMainFunction => write!(f, "Main function not found"), Error::StackOverflow => write!(f, "Stack overflow"), Error::UnknownFunction(name) => write!(f, "Unknown function: {}", name), - Error::UnknownFunctionCall(l, e) => write!(f, "Unknown function call at {}: {}", l, e), + Error::UnknownFunctionCall(function) => write!(f, "Unknown function call: {}", function), Error::InvalidAriphmeticOperation => write!(f, "Invalid ariphmetic operation"), } } @@ -81,10 +81,8 @@ impl VM { continue 'tco; }, - Call => { - let index = &self.stack.pop().unwrap(); - let args = &self.stack.pop().unwrap(); - self.call(index, args, self.instr_pointer)?; + Call { function } => { + self.call(function)?; continue 'tco; }, @@ -205,26 +203,21 @@ impl VM { Err(Error::UnknownFunction(name)) } - fn call(&mut self, index: &Type, args: &Type, line: isize) -> Result<(), Error> { - match index { - Type::Int(i) => { - match i { - 0 => Err(Error::UnknownFunctionCall(line, 0)), - 1 => { - print!("{}", args.fmt()); - Ok(()) - }, - 2 => { - let mut input = String::new(); - io::stdin().read_line(&mut input).unwrap(); - let input = input.trim().parse::().unwrap(); - self.stack.push(input); - Ok(()) - } - _ => Err(Error::UnknownFunctionCall(line, *i as isize)), - } - } - _ => {dbg!(index); Err(Error::UnknownFunctionCall(line, -1))}, + fn call(&mut self, function: &String) -> Result<(), Error> { + match function.as_str() { + "print" => { + let value = self.stack.pop().unwrap(); + println!("{}", value.fmt()); + return Ok(()); + }, + "read" => { + let mut input = String::new(); + io::stdin().read_line(&mut input).unwrap(); + let input = input.trim().parse::().unwrap(); + self.stack.push(input); + Ok(()) + }, + _ => { dbg!(function); Err(Error::UnknownFunctionCall(function.to_string())) }, } } }