Skip to content

Commit

Permalink
Merge pull request #241 from franklinsch/visit-literals
Browse files Browse the repository at this point in the history
Visit literal tokens in AST Passes
  • Loading branch information
franklinsch authored Jul 6, 2018
2 parents aee50c0 + 3845052 commit 8b163ba
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Sources/AST/ASTPass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public protocol ASTPass {
func process(functionCall: FunctionCall, passContext: ASTPassContext) -> ASTPassResult<FunctionCall>
func process(arrayLiteral: ArrayLiteral, passContext: ASTPassContext) -> ASTPassResult<ArrayLiteral>
func process(dictionaryLiteral: DictionaryLiteral, passContext: ASTPassContext) -> ASTPassResult<DictionaryLiteral>
func process(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token>
func process(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression>
func process(returnStatement: ReturnStatement, passContext: ASTPassContext) -> ASTPassResult<ReturnStatement>
func process(ifStatement: IfStatement, passContext: ASTPassContext) -> ASTPassResult<IfStatement>
Expand Down Expand Up @@ -60,6 +61,7 @@ public protocol ASTPass {
func postProcess(functionCall: FunctionCall, passContext: ASTPassContext) -> ASTPassResult<FunctionCall>
func postProcess(arrayLiteral: ArrayLiteral, passContext: ASTPassContext) -> ASTPassResult<ArrayLiteral>
func postProcess(dictionaryLiteral: DictionaryLiteral, passContext: ASTPassContext) -> ASTPassResult<DictionaryLiteral>
func postProcess(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token>
func postProcess(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression>
func postProcess(returnStatement: ReturnStatement, passContext: ASTPassContext) -> ASTPassResult<ReturnStatement>
func postProcess(ifStatement: IfStatement, passContext: ASTPassContext) -> ASTPassResult<IfStatement>
Expand Down Expand Up @@ -164,6 +166,10 @@ public struct AnyASTPass: ASTPass {
return base.process(dictionaryLiteral: dictionaryLiteral, passContext: passContext)
}

public func process(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return base.process(literalToken: literalToken, passContext: passContext)
}

public func process(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
return base.process(subscriptExpression: subscriptExpression, passContext: passContext)
}
Expand Down Expand Up @@ -266,7 +272,11 @@ public struct AnyASTPass: ASTPass {
}

public func postProcess(dictionaryLiteral: DictionaryLiteral, passContext: ASTPassContext) -> ASTPassResult<DictionaryLiteral> {
return base.process(dictionaryLiteral: dictionaryLiteral, passContext: passContext)
return base.postProcess(dictionaryLiteral: dictionaryLiteral, passContext: passContext)
}

public func postProcess(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return base.process(literalToken: literalToken, passContext: passContext)
}

public func postProcess(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
Expand Down
10 changes: 9 additions & 1 deletion Sources/AST/ASTVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ public struct ASTVisitor<Pass: ASTPass> {
processResult.element = .dictionaryLiteral(processResult.combining(visit(dictionaryLiteral, passContext: processResult.passContext)))
case .identifier(let identifier):
processResult.element = .identifier(processResult.combining(visit(identifier, passContext: processResult.passContext)))
case .literal(_), .self(_): break
case .literal(let literalToken):
processResult.element = .literal(processResult.combining(visit(literalToken, passContext: processResult.passContext)))
case .self(_): break
case .variableDeclaration(let variableDeclaration):
processResult.element = .variableDeclaration(processResult.combining(visit(variableDeclaration, passContext: processResult.passContext)))
case .subscriptExpression(let subscriptExpression):
Expand Down Expand Up @@ -416,6 +418,12 @@ public struct ASTVisitor<Pass: ASTPass> {
return ASTPassResult(element: postProcessResult.element, diagnostics: processResult.diagnostics + postProcessResult.diagnostics, passContext: postProcessResult.passContext)
}

func visit(_ literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
let processResult = pass.process(literalToken: literalToken, passContext: passContext)
let postProcessResult = pass.process(literalToken: processResult.element, passContext: processResult.passContext)
return ASTPassResult(element: postProcessResult.element, diagnostics: postProcessResult.diagnostics, passContext: passContext)
}

func visit(_ subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
var processResult = pass.process(subscriptExpression: subscriptExpression, passContext: passContext)

Expand Down
8 changes: 8 additions & 0 deletions Sources/IRGen/IULIAPreprocessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ public struct IULIAPreprocessor: ASTPass {
return ASTPassResult(element: dictionaryLiteral, diagnostics: [], passContext: passContext)
}

public func process(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return ASTPassResult(element: literalToken, diagnostics: [], passContext: passContext)
}

public func process(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
return ASTPassResult(element: subscriptExpression, diagnostics: [], passContext: passContext)
}
Expand Down Expand Up @@ -481,6 +485,10 @@ public struct IULIAPreprocessor: ASTPass {
return ASTPassResult(element: dictionaryLiteral, diagnostics: [], passContext: passContext)
}

public func postProcess(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return ASTPassResult(element: literalToken, diagnostics: [], passContext: passContext)
}

public func postProcess(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
return ASTPassResult(element: subscriptExpression, diagnostics: [], passContext: passContext)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/Optimizer/Optimizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public struct Optimizer: ASTPass {
return ASTPassResult(element: dictionaryLiteral, diagnostics: [], passContext: passContext)
}

public func process(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return ASTPassResult(element: literalToken, diagnostics: [], passContext: passContext)
}

public func process(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
return ASTPassResult(element: subscriptExpression, diagnostics: [], passContext: passContext)
}
Expand Down Expand Up @@ -207,6 +211,10 @@ public struct Optimizer: ASTPass {
return ASTPassResult(element: dictionaryLiteral, diagnostics: [], passContext: passContext)
}

public func postProcess(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return ASTPassResult(element: literalToken, diagnostics: [], passContext: passContext)
}

public func postProcess(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
return ASTPassResult(element: subscriptExpression, diagnostics: [], passContext: passContext)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/SemanticAnalyzer/SemanticAnalyzer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ public struct SemanticAnalyzer: ASTPass {
return ASTPassResult(element: dictionaryLiteral, diagnostics: [], passContext: passContext)
}

public func process(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return ASTPassResult(element: literalToken, diagnostics: [], passContext: passContext)
}

/// Whether an expression refers to a state property.
private func isStorageReference(expression: Expression, scopeContext: ScopeContext) -> Bool {
switch expression {
Expand Down Expand Up @@ -578,6 +582,10 @@ public struct SemanticAnalyzer: ASTPass {
return ASTPassResult(element: dictionaryLiteral, diagnostics: [], passContext: passContext)
}

public func postProcess(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return ASTPassResult(element: literalToken, diagnostics: [], passContext: passContext)
}

public func postProcess(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
return ASTPassResult(element: subscriptExpression, diagnostics: [], passContext: passContext)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/SemanticAnalyzer/TypeChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public struct TypeChecker: ASTPass {
return ASTPassResult(element: dictionaryLiteral, diagnostics: [], passContext: passContext)
}

public func process(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return ASTPassResult(element: literalToken, diagnostics: [], passContext: passContext)
}

public func process(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
return ASTPassResult(element: subscriptExpression, diagnostics: [], passContext: passContext)
}
Expand Down Expand Up @@ -296,6 +300,10 @@ public struct TypeChecker: ASTPass {
return ASTPassResult(element: dictionaryLiteral, diagnostics: [], passContext: passContext)
}

public func postProcess(literalToken: Token, passContext: ASTPassContext) -> ASTPassResult<Token> {
return ASTPassResult(element: literalToken, diagnostics: [], passContext: passContext)
}

public func postProcess(subscriptExpression: SubscriptExpression, passContext: ASTPassContext) -> ASTPassResult<SubscriptExpression> {
return ASTPassResult(element: subscriptExpression, diagnostics: [], passContext: passContext)
}
Expand Down

0 comments on commit 8b163ba

Please sign in to comment.