use crate::parser::Ast; use crate::parser::StringPart; use crate::spans::Spanned; use crate::value::*; use chumsky::prelude::SimpleSpan; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::FromPrimitive; use regex::Regex; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; #[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, ToPrimitive)] pub enum Op { Noop, Nothing, Nil, True, False, Constant, Jump, JumpIfFalse, JumpIfTrue, Pop, PopN, PushBinding, Store, StoreAt, Stash, Load, ResetMatch, Match, MatchNil, MatchTrue, MatchFalse, PanicIfNoMatch, MatchConstant, MatchString, PushStringMatches, MatchType, MatchTuple, MatchSplattedTuple, PushTuple, LoadTuple, LoadSplattedTuple, MatchList, MatchSplattedList, LoadList, LoadSplattedList, PushList, AppendList, ConcatList, PushDict, AppendDict, ConcatDict, LoadDictValue, MatchDict, MatchSplattedDict, DropDictEntry, PushBox, GetKey, PanicNoWhen, JumpIfNoMatch, JumpIfMatch, PanicNoMatch, TypeOf, JumpBack, JumpIfZero, Duplicate, Decrement, Truncate, MatchDepth, Panic, EmptyString, ConcatStrings, Stringify, Call, TailCall, Return, Partial, Eq, Add, Sub, Mult, Div, Unbox, BoxStore, Assert, Get, At, Not, Print, SetUpvalue, GetUpvalue, // Inc, // Dec, // Gt, // Gte, // Lt, // Lte, // Mod, // Round, // Ceil, // Floor, // Random, // Sqrt, // Assoc, // Concat, // Conj, // Count, // Disj, // Dissoc, // Range, // Rest, // Slice, // "atan_2" math/atan2 // "chars" chars // "cos" math/cos // "doc" doc // "downcase" string/ascii-lower // "pi" math/pi // "show" show // "sin" math/sin // "split" string/split // "str_slice" string/slice // "tan" math/tan // "trim" string/trim // "triml" string/triml // "trimr" string/trimr // "upcase" string/ascii-upper } impl std::fmt::Display for Op { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { use Op::*; let rep = match self { Noop => "noop", Nothing => "nothing", Nil => "nil", True => "true", False => "false", Constant => "constant", Jump => "jump", JumpIfFalse => "jump_if_false", JumpIfTrue => "jump_if_true", Pop => "pop", PopN => "pop_n", PushBinding => "push_binding", Store => "store", StoreAt => "store_at", Stash => "stash", Load => "load", Match => "match", MatchNil => "match_nil", MatchTrue => "match_true", MatchFalse => "match_false", ResetMatch => "reset_match", PanicIfNoMatch => "panic_if_no_match", MatchConstant => "match_constant", MatchString => "match_string", PushStringMatches => "push_string_matches", MatchType => "match_type", MatchTuple => "match_tuple", MatchSplattedTuple => "match_splatted_tuple", PushTuple => "push_tuple", LoadTuple => "load_tuple", LoadSplattedTuple => "load_splatted_tuple", MatchList => "match_list", MatchSplattedList => "match_splatted_list", LoadList => "load_list", LoadSplattedList => "load_splatted_list", PushList => "push_list", AppendList => "append_list", ConcatList => "concat_list", PushDict => "push_dict", AppendDict => "append_dict", ConcatDict => "concat_dict", LoadDictValue => "load_dict_value", MatchDict => "match_dict", MatchSplattedDict => "match_splatted_dict", DropDictEntry => "drop_dict_entry", PushBox => "push_box", GetKey => "get_key", PanicNoWhen => "panic_no_when", JumpIfNoMatch => "jump_if_no_match", JumpIfMatch => "jump_if_match", PanicNoMatch => "panic_no_match", TypeOf => "type_of", JumpBack => "jump_back", JumpIfZero => "jump_if_zero", Decrement => "decrement", Truncate => "truncate", Duplicate => "duplicate", MatchDepth => "match_depth", Panic => "panic", EmptyString => "empty_string", ConcatStrings => "concat_strings", Stringify => "stringify", Print => "print", Eq => "eq", Add => "add", Sub => "sub", Mult => "mult", Div => "div", Unbox => "unbox", BoxStore => "box_store", Assert => "assert", Get => "get", At => "at", Not => "not", Call => "call", Return => "return", Partial => "partial", TailCall => "tail_call", SetUpvalue => "set_upvalue", GetUpvalue => "get_upvalue", }; write!(f, "{rep}") } } #[derive(Clone, Debug, PartialEq)] pub struct Binding { name: &'static str, depth: isize, stack_pos: usize, } #[derive(Clone, Debug, PartialEq)] pub struct Upvalue { name: &'static str, stack_pos: usize, } #[derive(Clone, Debug)] pub struct StrPattern { pub words: Vec, pub re: Regex, } #[derive(Clone, Debug)] pub struct Chunk { pub constants: Vec, pub bytecode: Vec, pub keywords: Vec<&'static str>, pub string_patterns: Vec, } impl Chunk { pub fn dissasemble_instr(&self, i: &mut usize) { let op = Op::from_u8(self.bytecode[*i]).unwrap(); use Op::*; match op { Pop | Store | Stash | Load | Nil | True | False | MatchNil | MatchTrue | MatchFalse | PanicIfNoMatch | ResetMatch | GetKey | PanicNoWhen | PanicNoMatch | TypeOf | Duplicate | Decrement | Truncate | Noop | LoadTuple | LoadList | Eq | Add | Sub | Mult | Div | Unbox | BoxStore | Assert | Get | At | Not | Panic | EmptyString | ConcatStrings | Stringify | MatchType | Return | Match | Print | AppendList | ConcatList | PushList | PushDict | AppendDict | ConcatDict | Nothing => { println!("{i:04}: {op}") } Constant | MatchConstant => { let next = self.bytecode[*i + 1]; let value = &self.constants[next as usize].show(); println!("{i:04}: {:16} {next:03}: {value}", op.to_string()); *i += 1; } PushBinding | MatchTuple | MatchSplattedTuple | LoadSplattedTuple | MatchList | MatchSplattedList | LoadSplattedList | MatchDict | MatchSplattedDict | DropDictEntry | LoadDictValue | PushTuple | PushBox | MatchDepth | PopN | StoreAt | Call | SetUpvalue | GetUpvalue | Partial | MatchString | PushStringMatches | TailCall => { let next = self.bytecode[*i + 1]; println!("{i:04}: {:16} {next:03}", op.to_string()); *i += 1; } Jump | JumpIfFalse | JumpIfTrue | JumpIfNoMatch | JumpIfMatch | JumpBack | JumpIfZero => { let high = self.bytecode[*i + 1]; let low = self.bytecode[*i + 2]; let len = ((high as u16) << 8) + low as u16; println!("{i:04}: {:16} {len:05}", op.to_string()); *i += 2; } } } pub fn dissasemble(&self) { println!("IDX | CODE | INFO"); let mut i = 0; while i < self.bytecode.len() { self.dissasemble_instr(&mut i); i += 1; } } // pub fn kw_from(&self, kw: &str) -> Option { // self.kw_index_from(kw).map(Value::Keyword) // } // pub fn kw_index_from(&self, kw: &str) -> Option { // self.keywords.iter().position(|s| *s == kw) // } } #[derive(Debug, Clone, PartialEq)] struct LoopInfo { start: usize, stack_root: usize, } impl LoopInfo { fn new(start: usize, stack_root: usize) -> LoopInfo { LoopInfo { start, stack_root } } } fn get_builtin(name: &str, arity: usize) -> Option { match (name, arity) { ("type", 1) => Some(Op::TypeOf), ("eq?", 2) => Some(Op::Eq), ("add", 2) => Some(Op::Add), ("sub", 2) => Some(Op::Sub), ("mult", 2) => Some(Op::Mult), ("div", 2) => Some(Op::Div), ("unbox", 1) => Some(Op::Unbox), ("store!", 2) => Some(Op::BoxStore), ("assert!", 1) => Some(Op::Assert), ("get", 2) => Some(Op::Get), ("at", 2) => Some(Op::At), ("not", 1) => Some(Op::Not), ("print!", 1) => Some(Op::Print), _ => None, } } #[derive(Debug, Clone)] pub struct Compiler<'a> { pub chunk: Chunk, pub bindings: Vec, pub scope_depth: isize, pub match_depth: usize, pub stack_depth: usize, pub spans: Vec, pub nodes: Vec<&'static Ast>, pub ast: &'static Ast, pub span: SimpleSpan, pub src: &'static str, pub name: &'static str, pub enclosing: Option<&'a Compiler<'a>>, pub upvalues: Vec, loop_info: Vec, tail_pos: bool, } fn is_binding(expr: &Spanned) -> bool { let (ast, _) = expr; use Ast::*; match ast { Let(..) | LBox(..) => true, Fn(name, ..) => !name.is_empty(), _ => false, } } fn has_placeholder(args: &[Spanned]) -> bool { args.iter().any(|arg| matches!(arg, (Ast::Placeholder, _))) } impl<'a> Compiler<'a> { pub fn new( ast: &'static Spanned, name: &'static str, src: &'static str, enclosing: Option<&'a Compiler>, ) -> Compiler<'a> { let chunk = Chunk { constants: vec![], bytecode: vec![], keywords: vec![], string_patterns: vec![], }; Compiler { chunk, bindings: vec![], scope_depth: -1, match_depth: 0, stack_depth: 0, spans: vec![], nodes: vec![], ast: &ast.0, span: ast.1, loop_info: vec![], enclosing, upvalues: vec![], src, name, tail_pos: false, } } pub fn visit(&mut self, node: &'static Spanned) { let root_node = self.ast; let root_span = self.span; let (ast, span) = node; self.ast = ast; self.span = *span; self.compile(); self.ast = root_node; self.span = root_span; } fn jump(&mut self, op: Op, len: usize) { let low = len as u8; let high = (len >> 8) as u8; self.emit_op(op); self.chunk.bytecode.push(high); self.chunk.bytecode.push(low); } fn stub_jump(&mut self, op: Op) -> usize { let out = self.chunk.bytecode.len(); self.emit_op(op); self.emit_byte(0xff); self.emit_byte(0xff); out } fn patch_jump(&mut self, i: usize, len: usize) { let low = len as u8; let high = (len >> 8) as u8; self.chunk.bytecode[i + 1] = high; self.chunk.bytecode[i + 2] = low; } fn emit_constant(&mut self, val: Value) { let const_idx = if let Some(idx) = self.chunk.constants.iter().position(|v| *v == val) { idx } else { self.chunk.constants.push(val); self.chunk.constants.len() - 1 }; if const_idx > u8::MAX as usize { panic!( "internal Ludus compiler error: too many constants in chunk:{}:: {}", self.span, self.ast ) } self.emit_op(Op::Constant); self.emit_byte(const_idx); self.stack_depth += 1; } fn match_constant(&mut self, val: Value) { let constant_index = match self.chunk.constants.iter().position(|v| *v == val) { Some(idx) => idx, None => { self.chunk.constants.push(val); self.chunk.constants.len() - 1 } }; if constant_index > u8::MAX as usize { panic!( "internal Ludus compiler error: too many constants in chunk:{}:: {}", self.span, self.ast ) } self.emit_op(Op::MatchConstant); self.emit_byte(constant_index); } fn emit_op(&mut self, op: Op) { self.chunk.bytecode.push(op as u8); self.spans.push(self.span); } fn emit_byte(&mut self, byte: usize) { self.chunk.bytecode.push(byte as u8); self.spans.push(self.span); } fn len(&self) -> usize { self.chunk.bytecode.len() } fn bind(&mut self, name: &'static str) { let binding = Binding { name, depth: self.scope_depth, stack_pos: self.stack_depth - self.match_depth - 1, }; println!("{:?}", binding); println!("stack: {}; match: {}", self.stack_depth, self.match_depth); self.bindings.push(binding); } fn resolve_local(&self, name: &'static str) -> Option { for binding in self.bindings.iter() { if binding.name == name { return Some(binding.stack_pos); } } None } fn resolve_upvalue(&self, name: &'static str) -> Option { self.upvalues.iter().position(|uv| uv.name == name) } fn get_upvalue(&self, name: &'static str) -> Upvalue { let local = self.bindings.iter().find(|b| b.name == name); match local { Some(binding) => Upvalue { name, stack_pos: binding.stack_pos, }, None => self.enclosing.unwrap().get_upvalue(name), } } fn resolve_binding(&mut self, name: &'static str) { match self.resolve_local(name) { Some(position) => { self.emit_op(Op::PushBinding); self.emit_byte(position); self.stack_depth += 1; } None => match self.resolve_upvalue(name) { Some(position) => { println!("resolved upvalue: {name} at {position}"); self.emit_op(Op::GetUpvalue); self.emit_byte(position); self.stack_depth += 1; } None => { println!("setting upvalue: {name}"); let upvalue = self.get_upvalue(name); self.emit_op(Op::GetUpvalue); self.emit_byte(self.upvalues.len()); self.upvalues.push(upvalue); dbg!(&self.upvalues); self.stack_depth += 1; } }, } } fn pop(&mut self) { self.emit_op(Op::Pop); self.stack_depth -= 1; } fn pop_n(&mut self, n: usize) { match n { 0 => (), 1 => self.pop(), n => { self.emit_op(Op::PopN); self.emit_byte(n); self.stack_depth -= n; } } } fn enter_loop(&mut self) { self.loop_info .push(LoopInfo::new(self.len(), self.stack_depth)); } fn leave_loop(&mut self) { self.loop_info.pop(); } fn loop_info(&self) -> LoopInfo { self.loop_info.last().unwrap().clone() } fn loop_idx(&self) -> usize { self.loop_info.last().unwrap().start } fn loop_root(&self) -> usize { self.loop_info.last().unwrap().stack_root } pub fn compile(&mut self) { use Ast::*; match self.ast { Error => unreachable!(), Nil => { self.emit_op(Op::Nil); self.stack_depth += 1; } Number(n) => self.emit_constant(Value::Number(*n)), Boolean(b) => { self.emit_op(if *b { Op::True } else { Op::False }); self.stack_depth += 1; } String(s) => { self.emit_constant(Value::Interned(s)); } Keyword(s) => self.emit_constant(Value::Keyword(s)), Block(lines) => { let tail_pos = self.tail_pos; self.tail_pos = false; // increase the scope self.scope_depth += 1; // stash the stack depth let stack_depth = self.stack_depth; // evaluate all the lines but the last for expr in lines.iter().take(lines.len() - 1) { // evaluate the expression self.visit(expr); // if it doesn't bind a name, pop the result from the stack if !is_binding(expr) { self.pop(); } } // now, evaluate the last expression in the block let last_expr = lines.last().unwrap(); match last_expr { // if the last expression is a let form, // return the evaluated rhs instead of whatever is last on the stack // we do this by pretending it's a binding (Let(patt, expr), _) => { self.match_depth = 0; self.emit_op(Op::ResetMatch); self.visit(expr); let expr_pos = self.stack_depth - 1; self.visit(patt); self.emit_op(Op::PanicIfNoMatch); self.emit_op(Op::PushBinding); self.emit_byte(expr_pos); self.stack_depth += 1; } // otherwise, just evaluate it and leave the value on the stack _ => { self.tail_pos = tail_pos; self.visit(last_expr); } } // we've made a new value, so increase the stack level in the compiler self.stack_depth += 1; // store the value in the return register self.emit_op(Op::Store); // reset the scope self.scope_depth -= 1; while let Some(binding) = self.bindings.last() { if binding.depth > self.scope_depth { self.bindings.pop(); } else { break; } } // reset the stack self.pop_n(self.stack_depth - stack_depth - 1); // load the value from the return register self.emit_op(Op::Load); } If(cond, then, r#else) => { let tail_pos = self.tail_pos; self.tail_pos = false; self.visit(cond); let jif_idx = self.stub_jump(Op::JumpIfFalse); self.stack_depth -= 1; self.tail_pos = tail_pos; self.visit(then); let jump_idx = self.stub_jump(Op::Jump); self.visit(r#else); self.stack_depth -= 1; let end_idx = self.len(); let jif_offset = jump_idx - jif_idx; let jump_offset = end_idx - jump_idx - 1; self.patch_jump(jif_idx, jif_offset); self.patch_jump(jump_idx, jump_offset); } Let(patt, expr) => { self.match_depth = 0; self.emit_op(Op::ResetMatch); self.visit(expr); self.visit(patt); self.emit_op(Op::PanicIfNoMatch); } WordPattern(name) => { self.emit_op(Op::Match); self.bind(name); } Word(name) | Splat(name) => self.resolve_binding(name), PlaceholderPattern => { self.emit_op(Op::Match); } NilPattern => { self.emit_op(Op::MatchNil); } BooleanPattern(b) => { if *b { self.emit_op(Op::MatchTrue); } else { self.emit_op(Op::MatchFalse); } } NumberPattern(n) => { self.match_constant(Value::Number(*n)); } KeywordPattern(s) => { let existing_kw = self.chunk.keywords.iter().position(|kw| kw == s); let kw_index = match existing_kw { Some(index) => index, None => self.chunk.keywords.len(), }; if kw_index == self.chunk.keywords.len() { self.chunk.keywords.push(s); } self.match_constant(Value::Keyword(s)); } AsPattern(word, typ) => { self.emit_constant(Value::Keyword(typ)); self.emit_op(Op::MatchType); self.stack_depth -= 1; self.bind(word); } StringPattern(s) => { self.match_constant(Value::Interned(s)); } TuplePattern(members) => { // first, test the tuple against length // check if we're splatted // different opcodes w/ splats, but same logic let mut is_splatted = false; if let Some((Splattern(_), _)) = members.last() { is_splatted = true; self.emit_op(Op::MatchSplattedTuple); } else { self.emit_op(Op::MatchTuple); } self.emit_byte(members.len()); // skip everything if tuple lengths don't match let before_load_tup_idx = self.stub_jump(Op::JumpIfNoMatch); // set up the per-member conditional logic let mut jump_idxes = vec![]; // stash match_depth, and set it to the tuple len let match_depth = self.match_depth; self.match_depth = members.len(); // load the tuple and update the stack len if is_splatted { self.emit_op(Op::LoadSplattedTuple); self.emit_byte(members.len()); } else { self.emit_op(Op::LoadTuple); } self.stack_depth += members.len(); // visit each member for member in members { // reduce the match depth to start self.match_depth -= 1; self.emit_op(Op::MatchDepth); self.emit_byte(self.match_depth); // visit the pattern member self.visit(member); // and jump if there's no match jump_idxes.push(self.stub_jump(Op::JumpIfNoMatch)); } // if we get here--not having jumped on no match--we're matched; jump the "no match" code let jump_idx = self.stub_jump(Op::Jump); // patch up the previous no match jumps to jump to clean-up code for idx in jump_idxes { self.patch_jump(idx, self.len() - idx - 2) } // pop everything that was pushed // don't change the compiler stack representation, tho // we need this as cleanup code with no matches // the compiler should still have access to the bindings in this pattern self.emit_op(Op::PopN); self.emit_byte(members.len()); // patch up the tuple length match jump self.patch_jump(before_load_tup_idx, self.len() - before_load_tup_idx - 3); // patch up the yes-matches unconditional jump self.patch_jump(jump_idx, self.len() - jump_idx - 3); // finally, for any further matches (e.g. nested lists/tuples) // add increase the match depth, since we've added a bunch // of bindings to the stack self.match_depth = match_depth + members.len(); } ListPattern(members) => { let mut is_splatted = false; if let Some((Splattern(_), _)) = members.last() { is_splatted = true; self.emit_op(Op::MatchSplattedList) } else { self.emit_op(Op::MatchList); } // TODO: lists must be able to be longer than 256 elements; fix this self.emit_byte(members.len()); let before_load_tup_idx = self.stub_jump(Op::JumpIfNoMatch); let mut jump_idxes = vec![]; let match_depth = self.match_depth; self.match_depth = members.len(); if is_splatted { self.emit_op(Op::LoadSplattedList); self.emit_byte(members.len()); } else { self.emit_op(Op::LoadList); } self.stack_depth += members.len(); for member in members { self.match_depth -= 1; self.emit_op(Op::MatchDepth); self.emit_byte(self.match_depth); self.visit(member); jump_idxes.push(self.stub_jump(Op::JumpIfNoMatch)); } let jump_idx = self.stub_jump(Op::Jump); for idx in jump_idxes { self.patch_jump(idx, self.len() - idx - 2) } self.emit_op(Op::PopN); self.emit_byte(members.len()); self.patch_jump(before_load_tup_idx, self.len() - before_load_tup_idx - 3); self.patch_jump(jump_idx, self.len() - jump_idx - 3); self.match_depth = match_depth + members.len(); } DictPattern(pairs) => { // here's an algorithm for dealing with splatted dicts // check len to see it's at least as long as the pattern // then, match against all the values // then push the dict itself as last value // and then emit an opcode and constant/keyword to OMIT that key from the dict let mut is_splatted = false; if let Some((Splattern(_), _)) = pairs.last() { is_splatted = true; self.emit_op(Op::MatchSplattedDict); } else { self.emit_op(Op::MatchDict); } self.emit_byte(pairs.len()); let before_load_dict_idx = self.stub_jump(Op::JumpIfNoMatch); let mut jump_idxes = vec![]; let dict_stack_pos = self.stack_depth - self.match_depth - 1; let mut splattern = None; let mut pairs_len = pairs.len(); if is_splatted { splattern = pairs.last(); pairs_len -= 1; } let match_depth = self.match_depth; self.match_depth = 0; for pair in pairs.iter().take(pairs_len) { let (PairPattern(key, pattern), _) = pair else { unreachable!() }; self.emit_constant(Value::Keyword(key)); self.emit_op(Op::LoadDictValue); self.emit_byte(dict_stack_pos); self.visit(pattern); jump_idxes.push(self.stub_jump(Op::JumpIfNoMatch)); } if is_splatted { // pull the dict out of the stack // drop every value in the pattern self.emit_op(Op::PushBinding); self.emit_byte(dict_stack_pos); for pair in pairs.iter().take(pairs_len) { let (PairPattern(key, _), _) = pair else { unreachable!() }; self.emit_constant(Value::Keyword(key)); self.emit_op(Op::DropDictEntry); } if let Some(splatt) = splattern { self.visit(splatt); } } self.match_depth = match_depth + pairs.len(); let jump_idx = self.stub_jump(Op::Jump); for idx in jump_idxes { self.patch_jump(idx, self.len() - idx - 2); } self.emit_op(Op::PopN); self.emit_byte(pairs.len()); self.patch_jump(before_load_dict_idx, self.len() - before_load_dict_idx - 3); self.patch_jump(jump_idx, self.len() - jump_idx - 3); } Splattern(patt) => self.visit(patt), InterpolatedPattern(parts, _) => { println!("An interpolated pattern of {} parts", parts.len()); let mut pattern = "".to_string(); let mut words = vec![]; for (part, _) in parts { match part { StringPart::Word(word) => { println!("wordpart: {word}"); words.push(word.clone()); pattern.push_str("(.*)"); } StringPart::Data(data) => { println!("datapart: {data}"); let data = regex::escape(data); pattern.push_str(data.as_str()); } StringPart::Inline(..) => unreachable!(), } } let re = Regex::new(pattern.as_str()).unwrap(); let moar_words = words.clone(); let string_pattern = StrPattern { words, re }; let pattern_idx = self.chunk.string_patterns.len(); self.chunk.string_patterns.push(string_pattern); self.emit_op(Op::MatchString); self.emit_byte(pattern_idx); let jnm_idx = self.stub_jump(Op::JumpIfNoMatch); self.emit_op(Op::PushStringMatches); self.emit_byte(pattern_idx); for word in moar_words { let name: &'static str = std::string::String::leak(word); let binding = Binding { name, depth: self.scope_depth, stack_pos: self.stack_depth, }; self.bindings.push(binding); self.stack_depth += 1; } self.patch_jump(jnm_idx, self.len() - jnm_idx - 3); } PairPattern(_, _) => unreachable!(), Tuple(members) => { for member in members { self.visit(member); } self.emit_op(Op::PushTuple); self.emit_byte(members.len()); self.stack_depth = self.stack_depth + 1 - members.len(); } List(members) => { self.emit_op(Op::PushList); self.stack_depth += 1; for member in members { self.visit(member); if matches!(member, (Splat(..), _)) { self.emit_op(Op::ConcatList); } else { self.emit_op(Op::AppendList); } self.stack_depth -= 1; } } LBox(name, expr) => { self.visit(expr); self.emit_op(Op::PushBox); self.bind(name); } Dict(pairs) => { self.emit_op(Op::PushDict); self.stack_depth += 1; for pair in pairs.iter().rev() { self.visit(pair); if matches!(pair, (Splat(..), _)) { self.emit_op(Op::ConcatDict); self.stack_depth -= 1; } else { self.emit_op(Op::AppendDict); self.stack_depth -= 2; } } } Pair(key, value) => { self.emit_constant(Value::Keyword(key)); self.visit(value); } // TODO: thread tail position through this Synthetic(first, second, rest) => { let tail_pos = self.tail_pos; self.tail_pos = false; match (&first.0, &second.0) { (Word(_), Keyword(_)) => { self.visit(first); self.visit(second); self.emit_op(Op::GetKey); self.stack_depth -= 1; } (Keyword(_), Arguments(args)) => { self.visit(&args[0]); self.visit(first); self.emit_op(Op::GetKey); self.stack_depth -= 1; } (Or, Arguments(args)) => { let stack_depth = self.stack_depth; let mut jump_idxes = vec![]; if !args.is_empty() { for arg in args { self.visit(arg); self.emit_op(Op::Stash); jump_idxes.push(self.stub_jump(Op::JumpIfTrue)); } for idx in jump_idxes { self.patch_jump(idx, self.len() - idx); } self.emit_op(Op::Load); } else { self.emit_op(Op::False); } self.stack_depth = stack_depth + 1; } (And, Arguments(args)) => { let stack_depth = self.stack_depth; let mut jump_idxes = vec![]; if !args.is_empty() { for arg in args { self.visit(arg); self.emit_op(Op::Stash); jump_idxes.push(self.stub_jump(Op::JumpIfFalse)); } for idx in jump_idxes { self.patch_jump(idx, self.len() - idx); } self.emit_op(Op::Load); } else { self.emit_op(Op::True); } self.stack_depth = stack_depth + 1; } (Word(fn_name), Arguments(args)) => { if has_placeholder(args) { let arity = args.len(); for arg in args { self.visit(arg); } self.resolve_binding(fn_name); self.emit_op(Op::Partial); self.emit_byte(arity); self.stack_depth -= 1; } else { match get_builtin(fn_name, args.len()) { Some(code) => { for arg in args { self.visit(arg); } self.emit_op(code); self.stack_depth -= args.len() - 1; } None => { let arity = args.len(); for arg in args { self.visit(arg); } self.resolve_binding(fn_name); // if we're in tail position AND there aren't any rest args, this should be a tail call (I think) if rest.is_empty() && tail_pos { self.emit_op(Op::TailCall); } else { self.emit_op(Op::Call); } self.emit_byte(arity); self.stack_depth -= arity; } } } } _ => unreachable!(), } // the last term in rest should be in tail position if we are in tail position let num_rest_terms = rest.len(); for (i, (term, _)) in rest.iter().enumerate() { match term { Keyword(str) => { self.emit_constant(Value::Keyword(str)); self.emit_op(Op::GetKey); self.stack_depth -= 1; } Arguments(args) => { self.emit_op(Op::Stash); self.pop(); let arity = args.len(); for arg in args { self.visit(arg); } self.emit_op(Op::Load); if tail_pos && i == num_rest_terms - 1 { self.emit_op(Op::TailCall) } else { self.emit_op(Op::Call); } self.emit_byte(arity); self.stack_depth -= arity; } _ => unreachable!(), } } self.tail_pos = tail_pos; } When(clauses) => { let tail_pos = self.tail_pos; let mut jump_idxes = vec![]; let mut clauses = clauses.iter(); while let Some((WhenClause(cond, body), _)) = clauses.next() { self.tail_pos = false; self.visit(cond.as_ref()); let jif_jump_idx = self.stub_jump(Op::JumpIfFalse); self.stack_depth -= 1; self.tail_pos = tail_pos; self.visit(body); self.stack_depth -= 1; jump_idxes.push(self.stub_jump(Op::Jump)); self.patch_jump(jif_jump_idx, self.len() - jif_jump_idx - 3); } self.emit_op(Op::PanicNoWhen); for idx in jump_idxes { self.patch_jump(idx, self.len() - idx - 3); } self.stack_depth += 1; } WhenClause(..) => unreachable!(), Match(scrutinee, clauses) => { let tail_pos = self.tail_pos; self.tail_pos = false; self.visit(scrutinee.as_ref()); let stack_depth = self.stack_depth; let mut jump_idxes = vec![]; let mut clauses = clauses.iter(); while let Some((MatchClause(pattern, guard, body), _)) = clauses.next() { self.tail_pos = false; let mut no_match_jumps = vec![]; self.scope_depth += 1; self.match_depth = 0; self.visit(pattern); no_match_jumps.push(self.stub_jump(Op::JumpIfNoMatch)); if guard.is_some() { let guard_expr: &'static Spanned = Box::leak(Box::new(guard.clone().unwrap())); self.visit(guard_expr); no_match_jumps.push(self.stub_jump(Op::JumpIfFalse)); self.stack_depth -= 1; } self.tail_pos = tail_pos; self.visit(body); self.emit_op(Op::Store); self.scope_depth -= 1; while let Some(binding) = self.bindings.last() { if binding.depth > self.scope_depth { self.bindings.pop(); } else { break; } } self.pop_n(self.stack_depth - stack_depth); jump_idxes.push(self.stub_jump(Op::Jump)); for idx in no_match_jumps { self.patch_jump(idx, self.len() - idx - 3); } } self.emit_op(Op::PanicNoMatch); for idx in jump_idxes { self.patch_jump(idx, self.len() - idx - 3); } self.pop_n(self.stack_depth - stack_depth); self.emit_op(Op::Load); self.stack_depth += 1; } MatchClause(..) => unreachable!(), Fn(name, body, doc) => { let name = if name.is_empty() { "anonymous" } else { name }; let FnBody(fn_body) = &body.as_ref().0 else { unreachable!() }; let mut compilers: HashMap = HashMap::new(); let mut upvalues = vec![]; let mut has_splat = false; for clause in fn_body { let MatchClause(pattern, guard, clause_body) = &clause.0 else { unreachable!() }; let TuplePattern(pattern) = &pattern.0 else { unreachable!() }; if matches!(pattern.last(), Some((Splattern(_), _))) { has_splat = true; }; let arity = pattern.len() as u8; let compiler = match compilers.get_mut(&arity) { Some(compiler) => compiler, None => { let mut compiler = Compiler::new(clause, self.name, self.src, Some(self)); compiler.emit_op(Op::ResetMatch); compilers.insert(arity, compiler); compilers.get_mut(&arity).unwrap() } }; compiler.tail_pos = false; compiler.stack_depth += arity as usize; compiler.scope_depth += 1; compiler.match_depth = arity as usize; std::mem::swap(&mut upvalues, &mut compiler.upvalues); let mut tup_jump_idxes = vec![]; for member in pattern { compiler.match_depth -= 1; compiler.emit_op(Op::MatchDepth); compiler.emit_byte(compiler.match_depth); compiler.visit(member); tup_jump_idxes.push(compiler.stub_jump(Op::JumpIfNoMatch)); } if pattern.is_empty() { compiler.emit_op(Op::Match); } let jump_idx = compiler.stub_jump(Op::Jump); for idx in tup_jump_idxes { compiler.patch_jump(idx, compiler.len() - idx - 3); } compiler.emit_op(Op::PopN); compiler.emit_byte(arity as usize); compiler.patch_jump(jump_idx, compiler.len() - jump_idx - 3); let mut no_match_jumps = vec![]; no_match_jumps.push(compiler.stub_jump(Op::JumpIfNoMatch)); if guard.is_some() { let guard_expr: &'static Spanned = Box::leak(Box::new(guard.clone().unwrap())); compiler.visit(guard_expr); no_match_jumps.push(compiler.stub_jump(Op::JumpIfFalse)); compiler.stack_depth -= 1; } compiler.tail_pos = true; compiler.visit(clause_body); compiler.emit_op(Op::Store); compiler.scope_depth -= 1; while let Some(binding) = compiler.bindings.last() { if binding.depth > compiler.scope_depth { compiler.bindings.pop(); } else { break; } } compiler.pop_n(compiler.stack_depth); compiler.stack_depth = 0; compiler.emit_op(Op::Return); for idx in no_match_jumps { compiler.patch_jump(idx, compiler.len() - idx - 3); } compiler.scope_depth -= 1; std::mem::swap(&mut compiler.upvalues, &mut upvalues); } let mut compilers = compilers.into_iter().collect::>(); compilers.sort_by(|(a, _), (b, _)| a.cmp(b)); let mut arities = vec![]; let mut chunks = vec![]; for (arity, mut compiler) in compilers { compiler.emit_op(Op::PanicNoMatch); let chunk = compiler.chunk; if crate::DEBUG_COMPILE { println!("=== function chuncktion: {name}/{arity} ==="); chunk.dissasemble(); } arities.push(arity); chunks.push(chunk); } let splat = if has_splat { arities.iter().fold(0, |max, curr| max.max(*curr)) } else { 0 }; let lfn = crate::value::LFn::Defined { name, doc: *doc, arities, chunks, splat, closed: RefCell::new(vec![]), }; // TODO: check if the function is already declared, and pull out the relevant OnceCell if need be let init_val = Value::Fn(Rc::new(lfn)); self.emit_constant(init_val); self.bind(name); for upvalue in upvalues { self.emit_op(Op::SetUpvalue); self.emit_byte(upvalue.stack_pos); } } FnDeclaration(name) => { let lfn = Value::Fn(Rc::new(LFn::Declared { name })); self.emit_constant(lfn); self.bind(name); } FnBody(_) => unreachable!(), Repeat(times, body) => { self.visit(times); self.emit_op(Op::Truncate); // skip the decrement the first time self.emit_op(Op::Jump); self.emit_byte(0); self.emit_byte(1); // begin repeat self.emit_op(Op::Decrement); let repeat_begin = self.len(); self.emit_op(Op::Duplicate); let jiz_idx = self.stub_jump(Op::JumpIfZero); // compile the body self.visit(body); // pop whatever value the body returns self.pop(); let jump_back = self.stub_jump(Op::JumpBack); // set jump points self.patch_jump(jump_back, self.len() - repeat_begin - 2); self.patch_jump(jiz_idx, self.len() - repeat_begin - 4); self.pop(); self.emit_constant(Value::Nil); } Loop(value, clauses) => { let tail_pos = self.tail_pos; self.tail_pos = false; //algo: //first, put the values on the stack let (Ast::Tuple(members), _) = value.as_ref() else { unreachable!() }; for (i, member) in members.iter().enumerate() { self.visit(member); self.emit_op(Op::StoreAt); self.emit_byte(i); } let arity = members.len(); let stack_depth = self.stack_depth; //then, save the beginning of the loop self.emit_op(Op::Load); self.enter_loop(); self.stack_depth += arity; //next, compile each clause: let mut clauses = clauses.iter(); let mut jump_idxes = vec![]; while let Some((Ast::MatchClause(pattern, guard, body), _)) = clauses.next() { self.tail_pos = false; self.emit_op(Op::ResetMatch); self.scope_depth += 1; let (Ast::TuplePattern(members), _) = pattern.as_ref() else { unreachable!() }; self.match_depth = arity; let mut jnm_idxes = vec![]; for member in members { self.match_depth -= 1; self.emit_op(Op::MatchDepth); self.emit_byte(self.match_depth); self.visit(member); jnm_idxes.push(self.stub_jump(Op::JumpIfNoMatch)); } if guard.is_some() { let guard_expr: &'static Spanned = Box::leak(Box::new(guard.clone().unwrap())); self.visit(guard_expr); jnm_idxes.push(self.stub_jump(Op::JumpIfFalse)); self.stack_depth -= 1; } self.tail_pos = tail_pos; self.visit(body); self.emit_op(Op::Store); self.scope_depth -= 1; while let Some(binding) = self.bindings.last() { if binding.depth > self.scope_depth { self.bindings.pop(); } else { break; } } while self.stack_depth > stack_depth { self.pop(); } jump_idxes.push(self.stub_jump(Op::Jump)); for idx in jnm_idxes { self.patch_jump(idx, self.len() - idx - 3); } self.scope_depth -= 1; } self.emit_op(Op::PanicNoMatch); for idx in jump_idxes { self.patch_jump(idx, self.len() - idx - 3); } self.stack_depth -= arity; self.emit_op(Op::Load); self.stack_depth += 1; self.leave_loop(); } Recur(args) => { for (i, arg) in args.iter().enumerate() { self.visit(arg); self.emit_op(Op::StoreAt); self.emit_byte(i); } self.emit_op(Op::PopN); self.emit_byte(self.loop_root()); self.emit_op(Op::Load); self.jump(Op::JumpBack, self.len() - self.loop_idx()); } Panic(msg) => { self.visit(msg); self.emit_op(Op::Panic); } Interpolated(parts) => { self.emit_op(Op::EmptyString); self.stack_depth += 1; for part in parts { let str = &part.0; match str { StringPart::Inline(_) => unreachable!(), StringPart::Data(str) => { let allocated = Value::String(Rc::new(str.clone())); self.emit_constant(allocated); self.emit_op(Op::ConcatStrings); self.stack_depth -= 1; } StringPart::Word(word) => { self.resolve_binding(word); self.emit_op(Op::Stringify); self.emit_op(Op::ConcatStrings); } } } } Do(terms) => { let mut terms = terms.iter(); let first = terms.next().unwrap(); let mut terms = terms.rev(); let last = terms.next().unwrap(); let terms = terms.rev(); // put the first value on the stack let tail_pos = self.tail_pos; self.tail_pos = false; self.visit(first); for term in terms { self.visit(term); self.emit_op(Op::Call); self.emit_byte(1); self.stack_depth -= 1; } self.visit(last); if tail_pos { self.emit_op(Op::TailCall) } else { self.emit_op(Op::Call); } self.emit_byte(1); self.tail_pos = tail_pos; self.stack_depth -= 1; } Placeholder => { self.emit_op(Op::Nothing); } And | Or | Arguments(..) => unreachable!(), } } pub fn disassemble(&self) { println!("=== chunk: {} ===", self.name); self.chunk.dissasemble(); } }