diff --git a/Sources/Common/ProgramTypes.swift b/Sources/Common/ProgramTypes.swift index 0548738..027067f 100644 --- a/Sources/Common/ProgramTypes.swift +++ b/Sources/Common/ProgramTypes.swift @@ -72,7 +72,6 @@ public class Variable: TypedIdentifier { } } - /// The type for a P4 struct public struct P4Struct: P4Type { @@ -247,7 +246,6 @@ public class P4StringValue: P4Value { } } - public class Packet { public init() {} } diff --git a/Sources/Common/Support.swift b/Sources/Common/Support.swift index 40a0d7d..446705c 100644 --- a/Sources/Common/Support.swift +++ b/Sources/Common/Support.swift @@ -141,7 +141,9 @@ extension Result: CustomStringConvertible { #externalMacro(module: "Macros", type: "UseOkResult") @freestanding(expression) public macro UseErrorResult(_: Result) -> Error = #externalMacro(module: "Macros", type: "UseErrorResult") -@freestanding(codeItem) public macro RequireNodeType(node: N, type: String, nice_type_name: String) = +@freestanding(codeItem) public macro RequireNodeType( + node: N, type: String, nice_type_name: String +) = #externalMacro(module: "Macros", type: "RequireNodeType") @freestanding(codeItem) public macro RequireNodesType( nodes: N, type: [String], nice_type_names: [String] diff --git a/Sources/P4Compiler/Expression.swift b/Sources/P4Compiler/Expression.swift index 9047e31..2355d69 100644 --- a/Sources/P4Compiler/Expression.swift +++ b/Sources/P4Compiler/Expression.swift @@ -184,8 +184,9 @@ extension SelectExpression: CompilableExpression { var kses: [KeysetExpression] = Array() var kses_errors: [Error] = Array() - select_body_node.enumerateNamedChildren() { current_node in - let maybe_parsed_kse = KeysetExpression.compile(node: current_node, inTree: tree, withScopes: scopes) + select_body_node.enumerateNamedChildren { current_node in + let maybe_parsed_kse = KeysetExpression.compile( + node: current_node, inTree: tree, withScopes: scopes) if case .Ok(let parsed_kse) = maybe_parsed_kse { kses.append(parsed_kse as! KeysetExpression) } else { @@ -202,8 +203,8 @@ extension SelectExpression: CompilableExpression { }.joined(separator: ";\n")))) } return .Ok( - SelectExpression(withSelector: selector, withKeysetExpressions: kses), - ) + SelectExpression(withSelector: selector, withKeysetExpressions: kses), + ) } } @@ -211,38 +212,37 @@ extension KeysetExpression: CompilableExpression { static func compile( node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes ) -> Result { - if node.nodeType != "selectCase" { - return Result.Error(Error(withMessage: "Expected select case not found")) - } - - guard let keysetexpression_node = node.child(at: 0), - keysetexpression_node.nodeType == "keysetExpression" - else { - return Result.Error(Error(withMessage: "Missing keyset expression in select case")) - } - - guard let targetstate_node = node.child(at: 2), - targetstate_node.nodeType == "identifier" - else { - return Result.Error(Error(withMessage: "Missing target state in select case")) - } - - let maybe_parsed_keysetexpression = Expression.Compile( - node: keysetexpression_node, inTree: tree, withScopes: scopes) - guard case Result.Ok(let keysetexpression) = maybe_parsed_keysetexpression else { - return Result.Error(maybe_parsed_keysetexpression.error()!) - } - - let maybe_parsed_targetstate = Identifier.Compile( - node: targetstate_node, inTree: tree, withScopes: scopes) - guard case .Ok(let targetstate) = maybe_parsed_targetstate else { - return Result.Error(maybe_parsed_targetstate.error()!) - } - - return .Ok( - KeysetExpression( - withKey: keysetexpression, withNextState: targetstate) - ) + if node.nodeType != "selectCase" { + return Result.Error(Error(withMessage: "Expected select case not found")) } - } + guard let keysetexpression_node = node.child(at: 0), + keysetexpression_node.nodeType == "keysetExpression" + else { + return Result.Error(Error(withMessage: "Missing keyset expression in select case")) + } + + guard let targetstate_node = node.child(at: 2), + targetstate_node.nodeType == "identifier" + else { + return Result.Error(Error(withMessage: "Missing target state in select case")) + } + + let maybe_parsed_keysetexpression = Expression.Compile( + node: keysetexpression_node, inTree: tree, withScopes: scopes) + guard case Result.Ok(let keysetexpression) = maybe_parsed_keysetexpression else { + return Result.Error(maybe_parsed_keysetexpression.error()!) + } + + let maybe_parsed_targetstate = Identifier.Compile( + node: targetstate_node, inTree: tree, withScopes: scopes) + guard case .Ok(let targetstate) = maybe_parsed_targetstate else { + return Result.Error(maybe_parsed_targetstate.error()!) + } + + return .Ok( + KeysetExpression( + withKey: keysetexpression, withNextState: targetstate) + ) + } +} diff --git a/Sources/P4Compiler/Parser.swift b/Sources/P4Compiler/Parser.swift index c1177ff..2617ec8 100644 --- a/Sources/P4Compiler/Parser.swift +++ b/Sources/P4Compiler/Parser.swift @@ -147,15 +147,22 @@ public struct Parser { node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes ) -> Result<(ParserTransitionStatement, LexicalScopes)> { - #RequireNodeType(node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement") + #RequireNodeType( + node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement" + ) guard let tse_node = node.child(at: 1), - tse_node.nodeType! == "transitionSelectionExpression" else { - return .Error(ErrorOnNode(node: node, withError: "Could not find transition select expression")) + tse_node.nodeType! == "transitionSelectionExpression" + else { + return .Error( + ErrorOnNode(node: node, withError: "Could not find transition select expression")) } guard let next_node = tse_node.child(at: 0) else { - return .Error(ErrorOnNode(node: node, withError: "Could not find the next token in a transition selection expression")) + return .Error( + ErrorOnNode( + node: node, + withError: "Could not find the next token in a transition selection expression")) } // If the next node is an identifier, we have the simple form ... @@ -179,7 +186,8 @@ public struct Parser { switch SelectExpression.compile(node: next_node, inTree: tree, withScopes: scopes) { case .Ok(let tse): - .Ok((ParserTransitionStatement(withTransitionExpression: tse! as! SelectExpression), scopes)) + .Ok( + (ParserTransitionStatement(withTransitionExpression: tse! as! SelectExpression), scopes)) case .Error(let e): .Error(e) } } diff --git a/Sources/P4Compiler/Statement.swift b/Sources/P4Compiler/Statement.swift index eae6c63..945e2f1 100644 --- a/Sources/P4Compiler/Statement.swift +++ b/Sources/P4Compiler/Statement.swift @@ -26,7 +26,8 @@ extension BlockStatement: CompilableStatement { public static func Compile( node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes ) -> Result<(EvaluatableStatement, LexicalScopes)> { - #RequireNodeType(node: node, type: "blockStatement", nice_type_name: "block statement") + #RequireNodeType( + node: node, type: "blockStatement", nice_type_name: "block statement") var currentChildIdx = 0 var currentChildIdxSafe = 1 @@ -91,7 +92,8 @@ extension ConditionalStatement: CompilableStatement { node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes ) -> Result<(EvaluatableStatement, LexicalScopes)> { - #RequireNodeType(node: node, type: "conditionalStatement", nice_type_name: "conditional statement") + #RequireNodeType( + node: node, type: "conditionalStatement", nice_type_name: "conditional statement") let maybe_condition_expression = node.child(at: 2) guard let condition_expression = maybe_condition_expression, @@ -158,7 +160,8 @@ extension VariableDeclarationStatement: CompilableStatement { node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes ) -> Result<(EvaluatableStatement, LexicalScopes)> { - #RequireNodeType(node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement") + #RequireNodeType( + node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement") let maybe_typeref = node.child(at: 0) guard let typeref = maybe_typeref, @@ -235,7 +238,8 @@ extension ExpressionStatement: CompilableStatement { public static func Compile( node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes ) -> Result<(EvaluatableStatement, LexicalScopes)> { - #RequireNodeType(node: node, type: "expressionStatement", nice_type_name: "expression statement") + #RequireNodeType( + node: node, type: "expressionStatement", nice_type_name: "expression statement") let _ = node.child(at: 0) diff --git a/Sources/P4Runtime/Expressions.swift b/Sources/P4Runtime/Expressions.swift index 060d5e6..d2835ad 100644 --- a/Sources/P4Runtime/Expressions.swift +++ b/Sources/P4Runtime/Expressions.swift @@ -51,27 +51,27 @@ extension SelectExpression: EvaluatableExpression { } extension P4StringValue: EvaluatableExpression { - public func evaluate(execution: Common.ProgramExecution) -> Common.Result { - return .Ok(self) - } + public func evaluate(execution: Common.ProgramExecution) -> Common.Result { + return .Ok(self) + } } extension P4BooleanValue: EvaluatableExpression { - public func evaluate(execution: Common.ProgramExecution) -> Common.Result { - return .Ok(self) - } + public func evaluate(execution: Common.ProgramExecution) -> Common.Result { + return .Ok(self) + } } extension P4StructValue: EvaluatableExpression { - public func evaluate(execution: Common.ProgramExecution) -> Common.Result { - return .Ok(self) - } + public func evaluate(execution: Common.ProgramExecution) -> Common.Result { + return .Ok(self) + } } extension P4IntValue: EvaluatableExpression { - public func evaluate(execution: Common.ProgramExecution) -> Common.Result { - return .Ok(self) - } + public func evaluate(execution: Common.ProgramExecution) -> Common.Result { + return .Ok(self) + } } // Variables are evaluatable because they can be looked up by identifiers. @@ -83,4 +83,4 @@ extension TypedIdentifier: EvaluatableExpression { public func evaluate(execution: Common.ProgramExecution) -> Result { return execution.scopes.lookup(identifier: self) } -} \ No newline at end of file +} diff --git a/Sources/P4Runtime/Parser.swift b/Sources/P4Runtime/Parser.swift index 13e70fc..56a6523 100644 --- a/Sources/P4Runtime/Parser.swift +++ b/Sources/P4Runtime/Parser.swift @@ -46,9 +46,7 @@ public struct ParserStateDirectTransition: ParserStateInstance { } public var description: String { - get { - return "Instance of \(currrent_state)" - } + return "Instance of \(currrent_state)" } public let currrent_state: ParserState @@ -71,7 +69,7 @@ public struct ParserStateDirectTransition: ParserStateInstance { } program = program.setError(error: res.error()!).exit_scope() - + return (self, program.exit_scope()) } @@ -99,9 +97,7 @@ public struct ParserStateNoTransition: ParserStateInstance { } public var description: String { - get { - return "Instance of \(currrent_state)" - } + return "Instance of \(currrent_state)" } public let currrent_state: ParserState @@ -134,9 +130,7 @@ public struct ParserStateSelectTransition: ParserStateInstance { } public var description: String { - get { - return "Instance of \(currrent_state)" - } + return "Instance of \(currrent_state)" } public func execute( @@ -203,7 +197,7 @@ extension ParserStates: Compilable { public typealias ToCompile = ParserStates public typealias Compiled = (ParserStateInstance, [ParserStateInstance]) public static func compile(_ parser: ToCompile) -> Result { - var compiled_states:[ParserStateInstance] = Array() + var compiled_states: [ParserStateInstance] = Array() compiled_states.append(ParserStateNoTransition(currrent_state: accept)) compiled_states.append(ParserStateNoTransition(currrent_state: reject)) @@ -213,7 +207,7 @@ extension ParserStates: Compilable { // TODO: We assume that states are in transition-order! for state in parser.states { switch ParserState.compile(state) { - case .Ok(let compiled): + case .Ok(let compiled): if compiled.state().state == Identifier(name: "start") { start_state = compiled } @@ -252,7 +246,8 @@ extension ParserInstance: Compilable { public static func compile(_ parser: ToCompile) -> Result { return switch ParserStates.compile(parser.states) { - case .Ok(let (start_state, states)): Result.Ok(ParserInstance(start: start_state, states: states)) + case .Ok(let (start_state, states)): + Result.Ok(ParserInstance(start: start_state, states: states)) case .Error(let e): Result.Error(e) } } diff --git a/Sources/P4Runtime/Program.swift b/Sources/P4Runtime/Program.swift index af0d28a..54e8e80 100644 --- a/Sources/P4Runtime/Program.swift +++ b/Sources/P4Runtime/Program.swift @@ -66,4 +66,4 @@ extension ExpressionStatement: EvaluatableStatement { public func evaluate(execution: ProgramExecution) -> ProgramExecution { return execution } -} \ No newline at end of file +}