@@ -16,24 +16,22 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import Common
|
||||
import P4Lang
|
||||
import P4Runtime
|
||||
import SwiftTreeSitter
|
||||
import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension P4Boolean: CompilableType {
|
||||
public static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||
) -> Common.Result<(any Common.P4Type)?> {
|
||||
return type.text == "bool" ? .Ok(P4Boolean()) : .Ok(.none)
|
||||
extension P4Boolean: MaybeCompilableType {
|
||||
public static func MaybeCompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: ASTCompilerContext
|
||||
) -> Common.Result<(AST.Tipe)?> {
|
||||
return type.text == "bool" ? .Ok(AST.Tipe(P4QualifiedType(P4Boolean()))) : .Ok(.none)
|
||||
}
|
||||
}
|
||||
|
||||
extension P4Int: CompilableType {
|
||||
public static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||
) -> Common.Result<(any Common.P4Type)?> {
|
||||
extension P4Int: MaybeCompilableType {
|
||||
public static func MaybeCompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: ASTCompilerContext
|
||||
) -> Common.Result<(AST.Tipe)?> {
|
||||
|
||||
// Drill down, as appropriate.
|
||||
let base_type_node = type.child(at: 0)!
|
||||
@@ -50,7 +48,7 @@ extension P4Int: CompilableType {
|
||||
|
||||
#MustOr(
|
||||
result: int_node, thing: walker.getNext(),
|
||||
or: Result<P4Type?>.Error(
|
||||
or: Result<AST.Tipe?>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: type_node.toSourceLocation(),
|
||||
withError: "Missing elements in int type declaration")))
|
||||
@@ -67,49 +65,30 @@ extension P4Int: CompilableType {
|
||||
sourceLocation: bit_width_node.toSourceLocation(),
|
||||
withError: "Could not parse \(bit_width_node.text!) into integer"))
|
||||
}
|
||||
return .Ok(P4Int(BitWidth.Width(bit_width)))
|
||||
return .Ok(AST.Tipe(P4QualifiedType(P4Int(BitWidth.Width(bit_width)))))
|
||||
}
|
||||
return .Ok(P4Int())
|
||||
return .Ok(AST.Tipe(P4QualifiedType(P4Int(BitWidth.Infinite))))
|
||||
}
|
||||
}
|
||||
|
||||
extension P4String: CompilableType {
|
||||
extension P4String: MaybeCompilableType {
|
||||
public static func MaybeCompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: ASTCompilerContext
|
||||
) -> Common.Result<(AST.Tipe)?> {
|
||||
return type.text == "string" ? .Ok(AST.Tipe(P4QualifiedType(P4String()))) : .Ok(.none)
|
||||
}
|
||||
}
|
||||
|
||||
extension AST.Types: CompilableType {
|
||||
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)?> {
|
||||
|
||||
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
|
||||
{
|
||||
return .Ok(found_struct_type)
|
||||
}
|
||||
return .Ok(.none)
|
||||
}
|
||||
}
|
||||
|
||||
public struct Types {
|
||||
static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Result<P4QualifiedType> {
|
||||
let type_parsers: [CompilableType.Type] = [
|
||||
P4Boolean.self, P4Int.self, P4String.self, P4Struct.self,
|
||||
type: SwiftTreeSitter.Node, withContext context: ASTCompilerContext
|
||||
) -> Result<AST.Tipe> {
|
||||
let type_parsers: [MaybeCompilableType.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(P4QualifiedType(type))
|
||||
switch type_parser.MaybeCompileType(type: type, withContext: context) {
|
||||
case .Ok(.some(let type)): return .Ok(type)
|
||||
case .Ok(.none): continue
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user