diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-04-25 16:27:07 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-04-25 16:27:07 -0400 |
commit | 073f63433bf4a38d2c5a878872f191b708509aae (patch) | |
tree | b8e9da526f5cdd59bc4228d83631bfd48194e625 | |
parent | 9f38b099d81074d8a5f52ef519221d2bb9fce6ca (diff) | |
download | spectrum-073f63433bf4a38d2c5a878872f191b708509aae.tar.gz spectrum-073f63433bf4a38d2c5a878872f191b708509aae.zip |
rust: finish implementing typechecks
-rw-r--r-- | rust/spectrum.rs | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/rust/spectrum.rs b/rust/spectrum.rs index 9cdbbd2..38fb30c 100644 --- a/rust/spectrum.rs +++ b/rust/spectrum.rs @@ -18,8 +18,7 @@ const SCHEME_BUILTINS: [&'static str; 34] = [ "lambda", "quote", "cond", "else", "display", "define", "set!", "cons", "car", "cdr", - "boolean?", "symbol?", "procedure?", "pair?", "number?", "string?", - "null?", "atom?", "zero?", + "boolean?", "symbol?", "procedure?", "pair?", "number?", "string?", "null?", "atom?", "zero?", "eq?", "=", ">", ">=", "<", "<=", "+", "-", "*", "/", @@ -351,17 +350,36 @@ fn apply_typecheck(action: &str, args: Vec<SchemeExpr>) -> Result<SchemeExpr, St } let arg: &SchemeExpr = &args[0]; let ret: bool = match action { - "null?" => *arg == SchemeExpr::SchemeNull, - "zero?" => *arg == SchemeExpr::SchemeNum(0.0), + "boolean?" => *arg == SchemeExpr::SchemeTrue || *arg == SchemeExpr::SchemeFalse, + "symbol?" => match *arg { + SchemeExpr::SchemeSymbol(_) => true, + _ => false, + }, + "procedure?" => match *arg { + SchemeExpr::SchemeProcedure(_, _, _) => true, + _ => false, + }, + "pair?" => match *arg { + SchemeExpr::SchemeList(_) => true, + _ => false, + }, "number?" => match *arg { SchemeExpr::SchemeNum(_) => true, - _ => false}, + _ => false + }, + "string?" => match *arg { + SchemeExpr::SchemeStr(_) => true, + _ => false, + }, + "null?" => *arg == SchemeExpr::SchemeNull, "atom?" => match *arg { SchemeExpr::SchemeNull | SchemeExpr::SchemeTrue | SchemeExpr::SchemeFalse | SchemeExpr::SchemeNum(_) => true, - _ => false}, + _ => false + }, + "zero?" => *arg == SchemeExpr::SchemeNum(0.0), _ => { return Err(format!("unimplemented typecheck builtin: {}", action)); }, }; if ret { @@ -431,7 +449,8 @@ fn apply_action(list: &Vec<SchemeExpr>, &SchemeExpr::SchemeBuiltin(ref builtin) => { return match builtin.as_str() { "+" | "-" | "*" | "/" => apply_math_op(builtin, args), - "null?" | "number?" | "zero?" | "atom?" => apply_typecheck(builtin, args), + "boolean?" | "symbol?" | "procedure?" | "pair?" | "number?" | "string?" | + "null?" | "atom?" | "zero?" => apply_typecheck(builtin, args), "eq?" => { if args.len() != 2 { return Err(format!("eq? takes only two arguments")); |