aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-04-20 17:35:44 -0400
committerbnewbold <bnewbold@robocracy.org>2016-04-20 17:35:44 -0400
commit8038a673a628490e20275a16c3ca036c2d3babf6 (patch)
treeabcca81b649fa0155297e5fd357d0a5dc8a3a3d7
parenta430e62f164f5a3c921846d74ee65061036654c5 (diff)
downloadspectrum-8038a673a628490e20275a16c3ca036c2d3babf6.zip
spectrum-8038a673a628490e20275a16c3ca036c2d3babf6.tar.gz
rust: add SchemeProcedure enum type
-rw-r--r--minimal.rs25
1 files changed, 21 insertions, 4 deletions
diff --git a/minimal.rs b/minimal.rs
index 4990d15..55c67dc 100644
--- a/minimal.rs
+++ b/minimal.rs
@@ -23,6 +23,10 @@ enum SchemeExpr<'a> {
SchemeBuiltin(&'a str),
SchemeSymbol(&'a str),
SchemeStr(&'a str),
+ SchemeProcedure(
+ Vec<SchemeExpr<'a>>,
+ Vec<SchemeExpr<'a>>,
+ HashMap<&'a str, SchemeExpr<'a>>),
SchemeList(Vec<SchemeExpr<'a>>),
SchemeQuote(Vec<SchemeExpr<'a>>),
}
@@ -166,10 +170,22 @@ fn scheme_repr<'a>(ast: &SchemeExpr) -> Result<String, &'static str> {
&SchemeExpr::SchemeTrue => Ok("#t".to_string()),
&SchemeExpr::SchemeFalse => Ok("#f".to_string()),
&SchemeExpr::SchemeNull => Ok("'()".to_string()),
+ &SchemeExpr::SchemeNum(num) => Ok(format!("{}", num).to_string()),
&SchemeExpr::SchemeBuiltin(b)=> Ok(b.to_string()),
&SchemeExpr::SchemeStr(s)=> Ok(s.to_string()),
&SchemeExpr::SchemeSymbol(s)=> Ok(s.to_string()),
- &SchemeExpr::SchemeNum(num) => Ok(format!("{}", num).to_string()),
+ &SchemeExpr::SchemeProcedure(ref binds, ref body, _) => {
+ let mut ret = "(lambda (".to_string();
+ for bind in binds {
+ ret = ret + &try!(scheme_repr(&bind)) + " ";
+ }
+ ret = ret + ") ";
+ for expr in body {
+ ret = ret + &try!(scheme_repr(&expr));
+ }
+ ret = ret + ")";
+ Ok(ret)
+ },
&SchemeExpr::SchemeList(ref list) => {
let mut ret: String =
list.iter().fold("(".to_string(),
@@ -229,9 +245,10 @@ fn scheme_meaning<'a>(ast: &'a SchemeExpr, ctx: HashMap<&str, SchemeExpr<'a>>) -
&SchemeExpr::SchemeTrue => Ok(ast.clone()),
&SchemeExpr::SchemeFalse => Ok(ast.clone()),
&SchemeExpr::SchemeNull => Ok(ast.clone()),
- &SchemeExpr::SchemeStr(s) => Ok(ast.clone()),
- &SchemeExpr::SchemeNum(num) => Ok(ast.clone()),
- &SchemeExpr::SchemeBuiltin(b) => Ok(ast.clone()),
+ &SchemeExpr::SchemeStr(_) => Ok(ast.clone()),
+ &SchemeExpr::SchemeNum(_) => Ok(ast.clone()),
+ &SchemeExpr::SchemeBuiltin(_) => Ok(ast.clone()),
+ &SchemeExpr::SchemeProcedure(_, _, _) => Ok(ast.clone()),
&SchemeExpr::SchemeQuote(ref list)
=> Ok(SchemeExpr::SchemeList(list.clone())),
&SchemeExpr::SchemeSymbol(sym) => match ctx.get(sym) {