From e04ad073662ccbc1e64de9bf867bba5c7b86db3e Mon Sep 17 00:00:00 2001 From: Empa Date: Sat, 3 Feb 2024 01:09:24 +0100 Subject: [PATCH] Add native function --- crates/saft-bytecode/src/value.rs | 13 +++++++++++++ crates/saft/src/lib.rs | 10 ++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/saft-bytecode/src/value.rs b/crates/saft-bytecode/src/value.rs index f167b6a..e94a695 100644 --- a/crates/saft-bytecode/src/value.rs +++ b/crates/saft-bytecode/src/value.rs @@ -289,6 +289,19 @@ impl Cast for Value { } } +impl Cast for Value { + fn name() -> String { + "string".into() + } + + fn cast(&self) -> Option { + match self { + Value::String(s) => Some(s.clone()), + _ => None, + } + } +} + pub enum ValueType { Nil, Bool, diff --git a/crates/saft/src/lib.rs b/crates/saft/src/lib.rs index c828947..2169e0f 100644 --- a/crates/saft/src/lib.rs +++ b/crates/saft/src/lib.rs @@ -1,5 +1,6 @@ use std::rc::Rc; +use bytecode::natives; use codespan_reporting::{ files::SimpleFiles, term::{self, termcolor::StandardStream}, @@ -22,10 +23,11 @@ impl Saft { pub fn new() -> Self { let mut lowerer = saft_ast_to_ir::Lowerer::new(); - lowerer.add_item( - "print".into(), - saft_ir::Item::Builtin(bytecode::natives::print), - ); + let mut add_native = |native: bytecode::value::NativeFunction| { + lowerer.add_item(native.name.to_string(), saft_ir::Item::Builtin(native)); + }; + + add_native(natives::print); Self { lowerer,