From 424998c12880fac8fa41b680ff8aaf0be6cafb48 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 19 Feb 2017 15:17:01 -0800 Subject: [PATCH] Lists work! --- src/schala_lang/eval.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/schala_lang/eval.rs b/src/schala_lang/eval.rs index 3f96ee9..ff97feb 100644 --- a/src/schala_lang/eval.rs +++ b/src/schala_lang/eval.rs @@ -43,6 +43,15 @@ impl From for ReducedValue { } } +fn get_indexer(f: f64) -> Option { + if f.fract() == 0.0 { + if f.trunc() >= 0.0 { + return Some(f.trunc() as usize); + } + } + None +} + #[derive(Debug)] enum SideEffect { Print(String), @@ -329,7 +338,15 @@ impl<'a> Evaluator<'a> { return (Index(expr, index_expr), side_effect); } - match expr { + match (*expr, *index_expr) { + (ListLiteral(list_items), Number(n)) => { + let indexed_expr = get_indexer(n).and_then(|i| list_items.get(i)); + if let Some(e) = indexed_expr { + (e.clone(), None) + } else { + (Null, None) + } + } _ => (Null, None) } }