DEV: Tuple and List values are translatable.

This commit is contained in:
Jeremy Wall 2019-08-02 19:26:11 -05:00
parent 52a66bae8b
commit 422569c7b4
2 changed files with 22 additions and 19 deletions

View File

@ -616,23 +616,13 @@ fn simple_let_statements() {
#[test]
fn dot_expressions() {
let mut ops = vec![
Sym("foo".to_owned()),
InitList,
Val(Int(0)),
Element,
Val(Int(1)),
Element,
Val(Int(2)),
Element,
Bind,
assert_parse_cases![
"let foo = [0,1,2]; foo.0;" => P(Int(0)),
"let foo = [0,1,2]; foo.2;" => P(Int(2)),
"let tpl = { foo = 1 }; tpl.foo;" => P(Int(1)),
"let tpl = { foo = { bar = 2 } }; tpl.foo.bar;" => P(Int(2)),
"let tpl = { foo = [ 3 ] }; tpl.foo.0;" => P(Int(3)),
];
let stmts = parse(OffsetStrIter::from(dbg!("foo.0;")), None).unwrap();
ops.append(&mut translate::AST::translate(stmts));
let ops = Rc::new(ops);
let mut vm = VM::new("foo.ucg", ops.clone());
vm.run().unwrap();
}
#[test]

View File

@ -186,7 +186,7 @@ impl AST {
}
}
fn translate_value(value: Value, ops: &mut Vec<Op>) {
fn translate_value(value: Value, mut ops: &mut Vec<Op>) {
match value {
Value::Int(i) => ops.push(Op::Val(Primitive::Int(i.val))),
Value::Float(f) => ops.push(Op::Val(Primitive::Float(f.val))),
@ -196,8 +196,21 @@ impl AST {
Value::Symbol(s) => {
ops.push(Op::DeRef(s.val));
}
Value::Tuple(_flds) => unimplemented!("Select expression are not implmented yet"),
Value::List(_els) => unimplemented!("Select expression are not implmented yet"),
Value::Tuple(flds) => {
ops.push(Op::InitTuple);
for (k, v) in flds.val {
ops.push(Op::Sym(k.fragment));
Self::translate_expr(v, &mut ops);
ops.push(Op::Field);
}
}
Value::List(els) => {
ops.push(Op::InitList);
for el in els.elems {
Self::translate_expr(el, &mut ops);
ops.push(Op::Element);
}
}
}
}
}