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

refactor: cleanup unused, def => let

This commit is contained in:
azur 2022-02-04 09:46:47 +07:00 committed by Natapat Samutpong
parent a08f3ee5f0
commit 6d2cbd42ea
5 changed files with 13 additions and 19 deletions

View file

@ -22,7 +22,11 @@ Hello, World!
DONE: DONE:
- Parsing, Compiling, Running(VM) - Parsing, Compiling, Running(VM)
- Intrinsic: - Intrinsic:
- `fun`, `do`, `print`, `if` - Function definition: `fun`
- Variable definition: `let`
- Do blocks: `do`
- Printing: `print`
- Condition: `if`
- Math: - Math:
- `+` , `add` - `+` , `add`
- `-` , `sub` - `-` , `sub`

View file

@ -66,7 +66,7 @@ impl Compiler {
result.push(Instr::Jump { to: len(&else_) }); result.push(Instr::Jump { to: len(&else_) });
result.append(&mut else_); result.append(&mut else_);
}, },
"def" => { "let" => {
let var_name = match &cdr[0] { let var_name = match &cdr[0] {
Symbol(ref name) => name.clone(), Symbol(ref name) => name.clone(),
_ => return Err(format!("Expected variable name, got {}", cdr[0])), _ => return Err(format!("Expected variable name, got {}", cdr[0])),

View file

@ -10,19 +10,9 @@ pub enum Type {
Float(f64), Float(f64),
Boolean(bool), Boolean(bool),
String(String), String(String),
Symbol(String),
} }
impl Type { impl Type {
pub fn as_bool(&self) -> bool {
match self {
Type::Boolean(b) => *b,
Type::Int(i) => *i != 0,
Type::Float(f) => *f != 0.0,
Type::String(s) => !s.is_empty(),
_ => unreachable!(),
}
}
pub fn is_null(&self) -> bool { pub fn is_null(&self) -> bool {
match self { match self {
@ -48,7 +38,6 @@ impl Type {
false => "false".to_string(), false => "false".to_string(),
}, },
Type::String(s) => s.clone(), Type::String(s) => s.clone(),
_ => unreachable!(),
} }
} }
} }
@ -117,7 +106,6 @@ impl Display for Type {
Type::Float(fl) => write!(f, ":{}", fl), Type::Float(fl) => write!(f, ":{}", fl),
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::Symbol(s) => write!(f, "function_{}", s),
_ => unreachable!(), _ => unreachable!(),
} }
} }

View file

@ -7,7 +7,6 @@ pub enum Error {
StackOverflow, StackOverflow,
UnknownFunction(String), UnknownFunction(String),
UnknownFunctionCall(isize, isize), UnknownFunctionCall(isize, isize),
UnknownVariable(String),
InvalidAriphmeticOperation, InvalidAriphmeticOperation,
} }
@ -18,7 +17,6 @@ impl Display for Error {
Error::StackOverflow => write!(f, "Stack overflow"), Error::StackOverflow => write!(f, "Stack overflow"),
Error::UnknownFunction(name) => write!(f, "Unknown function: {}", name), Error::UnknownFunction(name) => write!(f, "Unknown function: {}", name),
Error::UnknownFunctionCall(l, e) => write!(f, "Unknown function call at {}: {}", l, e), Error::UnknownFunctionCall(l, e) => write!(f, "Unknown function call at {}: {}", l, e),
Error::UnknownVariable(name) => write!(f, "Unknown variable: {}", name),
Error::InvalidAriphmeticOperation => write!(f, "Invalid ariphmetic operation"), Error::InvalidAriphmeticOperation => write!(f, "Invalid ariphmetic operation"),
} }
} }

View file

@ -1,3 +1,7 @@
(fun main (fun return_true true)
(do (def name "Bobby")
(print name))) (fun main (do
(let name "John")
(if (return_true)
(print name)
(print "no"))))