refactor: remove src/render/value.ts

This commit is contained in:
harttle
2020-03-14 19:04:18 +08:00
committed by Jun Yang
parent 3dfdf982c9
commit f7ef9b61c1
5 changed files with 22 additions and 70 deletions
+21 -5
View File
@@ -1,8 +1,8 @@
import { assert } from '../util/assert'
import { isRange, rangeValue } from './range'
import { Value } from './value'
import { rangeLine } from '../parser/lexical'
import { parseLiteral } from '../parser/literal'
import { Context } from '../context/context'
import { toValue } from '../util/underscore'
import { range, toValue } from '../util/underscore'
import { isOperator, precedence, operatorImpls } from './operator'
import { Tokenizer } from '../parser/tokenizer'
@@ -14,7 +14,7 @@ export class Expression {
const tokenizer = new Tokenizer(str)
this.postfix = [...toPostfix(tokenizer.readExpression())]
}
public * evaluate (ctx: Context) {
public * evaluate (ctx: Context): any {
assert(ctx, 'unable to evaluate: context not defined')
for (const token of this.postfix) {
@@ -25,7 +25,10 @@ export class Expression {
this.operands.push(result)
} else if (isRange(token)) {
this.operands.push(yield rangeValue(token, ctx))
} else this.operands.push(yield new Value(token).evaluate(ctx))
} else {
const literal = parseLiteral(token)
this.operands.push(literal !== undefined ? literal : yield ctx.get(token))
}
}
return this.operands[0]
}
@@ -48,3 +51,16 @@ function * toPostfix (tokens: IterableIterator<string>): IterableIterator<string
yield ops.pop()!
}
}
function * rangeValue (token: string, ctx: Context) {
let match
if ((match = token.match(rangeLine))) {
const low = yield new Expression(match[1]).value(ctx)
const high = yield new Expression(match[2]).value(ctx)
return range(+low, +high + 1)
}
}
function isRange (str: string) {
return rangeLine.test(str)
}
-17
View File
@@ -1,17 +0,0 @@
import { rangeLine } from '../parser/lexical'
import { Context } from '../context/context'
import { range } from '../util/underscore'
import { Value } from './value'
export function isRange (str: string) {
return rangeLine.test(str)
}
export function * rangeValue (token: string, ctx: Context) {
let match
if ((match = token.match(rangeLine))) {
const low = yield new Value(match[1]).value(ctx)
const high = yield new Value(match[2]).value(ctx)
return range(+low, +high + 1)
}
}
-23
View File
@@ -1,23 +0,0 @@
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))
}
}