1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-09-28 07:27:35 +00:00

refactor: remove messy implementation

This commit is contained in:
Natapat Samutpong 2022-01-27 16:08:21 +07:00
parent aa433b7787
commit 8382f0b5af
4 changed files with 7 additions and 48 deletions

View file

@ -21,7 +21,6 @@ Hello, World!
```
## <img src="https://raw.githubusercontent.com/azur1s/bobbylisp/master/assets/icon.png" width="25"> Progress:
- [X] Lexer & Parser
- [ ] Syntax checker & Type checker
- [ ] Interpreter
- [X] Compiler
TODO:
- Quote, Quasiquote, etc.
- Optimizing

View file

@ -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<Sexpr>) -> Result<Vec<Instr>, String> {
let mut result = Vec::new();
// Vec<Sexpr> -> Vec<Type>
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<Type, String> {
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!(),
}
}

View file

@ -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(),
}

View file

@ -1 +1 @@
(print "Hello, hello world!")
(print "Hello, world!")