Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e17533dfc8 | |||
| fe88e447a9 |
@@ -140,16 +140,16 @@ extension Cli {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
let maybe_codegen = P4Runtime.CodeGenerator().codeGen(program)
|
let maybe_codegen = P4Runtime.CodeGenerator().codeGen(program)
|
||||||
guard case .Ok(let codegen) = maybe_codegen else {
|
guard case .Ok(let codegen) = maybe_codegen else {
|
||||||
let formatter = FormatterAnsi()
|
let formatter = FormatterAnsi()
|
||||||
print(ErrorWithLabel("Code Generation Error", maybe_codegen.error()!).format(formatter))
|
print(ErrorWithLabel("Code Generation Error", maybe_codegen.error()!).format(formatter))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
print("\(codegen.getGeneratedCode())")
|
print("\(codegen.getGeneratedCode())")
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -309,7 +309,10 @@ public struct AST {
|
|||||||
public var tipe: AST.Identifier
|
public var tipe: AST.Identifier
|
||||||
public let arguments: AST.ArgumentList
|
public let arguments: AST.ArgumentList
|
||||||
|
|
||||||
public init(named name: AST.Identifier, withType tipe: AST.Identifier, withArguments arguments: AST.ArgumentList) {
|
public init(
|
||||||
|
named name: AST.Identifier, withType tipe: AST.Identifier,
|
||||||
|
withArguments arguments: AST.ArgumentList
|
||||||
|
) {
|
||||||
self.name = name
|
self.name = name
|
||||||
self.arguments = arguments
|
self.arguments = arguments
|
||||||
self.tipe = tipe
|
self.tipe = tipe
|
||||||
@@ -643,7 +646,9 @@ public struct ASTCompilerContext {
|
|||||||
public let lexical_context_statements: [AST.AnStatement]?
|
public let lexical_context_statements: [AST.AnStatement]?
|
||||||
public let extern_context: Bool
|
public let extern_context: Bool
|
||||||
|
|
||||||
public init(_ name: AST.Identifier? = .none, _ stmts: [AST.AnStatement]? = .none, _ extern: Bool = false) {
|
public init(
|
||||||
|
_ name: AST.Identifier? = .none, _ stmts: [AST.AnStatement]? = .none, _ extern: Bool = false
|
||||||
|
) {
|
||||||
self.lexical_context_name = name
|
self.lexical_context_name = name
|
||||||
self.lexical_context_statements = stmts
|
self.lexical_context_statements = stmts
|
||||||
self.extern_context = extern
|
self.extern_context = extern
|
||||||
@@ -661,5 +666,3 @@ public struct ASTCompilerContext {
|
|||||||
return ASTCompilerContext(self.lexical_context_name, self.lexical_context_statements, extern)
|
return ASTCompilerContext(self.lexical_context_name, self.lexical_context_statements, extern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -93,7 +93,8 @@ extension AST.FunctionDeclaration: Compilable {
|
|||||||
sourceLocation: function_declaration_node.toSourceLocation(),
|
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||||
withError: "Missing function declaration component")))
|
withError: "Missing function declaration component")))
|
||||||
|
|
||||||
let maybe_function_name = AST.Identifier.CompileExpression(node: current_node!, withContext: context)
|
let maybe_function_name = AST.Identifier.CompileExpression(
|
||||||
|
node: current_node!, withContext: context)
|
||||||
guard case .Ok(let function_name) = maybe_function_name else {
|
guard case .Ok(let function_name) = maybe_function_name else {
|
||||||
return .Error(maybe_function_name.error()!)
|
return .Error(maybe_function_name.error()!)
|
||||||
}
|
}
|
||||||
@@ -476,9 +477,11 @@ extension AST.Control: Compilable {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
return .Ok(AST.Control(
|
return .Ok(
|
||||||
named: control_name as! AST.Identifier, withParameters: control_parameters, withTable: tables[0],
|
AST.Control(
|
||||||
withActions: AST.Actions(withActions: actions), withApply: apply))
|
named: control_name as! AST.Identifier, withParameters: control_parameters,
|
||||||
|
withTable: tables[0],
|
||||||
|
withActions: AST.Actions(withActions: actions), withApply: apply))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -133,7 +133,8 @@ extension AST.Expression: Compilable {
|
|||||||
|
|
||||||
let expression_parsers: [CompilableExpression.Type] = [
|
let expression_parsers: [CompilableExpression.Type] = [
|
||||||
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, AST.Identifier.self,
|
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, AST.Identifier.self,
|
||||||
AST.BinaryOperatorExpression.self, AST.ArrayAccessExpression.self, AST.FieldAccessExpression.self,
|
AST.BinaryOperatorExpression.self, AST.ArrayAccessExpression.self,
|
||||||
|
AST.FieldAccessExpression.self,
|
||||||
AST.FunctionCall.self,
|
AST.FunctionCall.self,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ extension AST.Statement: Compilable {
|
|||||||
"assignmentStatement": AST.ParserAssignmentStatement.self,
|
"assignmentStatement": AST.ParserAssignmentStatement.self,
|
||||||
"expressionStatement": AST.ExpressionStatement.self,
|
"expressionStatement": AST.ExpressionStatement.self,
|
||||||
"variableDeclaration": AST.VariableDeclarationStatement.self,
|
"variableDeclaration": AST.VariableDeclarationStatement.self,
|
||||||
"conditionalStatement": AST.ConditionalStatement.self, "blockStatement": AST.BlockStatement.self,
|
"conditionalStatement": AST.ConditionalStatement.self,
|
||||||
|
"blockStatement": AST.BlockStatement.self,
|
||||||
"return_statement": AST.ReturnStatement.self,
|
"return_statement": AST.ReturnStatement.self,
|
||||||
]
|
]
|
||||||
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
||||||
@@ -176,7 +177,8 @@ extension AST.ParserState: Compilable {
|
|||||||
sourceLocation: node.toSourceLocation(),
|
sourceLocation: node.toSourceLocation(),
|
||||||
withError: "Missing transition statement of state declaration")))
|
withError: "Missing transition statement of state declaration")))
|
||||||
|
|
||||||
let updated_context = context.update(withContextName: (state_identifier as! AST.Identifier)).update(withContextStatements: parsed_s)
|
let updated_context = context.update(withContextName: (state_identifier as! AST.Identifier))
|
||||||
|
.update(withContextStatements: parsed_s)
|
||||||
|
|
||||||
return AST.TransitionStatement.Compile(node: current_node!, withContext: updated_context)
|
return AST.TransitionStatement.Compile(node: current_node!, withContext: updated_context)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,11 @@ public protocol ASTVisitor<T> {
|
|||||||
func visit(node: AST.Identifier, driver: ASTVisitorDriver, context: T) -> Result<T>
|
func visit(node: AST.Identifier, driver: ASTVisitorDriver, context: T) -> Result<T>
|
||||||
|
|
||||||
func visit(node: AST.Parser, driver: ASTVisitorDriver, context: T) -> Result<T>
|
func visit(node: AST.Parser, driver: ASTVisitorDriver, context: T) -> Result<T>
|
||||||
func visit(node: AST.ParserStateDirectTransition, driver: ASTVisitorDriver, context: T) -> Result<T>
|
func visit(
|
||||||
|
node: AST.ParserStateDirectTransition, driver: ASTVisitorDriver, context: T
|
||||||
|
) -> Result<T>
|
||||||
func visit(node: AST.ParserStateNoTransition, driver: ASTVisitorDriver, context: T) -> Result<T>
|
func visit(node: AST.ParserStateNoTransition, driver: ASTVisitorDriver, context: T) -> Result<T>
|
||||||
func visit(node: AST.ParserStateSelectTransition, driver: ASTVisitorDriver, context: T) -> Result<T>
|
func visit(
|
||||||
|
node: AST.ParserStateSelectTransition, driver: ASTVisitorDriver, context: T
|
||||||
|
) -> Result<T>
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,20 @@
|
|||||||
|
// 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 Common
|
||||||
|
|
||||||
public struct ASTVisitorDriver {
|
public struct ASTVisitorDriver {
|
||||||
|
|||||||
Reference in New Issue
Block a user