diff --git a/src/compiler.rs b/src/compiler.rs index 2427253..0e5ccd6 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -76,6 +76,7 @@ pub enum Op { At, Not, + Print, // Inc, // Dec, // Gt, @@ -169,6 +170,7 @@ impl std::fmt::Display for Op { EmptyString => "empty_string", ConcatStrings => "concat_strings", Stringify => "stringify", + Print => "print", Eq => "eq", Add => "add", @@ -214,7 +216,7 @@ impl Chunk { | 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 => { + | ConcatStrings | Stringify | MatchType | Return | Match | Print => { println!("{i:04}: {op}") } Constant | MatchConstant => { @@ -277,6 +279,7 @@ fn get_builtin(name: &str, arity: usize) -> Option { ("get", 2) => Some(Op::Get), ("at", 2) => Some(Op::At), ("not", 1) => Some(Op::Not), + ("print!", 1) => Some(Op::Print), _ => None, } @@ -859,7 +862,6 @@ impl Compiler { }, _ => unreachable!(), } - // TODO: implement longer synthetic expressions for (term, _) in rest { match term { Keyword(str) => { diff --git a/src/main.rs b/src/main.rs index 1841443..488ca23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,9 +76,11 @@ pub fn run(src: &'static str) { pub fn main() { env::set_var("RUST_BACKTRACE", "1"); let src = " -fn foo () -> fn () -> fn () -> #{:foo :bar} +fn bang! () -> print! (:bang) -foo () () () :foo +bang! () +bang! () +bang! () "; run(src); } diff --git a/src/vm.rs b/src/vm.rs index 8eccd98..023b6ba 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -706,6 +706,11 @@ impl Vm { swap(&mut self.return_register[0], &mut value); self.push(value); } + Print => { + println!("{}", self.pop().show()); + self.push(Value::Keyword("ok")); + self.ip += 1; + } } } }