add a print builtin/opcode

This commit is contained in:
Scott Richmond 2025-06-03 19:26:01 -04:00
parent c497b90a16
commit bf2b16c30f
3 changed files with 13 additions and 4 deletions

View File

@ -76,6 +76,7 @@ pub enum Op {
At, At,
Not, Not,
Print,
// Inc, // Inc,
// Dec, // Dec,
// Gt, // Gt,
@ -169,6 +170,7 @@ impl std::fmt::Display for Op {
EmptyString => "empty_string", EmptyString => "empty_string",
ConcatStrings => "concat_strings", ConcatStrings => "concat_strings",
Stringify => "stringify", Stringify => "stringify",
Print => "print",
Eq => "eq", Eq => "eq",
Add => "add", Add => "add",
@ -214,7 +216,7 @@ impl Chunk {
| PanicIfNoMatch | ResetMatch | GetKey | PanicNoWhen | PanicNoMatch | TypeOf | PanicIfNoMatch | ResetMatch | GetKey | PanicNoWhen | PanicNoMatch | TypeOf
| Duplicate | Decrement | Truncate | Noop | LoadTuple | LoadList | Eq | Add | Sub | Duplicate | Decrement | Truncate | Noop | LoadTuple | LoadList | Eq | Add | Sub
| Mult | Div | Unbox | BoxStore | Assert | Get | At | Not | Panic | EmptyString | 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}") println!("{i:04}: {op}")
} }
Constant | MatchConstant => { Constant | MatchConstant => {
@ -277,6 +279,7 @@ fn get_builtin(name: &str, arity: usize) -> Option<Op> {
("get", 2) => Some(Op::Get), ("get", 2) => Some(Op::Get),
("at", 2) => Some(Op::At), ("at", 2) => Some(Op::At),
("not", 1) => Some(Op::Not), ("not", 1) => Some(Op::Not),
("print!", 1) => Some(Op::Print),
_ => None, _ => None,
} }
@ -859,7 +862,6 @@ impl Compiler {
}, },
_ => unreachable!(), _ => unreachable!(),
} }
// TODO: implement longer synthetic expressions
for (term, _) in rest { for (term, _) in rest {
match term { match term {
Keyword(str) => { Keyword(str) => {

View File

@ -76,9 +76,11 @@ 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 () -> fn () -> fn () -> #{:foo :bar} fn bang! () -> print! (:bang)
foo () () () :foo bang! ()
bang! ()
bang! ()
"; ";
run(src); run(src);
} }

View File

@ -706,6 +706,11 @@ impl Vm {
swap(&mut self.return_register[0], &mut value); swap(&mut self.return_register[0], &mut value);
self.push(value); self.push(value);
} }
Print => {
println!("{}", self.pop().show());
self.push(Value::Keyword("ok"));
self.ip += 1;
}
} }
} }
} }