From e93e9e7ff4a73bc98a0dcd410eff87dffff3aa1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 30 Jan 2021 12:10:49 +0100 Subject: external functions proc macro --- src/main.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index a97cd43..10b88ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use std::path::{Path, PathBuf}; use tihdy::run_file; -use tihdy::vm::Value; struct Args { file: Option, @@ -11,12 +10,14 @@ struct Args { fn main() { let args = parse_args(); let file = args.file.unwrap_or_else(|| Path::new("tests/simple.tdy").to_owned()); - if let Err(errs) = run_file(&file, args.print, vec![(String::from("hello"), hello)]) { - for err in errs.iter() { - println!("{}", err); - } - println!(" {} errors occured.", errs.len()); + let errs = match run_file(&file, args.print, vec![(String::from("extern_test"), extern_test)]) { + Err(it) => it, + _ => return, + }; + for err in errs.iter() { + println!("{}", err); } + println!(" {} errors occured.", errs.len()); } fn parse_args() -> Args { @@ -38,14 +39,12 @@ fn parse_args() -> Args { args } -pub fn hello(parameters: &[Value]) -> Value { - match parameters { - [Value::String(s)] => { - println!("{}", s); - } - _ => { - println!("Bad parameters"); - } - } - Value::Nil -} +tihdy_derive::extern_function!( + extern_test + [tihdy::vm::Value::Float(x), tihdy::vm::Value::Float(y)] -> tihdy::vm::Type::Float => { + Ok(tihdy::vm::Value::Float(x + y)) + }, + [tihdy::vm::Value::Float(x)] -> tihdy::vm::Type::Float => { + Ok(tihdy::vm::Value::Float(*x)) + }, +); -- cgit v1.2.1