Skip to content

Commit

Permalink
🔥 remove type
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmai-dev committed Jun 9, 2024
1 parent c64b900 commit 6e52a62
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 31 deletions.
24 changes: 1 addition & 23 deletions Sources/Ast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@

import Foundation

protocol Statement {
var type: String { get }
}
protocol Statement {}

struct Program: Statement {
let type: String = "Program"
var body: [Statement] = []
}

Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions Sources/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down Expand Up @@ -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"
)
}

Expand All @@ -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"
)
}

Expand Down
9 changes: 4 additions & 5 deletions Sources/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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]))"
)
}
}
Expand Down Expand Up @@ -480,7 +480,6 @@ struct Interpreter {
step: step.value as? Int
)
)

}
else if let object = object as? StringValue {
return StringValue(
Expand Down Expand Up @@ -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)")
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6e52a62

Please sign in to comment.