diff options
Diffstat (limited to 'src/builtins.rs')
| -rw-r--r-- | src/builtins.rs | 66 |
1 files changed, 59 insertions, 7 deletions
diff --git a/src/builtins.rs b/src/builtins.rs index dbc17eb..b8b789f 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -4,6 +4,8 @@ use std::rc::Rc; pub const B_INTEGER_EQ0: &str = "int.eq0?"; pub const B_INTEGER_INCREMENT: &str = "int.increment"; +pub const B_INTEGER_ADD: &str = "int.add"; +pub const B_INTEGER_ADD_1: &str = "##int.add_1"; pub fn make_boolean_true_function() -> Term { let x = advance_v(); @@ -27,13 +29,18 @@ pub fn try_ast_symbol_to_builtin_term(symbol: &ast::Symbol) -> Option<Term> { "true" => Some(make_boolean_true_function()), "false" => Some(make_boolean_false_function()), "id" => Some(make_identity_function()), - B_INTEGER_EQ0 => Some(Term::Builtin(String::from(B_INTEGER_EQ0))), - B_INTEGER_INCREMENT => Some(Term::Builtin(String::from(B_INTEGER_INCREMENT))), + B_INTEGER_EQ0 => Some(Term::Builtin(String::from(B_INTEGER_EQ0), vec![])), + B_INTEGER_INCREMENT => Some(Term::Builtin(String::from(B_INTEGER_INCREMENT), vec![])), + B_INTEGER_ADD => Some(Term::Builtin(String::from(B_INTEGER_ADD), vec![])), _ => None, } } -pub fn evaluate_builtin(symbol: &String, rhs: Rc<Term>) -> Result<(Rc<Term>, usize), String> { +pub fn evaluate_builtin( + symbol: &String, + bound_arguments: &Vec<Value>, + rhs: Rc<Term>, +) -> Result<(Rc<Term>, usize), String> { let result_term = match symbol.as_str() { B_INTEGER_EQ0 => match &*rhs { Term::Primitive(primitive) => match primitive { @@ -46,24 +53,69 @@ pub fn evaluate_builtin(symbol: &String, rhs: Rc<Term>) -> Result<(Rc<Term>, usi } other => { return Err(format!( - "[runtime] cannot apply builtin {} to argument type {}", + "[runtime] cannot apply builtin {} to argument {}", symbol, other )) } }, - _ => return Ok((Rc::new(Term::Builtin(symbol.clone())), 0)), + _ => return Ok((Rc::new(Term::Builtin(symbol.clone(), vec![])), 0)), }, B_INTEGER_INCREMENT => match &*rhs { Term::Primitive(primitive) => match primitive { Value::Integer(value) => Term::Primitive(Value::Integer(value + 1)), other => { return Err(format!( - "[runtime] cannot apply builtin {} to argument type {}", + "[runtime] cannot apply builtin {} to argument {}", symbol, other )) } }, - _ => return Ok((Rc::new(Term::Builtin(symbol.clone())), 0)), + _ => return Ok((Rc::new(Term::Builtin(symbol.clone(), vec![])), 0)), + }, + B_INTEGER_ADD => match &*rhs { + Term::Primitive(primitive) => match primitive { + Value::Integer(_) => { + Term::Builtin(String::from(B_INTEGER_ADD_1), vec![primitive.clone()]) + } + other => { + return Err(format!( + "[runtime] cannot apply builtin {} to argument {}", + symbol, other + )) + } + }, + _ => return Ok((Rc::new(Term::Builtin(symbol.clone(), vec![])), 0)), + }, + B_INTEGER_ADD_1 => match &*rhs { + Term::Primitive(primitive) => match primitive { + Value::Integer(value) => { + assert_eq!(bound_arguments.len(), 1); + let summand = &bound_arguments[0]; + match summand { + Value::Integer(summand_value) => { + Term::Primitive(Value::Integer(summand_value + value)) + } + other => { + return Err(format!( + "[runtime] cannot apply builtin {} to argument {}", + symbol, other + )) + } + } + } + other => { + return Err(format!( + "[runtime] cannot apply builtin {} to argument {}", + symbol, other + )) + } + }, + _ => { + return Ok(( + Rc::new(Term::Builtin(symbol.clone(), bound_arguments.clone())), + 0, + )) + } }, _ => return Err(format!("[runtime] invalid builtin evaluated: {}", symbol)), }; |
