Compare commits

...

2 Commits

Author SHA1 Message Date
Scott Richmond
b8b720b877 add do forms 2025-06-05 12:15:49 -04:00
Scott Richmond
23b8beb291 fix extra pop regression 2025-06-05 12:04:02 -04:00
2 changed files with 31 additions and 13 deletions

View File

@ -316,10 +316,14 @@ pub struct Compiler<'a> {
loop_info: Vec<LoopInfo>, loop_info: Vec<LoopInfo>,
} }
fn is_let(expr: &Spanned<Ast>) -> bool { fn is_binding(expr: &Spanned<Ast>) -> bool {
let (ast, _) = expr; let (ast, _) = expr;
use Ast::*; use Ast::*;
matches!(ast, Let(..)) match ast {
Let(..) | LBox(..) => true,
Fn(name, ..) => !name.is_empty(),
_ => false,
}
} }
impl<'a> Compiler<'a> { impl<'a> Compiler<'a> {
@ -463,6 +467,7 @@ impl<'a> Compiler<'a> {
} }
fn pop(&mut self) { fn pop(&mut self) {
println!("Popping from: {}", self.ast);
self.emit_op(Op::Pop); self.emit_op(Op::Pop);
self.stack_depth -= 1; self.stack_depth -= 1;
} }
@ -515,7 +520,7 @@ impl<'a> Compiler<'a> {
self.scope_depth += 1; self.scope_depth += 1;
let stack_depth = self.stack_depth; let stack_depth = self.stack_depth;
for expr in lines.iter().take(lines.len() - 1) { for expr in lines.iter().take(lines.len() - 1) {
if is_let(expr) { if is_binding(expr) {
self.visit(expr); self.visit(expr);
} else { } else {
self.visit(expr); self.visit(expr);
@ -846,8 +851,8 @@ impl<'a> Compiler<'a> {
self.emit_op(Op::PushBinding); self.emit_op(Op::PushBinding);
self.stack_depth += 1; self.stack_depth += 1;
let biter = self.bindings.iter().rev(); // let biter = self.bindings.iter().rev();
for binding in biter { for binding in self.bindings.iter() {
if binding.name == *fn_name { if binding.name == *fn_name {
self.emit_byte(binding.stack_pos); self.emit_byte(binding.stack_pos);
break; break;
@ -1256,12 +1261,21 @@ impl<'a> Compiler<'a> {
} }
} }
} }
Arguments(..) Do(terms) => {
| Placeholder let mut terms = terms.iter();
| Do(..) let first = terms.next().unwrap();
| Splat(..) // put the first value on the stack
| InterpolatedPattern(..) self.visit(first);
| Splattern(..) => todo!(), for term in terms {
self.visit(term);
self.emit_op(Op::Call);
self.emit_byte(1);
self.stack_depth -= 1;
}
}
Arguments(..) | Placeholder | Splat(..) | InterpolatedPattern(..) | Splattern(..) => {
todo!()
}
And | Or => unreachable!(), And | Or => unreachable!(),
} }
} }

View File

@ -76,9 +76,13 @@ pub fn run(src: &'static str) {
pub fn main() { pub fn main() {
env::set_var("RUST_BACKTRACE", "1"); env::set_var("RUST_BACKTRACE", "1");
let src = " let src = "
fn foo () -> :foo fn increase (x) -> add (x, 1)
foo fn double (x) -> mult (x, 2)
fn halve (x) -> div (x, 2)
do 1 > increase > double > increase > halve
"; ";