Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/expressions/src/lib/ast_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as ast from './ast.js';

export interface AstFactory<E extends ast.Expression> {
empty(): E;
getValue(receiver: Record<string, any>, name: string): any;
literal(value: ast.LiteralValue): E;
id(name: string): E;
unary(operator: string, expression: E): E;
Expand All @@ -30,6 +31,10 @@ export class DefaultAstFactory implements AstFactory<ast.Expression> {
return {type: 'Empty'};
}

getValue(receiver: Record<string, any>, name: string) {
return receiver?.[name];
}

// TODO(justinfagnani): just use a JS literal?
literal(value: ast.LiteralValue): ast.Literal {
return {
Expand Down
15 changes: 12 additions & 3 deletions packages/expressions/src/lib/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export class EvalAstFactory implements AstFactory<Expression> {
};
}

getValue(receiver: Record<string, any>, name: string) {
return receiver?.[name];
}

// TODO(justinfagnani): just use a JS literal?
literal(v: string): Literal {
return {
Expand All @@ -145,13 +149,14 @@ export class EvalAstFactory implements AstFactory<Expression> {
}

id(v: string): ID {
const astFactoryInstance = this;
return {
type: 'ID',
value: v,
evaluate(scope) {
// TODO(justinfagnani): this prevents access to properties named 'this'
if (this.value === 'this') return scope;
return scope?.[this.value];
return astFactoryInstance.getValue(scope, this.value);
},
getIds(idents) {
idents.push(this.value);
Expand Down Expand Up @@ -220,12 +225,14 @@ export class EvalAstFactory implements AstFactory<Expression> {
}

getter(g: Expression, n: string): Getter {
const astFactoryInstance = this;

return {
type: 'Getter',
receiver: g,
name: n,
evaluate(scope) {
return this.receiver.evaluate(scope)?.[this.name];
return astFactoryInstance.getValue(this.receiver.evaluate(scope), this.name);
},
getIds(idents) {
this.receiver.getIds(idents);
Expand Down Expand Up @@ -267,12 +274,14 @@ export class EvalAstFactory implements AstFactory<Expression> {
}

index(e: Expression, a: Expression): Index {
const astFactoryInstance = this;

return {
type: 'Index',
receiver: e,
argument: a,
evaluate(scope) {
return this.receiver.evaluate(scope)?.[this.argument.evaluate(scope)];
return astFactoryInstance.getValue(this.receiver.evaluate(scope), this.argument.evaluate(scope));
},
getIds(idents) {
this.receiver.getIds(idents);
Expand Down