From 8382f0b5afa7a55ac0c087fb0470acef0497392b Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 27 Jan 2022 16:08:21 +0700 Subject: [PATCH] refactor: remove messy implementation --- README.md | 7 +++--- blspc/src/compiler/compile.rs | 43 +---------------------------------- blspc/src/compiler/parser.rs | 3 ++- example/hello.blsp | 2 +- 4 files changed, 7 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 6ec9d26..fb70f12 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,6 @@ Hello, World! ``` ## Progress: -- [X] Lexer & Parser -- [ ] Syntax checker & Type checker -- [ ] Interpreter -- [X] Compiler +TODO: +- Quote, Quasiquote, etc. +- Optimizing diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index 4bacdc8..64d26eb 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -46,12 +46,7 @@ impl Compiler { for c in cdr { result.append(&mut self.compile(c, depth + 1)?); } - } - "list" => { - let mut joined = Vec::new(); - for c in cdr { joined.push(c); } - result.append(&mut self.compile_quoted(&joined)?); - } + }, "if" => { // TODO: Remove .clone() let mut cond = self.compile(cdr[0].clone(), depth + 1)?; @@ -211,40 +206,4 @@ impl Compiler { Ok(result) } - - fn compile_quoted(&mut self, atom: &Vec) -> Result, String> { - let mut result = Vec::new(); - - // Vec -> Vec - let mut types = Vec::new(); - for a in atom { - types.push(sexpr_to_type(a)?); - } - - result.push(Instr::Push { - value: Type::Array(types), - label: self.next_label(), - }); - - Ok(result) - } -} - -fn sexpr_to_type(sexpr: &Sexpr) -> Result { - match sexpr { - Int(i) => Ok(Type::Int(*i)), - Float(f) => Ok(Type::Float(*f)), - Str(s) => Ok(Type::String(s.clone())), - Boolean(b) => Ok(Type::Boolean(*b)), - Symbol(s) => Ok(Type::String(s.to_string())), - Cons(car, cdr) => { - let mut array = Vec::new(); - array.push(sexpr_to_type(car)?); - for item in cdr { - array.push(sexpr_to_type(item)?); - } - Ok(Type::Array(array)) - }, - _ => unimplemented!(), - } } \ No newline at end of file diff --git a/blspc/src/compiler/parser.rs b/blspc/src/compiler/parser.rs index 82ea29b..97bbf24 100644 --- a/blspc/src/compiler/parser.rs +++ b/blspc/src/compiler/parser.rs @@ -58,7 +58,8 @@ impl Parser { match self.peek() { Some(s) => match s.as_str() { ")" => Err(format!("Unexpected ')' at position {}", self.position)), - "'" => { self.next(); self.parse_quote_sequence(")")}, + // TODO: Handle quote and that stuff. + "'" => { unimplemented!() }, "(" => self.parse_sequence(")"), _ => self.parse_atom(), } diff --git a/example/hello.blsp b/example/hello.blsp index 89d01f8..0ae84b3 100644 --- a/example/hello.blsp +++ b/example/hello.blsp @@ -1 +1 @@ -(print "Hello, hello world!") \ No newline at end of file +(print "Hello, world!") \ No newline at end of file