-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrite expression evaluation, fix #130
- Loading branch information
Showing
31 changed files
with
407 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { last } from '../util/underscore' | ||
import { NullDrop } from '../drop/null-drop' | ||
import { EmptyDrop } from '../drop/empty-drop' | ||
import { BlankDrop } from '../drop/blank-drop' | ||
|
||
type literal = true | false | NullDrop | EmptyDrop | BlankDrop | number | string | ||
|
||
export function parseLiteral (str: string): literal | undefined { | ||
str = str.trim() | ||
|
||
if (str === 'true') return true | ||
if (str === 'false') return false | ||
if (str === 'nil' || str === 'null') return new NullDrop() | ||
if (str === 'empty') return new EmptyDrop() | ||
if (str === 'blank') return new BlankDrop() | ||
if (!isNaN(Number(str))) return Number(str) | ||
if ((str[0] === '"' || str[0] === "'") && str[0] === last(str)) return str.slice(1, -1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function isTruthy (val: any): boolean { | ||
return !isFalsy(val) | ||
} | ||
export function isFalsy (val: any): boolean { | ||
return val === false || undefined === val || val === null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { assert } from '../util/assert' | ||
import { isRange, rangeValue } from './range' | ||
import { Value } from './value' | ||
import { Context } from '../context/context' | ||
import { toValue } from '../util/underscore' | ||
import { isOperator, precedence, operatorImpls } from './operator' | ||
|
||
export class Expression { | ||
private str: string | ||
|
||
public constructor (str: string = '') { | ||
this.str = str | ||
} | ||
public evaluate (ctx: Context): any { | ||
assert(ctx, 'unable to evaluate: context not defined') | ||
|
||
const operands = [] | ||
for (const token of toPostfix(this.str)) { | ||
if (isOperator(token)) { | ||
const r = operands.pop() | ||
const l = operands.pop() | ||
const result = operatorImpls[token](l, r) | ||
operands.push(result) | ||
continue | ||
} | ||
if (isRange(token)) { | ||
operands.push(rangeValue(token, ctx)) | ||
continue | ||
} | ||
operands.push(new Value(token).evaluate(ctx)) | ||
} | ||
return operands[0] | ||
} | ||
public value (ctx: Context): any { | ||
return toValue(this.evaluate(ctx)) | ||
} | ||
} | ||
|
||
function * tokenize (expr: string): IterableIterator<string> { | ||
const N = expr.length | ||
let str = '' | ||
const pairs = { '"': '"', "'": "'", '[': ']', '(': ')' } | ||
|
||
for (let i = 0; i < N; i++) { | ||
const c = expr[i] | ||
switch (c) { | ||
case '[': | ||
case '"': | ||
case "'": | ||
str += c | ||
while (i + 1 < N) { | ||
str += expr[++i] | ||
if (expr[i] === pairs[c]) break | ||
} | ||
break | ||
case ' ': | ||
case '\t': | ||
case '\n': | ||
if (str) yield str | ||
str = '' | ||
break | ||
default: | ||
str += c | ||
} | ||
} | ||
if (str) yield str | ||
} | ||
|
||
function * toPostfix (expr: string): IterableIterator<string> { | ||
const ops = [] | ||
for (const token of tokenize(expr)) { | ||
if (isOperator(token)) { | ||
while (ops.length && precedence[ops[ops.length - 1]] > precedence[token]) { | ||
yield ops.pop()! | ||
} | ||
ops.push(token) | ||
} else yield token | ||
} | ||
while (ops.length) { | ||
yield ops.pop()! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { isComparable } from '../drop/icomparable' | ||
import { isFunction } from '../util/underscore' | ||
import { isTruthy } from '../render/boolean' | ||
|
||
export const precedence = { | ||
'==': 1, | ||
'!=': 1, | ||
'>': 1, | ||
'<': 1, | ||
'>=': 1, | ||
'<=': 1, | ||
'contains': 1, | ||
'and': 0, | ||
'or': 0 | ||
} | ||
|
||
export const operatorImpls: {[key: string]: (lhs: any, rhs: any) => boolean} = { | ||
'==': (l: any, r: any) => { | ||
if (isComparable(l)) return l.equals(r) | ||
if (isComparable(r)) return r.equals(l) | ||
return l === r | ||
}, | ||
'!=': (l: any, r: any) => { | ||
if (isComparable(l)) return !l.equals(r) | ||
if (isComparable(r)) return !r.equals(l) | ||
return l !== r | ||
}, | ||
'>': (l: any, r: any) => { | ||
if (isComparable(l)) return l.gt(r) | ||
if (isComparable(r)) return r.lt(l) | ||
return l > r | ||
}, | ||
'<': (l: any, r: any) => { | ||
if (isComparable(l)) return l.lt(r) | ||
if (isComparable(r)) return r.gt(l) | ||
return l < r | ||
}, | ||
'>=': (l: any, r: any) => { | ||
if (isComparable(l)) return l.geq(r) | ||
if (isComparable(r)) return r.leq(l) | ||
return l >= r | ||
}, | ||
'<=': (l: any, r: any) => { | ||
if (isComparable(l)) return l.leq(r) | ||
if (isComparable(r)) return r.geq(l) | ||
return l <= r | ||
}, | ||
'contains': (l: any, r: any) => { | ||
return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false | ||
}, | ||
'and': (l: any, r: any) => isTruthy(l) && isTruthy(r), | ||
'or': (l: any, r: any) => isTruthy(l) || isTruthy(r) | ||
} | ||
|
||
const list = Object.keys(precedence) | ||
|
||
export function isOperator (token: string) { | ||
return list.includes(token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { rangeLine } from '../parser/lexical' | ||
import { Context } from '../context/context' | ||
import { range } from '../util/underscore' | ||
import { Value } from './value' | ||
|
||
export function isRange (token: string = '') { | ||
return token[0] === '(' && token[token.length - 1] === ')' | ||
} | ||
|
||
export function rangeValue (token: string = '', ctx: Context) { | ||
let match | ||
if ((match = token.match(rangeLine))) { | ||
const low = new Value(match[1]).value(ctx) | ||
const high = new Value(match[2]).value(ctx) | ||
return range(+low, +high + 1) | ||
} | ||
} |
Oops, something went wrong.