Skip to content

Commit

Permalink
Add native function
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaqqer committed Feb 3, 2024
1 parent 931e8bd commit e04ad07
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 13 additions & 0 deletions crates/saft-bytecode/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ impl Cast<f64> for Value {
}
}

impl Cast<String> for Value {
fn name() -> String {
"string".into()
}

fn cast(&self) -> Option<String> {
match self {
Value::String(s) => Some(s.clone()),
_ => None,
}
}
}

pub enum ValueType {
Nil,
Bool,
Expand Down
10 changes: 6 additions & 4 deletions crates/saft/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::rc::Rc;

use bytecode::natives;
use codespan_reporting::{
files::SimpleFiles,
term::{self, termcolor::StandardStream},
Expand All @@ -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,
Expand Down

0 comments on commit e04ad07

Please sign in to comment.