Skip to content

Commit

Permalink
refactor: remove src/render/value.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 10, 2020
1 parent b689869 commit e57c919
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/parser/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Tokenizer {

readFilterArg (): FilterArg | null {
const key = this.readValue()
if (!key) return null
if (!key.size()) return null
this.readBlank()
if (this.peek() === ':') {
this.read()
Expand Down
26 changes: 21 additions & 5 deletions src/render/expression.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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) {
Expand All @@ -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]
}
Expand All @@ -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 changes: 0 additions & 17 deletions src/render/range.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/render/value.ts

This file was deleted.

24 changes: 0 additions & 24 deletions test/unit/render/value.ts

This file was deleted.

0 comments on commit e57c919

Please sign in to comment.