common, codegen: Implement Visitor And Use For CodeGen
Continuous Integration / Grammar Tests (push) Successful in 4m6s
Continuous Integration / Library Format Tests (push) Successful in 5m5s
Continuous Integration / Library Tests (push) Successful in 9m0s
Continuous Integration / Cli Tests (push) Successful in 4m57s

Implement a generic visitor for components of a P4 program and use
it to start P4 code generation (according to the behavioral model).

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-15 08:14:20 -04:00
parent f1f20e96a2
commit 12fa43d9f9
7 changed files with 678 additions and 18 deletions
+95
View File
@@ -0,0 +1,95 @@
// 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 protocol Visitable {
}
public protocol LanguageVisitor<Context> {
associatedtype Context
// Program
func visit(_ program: Program, _ c: VisitorContext<Context>) -> Result<VisitorContext<Context>>
// Parser
func visit(_ parser: Parser, _ c: VisitorContext<Context>) -> Result<VisitorContext<Context>>
func visit(
_ parser_state: InstantiatedParserState, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
// Statements
func visit(
_ variable_declaration: VariableDeclarationStatement, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ conditional: ConditionalStatement, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ block: BlockStatement, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ rtn: ReturnStatement, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ apply: ApplyStatement, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
// Expressions
func visit(
_ keyset: KeysetExpression, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ select_case: SelectCaseExpression, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ select: SelectExpression, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ array_access: ArrayAccessExpression, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ field_access: FieldAccessExpression, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ function_call: FunctionCall, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ binary_operator: BinaryOperatorExpression, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
// Declarations
func visit(_ decl: Declaration, _ c: VisitorContext<Context>) -> Result<VisitorContext<Context>>
func visit(
_ extern_decl: ExternDeclaration, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ func_decl: FunctionDeclaration, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
// Control
func visit(_ action: Action, _ c: VisitorContext<Context>) -> Result<VisitorContext<Context>>
func visit(
_ table_key_entry: TableKeyEntry, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(
_ table_property_list: TablePropertyList, _ c: VisitorContext<Context>
) -> Result<VisitorContext<Context>>
func visit(_ table: Table, _ c: VisitorContext<Context>) -> Result<VisitorContext<Context>>
func visit(_ control: Control, _ c: VisitorContext<Context>) -> Result<VisitorContext<Context>>
}