mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
refactor: rewrite expression evaluation, fix #130
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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()!
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
import * as lexical from '../parser/lexical'
|
||||
import { assert } from '../util/assert'
|
||||
import { Context } from '../context/context'
|
||||
import { range, last, isFunction, toValue } from '../util/underscore'
|
||||
import { isComparable } from '../drop/icomparable'
|
||||
import { NullDrop } from '../drop/null-drop'
|
||||
import { EmptyDrop } from '../drop/empty-drop'
|
||||
import { BlankDrop } from '../drop/blank-drop'
|
||||
|
||||
const binaryOperators: {[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)
|
||||
}
|
||||
|
||||
export function parseExp (exp: string, ctx: Context): any {
|
||||
assert(ctx, 'unable to parseExp: scope undefined')
|
||||
const operatorREs = lexical.operators
|
||||
let match
|
||||
for (let i = 0; i < operatorREs.length; i++) {
|
||||
const operatorRE = operatorREs[i]
|
||||
const expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`)
|
||||
if ((match = exp.match(expRE))) {
|
||||
const l = parseExp(match[1], ctx)
|
||||
const op = binaryOperators[match[2].trim()]
|
||||
const r = parseExp(match[3], ctx)
|
||||
return op(l, r)
|
||||
}
|
||||
}
|
||||
|
||||
if ((match = exp.match(lexical.rangeLine))) {
|
||||
const low = evalValue(match[1], ctx)
|
||||
const high = evalValue(match[2], ctx)
|
||||
return range(+low, +high + 1)
|
||||
}
|
||||
|
||||
return parseValue(exp, ctx)
|
||||
}
|
||||
|
||||
export function evalExp (str: string, ctx: Context): any {
|
||||
return toValue(parseExp(str, ctx))
|
||||
}
|
||||
|
||||
export function parseValue (str: string | undefined, ctx: Context): any {
|
||||
if (!str) return null
|
||||
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)
|
||||
return ctx.get(str)
|
||||
}
|
||||
|
||||
export function evalValue (str: string | undefined, ctx: Context) {
|
||||
return toValue(parseValue(str, ctx))
|
||||
}
|
||||
|
||||
export function isTruthy (val: any): boolean {
|
||||
return !isFalsy(val)
|
||||
}
|
||||
|
||||
export function isFalsy (val: any): boolean {
|
||||
return val === false || undefined === val || val === null
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { toValue } from '../util/underscore'
|
||||
import { Context } from '../context/context'
|
||||
import { parseLiteral } from '../parser/literal'
|
||||
|
||||
export class Value {
|
||||
private str: string
|
||||
|
||||
public constructor (str: string = '') {
|
||||
this.str = str
|
||||
}
|
||||
|
||||
public evaluate (ctx: Context) {
|
||||
const literalValue = parseLiteral(this.str)
|
||||
if (literalValue !== undefined) {
|
||||
return literalValue
|
||||
}
|
||||
return ctx.get(this.str)
|
||||
}
|
||||
|
||||
public value (ctx: Context) {
|
||||
return toValue(this.evaluate(ctx))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user