diff --git a/Sources/Common/Compiler.swift b/Sources/Common/Compiler.swift index 059ab59..931bf16 100644 --- a/Sources/Common/Compiler.swift +++ b/Sources/Common/Compiler.swift @@ -26,4 +26,3 @@ public typealias TypeTypeScope = Scope /// Scopes that resolve type identifiers to their types. public typealias TypeTypeScopes = Scopes - diff --git a/Sources/Common/ProgramTypes.swift b/Sources/Common/ProgramTypes.swift index faf45ca..626fc27 100644 --- a/Sources/Common/ProgramTypes.swift +++ b/Sources/Common/ProgramTypes.swift @@ -120,12 +120,13 @@ public struct P4StructFields: Sequence, CustomStringConvertible, Equatable { public func describe_with_values(values: [P4Value?]) -> String { assert(values.count == self.count()) - return zip(self.fields, values).map() { (field, value) in - let actual_value = if let v = value { - v.description - } else { - "Unset" - } + return zip(self.fields, values).map { (field, value) in + let actual_value = + if let v = value { + v.description + } else { + "Unset" + } return String("\(field): \(actual_value)") }.joined(separator: "; ") } @@ -211,7 +212,11 @@ public class P4StructValue: P4Value { for field_idx in 0.. Bool } -public extension P4Value { - func evaluate(execution: ProgramExecution) -> Result { +extension P4Value { + public func evaluate(execution: ProgramExecution) -> Result { return Result.Ok(self) } } public protocol EvaluatableLValueExpression: EvaluatableExpression { - func set(to: P4Value, inScopes scopes: VarValueScopes, duringExecution execution: ProgramExecution) -> Result<(VarValueScopes, P4Value)> + func set( + to: P4Value, inScopes scopes: VarValueScopes, duringExecution execution: ProgramExecution + ) -> Result<(VarValueScopes, P4Value)> func check(to: EvaluatableExpression, inScopes scopes: VarTypeScopes) -> Result<()> } diff --git a/Sources/P4Compiler/Expression.swift b/Sources/P4Compiler/Expression.swift index 8f93d1e..5f2ddf8 100644 --- a/Sources/P4Compiler/Expression.swift +++ b/Sources/P4Compiler/Expression.swift @@ -62,8 +62,8 @@ extension TypedIdentifier: CompilableLValueExpression { #SkipUnlessNodeType( node: expression, type: "identifier") - let maybe_parsed_expression = TypedIdentifier.compile(node: node, withContext: context) - guard case .Ok(let maybe_typed_identifier) = maybe_parsed_expression else { + let maybe_parsed_expression = TypedIdentifier.compile(node: node, withContext: context) + guard case .Ok(let maybe_typed_identifier) = maybe_parsed_expression else { return .Error(maybe_parsed_expression.error()!) } @@ -525,8 +525,8 @@ extension FieldAccessExpression: CompilableLValueExpression { #SkipUnlessNodeType( node: expression, type: "fieldAccessExpression") - let maybe_parsed_expression = FieldAccessExpression.compile(node: node, withContext: context) - guard case .Ok(let maybe_field_access_expression) = maybe_parsed_expression else { + let maybe_parsed_expression = FieldAccessExpression.compile(node: node, withContext: context) + guard case .Ok(let maybe_field_access_expression) = maybe_parsed_expression else { return .Error(maybe_parsed_expression.error()!) } @@ -544,8 +544,8 @@ extension ArrayAccessExpression: CompilableLValueExpression { #SkipUnlessNodeType( node: expression, type: "arrayAccessExpression") - let maybe_parsed_expression = ArrayAccessExpression.compile(node: node, withContext: context) - guard case .Ok(let maybe_array_access_expression) = maybe_parsed_expression else { + let maybe_parsed_expression = ArrayAccessExpression.compile(node: node, withContext: context) + guard case .Ok(let maybe_array_access_expression) = maybe_parsed_expression else { return .Error(maybe_parsed_expression.error()!) } @@ -553,4 +553,4 @@ extension ArrayAccessExpression: CompilableLValueExpression { return Result.Ok(array_access_expression) } -} \ No newline at end of file +} diff --git a/Sources/P4Compiler/Parser.swift b/Sources/P4Compiler/Parser.swift index acdc5a8..510c9ce 100644 --- a/Sources/P4Compiler/Parser.swift +++ b/Sources/P4Compiler/Parser.swift @@ -22,7 +22,6 @@ import SwiftTreeSitter import TreeSitterExtensions import TreeSitterP4 - public struct Parser { public struct LocalElements { static func Compile( @@ -165,9 +164,11 @@ public struct Parser { } if !parse_errs.isEmpty { - return Result.Error(Error(withMessage: parse_errs.map() { err in - return String(err.msg) - }.joined(separator: ";"))) + return Result.Error( + Error( + withMessage: parse_errs.map { err in + return String(err.msg) + }.joined(separator: ";"))) } return Result.Ok((parsed_s, current_context)) } @@ -246,9 +247,11 @@ public struct Parser { } if !parse_errs.isEmpty { - return Result.Error(Error(withMessage: parse_errs.map() { err in - return String(err.msg) - }.joined(separator: ";"))) + return Result.Error( + Error( + withMessage: parse_errs.map { err in + return String(err.msg) + }.joined(separator: ";"))) } if node.childCount < currentChildIdxSafe { diff --git a/Sources/P4Compiler/Program.swift b/Sources/P4Compiler/Program.swift index 930b401..5bbe881 100644 --- a/Sources/P4Compiler/Program.swift +++ b/Sources/P4Compiler/Program.swift @@ -27,12 +27,15 @@ public struct Program { return Program.Compile(source, withGlobalInstances: .none, withGlobalTypes: .none) } - public static func Compile(_ source: String, withGlobalInstances globalInstances: VarTypeScopes) -> Result { + public static func Compile( + _ source: String, withGlobalInstances globalInstances: VarTypeScopes + ) -> Result { return Program.Compile(source, withGlobalInstances: globalInstances, withGlobalTypes: .none) } public static func Compile( - _ source: String, withGlobalInstances globalInstances: VarTypeScopes?, withGlobalTypes globalTypes: TypeTypeScopes? + _ source: String, withGlobalInstances globalInstances: VarTypeScopes?, + withGlobalTypes globalTypes: TypeTypeScopes? ) -> Result { let maybe_parser = ConfigureP4Parser() diff --git a/Sources/P4Compiler/Protocols.swift b/Sources/P4Compiler/Protocols.swift index 0186aa6..ee3b562 100644 --- a/Sources/P4Compiler/Protocols.swift +++ b/Sources/P4Compiler/Protocols.swift @@ -33,5 +33,7 @@ public protocol CompilableValue { } public protocol CompilableType { - static func CompileType(type: SwiftTreeSitter.Node, withContext: CompilerContext) -> Result + static func CompileType( + type: SwiftTreeSitter.Node, withContext: CompilerContext + ) -> Result } diff --git a/Sources/P4Compiler/Statement.swift b/Sources/P4Compiler/Statement.swift index e77a01e..42e0328 100644 --- a/Sources/P4Compiler/Statement.swift +++ b/Sources/P4Compiler/Statement.swift @@ -191,7 +191,8 @@ extension VariableDeclarationStatement: CompilableStatement { Error(withMessage: "Could not parse variable name")) } - guard case .Ok(let declaration_p4_type) = Types.CompileType(type: typeref, withContext: context) else { + guard case .Ok(let declaration_p4_type) = Types.CompileType(type: typeref, withContext: context) + else { return Result.Error( Error(withMessage: "Could not parse a P4 type from \(typeref.text!)")) } diff --git a/Sources/P4Compiler/Types.swift b/Sources/P4Compiler/Types.swift index 627f064..904312e 100644 --- a/Sources/P4Compiler/Types.swift +++ b/Sources/P4Compiler/Types.swift @@ -23,31 +23,40 @@ import TreeSitterExtensions import TreeSitterP4 extension P4Boolean: CompilableType { - public static func CompileType(type: SwiftTreeSitter.Node, withContext: CompilerContext) -> Common.Result<(any Common.P4Type)?> { + public static func CompileType( + type: SwiftTreeSitter.Node, withContext: CompilerContext + ) -> Common.Result<(any Common.P4Type)?> { return type.text == "bool" ? .Ok(P4Boolean()) : .Ok(.none) } } extension P4Int: CompilableType { - public static func CompileType(type: SwiftTreeSitter.Node, withContext: CompilerContext) -> Common.Result<(any Common.P4Type)?> { + public static func CompileType( + type: SwiftTreeSitter.Node, withContext: CompilerContext + ) -> Common.Result<(any Common.P4Type)?> { return type.text == "int" ? .Ok(P4Int()) : .Ok(.none) } } extension P4String: CompilableType { - public static func CompileType(type: SwiftTreeSitter.Node, withContext: CompilerContext) -> Common.Result<(any Common.P4Type)?> { + public static func CompileType( + type: SwiftTreeSitter.Node, withContext: CompilerContext + ) -> Common.Result<(any Common.P4Type)?> { return type.text == "string" ? .Ok(P4String()) : .Ok(.none) } } extension P4Struct: CompilableType { - public static func CompileType(type: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Common.Result<(any Common.P4Type)?> { + public static func CompileType( + type: SwiftTreeSitter.Node, withContext context: CompilerContext + ) -> Common.Result<(any Common.P4Type)?> { let maybe_parsed_type_id = Identifier.Compile(node: type, withContext: context) guard case .Ok(let parsed_type_id) = maybe_parsed_type_id else { return .Error(maybe_parsed_type_id.error()!) } if case .Ok(let found_type) = context.types.lookup(identifier: parsed_type_id), - let found_struct_type = found_type as? P4Struct { + let found_struct_type = found_type as? P4Struct + { return .Ok(found_struct_type) } return .Ok(.none) @@ -55,8 +64,12 @@ extension P4Struct: CompilableType { } public struct Types { - static func CompileType(type: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Result { - let type_parsers: [CompilableType.Type] = [P4Boolean.self, P4Int.self, P4String.self, P4Struct.self] + static func CompileType( + type: SwiftTreeSitter.Node, withContext context: CompilerContext + ) -> Result { + let type_parsers: [CompilableType.Type] = [ + P4Boolean.self, P4Int.self, P4String.self, P4Struct.self, + ] for type_parser in type_parsers { switch type_parser.CompileType(type: type, withContext: context) { case .Ok(.some(let type)): return .Ok(type) diff --git a/Sources/P4Lang/Parser.swift b/Sources/P4Lang/Parser.swift index d252cc9..a4f41d4 100644 --- a/Sources/P4Lang/Parser.swift +++ b/Sources/P4Lang/Parser.swift @@ -29,7 +29,9 @@ public struct ParserAssignmentStatement { public let lvalue: EvaluatableLValueExpression public let value: EvaluatableExpression - public init(withLValue lvalue: EvaluatableLValueExpression, withValue value: EvaluatableExpression) { + public init( + withLValue lvalue: EvaluatableLValueExpression, withValue value: EvaluatableExpression + ) { self.lvalue = lvalue self.value = value } diff --git a/Sources/P4Runtime/Expressions.swift b/Sources/P4Runtime/Expressions.swift index 733f728..656ed0c 100644 --- a/Sources/P4Runtime/Expressions.swift +++ b/Sources/P4Runtime/Expressions.swift @@ -66,7 +66,8 @@ extension TypedIdentifier: EvaluatableExpression { // Variables are evaluatable because they can be looked up by identifiers. extension TypedIdentifier: EvaluatableLValueExpression { public func set( - to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, duringExecution execution: ProgramExecution + to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, + duringExecution execution: ProgramExecution ) -> Common.Result<(Common.VarValueScopes, P4Value)> { if case .Error(let e) = scopes.lookup(identifier: self) { return .Error(e) @@ -75,13 +76,18 @@ extension TypedIdentifier: EvaluatableLValueExpression { return .Ok((scopes.set(identifier: self, withValue: to), to)) } - public func check(to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes) -> Result<()> { + public func check( + to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes + ) -> Result<()> { guard case .Ok(let type) = scopes.lookup(identifier: self) else { return .Error(Error(withMessage: "Cannot assign to identifier not in scope")) } if !type.eq(rhs: to.type()) { - return .Error(Error(withMessage: "Cannot assign value with type \(to.type()) to identifier \(self) with type \(type)")) + return .Error( + Error( + withMessage: + "Cannot assign value with type \(to.type()) to identifier \(self) with type \(type)")) } return .Ok(()) } @@ -145,7 +151,8 @@ extension ArrayAccessExpression: EvaluatableExpression { extension ArrayAccessExpression: EvaluatableLValueExpression { public func set( - to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, duringExecution execution: ProgramExecution + to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, + duringExecution execution: ProgramExecution ) -> Common.Result<(Common.VarValueScopes, P4Value)> { // For purposes of documentation, assume the field access expression we are evaluating is // (strct_id)[indexor] = new_value @@ -155,19 +162,21 @@ extension ArrayAccessExpression: EvaluatableLValueExpression { // First, evaluate strct_id and make sure that it names a struct. let maybe_value = self.name.evaluate(execution: execution) guard case .Ok(let value) = maybe_value else { - return Result.Error(Error(withMessage: "\(self.name) cannot be evaluated: \(maybe_value.error()!)")) + return Result.Error( + Error(withMessage: "\(self.name) cannot be evaluated: \(maybe_value.error()!)")) } guard let array_value = value as? P4ArrayValue else { - return Result.Error(Error(withMessage: "\(self.name) does not identify a struct")) + return Result.Error(Error(withMessage: "\(self.name) does not identify a struct")) } // Now, get the indexor! let maybe_indexor_value = self.indexor.evaluate(execution: execution) guard case .Ok(let indexor_value) = maybe_indexor_value else { - return Result.Error(Error(withMessage: "\(self.indexor) cannot be evaluated: \(maybe_indexor_value.error()!)")) + return Result.Error( + Error(withMessage: "\(self.indexor) cannot be evaluated: \(maybe_indexor_value.error()!)")) } guard let indexor_int = indexor_value as? P4IntValue else { - return Result.Error(Error(withMessage: "\(self.indexor) cannot be used to index an array")) + return Result.Error(Error(withMessage: "\(self.indexor) cannot be used to index an array")) } // Now we have an array and an index! @@ -187,7 +196,11 @@ extension ArrayAccessExpression: EvaluatableLValueExpression { ) -> Common.Result<()> { if !self.type.value_type().eq(rhs: to.type()) { - return .Error(Error(withMessage: "Cannot assign value of type \(to.type()) to array with values of type \(self.name.type())")) + return .Error( + Error( + withMessage: + "Cannot assign value of type \(to.type()) to array with values of type \(self.name.type())" + )) } return .Ok(()) } @@ -219,7 +232,8 @@ extension FieldAccessExpression: EvaluatableExpression { extension FieldAccessExpression: EvaluatableLValueExpression { public func set( - to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, duringExecution execution: ProgramExecution + to: any Common.P4Value, inScopes scopes: Common.VarValueScopes, + duringExecution execution: ProgramExecution ) -> Common.Result<(Common.VarValueScopes, P4Value)> { // For purposes of documentation, assume the field access expression we are evaluating is // (strct_id).field_id = new_field_value @@ -229,11 +243,12 @@ extension FieldAccessExpression: EvaluatableLValueExpression { // First, evaluate strct_id and make sure that it names a struct. let maybe_value = self.strct.evaluate(execution: execution) guard case .Ok(let value) = maybe_value else { - return Result.Error(Error(withMessage: "\(self.strct) cannot be evaluated: \(maybe_value.error()!)")) + return Result.Error( + Error(withMessage: "\(self.strct) cannot be evaluated: \(maybe_value.error()!)")) } guard let struct_value = value as? P4StructValue else { - return Result.Error(Error(withMessage: "\(self.strct) does not identify a struct")) + return Result.Error(Error(withMessage: "\(self.strct) does not identify a struct")) } // Now we know that struct_id identifies a structure value. @@ -255,9 +270,12 @@ extension FieldAccessExpression: EvaluatableLValueExpression { to: any Common.EvaluatableExpression, inScopes scopes: Common.VarTypeScopes ) -> Common.Result<()> { - if !self.field.type.eq(rhs:to.type()) { - return .Error(Error(withMessage: "Cannot assign value of type \(to.type()) to field with type \(self.field.type)")) + if !self.field.type.eq(rhs: to.type()) { + return .Error( + Error( + withMessage: + "Cannot assign value of type \(to.type()) to field with type \(self.field.type)")) } return .Ok(()) } -} \ No newline at end of file +}