Implement Access AST node

For name.value lookups
This commit is contained in:
Greg Shuflin 2021-10-30 21:22:15 -07:00
parent 1c6545fb74
commit f0e4b50c99
6 changed files with 1144 additions and 1078 deletions

View File

@ -196,6 +196,7 @@ pub enum ExpressionKind {
WhileExpression { condition: Option<Box<Expression>>, body: Block },
ForExpression { enumerators: Vec<Enumerator>, body: Box<ForBody> },
Lambda { params: Vec<FormalParam>, type_anno: Option<TypeIdentifier>, body: Block },
Access { name: Rc<String>, expr: Box<Expression> },
ListLiteral(Vec<Expression>),
}

View File

@ -128,6 +128,9 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
Lambda { params: _, type_anno: _, body } => {
walk_block(v, body);
}
Access { name: _, expr } => {
walk_expression(v, expr);
}
ListLiteral(exprs) =>
for expr in exprs {
walk_expression(v, expr);

View File

@ -120,6 +120,7 @@ fn render_expression(expr: &Expression, indent: usize, buf: &mut String) {
newline(buf);
do_indent(indent, buf);
}
Access { .. } => buf.push_str("<access-expr>"),
ListLiteral(..) => buf.push_str("<list-literal>"),
}
buf.push(')');

File diff suppressed because it is too large Load Diff

View File

@ -203,11 +203,36 @@ fn operators() {
#[test]
fn accessors() {
/*
assert_expr!("a.b");
assert_expr!("a.b.c.d()");
assert_expr!("a.b().c.d()");
*/
use ExpressionKind::*;
assert_expr!("a.b", expr(Access { name: rc("b"), expr: bx(expr(Value(qn!(a)))) }));
assert_expr!(
"a.b.c",
expr(Access {
name: rc("c"),
expr: bx(expr(Access { name: rc("b"), expr: bx(expr(Value(qn!(a)))) }))
})
);
assert_expr!(
"a.b.c(3)",
expr(Call {
f: bx(expr(Access {
name: rc("c"),
expr: bx(expr(Access { name: rc("b"), expr: bx(expr(Value(qn!(a)))) }))
})),
arguments: vec![InvocationArgument::Positional(expr(NatLiteral(3)))],
})
);
assert_expr!(
"a.b().c",
expr(Access {
name: rc("c"),
expr: bx(expr(Call {
f: bx(expr(Access { name: rc("b"), expr: bx(expr(Value(qn!(a)))) })),
arguments: vec![]
}))
})
);
}
#[test]

View File

@ -179,6 +179,7 @@ impl<'a, 'b> Reducer<'a, 'b> {
WhileExpression { .. } => Expression::ReductionError("While expr not implemented".to_string()),
ForExpression { .. } => Expression::ReductionError("For expr not implemented".to_string()),
ListLiteral { .. } => Expression::ReductionError("ListLiteral expr not implemented".to_string()),
Access { .. } => unimplemented!(),
}
}