From cac125bd1cbae94f0e8b2e16a740c680fe4b9a5e Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 27 Jan 2022 10:43:46 +0700 Subject: [PATCH] feat: simpler instruction parser --- blspc/example/if.bbb | 19 ------ blspc/src/vm/instr.rs | 30 ++++----- blspc/src/vm/parser.rs | 95 ++++++++++++++------------- {blspc/example => example}/hello.blsp | 0 {blspc/example => example}/if.blsp | 0 {blspc/example => example}/math.blsp | 0 {blspc/example => example}/s.blsp | 0 7 files changed, 64 insertions(+), 80 deletions(-) delete mode 100644 blspc/example/if.bbb rename {blspc/example => example}/hello.blsp (100%) rename {blspc/example => example}/if.blsp (100%) rename {blspc/example => example}/math.blsp (100%) rename {blspc/example => example}/s.blsp (100%) diff --git a/blspc/example/if.bbb b/blspc/example/if.bbb deleted file mode 100644 index af8719a..0000000 --- a/blspc/example/if.bbb +++ /dev/null @@ -1,19 +0,0 @@ -; ------------------------------ -; (if true (print (+ 34 35)) (print "False")) -; ------- Condition block ------ -1: STORE r1 $true ; r1 = True -2: JUMP_IF_FALSE r1 9 ; if -; --------- True block --------- -3: STORE r2 $34 ; r2 = 34 -4: STORE r3 $35 ; r3 = 35 -5: IADD r2 r3 r4 ; r2 + r3 -> r4 -6: STORE r5 $1 ; r5 = 1 (function calling) -7: CALL r5 r4 ; call 1 (print) 34 (r4) -8: JUMP 12 -; -------- False block --------- -9: STORE r6 $"False" ; r6 = "False" -10: STORE r7 $1 ; r7 = 1 (function calling) -11: CALL r7 r6 ; call 1 (print) "False" (r6) -12: STORE r8 $0 ; r8 = 0 (return value) -13: RETURN r8 ; return 0 (exit code) -; ------------ End ------------- \ No newline at end of file diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index ac95168..707fdf2 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -12,9 +12,9 @@ pub enum Type { impl Display for Type { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - Type::Int(i) => write!(f, "${}", i), - Type::Float(fl) => write!(f, "${}", fl), - Type::Boolean(b) => write!(f, "${}", b), + Type::Int(i) => write!(f, ":{}", i), + Type::Float(fl) => write!(f, ":{}", fl), + Type::Boolean(b) => write!(f, ":{}", b), Type::String(s) => write!(f, "$\"{}\"", s), } } @@ -24,7 +24,7 @@ impl FromStr for Type { type Err = String; fn from_str(s: &str) -> Result { - if !s.starts_with("$") { + if !s.starts_with("$") && !s.starts_with(":") { return Err(format!("Invalid literal: {}", s)); } @@ -92,17 +92,17 @@ impl Display for Instr { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { // Instr::Load { address, label } => write!(f, "{}: LOAD {}", label, address), - Instr::Store { address, value , label} => write!(f, "{}: STORE {} {}", label, address, value), - Instr::Call { address, args, label } => write!(f, "{}: CALL {} {}", label, address, args), - Instr::Push { value, label } => write!(f, "{}: PUSH {}", label, value), - Instr::Pop { address, label } => write!(f, "{}: POP {}", label, address), - Instr::Add { label } => write!(f, "{}: ADD", label), - Instr::Sub { label } => write!(f, "{}: SUB", label), - Instr::Mul { label } => write!(f, "{}: MUL", label), - Instr::Div { label } => write!(f, "{}: DIV", label), - Instr::Jump { to, label } => write!(f, "{}: JUMP {}", label, to), - Instr::PopJumpIfFalse { to, label } => write!(f, "{}: POP_JUMP_IF_FALSE {}", label, to), - Instr::Return { value, label } => write!(f, "{}: RETURN {}", label, value), + Instr::Store { address, value , label} => write!(f, "{} STORE {} {}", label, address, value), + Instr::Call { address, args, label } => write!(f, "{} CALL {} {}", label, address, args), + Instr::Push { value, label } => write!(f, "{} PUSH {}", label, value), + Instr::Pop { address, label } => write!(f, "{} POP {}", label, address), + Instr::Add { label } => write!(f, "{} ADD", label), + Instr::Sub { label } => write!(f, "{} SUB", label), + Instr::Mul { label } => write!(f, "{} MUL", label), + Instr::Div { label } => write!(f, "{} DIV", label), + Instr::Jump { to, label } => write!(f, "{} JMP {}", label, to), + Instr::PopJumpIfFalse { to, label } => write!(f, "{} POP_JUMP_IF_FALSE {}", label, to), + Instr::Return { value, label } => write!(f, "{} RETURN {}", label, value), } } } \ No newline at end of file diff --git a/blspc/src/vm/parser.rs b/blspc/src/vm/parser.rs index c81fd0b..1bc597b 100644 --- a/blspc/src/vm/parser.rs +++ b/blspc/src/vm/parser.rs @@ -1,57 +1,60 @@ +use regex::Regex; + use crate::vm::instr::*; +const REGEX: &str = r###"[^\s\$";]+|\$"[^"]*"|;.*"###; + +macro_rules! value { ($s:expr) => { $s.parse::().unwrap() }; } +macro_rules! register { ($s:expr) => { $s.parse::().unwrap() }; } +macro_rules! label { ($s:expr) => { $s.parse::().unwrap() }; } + pub fn parse_instr(src: &str) -> Vec { + let regex = Regex::new(REGEX).unwrap(); let mut result = Vec::new(); for line in src.lines() { - //