Refactor Type System

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-06 07:46:18 -05:00
parent bc700509c1
commit c3fdfb62e8
11 changed files with 395 additions and 199 deletions
+22
View File
@@ -0,0 +1,22 @@
// 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
public struct Instantiation {
}
+14 -6
View File
@@ -85,13 +85,21 @@ public struct ParserStates {
nonisolated(unsafe) public let accept: ParserState = ParserState(name: "accept")
nonisolated(unsafe) public let reject: ParserState = ParserState(name: "reject")
public struct Parser {
public struct Parser: P4Type {
public var states: [ParserState] = Array()
public var count: Int {
states.count
}
public init() {}
public var name: Identifier
public init(withName name: Identifier) {
self.name = name
}
public static func create() -> any P4Type {
return Parser(withName: Identifier(name: ""))
}
public func findStartState() -> ParserState? {
for state in states {
@@ -103,25 +111,25 @@ public struct Parser {
}
}
public class ParserExecution: ProgramExecution {
public class ParserInstance: ProgramExecution {
private init(state: ParserState) {
self.state = state
super.init()
}
public static func create(_ parser: Parser) -> Result<ParserExecution> {
public static func create(_ parser: Parser) -> Result<ParserInstance> {
guard let start_state = parser.findStartState() else {
return Result.Error(Error(withMessage: "Could not find the start state"))
}
let new = ParserExecution(state: start_state)
let new = ParserInstance(state: start_state)
return Result.Ok(new)
}
public var state: ParserState
public func transition(toNextState state: ParserState) -> ParserExecution {
public func transition(toNextState state: ParserState) -> ParserInstance {
let next = self
next.state = state
return next
+24 -1
View File
@@ -15,7 +15,30 @@
// 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
public struct Program {
public var parsers: [Parser] = Array()
public var types: [P4Type] = Array()
/// Find the program's main parser
///
/// Note: For now, the main parser is expected to be named main_parser.
public func starting_parser() -> Result<Parser> {
return self.find_parser(withName: Identifier(name: "main_parser"))
}
public func find_parser(withName name: Identifier) -> Result<Parser> {
for type in self.types {
print("type: \(type)")
guard let parser = type as? Parser else {
continue
}
if parser.name == name {
return .Ok(parser)
}
}
return .Error(Error(withMessage: "Could not find parser named \(name)"))
}
public init() {}
}