Allow Declaring Instances of Structs
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -23,27 +23,42 @@ import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension P4Boolean: CompilableType {
|
||||
public static func CompileType(type: String) -> Common.Result<(any Common.P4Type)?> {
|
||||
return type == "bool" ? .Ok(P4Boolean()) : .Ok(.none)
|
||||
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: String) -> Common.Result<(any Common.P4Type)?> {
|
||||
return type == "int" ? .Ok(P4Int()) : .Ok(.none)
|
||||
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: String) -> Common.Result<(any Common.P4Type)?> {
|
||||
return type == "string" ? .Ok(P4String()) : .Ok(.none)
|
||||
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 CompileBasicType(type: String) -> Result<P4Type> {
|
||||
let type_parsers: [CompilableType.Type] = [P4Boolean.self, P4Int.self, P4String.self]
|
||||
static func CompileType(type: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Result<P4Type> {
|
||||
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) {
|
||||
switch type_parser.CompileType(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