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

@ -19,17 +19,17 @@ impl Compiler {
self.register_pointer += 1; self.register_pointer += 1;
r r
} }
fn current_register(&self) -> Register { fn current_register(&self) -> Register {
Register { value: self.register_pointer - 1 } Register { value: self.register_pointer - 1 }
} }
fn next_label(&mut self) -> usize { fn next_label(&mut self) -> usize {
let l = self.label_pointer; let l = self.label_pointer;
self.label_pointer += 1; self.label_pointer += 1;
l l
} }
fn current_label(&self) -> usize { fn current_label(&self) -> usize {
self.label_pointer - 1 self.label_pointer - 1
} }
@ -56,7 +56,7 @@ impl Compiler {
// TODO: Remove .clone() // TODO: Remove .clone()
let mut cond = self.compile(cdr[0].clone(), depth + 1)?; let mut cond = self.compile(cdr[0].clone(), depth + 1)?;
result.append(&mut cond); result.append(&mut cond);
result.push(Instr::PopJumpIfFalse { result.push(Instr::PopJumpIfFalse {
to: 999, // To be replaced later to: 999, // To be replaced later
label: self.next_label(), label: self.next_label(),
@ -64,10 +64,10 @@ impl Compiler {
let mut then = self.compile(cdr[1].clone(), depth + 1)?; let mut then = self.compile(cdr[1].clone(), depth + 1)?;
let jump_label = self.next_label(); let jump_label = self.next_label();
let mut else_ = self.compile(cdr[2].clone(), depth + 1)?; let mut else_ = self.compile(cdr[2].clone(), depth + 1)?;
let else_label = self.current_label() - else_.len() + 1; let else_label = self.current_label() - else_.len() + 1;
let idx = result.len() - 1; let idx = result.len() - 1;
match result[idx] { match result[idx] {
Instr::PopJumpIfFalse { to: _, label: l } => { Instr::PopJumpIfFalse { to: _, label: l } => {
@ -75,7 +75,7 @@ impl Compiler {
} }
_ => unreachable!(), _ => unreachable!(),
} }
result.append(&mut then); result.append(&mut then);
result.push(Instr::Jump { result.push(Instr::Jump {
to: self.current_label() + 1, to: self.current_label() + 1,
@ -107,17 +107,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();
@ -154,7 +143,7 @@ impl Compiler {
Ok(result) Ok(result)
} }
fn compile_intrinsic(&mut self, intrinsic: &String, args: &[Sexpr], depth: usize) -> Result<Vec<Instr>, String> { fn compile_intrinsic(&mut self, intrinsic: &String, args: &[Sexpr], depth: usize) -> Result<Vec<Instr>, String> {
let mut result = Vec::new(); let mut result = Vec::new();
@ -184,37 +173,37 @@ impl Compiler {
"add" | "+" => { "add" | "+" => {
let mut lhs = self.compile_atom(&args[0], depth + 1)?; let mut lhs = self.compile_atom(&args[0], depth + 1)?;
result.append(&mut lhs); result.append(&mut lhs);
let mut rhs = self.compile_atom(&args[1], depth + 1)?; let mut rhs = self.compile_atom(&args[1], depth + 1)?;
result.append(&mut rhs); result.append(&mut rhs);
result.push(Instr::Add { label: self.next_label() }); result.push(Instr::Add { label: self.next_label() });
}, },
"sub" | "-" => { "sub" | "-" => {
let mut lhs = self.compile_atom(&args[0], depth + 1)?; let mut lhs = self.compile_atom(&args[0], depth + 1)?;
result.append(&mut lhs); result.append(&mut lhs);
let mut rhs = self.compile_atom(&args[1], depth + 1)?; let mut rhs = self.compile_atom(&args[1], depth + 1)?;
result.append(&mut rhs); result.append(&mut rhs);
result.push(Instr::Sub { label: self.next_label() }); result.push(Instr::Sub { label: self.next_label() });
}, },
"mul" | "*" => { "mul" | "*" => {
let mut lhs = self.compile_atom(&args[0], depth + 1)?; let mut lhs = self.compile_atom(&args[0], depth + 1)?;
result.append(&mut lhs); result.append(&mut lhs);
let mut rhs = self.compile_atom(&args[1], depth + 1)?; let mut rhs = self.compile_atom(&args[1], depth + 1)?;
result.append(&mut rhs); result.append(&mut rhs);
result.push(Instr::Mul { label: self.next_label() }); result.push(Instr::Mul { label: self.next_label() });
}, },
"div" | "/" => { "div" | "/" => {
let mut lhs = self.compile_atom(&args[0], depth + 1)?; let mut lhs = self.compile_atom(&args[0], depth + 1)?;
result.append(&mut lhs); result.append(&mut lhs);
let mut rhs = self.compile_atom(&args[1], depth + 1)?; let mut rhs = self.compile_atom(&args[1], depth + 1)?;
result.append(&mut rhs); result.append(&mut rhs);
result.push(Instr::Div { label: self.next_label() }); result.push(Instr::Div { label: self.next_label() });
}, },
_ => return Err(format!("Unknown intrinsic: {}", intrinsic)), _ => return Err(format!("Unknown intrinsic: {}", intrinsic)),
@ -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
@ -132,12 +132,12 @@ impl Display for Type {
Type::Boolean(b) => write!(f, ":{}", b), Type::Boolean(b) => write!(f, ":{}", b),
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, " ]]")
} }
} }
} }