Skip to content

Commit

Permalink
Add new chat template test case && fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmai-dev committed Mar 24, 2024
1 parent f94dcd5 commit d2e467d
Show file tree
Hide file tree
Showing 6 changed files with 633 additions and 99 deletions.
43 changes: 43 additions & 0 deletions Sources/Jinja/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class Environment {
self.parent = parent
}

func isFunction<T>(_ value: Any, functionType: T.Type) -> Bool {
value is T
}

func convertToRuntimeValues(input: Any) throws -> any RuntimeValue {
switch input {
case let value as Bool:
Expand All @@ -105,6 +109,45 @@ class Environment {
return NumericValue(value: value)
case let value as String:
return StringValue(value: value)
case let fn as (String) throws -> Void:
return FunctionValue { args, _ in
var arg = ""
switch args[0].value {
case let value as String:
arg = value
case let value as Bool:
arg = String(value)
default:
throw JinjaError.runtimeError("Unknown arg type:\(type(of: args[0].value))")
}

try fn(arg)
return NullValue()
}
case let fn as (Bool) throws -> Void:
return FunctionValue { args, _ in
try fn(args[0].value as! Bool)
return NullValue()
}
case let fn as (Int, Int?, Int) -> [Int]:
return FunctionValue { args, _ in
let result = fn(args[0].value as! Int, args[1].value as? Int, args[2].value as! Int)
return try self.convertToRuntimeValues(input: result)
}
case let values as [Any]:
var items: [any RuntimeValue] = []
for value in values {
try items.append(self.convertToRuntimeValues(input: value))
}
return ArrayValue(value: items)
case let dictionary as [String: String]:
var object: [String: any RuntimeValue] = [:]

for (key, value) in dictionary {
object[key] = StringValue(value: value)
}

return ObjectValue(value: object)
default:
throw JinjaError.runtimeError("Cannot convert to runtime value: \(input) type:\(type(of: input))")
}
Expand Down
18 changes: 12 additions & 6 deletions Sources/Jinja/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func parse(tokens: [Token]) throws -> Program {
throw JinjaError.syntaxError("Expected 0-3 arguments for slice expression")
}

return SliceExpression(start: slices[0] as? Expression, stop: slices[1] as? Expression, step: slices[2] as? Expression)
return SliceExpression(
start: slices[0] as? Expression,
stop: slices.count > 1 ? slices[1] as? Expression : nil,
step: slices.count > 2 ? slices[2] as? Expression : nil
)
}

return slices[0]!
Expand Down Expand Up @@ -243,9 +247,11 @@ func parse(tokens: [Token]) throws -> Program {
right = UnaryExpression(operation: operation, argument: argument as! Expression)
}

let expression = try parseComparisonExpression()

return right ?? expression
if let right {
return right
} else {
return try parseComparisonExpression()
}
}

func parseLogicalAndExpression() throws -> Statement {
Expand Down Expand Up @@ -351,7 +357,7 @@ func parse(tokens: [Token]) throws -> Program {
switch token.type {
case .numericLiteral:
current += 1
return NumericLiteral(value: token.value as! (any Numeric))
return NumericLiteral(value: Int(token.value) ?? 0)
case .stringLiteral:
current += 1
return StringLiteral(value: token.value)
Expand Down Expand Up @@ -440,7 +446,7 @@ func parse(tokens: [Token]) throws -> Program {
_ = try expect(type: .in, error: "Expected `in` keyword following loop variable")

let iterable = try parseExpression()

_ = try expect(type: .closeStatement, error: "Expected closing statement token")

var body: [Statement] = []
Expand Down
Loading

0 comments on commit d2e467d

Please sign in to comment.