Skip to content

Commit

Permalink
Refactored type guard selection for reuse.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Oct 24, 2023
1 parent 5e9b0cb commit d084e81
Show file tree
Hide file tree
Showing 46 changed files with 168 additions and 140 deletions.
2 changes: 1 addition & 1 deletion src/basis/InternalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class InternalExpression extends SimpleExpression {
return this;
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
Expand Down
2 changes: 1 addition & 1 deletion src/basis/Iteration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class Iteration<State = any> extends Expression {
return this;
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
Expand Down
14 changes: 7 additions & 7 deletions src/nodes/BinaryEvaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export default class BinaryEvaluate extends Expression {
/**
* Type checks narrow the set to the specified type, if contained in the set and if the check is on the same bind.
* */
evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
Expand All @@ -343,13 +343,13 @@ export default class BinaryEvaluate extends Expression {
// If conjunction, then we compute the intersection of the left and right's possible types.
// Note that we pass the left's possible types because we don't evaluate the right if the left isn't true.
if (this.getOperator() === AND_SYMBOL) {
const left = this.left.evaluateTypeSet(
const left = this.left.evaluateTypeGuards(
bind,
original,
current,
context
);
const right = this.right.evaluateTypeSet(
const right = this.right.evaluateTypeGuards(
bind,
original,
left,
Expand All @@ -360,13 +360,13 @@ export default class BinaryEvaluate extends Expression {
// If disjunction of type checks, then we return the union.
// Note that we pass the left's possible types because we don't evaluate the right if the left is true.
else if (this.getOperator() === OR_SYMBOL) {
const left = this.left.evaluateTypeSet(
const left = this.left.evaluateTypeGuards(
bind,
original,
current,
context
);
const right = this.right.evaluateTypeSet(
const right = this.right.evaluateTypeGuards(
bind,
original,
left,
Expand Down Expand Up @@ -433,8 +433,8 @@ export default class BinaryEvaluate extends Expression {
}

// Otherwise, just pass the types down and return the original types.
this.left.evaluateTypeSet(bind, original, current, context);
this.right.evaluateTypeSet(bind, original, current, context);
this.left.evaluateTypeGuards(bind, original, current, context);
this.right.evaluateTypeGuards(bind, original, current, context);
return current;
}

Expand Down
4 changes: 2 additions & 2 deletions src/nodes/Bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ export default class Bind extends Expression {
return true;
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
): TypeSet {
return this.value === undefined
? current
: this.value.evaluateTypeSet(bind, original, current, context);
: this.value.evaluateTypeGuards(bind, original, current, context);
}

hasName(name: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export default class Block extends Expression {
* Blocks don't do any type checks, but we do have them delegate type checks to their final expression.
* since we use them for parentheticals in boolean logic.
* */
evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
Expand All @@ -316,7 +316,7 @@ export default class Block extends Expression {
if (this.statements.length === 0) return current;
const last = this.statements[this.statements.length - 1];
return last instanceof Expression
? last.evaluateTypeSet(bind, original, current, context)
? last.evaluateTypeGuards(bind, original, current, context)
: current;
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/BooleanLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class BooleanLiteral extends Literal {
return this.value.text.toString() === TRUE_SYMBOL;
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/Borrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export default class Borrow extends SimpleExpression {
: definition.getType(context);
}

evaluateTypeSet(_: Bind, __: TypeSet, current: TypeSet): TypeSet {
evaluateTypeGuards(_: Bind, __: TypeSet, current: TypeSet): TypeSet {
return current;
}

Expand Down
4 changes: 2 additions & 2 deletions src/nodes/Changed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ export default class Changed extends SimpleExpression {
return new BoolValue(this, evaluator.didStreamCauseReaction(stream));
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
if (this.stream instanceof Expression)
this.stream.evaluateTypeSet(bind, original, current, context);
this.stream.evaluateTypeGuards(bind, original, current, context);
return current;
}

Expand Down
8 changes: 4 additions & 4 deletions src/nodes/Conditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ export default class Conditional extends Expression {
/**
* Type checks narrow the set to the specified type, if contained in the set and if the check is on the same bind.
* */
evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
// Evaluate the condition with the current types.
const revisedTypes = this.condition.evaluateTypeSet(
const revisedTypes = this.condition.evaluateTypeGuards(
bind,
original,
current,
Expand All @@ -194,11 +194,11 @@ export default class Conditional extends Expression {

// Evaluate the yes branch with the revised types.
if (this.yes instanceof Expression)
this.yes.evaluateTypeSet(bind, original, revisedTypes, context);
this.yes.evaluateTypeGuards(bind, original, revisedTypes, context);

// Evaluate the no branch with the complement of the revised types, unless they weren't guarded, in which case we pass through the current types.
if (this.no instanceof Expression) {
this.no.evaluateTypeSet(
this.no.evaluateTypeGuards(
bind,
original,
guarded ? current.difference(revisedTypes, context) : current,
Expand Down
9 changes: 7 additions & 2 deletions src/nodes/ConversionDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,19 @@ export default class ConversionDefinition extends DefinitionExpression {
return value;
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
if (this.expression instanceof Expression)
this.expression.evaluateTypeSet(bind, original, current, context);
this.expression.evaluateTypeGuards(
bind,
original,
current,
context
);
return current;
}

Expand Down
9 changes: 7 additions & 2 deletions src/nodes/Convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,19 @@ export default class Convert extends Expression {
return evaluator.popValue(this);
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
if (this.expression instanceof Expression)
this.expression.evaluateTypeSet(bind, original, current, context);
this.expression.evaluateTypeGuards(
bind,
original,
current,
context
);
return current;
}

Expand Down
6 changes: 3 additions & 3 deletions src/nodes/Delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,16 @@ export default class Delete extends Expression {
return new TableValue(this, table.type, list);
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
if (this.table instanceof Expression)
this.table.evaluateTypeSet(bind, original, current, context);
this.table.evaluateTypeGuards(bind, original, current, context);
if (this.query instanceof Expression)
this.query.evaluateTypeSet(bind, original, current, context);
this.query.evaluateTypeGuards(bind, original, current, context);
return current;
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/DocumentedExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class DocumentedExpression extends SimpleExpression {
) as this;
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/Evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,17 +898,17 @@ export default class Evaluate extends Expression {
return bindings;
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
if (this.fun instanceof Expression)
this.fun.evaluateTypeSet(bind, original, current, context);
this.fun.evaluateTypeGuards(bind, original, current, context);
this.inputs.forEach((input) => {
if (input instanceof Expression)
input.evaluateTypeSet(bind, original, current, context);
input.evaluateTypeGuards(bind, original, current, context);
});
return current;
}
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default abstract class Expression extends Node {
* Used to determine what types are possible for a given after evaluating this expression, implementing type guards.
* Most expressions do not manipulate possible types at all; primarily is just logical operators and type checks.
* */
abstract evaluateTypeSet(
abstract evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/ExpressionPlaceholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default class ExpressionPlaceholder extends SimpleExpression {
return new UnimplementedException(evaluator, this);
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/FormattedLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default class FormattedLiteral extends Literal {
return FormattedType.make();
}

evaluateTypeSet(_: Bind, __: TypeSet, current: TypeSet): TypeSet {
evaluateTypeGuards(_: Bind, __: TypeSet, current: TypeSet): TypeSet {
return current;
}

Expand Down
9 changes: 7 additions & 2 deletions src/nodes/FunctionDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,19 @@ export default class FunctionDefinition extends DefinitionExpression {
);
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
if (this.expression !== undefined)
this.expression.evaluateTypeSet(bind, original, current, context);
this.expression.evaluateTypeGuards(
bind,
original,
current,
context
);
return current;
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/Initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class Initial extends SimpleExpression {
return new BoolValue(this, evaluator.isInitialEvaluation());
}

evaluateTypeSet(_: Bind, __: TypeSet, current: TypeSet) {
evaluateTypeGuards(_: Bind, __: TypeSet, current: TypeSet) {
return current;
}

Expand Down
6 changes: 3 additions & 3 deletions src/nodes/Insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,16 @@ export default class Insert extends Expression {
return row instanceof StructureValue ? table.insert(this, row) : row;
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
if (this.table instanceof Expression)
this.table.evaluateTypeSet(bind, original, current, context);
this.table.evaluateTypeGuards(bind, original, current, context);
if (this.row instanceof Expression)
this.row.evaluateTypeSet(bind, original, current, context);
this.row.evaluateTypeGuards(bind, original, current, context);
return current;
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/Is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class Is extends Expression {
/**
* Type checks narrow the set to the specified type, if contained in the set and if the check is on the same bind.
* */
evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
_: TypeSet,
current: TypeSet,
Expand Down
6 changes: 5 additions & 1 deletion src/nodes/IsLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ export default class IsLocale extends SimpleExpression {
getDependencies(): Expression[] {
return [];
}
evaluateTypeSet(bind: Bind, original: TypeSet, current: TypeSet): TypeSet {
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet
): TypeSet {
return current;
}

Expand Down
6 changes: 3 additions & 3 deletions src/nodes/ListAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ export default class ListAccess extends Expression {
return list.get(index);
}

evaluateTypeSet(
evaluateTypeGuards(
bind: Bind,
original: TypeSet,
current: TypeSet,
context: Context
) {
if (this.list instanceof Expression)
this.list.evaluateTypeSet(bind, original, current, context);
this.list.evaluateTypeGuards(bind, original, current, context);
if (this.index instanceof Expression)
this.index.evaluateTypeSet(bind, original, current, context);
this.index.evaluateTypeGuards(bind, original, current, context);
return current;
}

Expand Down
Loading

0 comments on commit d084e81

Please sign in to comment.