1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

feat: i forgot what i did

it was about list i think i forgot
This commit is contained in:
Natapat Samutpong 2022-01-27 15:50:31 +07:00
parent a3d1075a57
commit aa433b7787
2 changed files with 58 additions and 33 deletions

View file

@ -108,17 +108,6 @@ impl Compiler {
Ok(result) Ok(result)
} }
fn compile_quoted(&mut self, atom: &Vec<Sexpr>) -> Result<Vec<Instr>, String> {
let mut result = Vec::new();
result.push(Instr::Push {
value: Type::String(format!("({})", atom.iter().map(|x| format!("{}", x)).collect::<Vec<String>>().join(" "))),
label: self.next_label(),
});
Ok(result)
}
fn compile_atom(&mut self, atom: &Sexpr, depth: usize) -> Result<Vec<Instr>, String> { fn compile_atom(&mut self, atom: &Sexpr, depth: usize) -> Result<Vec<Instr>, String> {
let mut result = Vec::new(); let mut result = Vec::new();
@ -222,4 +211,40 @@ impl Compiler {
Ok(result) 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

@ -55,9 +55,9 @@ impl Type {
Type::String(s) => s.clone(), Type::String(s) => s.clone(),
Type::Array(a) => { Type::Array(a) => {
let mut s = "[".to_string(); let mut s = "[".to_string();
for t in a { for (i, t) in a.iter().enumerate() {
s.push_str(&t.fmt()); s.push_str(&t.fmt());
s.push_str(", "); if i < a.len() - 1 { s.push_str(", "); }
} }
s.push_str("]"); s.push_str("]");
s s
@ -133,9 +133,9 @@ impl Display for Type {
Type::String(s) => write!(f, "$\"{}\"", s), Type::String(s) => write!(f, "$\"{}\"", s),
Type::Array(a) => { Type::Array(a) => {
write!(f, "[[ ")?; write!(f, "[[ ")?;
for t in a { for (i, t) in a.iter().enumerate() {
write!(f, "{}", t)?; write!(f, "{}", t)?;
write!(f, ", ")?; if i < a.len() - 1 { write!(f, ", ")?; }
} }
write!(f, " ]]") write!(f, " ]]")
} }