compiler: Refactor Language Element Tags
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
+71
-67
@@ -17,31 +17,34 @@
|
||||
|
||||
import Common
|
||||
|
||||
extension P4Value: CST.AnExpression {}
|
||||
extension P4Value: CST.Categories.Expression {}
|
||||
|
||||
public struct CST {
|
||||
|
||||
public protocol AnExpression {}
|
||||
public protocol Statement {}
|
||||
public protocol AnState {}
|
||||
public struct Categories {
|
||||
public protocol Expression {}
|
||||
public protocol Statement {}
|
||||
public protocol State {}
|
||||
public protocol Declaration: Categories.Statement {}
|
||||
}
|
||||
|
||||
struct Expression {}
|
||||
|
||||
public struct Statements {
|
||||
public let statements: [Statement]
|
||||
public let statements: [Categories.Statement]
|
||||
|
||||
public init(_ s: [Statement]) {
|
||||
public init(_ s: [Categories.Statement]) {
|
||||
self.statements = s
|
||||
}
|
||||
}
|
||||
|
||||
public struct VariableDeclarationStatement: Statement {
|
||||
public var initializer: AnExpression?
|
||||
public struct VariableDeclarationStatement: Categories.Statement {
|
||||
public var initializer: Categories.Expression?
|
||||
public var identifier: CST.Identifier
|
||||
public var tipe: CST.Tipe
|
||||
|
||||
public init(
|
||||
identifier: Identifier, withType tipe: CST.Tipe, withInitializer initializer: AnExpression?
|
||||
identifier: Identifier, withType tipe: CST.Tipe, withInitializer initializer: Categories.Expression?
|
||||
) {
|
||||
self.identifier = identifier
|
||||
self.initializer = initializer
|
||||
@@ -49,20 +52,20 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ConditionalStatement: Statement {
|
||||
public var condition: AnExpression
|
||||
public var thenn: Statement
|
||||
public var elss: Statement?
|
||||
public struct ConditionalStatement: Categories.Statement {
|
||||
public var condition: Categories.Expression
|
||||
public var thenn: Categories.Statement
|
||||
public var elss: Categories.Statement?
|
||||
|
||||
public init(condition: AnExpression, withThen thenn: Statement) {
|
||||
public init(condition: Categories.Expression, withThen thenn: Categories.Statement) {
|
||||
self.condition = condition
|
||||
self.thenn = thenn
|
||||
self.elss = .none
|
||||
}
|
||||
|
||||
public init(
|
||||
condition: AnExpression, withThen thenn: Statement,
|
||||
andElse elss: Statement
|
||||
condition: Categories.Expression, withThen thenn: Categories.Statement,
|
||||
andElse elss: Categories.Statement
|
||||
) {
|
||||
self.condition = condition
|
||||
self.thenn = thenn
|
||||
@@ -70,7 +73,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct BlockStatement: Statement {
|
||||
public struct BlockStatement: Categories.Statement {
|
||||
public var statements: Statements
|
||||
|
||||
public init(_ statements: Statements) {
|
||||
@@ -79,15 +82,15 @@ public struct CST {
|
||||
|
||||
}
|
||||
|
||||
public struct ReturnStatement: Statement {
|
||||
public let value: AnExpression
|
||||
public struct ReturnStatement: Categories.Statement {
|
||||
public let value: Categories.Expression
|
||||
|
||||
public init(_ value: AnExpression) {
|
||||
public init(_ value: Categories.Expression) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
public struct ApplyStatement: Statement {
|
||||
public struct ApplyStatement: Categories.Statement {
|
||||
public let body: CST.BlockStatement?
|
||||
|
||||
public init() { self.body = .none }
|
||||
@@ -223,11 +226,10 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public protocol AnDeclaration: Statement {}
|
||||
|
||||
public struct Declaration {}
|
||||
|
||||
public struct Control: CustomStringConvertible, AnDeclaration {
|
||||
public struct Control: CustomStringConvertible, Categories.Declaration {
|
||||
public var description: String {
|
||||
return "Control named \(self._name) \(self.parameters) \(self.actions) \(self.table)"
|
||||
}
|
||||
@@ -272,15 +274,15 @@ public struct CST {
|
||||
|
||||
}
|
||||
|
||||
public struct ExternDeclaration: AnDeclaration {
|
||||
public let declaration: CST.AnDeclaration
|
||||
public struct ExternDeclaration: Categories.Declaration {
|
||||
public let declaration: CST.Categories.Declaration
|
||||
|
||||
public init(_ declaration: CST.AnDeclaration) {
|
||||
public init(_ declaration: CST.Categories.Declaration) {
|
||||
self.declaration = declaration
|
||||
}
|
||||
}
|
||||
|
||||
public struct FunctionDeclaration: AnDeclaration {
|
||||
public struct FunctionDeclaration: Categories.Declaration {
|
||||
public var description: String {
|
||||
return "Function named \(self.name) that returns \(self.tipe) with parameters \(self.params)"
|
||||
}
|
||||
@@ -302,7 +304,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct StructDeclaration: AnDeclaration {
|
||||
public struct StructDeclaration: Categories.Declaration {
|
||||
public let fields: [CST.VariableDeclarationStatement]
|
||||
public let identifier: CST.Identifier
|
||||
public init(_ id: CST.Identifier, _ fields: [CST.VariableDeclarationStatement]) {
|
||||
@@ -311,7 +313,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct Instantiation: Statement {
|
||||
public struct Instantiation: Categories.Statement {
|
||||
public let name: CST.Identifier
|
||||
public var tipe: CST.Identifier
|
||||
public let arguments: CST.ArgumentList
|
||||
@@ -326,34 +328,34 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ExpressionStatement: Statement {
|
||||
public let expression: AnExpression
|
||||
public struct ExpressionStatement: Categories.Statement {
|
||||
public let expression: Categories.Expression
|
||||
|
||||
public init(_ expr: AnExpression) {
|
||||
public init(_ expr: Categories.Expression) {
|
||||
self.expression = expr
|
||||
}
|
||||
}
|
||||
|
||||
public struct Identifier: AnExpression {
|
||||
public struct Identifier: Categories.Expression {
|
||||
public let id: Common.Identifier
|
||||
public init(_ id: Common.Identifier) {
|
||||
self.id = id
|
||||
}
|
||||
}
|
||||
|
||||
public struct Literal: AnExpression {
|
||||
public struct Literal: Categories.Expression {
|
||||
public let literal: P4Value
|
||||
public init(_ literal: P4Value) {
|
||||
self.literal = literal
|
||||
}
|
||||
}
|
||||
|
||||
public enum KeysetExpression: AnExpression {
|
||||
public enum KeysetExpression: Categories.Expression {
|
||||
case Default
|
||||
case Value(AnExpression)
|
||||
case Value(Categories.Expression)
|
||||
}
|
||||
|
||||
public struct SelectCaseExpression: AnExpression {
|
||||
public struct SelectCaseExpression: Categories.Expression {
|
||||
public let key: CST.KeysetExpression
|
||||
public let next_state_identifier: CST.Identifier
|
||||
|
||||
@@ -363,12 +365,12 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct SelectExpression: AnExpression {
|
||||
public let selector: AnExpression
|
||||
public struct SelectExpression: Categories.Expression {
|
||||
public let selector: Categories.Expression
|
||||
public let case_expressions: [CST.SelectCaseExpression]
|
||||
|
||||
public init(
|
||||
withSelector selector: AnExpression,
|
||||
withSelector selector: Categories.Expression,
|
||||
withSelectCaseExpressions sces: [CST.SelectCaseExpression]
|
||||
) {
|
||||
self.selector = selector
|
||||
@@ -397,15 +399,15 @@ public struct CST {
|
||||
case Or
|
||||
}
|
||||
|
||||
public struct BinaryOperatorExpression: AnExpression {
|
||||
public let left: AnExpression
|
||||
public let right: AnExpression
|
||||
public struct BinaryOperatorExpression: Categories.Expression {
|
||||
public let left: Categories.Expression
|
||||
public let right: Categories.Expression
|
||||
public let type: BinaryOperatorExpressionType
|
||||
|
||||
public init(
|
||||
withType tipe: BinaryOperatorExpressionType,
|
||||
withLhs lhs: AnExpression,
|
||||
withRhs rhs: AnExpression
|
||||
withLhs lhs: Categories.Expression,
|
||||
withRhs rhs: Categories.Expression
|
||||
) {
|
||||
self.type = tipe
|
||||
self.left = lhs
|
||||
@@ -413,30 +415,30 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ArrayAccessExpression: AnExpression {
|
||||
public let indexor: AnExpression
|
||||
public let name: AnExpression
|
||||
public struct ArrayAccessExpression: Categories.Expression {
|
||||
public let indexor: Categories.Expression
|
||||
public let name: Categories.Expression
|
||||
|
||||
public init(
|
||||
withName name: AnExpression,
|
||||
withIndexor indexor: AnExpression
|
||||
withName name: Categories.Expression,
|
||||
withIndexor indexor: Categories.Expression
|
||||
) {
|
||||
self.name = name
|
||||
self.indexor = indexor
|
||||
}
|
||||
}
|
||||
|
||||
public struct FieldAccessExpression: AnExpression {
|
||||
public struct FieldAccessExpression: Categories.Expression {
|
||||
public let field: Identifier
|
||||
public let strct: AnExpression
|
||||
public let strct: Categories.Expression
|
||||
|
||||
public init(withStruct strct: AnExpression, withField field: Identifier) {
|
||||
public init(withStruct strct: Categories.Expression, withField field: Identifier) {
|
||||
self.strct = strct
|
||||
self.field = field
|
||||
}
|
||||
}
|
||||
|
||||
public struct FunctionCall: AnExpression {
|
||||
public struct FunctionCall: Categories.Expression {
|
||||
public let callee: Identifier
|
||||
public let arguments: ArgumentList
|
||||
|
||||
@@ -450,12 +452,12 @@ public struct CST {
|
||||
|
||||
public struct LocalElement {}
|
||||
|
||||
public struct ParserAssignmentStatement: Statement {
|
||||
public let lvalue: AnExpression
|
||||
public let value: AnExpression
|
||||
public struct ParserAssignmentStatement: Categories.Statement {
|
||||
public let lvalue: Categories.Expression
|
||||
public let value: Categories.Expression
|
||||
|
||||
public init(
|
||||
withLValue lvalue: AnExpression, withValue value: AnExpression
|
||||
withLValue lvalue: Categories.Expression, withValue value: Categories.Expression
|
||||
) {
|
||||
self.lvalue = lvalue
|
||||
self.value = value
|
||||
@@ -491,7 +493,7 @@ public struct CST {
|
||||
/// Only defined to define Compilable extension.
|
||||
public struct TransitionStatement {}
|
||||
|
||||
public class ParserStateDirectTransition: ParserState, AnState {
|
||||
public class ParserStateDirectTransition: ParserState, Categories.State {
|
||||
public let next_state_identifier: Identifier?
|
||||
|
||||
public init(
|
||||
@@ -503,7 +505,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateNoTransition: ParserState, AnState {
|
||||
public class ParserStateNoTransition: ParserState, Categories.State {
|
||||
/// Construct a ParserState
|
||||
public init(
|
||||
name: Identifier, withStatements stmts: CST.Statements? = .none
|
||||
@@ -512,7 +514,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateSelectTransition: ParserState, AnState {
|
||||
public class ParserStateSelectTransition: ParserState, Categories.State {
|
||||
|
||||
public let te: SelectExpression
|
||||
|
||||
@@ -526,17 +528,17 @@ public struct CST {
|
||||
}
|
||||
|
||||
public struct ParserStates {
|
||||
public var states: [AnState] = Array()
|
||||
public var states: [Categories.State] = Array()
|
||||
|
||||
public func count() -> Int {
|
||||
return states.count
|
||||
}
|
||||
|
||||
public init(_ states: [AnState] = Array()) {
|
||||
public init(_ states: [Categories.State] = Array()) {
|
||||
self.states = states
|
||||
}
|
||||
|
||||
public func append(state: AnState) -> ParserStates {
|
||||
public func append(state: Categories.State) -> ParserStates {
|
||||
var new_states = self.states
|
||||
new_states.append(state)
|
||||
return ParserStates(new_states)
|
||||
@@ -546,7 +548,7 @@ public struct CST {
|
||||
/// A P4 Parser
|
||||
///
|
||||
/// Note: A Parser is a type
|
||||
public struct Parser: AnDeclaration {
|
||||
public struct Parser: Categories.Declaration {
|
||||
public var states: ParserStates
|
||||
|
||||
public var name: Identifier
|
||||
@@ -632,9 +634,9 @@ public struct CST {
|
||||
|
||||
public struct Argument {
|
||||
public let index: Int
|
||||
public let argument: CST.AnExpression
|
||||
public let argument: Categories.Expression
|
||||
|
||||
public init(_ argument: CST.AnExpression, atIndex index: Int) {
|
||||
public init(_ argument: Categories.Expression, atIndex index: Int) {
|
||||
self.argument = argument
|
||||
self.index = index
|
||||
}
|
||||
@@ -646,6 +648,8 @@ public struct CST {
|
||||
self.statements = stmts
|
||||
}
|
||||
}
|
||||
|
||||
public struct Statement {}
|
||||
}
|
||||
|
||||
public struct CSTCompilerContext {
|
||||
|
||||
Reference in New Issue
Block a user