Parse variables as expressions

removed static boolean expressions
added test for rendering template with boolean expression
This commit is contained in:
Ilya Puchka
2017-12-23 22:01:04 +01:00
committed by David Jennes
parent 242bea54c3
commit 6649b7e716
5 changed files with 54 additions and 24 deletions

View File

@@ -1,7 +1,13 @@
public protocol Expression: CustomStringConvertible {
public protocol Expression: CustomStringConvertible, Resolvable {
func evaluate(context: Context) throws -> Bool
}
extension Expression {
func resolve(_ context: Context) throws -> Any? {
try "\(evaluate(context: context))"
}
}
protocol InfixOperator: Expression {
init(lhs: Expression, rhs: Expression)
}
@@ -37,8 +43,12 @@ final class VariableExpression: Expression, CustomStringConvertible {
"(variable: \(variable))"
}
func resolve(_ context: Context) throws -> Any? {
try variable.resolve(context)
}
/// Resolves a variable in the given context as boolean
func resolve(context: Context, variable: Resolvable) throws -> Bool {
func evaluate(context: Context) throws -> Bool {
let result = try variable.resolve(context)
var truthy = false
@@ -58,10 +68,6 @@ final class VariableExpression: Expression, CustomStringConvertible {
return truthy
}
func evaluate(context: Context) throws -> Bool {
try resolve(context: context, variable: variable)
}
}
final class NotExpression: Expression, PrefixOperator, CustomStringConvertible {