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

Compare commits

..

No commits in common. "0a62f145711f9d7b23cbbddd220942709a848ea2" and "07e3af26640b1b7a39b2650ac1fbdf73f568f7ee" have entirely different histories.

6 changed files with 28 additions and 79 deletions

View file

@ -43,15 +43,15 @@ impl Compiler {
parser::BinaryOp::Sub => Instr::NumSub,
parser::BinaryOp::Mul => Instr::NumMul,
parser::BinaryOp::Div => Instr::NumDiv,
parser::BinaryOp::Eq => Instr::NumEq,
parser::BinaryOp::Ne => todo!(),
parser::BinaryOp::Lt => todo!(),
parser::BinaryOp::Gt => todo!(),
parser::BinaryOp::Le => todo!(),
parser::BinaryOp::Ge => todo!(),
// parser::BinaryOp::Eq => Instr::Eq,
// parser::BinaryOp::Ne => Instr::Neq,
// parser::BinaryOp::Lt => Instr::Lt,
// parser::BinaryOp::Gt => Instr::Gt,
// parser::BinaryOp::Le => Instr::Lte,
// parser::BinaryOp::Ge => Instr::Gte,
parser::BinaryOp::And => Instr::BoolAnd,
parser::BinaryOp::Or => Instr::BoolOr,
parser::BinaryOp::Pipe => todo!(),
_ => todo!(),
});
instrs
}
@ -128,13 +128,10 @@ impl Compiler {
match stmt {
Stmt::Fun(name, args, body) => {
let is_main = name == "main";
let mut instrs = match body.0 {
// If the body is a lambda then we don't have to compile
// it into a function
Expr::Lambda(_, _) => self.compile_expr(body.0),
_ => vec![Instr::FuncMake(args, self.compile_expr(body.0))],
};
instrs.push(Instr::Set(name));
let mut instrs = vec![
Instr::FuncMake(args, self.compile_expr(body.0)),
Instr::Set(name),
];
if is_main {
instrs.pop();
instrs.push(Instr::FuncApply);

View file

@ -13,12 +13,9 @@ fn main() {
if let Some(ast) = ast {
let mut compiler = Compiler::new();
let instrs = compiler.compile_program(ast);
// instrs.iter().for_each(|i| println!("{:?}", i));
instrs.iter().for_each(|i| println!("{:?}", i));
let mut executor = Executor::new(instrs);
match executor.run_with(|exec| {
// println!("{:?}", exec.stack);
Ok(())
}) {
match executor.run() {
Ok(_) => {}
Err(e) => println!("Runtime error: {:?}", e),
}

View file

@ -1,22 +1,4 @@
// start
fun foo x = do
69 // unused
print("Hi")
x
end
/* block comment
fun invalid =
*/
fun fac n = if n == 0 then 1 else n * fac(n - 1)
fun main = do
print(foo(1))
print(fac(5))
let foo = if true then 10 else 5 in
print(foo)
end
// end

View file

@ -174,25 +174,15 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = Simple<char>> {
.or(symbol)
.or(delim)
.or(keyword)
.map_with_span(move |token, span| (token, span))
.padded()
.recover_with(skip_then_retry_until([]));
let comments = just('/')
.then_ignore(
just('*')
.ignore_then(take_until(just("*/")).ignored())
.or(just('/').ignore_then(none_of('\n').ignored().repeated().ignored())),
)
.padded()
.ignored()
.repeated();
let comment = just("--").then(take_until(just('\n'))).padded();
token
.padded_by(comments)
.repeated()
.padded_by(comment.repeated())
.map_with_span(|token, span| (token, span))
.padded()
.then_ignore(end())
.repeated()
}
pub fn lex(src: String) -> (Option<Vec<(Token, Span)>>, Vec<Simple<char>>) {

View file

@ -38,15 +38,6 @@ impl Executor {
Ok(())
}
pub fn run_with<F: Fn(&mut Self) -> Result<(), Error>>(&mut self, f: F) -> Result<(), Error> {
while self.ip < self.instrs.len() {
self.step()?;
self.ip += 1;
f(self)?;
}
Ok(())
}
fn err(&self, msg: &str) -> Error {
Error::make(msg, self.ip)
}
@ -102,10 +93,10 @@ impl Executor {
.ok_or_else(|| self.err("invalid instruction pointer"))?;
macro_rules! impl_num_binop {
($op:tt, $ret:ident) => {
($op:tt) => {
match (self.pop()?, self.pop()?) {
(Value::Num(a), Value::Num(b)) => {
self.stack.push(Value::$ret(a $op b));
self.stack.push(Value::Num(a $op b));
}
_ => return Err(Error::make("can't apply operator to non-numbers", self.ip)),
}
@ -126,12 +117,11 @@ impl Executor {
Instr::NumPush(x) => {
self.push(Value::Num(*x))?;
}
Instr::NumAdd => impl_num_binop!(+, Num),
Instr::NumSub => impl_num_binop!(-, Num),
Instr::NumMul => impl_num_binop!(*, Num),
Instr::NumDiv => impl_num_binop!(/, Num),
Instr::NumMod => impl_num_binop!(%, Num),
Instr::NumEq => impl_num_binop!(==, Bool),
Instr::NumAdd => impl_num_binop!(+),
Instr::NumSub => impl_num_binop!(-),
Instr::NumMul => impl_num_binop!(*),
Instr::NumDiv => impl_num_binop!(/),
Instr::NumMod => impl_num_binop!(%),
Instr::BoolPush(x) => {
self.push(Value::Bool(*x))?;

View file

@ -103,8 +103,7 @@ pub enum Instr {
NumSub, // │ 1 byte
NumMul, // │
NumDiv, // │
NumMod, // │
NumEq, // ┘
NumMod, // ┘
BoolPush(bool), // 2 bytes: 1 byte for the enum, 1 byte for the bool
BoolAnd, // ┐ 1 byte
@ -172,12 +171,7 @@ impl Instr {
pub fn size(&self) -> usize {
match self {
Instr::NumPush(_) => 1 + std::mem::size_of::<i64>(),
Instr::NumAdd
| Instr::NumSub
| Instr::NumMul
| Instr::NumDiv
| Instr::NumMod
| Instr::NumEq => 1,
Instr::NumAdd | Instr::NumSub | Instr::NumMul | Instr::NumDiv | Instr::NumMod => 1,
Instr::BoolPush(_) => 1 + std::mem::size_of::<bool>(),
Instr::BoolAnd | Instr::BoolOr | Instr::BoolNot => 1,
@ -233,7 +227,6 @@ impl Instr {
Instr::NumMul => bytes.push(index!()),
Instr::NumDiv => bytes.push(index!()),
Instr::NumMod => bytes.push(index!()),
Instr::NumEq => bytes.push(index!()),
Instr::BoolPush(b) => {
bytes.push(index!());