diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index 6216b19..4bacdc8 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -19,17 +19,17 @@ impl Compiler { self.register_pointer += 1; r } - + fn current_register(&self) -> Register { Register { value: self.register_pointer - 1 } } - + fn next_label(&mut self) -> usize { let l = self.label_pointer; self.label_pointer += 1; l } - + fn current_label(&self) -> usize { self.label_pointer - 1 } @@ -56,7 +56,7 @@ impl Compiler { // TODO: Remove .clone() let mut cond = self.compile(cdr[0].clone(), depth + 1)?; result.append(&mut cond); - + result.push(Instr::PopJumpIfFalse { to: 999, // To be replaced later label: self.next_label(), @@ -64,10 +64,10 @@ impl Compiler { let mut then = self.compile(cdr[1].clone(), depth + 1)?; let jump_label = self.next_label(); - + let mut else_ = self.compile(cdr[2].clone(), depth + 1)?; let else_label = self.current_label() - else_.len() + 1; - + let idx = result.len() - 1; match result[idx] { Instr::PopJumpIfFalse { to: _, label: l } => { @@ -75,7 +75,7 @@ impl Compiler { } _ => unreachable!(), } - + result.append(&mut then); result.push(Instr::Jump { to: self.current_label() + 1, @@ -107,17 +107,6 @@ impl Compiler { } Ok(result) } - - fn compile_quoted(&mut self, atom: &Vec) -> Result, String> { - let mut result = Vec::new(); - - result.push(Instr::Push { - value: Type::String(format!("({})", atom.iter().map(|x| format!("{}", x)).collect::>().join(" "))), - label: self.next_label(), - }); - - Ok(result) - } fn compile_atom(&mut self, atom: &Sexpr, depth: usize) -> Result, String> { let mut result = Vec::new(); @@ -154,7 +143,7 @@ impl Compiler { Ok(result) } - + fn compile_intrinsic(&mut self, intrinsic: &String, args: &[Sexpr], depth: usize) -> Result, String> { let mut result = Vec::new(); @@ -184,37 +173,37 @@ impl Compiler { "add" | "+" => { let mut lhs = self.compile_atom(&args[0], depth + 1)?; result.append(&mut lhs); - + let mut rhs = self.compile_atom(&args[1], depth + 1)?; result.append(&mut rhs); - + result.push(Instr::Add { label: self.next_label() }); }, "sub" | "-" => { let mut lhs = self.compile_atom(&args[0], depth + 1)?; result.append(&mut lhs); - + let mut rhs = self.compile_atom(&args[1], depth + 1)?; result.append(&mut rhs); - + result.push(Instr::Sub { label: self.next_label() }); }, "mul" | "*" => { let mut lhs = self.compile_atom(&args[0], depth + 1)?; result.append(&mut lhs); - + let mut rhs = self.compile_atom(&args[1], depth + 1)?; result.append(&mut rhs); - + result.push(Instr::Mul { label: self.next_label() }); }, "div" | "/" => { let mut lhs = self.compile_atom(&args[0], depth + 1)?; result.append(&mut lhs); - + let mut rhs = self.compile_atom(&args[1], depth + 1)?; result.append(&mut rhs); - + result.push(Instr::Div { label: self.next_label() }); }, _ => return Err(format!("Unknown intrinsic: {}", intrinsic)), @@ -222,4 +211,40 @@ 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/vm/instr.rs b/blspc/src/vm/instr.rs index 3b93c70..08acac3 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -55,9 +55,9 @@ impl Type { Type::String(s) => s.clone(), Type::Array(a) => { let mut s = "[".to_string(); - for t in a { + for (i, t) in a.iter().enumerate() { s.push_str(&t.fmt()); - s.push_str(", "); + if i < a.len() - 1 { s.push_str(", "); } } s.push_str("]"); s @@ -132,12 +132,12 @@ impl Display for Type { Type::Boolean(b) => write!(f, ":{}", b), Type::String(s) => write!(f, "$\"{}\"", s), Type::Array(a) => { - write!(f, "[[")?; - for t in a { + write!(f, "[[ ")?; + for (i, t) in a.iter().enumerate() { write!(f, "{}", t)?; - write!(f, ", ")?; + if i < a.len() - 1 { write!(f, ", ")?; } } - write!(f, "]]") + write!(f, " ]]") } } }