diff --git a/Sources/Ast.swift b/Sources/Ast.swift index 89c2cf1..3789a2c 100644 --- a/Sources/Ast.swift +++ b/Sources/Ast.swift @@ -7,12 +7,9 @@ import Foundation -protocol Statement { - var type: String { get } -} +protocol Statement {} struct Program: Statement { - let type: String = "Program" var body: [Statement] = [] } @@ -24,50 +21,41 @@ protocol Literal: Expression { } struct StringLiteral: Literal { - let type: String = "StringLiteral" var value: String } struct NumericLiteral: Literal { - let type: String = "NumericLiteral" var value: any Numeric } struct BoolLiteral: Literal { - let type: String = "BoolLiteral" var value: Bool } struct ArrayLiteral: Literal { - let type: String = "ArrayLiteral" var value: [Expression] } struct TupleLiteral: Literal { - let type: String = "TupleLiteral" var value: [Expression] } struct ObjectLiteral: Literal { - let type: String = "ObjectLiteral" var value: [(Expression, Expression)] } struct Set: Statement { - let type: String = "Set" var assignee: Expression var value: Expression } struct If: Statement { - let type: String = "If" var test: Expression var body: [Statement] var alternate: [Statement] } struct Identifier: Expression { - let type: String = "Identifier" var value: String } @@ -76,27 +64,23 @@ extension Identifier: Loopvar {} extension TupleLiteral: Loopvar {} struct For: Statement { - let type: String = "For" var loopvar: Loopvar var iterable: Expression var body: [Statement] } struct MemberExpression: Expression { - let type: String = "MemberExpression" var object: Expression var property: Expression var computed: Bool } struct CallExpression: Expression { - let type: String = "CallExpression" var callee: Expression var args: [Expression] } struct BinaryExpression: Expression { - let type: String = "BinaryExpression" var operation: Token var left: Expression var right: Expression @@ -107,38 +91,32 @@ extension Identifier: Filter {} extension CallExpression: Filter {} struct FilterExpression: Expression { - let type: String = "FilterExpression" var operand: Expression var filter: Filter } struct TestExpression: Expression { - let type: String = "TestExpression" var operand: Expression var negate: Bool var test: Identifier } struct UnaryExpression: Expression { - let type: String = "UnaryExpression" var operation: Token var argument: Expression } struct LogicalNegationExpression: Expression { - let type: String = "LogicalNegationExpression" var argument: Expression } struct SliceExpression: Expression { - let type: String = "SliceExpression" var start: Expression? var stop: Expression? var step: Expression? } struct KeywordArgumentExpression: Expression { - let type: String = "KeywordArgumentExpression" var key: Identifier var value: any Expression } diff --git a/Sources/Parser.swift b/Sources/Parser.swift index 7440bf1..aedbb69 100644 --- a/Sources/Parser.swift +++ b/Sources/Parser.swift @@ -134,7 +134,7 @@ func parse(tokens: [Token]) throws -> Program { } else { property = try parsePrimaryExpression() - if property.type != "Identifier" { + if !(property is Identifier) { throw JinjaError.syntaxError("Expected identifier following dot operator") } } @@ -450,7 +450,7 @@ func parse(tokens: [Token]) throws -> Program { if !(loopVariable is Identifier || loopVariable is TupleLiteral) { throw JinjaError.syntaxError( - "Expected identifier/tuple for the loop variable, got \(loopVariable.type) instead" + "Expected identifier/tuple for the loop variable, got \(type(of:loopVariable)) instead" ) } @@ -470,7 +470,7 @@ func parse(tokens: [Token]) throws -> Program { } throw JinjaError.syntaxError( - "Expected identifier/tuple for the loop variable, got \(loopVariable.type) instead" + "Expected identifier/tuple for the loop variable, got \(type(of:loopVariable)) instead" ) } diff --git a/Sources/Runtime.swift b/Sources/Runtime.swift index c0e981b..e227909 100644 --- a/Sources/Runtime.swift +++ b/Sources/Runtime.swift @@ -235,7 +235,7 @@ struct Interpreter { } } else { - throw JinjaError.runtimeError("Invalid assignee type: \(node.assignee.type)") + throw JinjaError.runtimeError("Invalid assignee type: \(type(of: node.assignee))") } return NullValue() @@ -297,7 +297,7 @@ struct Interpreter { } else { throw JinjaError.runtimeError( - "Cannot unpack non-identifier type: \(tupleLiteral.value[j].type)" + "Cannot unpack non-identifier type: \(type(of:tupleLiteral.value[j]))" ) } } @@ -480,7 +480,6 @@ struct Interpreter { step: step.value as? Int ) ) - } else if let object = object as? StringValue { return StringValue( @@ -570,7 +569,7 @@ struct Interpreter { return BooleanValue(value: !argument.value) } - throw JinjaError.syntaxError("Unknown argument type: \(node.argument.type)") + throw JinjaError.syntaxError("Unknown argument type: \(type(of:node.argument))") default: throw JinjaError.syntaxError("Unknown operator: \(node.operation.value)") } @@ -707,7 +706,7 @@ struct Interpreter { case let statement as FilterExpression: return try self.evaluateFilterExpression(node: statement, environment: environment) default: - throw JinjaError.runtimeError("Unknown node type: \(statement.type)") + throw JinjaError.runtimeError("Unknown node type: \(type(of:statement))") } } else {