diff --git a/schala-lang/language/src/reduced_ir/mod.rs b/schala-lang/language/src/reduced_ir/mod.rs index b95a7c6..e4eb3f5 100644 --- a/schala-lang/language/src/reduced_ir/mod.rs +++ b/schala-lang/language/src/reduced_ir/mod.rs @@ -187,7 +187,7 @@ impl<'a, 'b> Reducer<'a, 'b> { Expression::Loop { cond, statements } } ForExpression { .. } => Expression::ReductionError("For expr not implemented".to_string()), - ListLiteral { .. } => Expression::ReductionError("ListLiteral expr not implemented".to_string()), + ListLiteral(items) => Expression::List(items.iter().map(|item| self.expression(item)).collect()), Access { name, expr } => Expression::Access { name: name.as_ref().to_string(), expr: Box::new(self.expression(expr)) }, } diff --git a/schala-lang/language/src/reduced_ir/types.rs b/schala-lang/language/src/reduced_ir/types.rs index 5927c88..21340bf 100644 --- a/schala-lang/language/src/reduced_ir/types.rs +++ b/schala-lang/language/src/reduced_ir/types.rs @@ -51,6 +51,7 @@ pub enum Statement { pub enum Expression { Literal(Literal), Tuple(Vec), + List(Vec), Lookup(Lookup), Assign { lval: DefId, rval: Box }, Access { name: String, expr: Box }, diff --git a/schala-lang/language/src/tree_walk_eval/evaluator.rs b/schala-lang/language/src/tree_walk_eval/evaluator.rs index 1f4bbc0..a242fab 100644 --- a/schala-lang/language/src/tree_walk_eval/evaluator.rs +++ b/schala-lang/language/src/tree_walk_eval/evaluator.rs @@ -111,6 +111,12 @@ impl<'a, 'b> Evaluator<'a, 'b> { .map(|expr| self.expression(expr)) .collect::>>()?, ), + Expression::List(items) => Primitive::List( + items + .into_iter() + .map(|expr| self.expression(expr)) + .collect::>>()?, + ), Expression::Lookup(kind) => match kind { Lookup::Function(ref id) => { let mem = id.into(); diff --git a/schala-lang/language/src/tree_walk_eval/mod.rs b/schala-lang/language/src/tree_walk_eval/mod.rs index 6468d95..5d22a1e 100644 --- a/schala-lang/language/src/tree_walk_eval/mod.rs +++ b/schala-lang/language/src/tree_walk_eval/mod.rs @@ -61,16 +61,16 @@ impl RuntimeError { } } -fn paren_wrapped(terms: impl Iterator) -> String { +fn delim_wrapped(lhs: char, rhs: char, terms: impl Iterator) -> String { let mut buf = String::new(); - write!(buf, "(").unwrap(); + write!(buf, "{}", lhs).unwrap(); for term in terms.map(Some).intersperse(None) { match term { Some(e) => write!(buf, "{}", e).unwrap(), None => write!(buf, ", ").unwrap(), }; } - write!(buf, ")").unwrap(); + write!(buf, "{}", rhs).unwrap(); buf } @@ -110,6 +110,7 @@ impl From for RuntimeValue { #[derive(Debug, Clone)] enum Primitive { Tuple(Vec), + List(Vec), Literal(Literal), Callable(Callable), Object { type_id: TypeId, tag: u32, ordered_fields: Option>, items: Vec }, @@ -124,7 +125,7 @@ impl Primitive { format!( "{}{}", type_context.variant_local_name(type_id, *tag).unwrap(), - paren_wrapped(items.iter().map(|item| item.to_repl(type_context))) + delim_wrapped('(', ')', items.iter().map(|item| item.to_repl(type_context))) ) } Primitive::Object { type_id, items, tag, ordered_fields: Some(fields) } => { @@ -145,7 +146,8 @@ impl Primitive { Literal::Bool(b) => format!("{}", b), Literal::StringLit(s) => format!("\"{}\"", s), }, - Primitive::Tuple(terms) => paren_wrapped(terms.iter().map(|x| x.to_repl(type_context))), + Primitive::Tuple(terms) => delim_wrapped('(', ')', terms.iter().map(|x| x.to_repl(type_context))), + Primitive::List(terms) => delim_wrapped('[', ']', terms.iter().map(|x| x.to_repl(type_context))), Primitive::Callable(..) => "".to_string(), } } diff --git a/schala-lang/language/src/tree_walk_eval/test.rs b/schala-lang/language/src/tree_walk_eval/test.rs index 2184776..88dfcdb 100644 --- a/schala-lang/language/src/tree_walk_eval/test.rs +++ b/schala-lang/language/src/tree_walk_eval/test.rs @@ -526,3 +526,14 @@ while a < 10 { acc"#; eval_assert(source, "19"); } + +#[test] +fn list_literals() { + eval_assert( + r#" +let a = [7, 8, 9] +a +"#, + "[7, 8, 9]", + ); +}