Files
gp4/Sources/P4Compiler/Types.swift
T
Will Hawkins 2fd5ecf8d6 Work On Derived Types
1. Add support for field access
2. Add support for proper type checking of array access
3. Add tests for nested field and array access

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
2026-03-16 08:31:16 -04:00

55 lines
1.8 KiB
Swift

// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// 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: String) -> Common.Result<(any Common.P4Type)?> {
return type == "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)
}
}
extension P4String: CompilableType {
public static func CompileType(type: String) -> Common.Result<(any Common.P4Type)?> {
return type == "string" ? .Ok(P4String()) : .Ok(.none)
}
}
public struct Types {
static func CompileBasicType(type: String) -> Result<P4Type> {
let type_parsers: [CompilableType.Type] = [P4Boolean.self, P4Int.self, P4String.self]
for type_parser in type_parsers {
switch type_parser.CompileType(type: type) {
case .Ok(.some(let type)): return .Ok(type)
case .Ok(.none): continue
case .Error(let e): return .Error(e)
}
}
return Result.Error(Error(withMessage: "Type name not recognized"))
}
}