mech_interpreter/functions.rs
1use crate::*;
2
3// Functions
4// ----------------------------------------------------------------------------
5
6pub fn function_define(fxn_def: &FunctionDefine, p: &Interpreter) -> MResult<FunctionDefinition> {
7 /*let fxn_name_id = fxn_def.name.hash();
8 let mut new_fxn = FunctionDefinition::new(fxn_name_id,fxn_def.name.to_string(), fxn_def.clone());
9 for input_arg in &fxn_def.input {
10 let arg_id = input_arg.name.hash();
11 new_fxn.input.insert(arg_id,input_arg.kind.clone());
12 let in_arg = Value::F64(Ref::new(F64::new(0.0)));
13 new_fxn.symbols.borrow_mut().insert(arg_id, in_arg, false);
14 }
15 let output_arg_ids = fxn_def.output.iter().map(|output_arg| {
16 let arg_id = output_arg.name.hash();
17 new_fxn.output.insert(arg_id,output_arg.kind.clone());
18 arg_id
19 }).collect::<Vec<u64>>();
20
21 for stmnt in &fxn_def.statements {
22 let result = statement(stmnt, new_fxn.plan.clone(), new_fxn.symbols.clone(), functions.clone());
23 }
24 // get the output cell
25 {
26 let symbol_brrw = new_fxn.symbols.borrow();
27 for arg_id in output_arg_ids {
28 match symbol_brrw.get(arg_id) {
29 Some(cell) => new_fxn.out = cell.clone(),
30 None => { return Err(MechError{file: file!().to_string(), tokens: fxn_def.output.iter().flat_map(|a| a.tokens()).collect(), msg: "".to_string(), id: line!(), kind: MechErrorKind::OutputUndefinedInFunctionBody(arg_id)});}
31 }
32 }
33 }
34 Ok(new_fxn)*/
35 todo!("Function define needs to be redone!");
36}
37
38pub fn function_call(fxn_call: &FunctionCall, p: &Interpreter) -> MResult<Value> {
39 let plan = p.plan();
40 let functions = p.functions();
41 let fxn_name_id = fxn_call.name.hash();
42 let fxns_brrw = functions.borrow();
43 match fxns_brrw.functions.get(&fxn_name_id) {
44 Some(fxn) => {
45 todo!();
46 }
47 None => {
48 match fxns_brrw.function_compilers.get(&fxn_name_id) {
49 Some(fxn_compiler) => {
50 let mut input_arg_values = vec![];
51 for (arg_name, arg_expr) in fxn_call.args.iter() {
52 let result = expression(&arg_expr, p)?;
53 input_arg_values.push(result);
54 }
55 match fxn_compiler.compile(&input_arg_values) {
56 Ok(new_fxn) => {
57 let mut plan_brrw = plan.borrow_mut();
58 new_fxn.solve();
59 let result = new_fxn.out();
60 plan_brrw.push(new_fxn);
61 return Ok(result)
62 }
63 Err(x) => {return Err(x);}
64 }
65 }
66 None => {return Err(MechError2::new(
67 MissingFunctionError{ function_id: fxn_name_id },
68 None
69 ).with_compiler_loc().with_tokens(fxn_call.name.tokens())
70 );}
71 }
72 }
73 }
74 unreachable!()
75}
76
77#[derive(Debug, Clone)]
78pub struct MissingFunctionError {
79 pub function_id: u64,
80}
81impl MechErrorKind2 for MissingFunctionError {
82 fn name(&self) -> &str { "MissingFunction" }
83 fn message(&self) -> String {
84 format!("Function with id {} not found", self.function_id)
85 }
86}