Files
gp4/Sources/P4Compiler/Types.swift
T
Will Hawkins a7d8fd1304
Continuous Integration / Grammar Tests (push) Successful in 4m13s
Continuous Integration / Library Format Tests (push) Successful in 5m17s
Continuous Integration / Library Tests (push) Failing after 8m34s
Continuous Integration / Cli Tests (push) Failing after 4m40s
grammar,compiler: Add Support For Fixed-Width Integers
Distinguishing between signed and unsigned fixed-width integer
types must still be done.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
2026-05-18 06:53:22 -04:00

120 lines
3.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: 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)?> {
// Drill down, as appropriate.
let base_type_node = type.child(at: 0)!
#SkipUnlessNodeType<SwiftTreeSitter.Node>(
node: base_type_node, type: "baseType")
let type_node = base_type_node.child(at: 0)!
#SkipUnlessNodeType<SwiftTreeSitter.Node>(
node: type_node, type: "int_type")
var walker = Walker(node: type_node)
var int_node: Node? = .none
#MustOr(
result: int_node, thing: walker.getNext(),
or: Result<P4Type?>.Error(
ErrorWithLocation(
sourceLocation: type_node.toSourceLocation(),
withError: "Missing elements in int type declaration")))
// Move passed the keyword.
walker.next()
if let bit_width_node = walker.getNext() {
guard let bit_width = Int(bit_width_node.child(at: 1)!.text!),
bit_width != 0
else {
return .Error(
ErrorWithLocation(
sourceLocation: bit_width_node.toSourceLocation(),
withError: "Could not parse \(bit_width_node.text!) into integer"))
}
return .Ok(P4Int(BitWidth.Width(bit_width)))
}
return .Ok(P4Int())
}
}
extension P4String: 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,
]
for type_parser in type_parsers {
switch type_parser.CompileType(type: type, withContext: context) {
case .Ok(.some(let type)): return .Ok(P4QualifiedType(type))
case .Ok(.none): continue
case .Error(let e): return .Error(e)
}
}
return Result.Error(Error(withMessage: "Type name not recognized"))
}
}