Skip to content

Commit

Permalink
expression cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Deubler <[email protected]>
  • Loading branch information
TerminalTim committed Jul 19, 2024
1 parent 6e00a9a commit 754d7b1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 144 deletions.
29 changes: 0 additions & 29 deletions packages/common/src/Expressions/ConditionalExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,6 @@ export class CaseExpression extends Expression {
super(json, env);
}

// override clone(jsonExp) {
// let c = new this.constructor(jsonExp || this.toJSON(), this.env);
// c.runtime = this.runtime;
// return c;
// }


__dynamic(context?): boolean | Expression {
const operands: JSONExpression = [this.json[0]];
let partial = false;
let dynamic: false | Expression = false;
for (let i = 1, {json} = this, len = json.length; i < len; i++) {
let o = this.compileOperand(i);
if (Expression.isExpression(o)) {
let isDynamic = o.dynamic(context);
if (isDynamic) {
dynamic = this;
let isClonedExpression = typeof isDynamic === 'object' && isDynamic != o;
if (isClonedExpression) {
partial = true;
o = isDynamic;
}
}
}
operands[i] = o;
}
return partial ? this.clone(operands) : dynamic;
}

dynamic(context: Context): false | Expression {
const operands: JSONExpression = [this.json[0]];
let partial = false;
Expand Down
55 changes: 1 addition & 54 deletions packages/common/src/Expressions/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ export abstract class Expression implements IExpression {
}

static isDynamicExpression(exp: Expression, context: Context): false | Expression {
// return false;
// return this.isExpression(exp) && false;
return this.isExpression(exp) ? exp.dynamic(context) : false;
return this.isExpression(exp) && exp.dynamic(context);
}

protected env: ExpressionParser;

json: JSONExpression;

supportsPartialEval: boolean;

constructor(json: JSONExpression, env: ExpressionParser) {
// this.id = ++expId;
this.json = json;
Expand Down Expand Up @@ -90,55 +86,6 @@ export abstract class Expression implements IExpression {
return partial ? this.clone(operands) : dynamic;
}

// dynamic(context?): boolean | JSONExpression {
// const operands: JSONExpression = [this.json[0]];
// let partial = false;
// let partialId = '';
// let dynamic = false;
//
// for (let i = 1, {json} = this, len = json.length; i < len; i++) {
// let o = this.compileOperand(i);
// // let isDynamic = Expression.isDynamicExpression(o, context);
// if (Expression.isExpression(o)) {
// let isDynamic = o.dynamic(context);
// if (isDynamic) {
// dynamic = true;
// if (isDynamic!=true) {
// partial = true;
// o = isDynamic;
// // partialId += o.getId();
// } else
// if (o.supportsPartialEval) {
// partial = true;
// o = o.eval(context);
// // partialId += (Expression.isExpression(o) ? 'p' + o.getId() : 'v' + o);
// }
// }
// }
//
// operands[i] = o;
// }
//
// if (partial) {
// let exp = this.clone(operands);
// // exp.id = this.getId();
// // exp.id = this.getId() + partialId;
// // console.log(exp.id);
// // debugger;
// return exp;
// }
// return dynamic;
// // return partialResult ? this.clone(operands) : dynamic;
//
// // for (let i = 1, {json} = this, len = json.length; i < len; i++) {
// // let exp = this.compileOperand(i);
// // if (Expression.isDynamicExpression(exp)) {
// // return true;
// // }
// // }
// // return false;
// }

protected compileOperand(index: number) {
return this.json[index] = this.env.parseJSON(this.json[index]);
}
Expand Down
33 changes: 17 additions & 16 deletions packages/common/src/Expressions/LogicalExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class AnyExpression extends Expression {

export class isFalseExpression extends Expression {
static operator = '!';

eval(context) {
let val = this.operand(1, context);
return !val;
Expand Down Expand Up @@ -96,22 +97,22 @@ export class NoneExpression extends Expression {
}


class CompareExpression extends Expression {
// dynamic(): boolean {
// const a = this.compileOperand(1);
// if (Expression.isDynamicExpression(a)) {
// return true;
// }
// const b = this.compileOperand(2);
// if (Expression.isDynamicExpression(b)) {
// return true;
// }
// return false;
// }

eval(context) {
}
}
const CompareExpression = Expression;
// class CompareExpression extends Expression {
// dynamic(context: Context): false|Expression {
// const a = this.compileOperand(1);
// if (Expression.isDynamicExpression(a, context)) {
// return this;
// }
// const b = this.compileOperand(2);
// if (Expression.isDynamicExpression(b, context)) {
// return this;
// }
// return false;
// }
// eval(context) {
// }
// }

export class EqualsExpression extends CompareExpression {
static operator = '==';
Expand Down
49 changes: 4 additions & 45 deletions packages/common/src/Expressions/MathExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,28 @@
* License-Filename: LICENSE
*/

import {Expression} from './Expression';
import {Context, Expression} from './Expression';

const SimpleOperatorExpression = Expression;
// class SimpleOperatorExpression extends Expression {
// dynamic(): boolean {
// return Expression.isDynamicExpression(this.compileOperand(1)) ||
// Expression.isDynamicExpression(this.compileOperand(2));
// dynamic(context: Context): false|Expression {
// return Expression.isDynamicExpression(this.compileOperand(1), context) ||
// Expression.isDynamicExpression(this.compileOperand(2), context);
// }
//
// eval(context) {
// }
// }

export class SumExpression extends SimpleOperatorExpression {
static operator = '+';

// dynamic(context?): boolean | JSONExpression {
// return super.dynamic(context);
// }

// dynamic(context): boolean|JSONExpression {
// // for (let i = 1, {json} = this, len = json.length; i < len; i++) {
// // let exp = this.compileOperand(i);
// // if (Expression.isDynamicExpression(exp)) {
// // if (exp.supportsPartialEval) {
// //
// // }
// // }
// // }
//
// const operands: JSONExpression = [this.json[0]];
// let partialResult = false;
// let dynamic = false;
// for (let i = 1, {json} = this, len = json.length; i < len; i++) {
// let o = this.compileOperand(i);
// if (Expression.isDynamicExpression(o)) {
// dynamic = true;
// if (o.supportsPartialEval) {
// partialResult = true;
// o = o.eval(context);
// }
// }
// operands[i] = o;
// }
// return partialResult ? operands : dynamic;
// }

eval(context) {
const {json} = this;
// this.env._expressionRequiresLiveMode ||= this;
let sum = 0;
// max 17.5ms
for (let i = 1, {length} = json; i < length; i++) {
let value = this.operand(i, context);

// if (this.dynamicInterrupt) {
// debugger;
// return sum;
// }

sum += Number(value) || 0;
}
// this.env._expressionRequiresLiveMode = null;
return sum;
}
}
Expand Down

0 comments on commit 754d7b1

Please sign in to comment.