Refactor Runtime

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-19 22:50:19 -05:00
parent 3693bdc02d
commit d9c8c5aeb0
17 changed files with 897 additions and 439 deletions
+1 -22
View File
@@ -26,28 +26,6 @@ import TreeSitterP4
@testable import Parser
@Test func test_simple_parser() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
transition start;
}
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
#expect(parser.states.count() == 1)
let state = try! #require(parser.states.find(withName: "start"))
#expect(state.state_name == "start")
#expect(state.statements.count == 0)
#expect(#RequireOkResult(parser.states.semantic_check()))
let next_state = try! #require(state.next_state)
#expect(next_state == state)
}
@Test func test_simple_parser_syntax_error() async throws {
let simple_parser_declaration = """
parser main_parser() {
@@ -87,6 +65,7 @@ import TreeSitterP4
parser main_parser() {
state start {
true;
false;
transition start;
}
};
+10 -75
View File
@@ -38,14 +38,8 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
#expect(#RequireOkResult(Runtime.ParserRuntime.create(program: program)))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
guard case Common.Result.Ok(let (state_result, _)) = runtime.run(input: Packet())
else {
assert(false)
}
let (state_result, _) = try! #UseOkResult(runtime.run())
// We should be in the accept state.
#expect(state_result == Lang.accept)
@@ -62,15 +56,8 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
#expect(#RequireOkResult(Runtime.ParserRuntime.create(program: program)))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
guard case Common.Result.Ok(let (state_result, _)) = runtime.run(input: Packet())
else {
assert(false)
}
let (state_result, _) = try! #UseOkResult(runtime.run())
// We should be in the accept state.
#expect(state_result == Lang.reject)
}
@@ -87,55 +74,13 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
#expect(
#RequireErrorResult<ParserRuntime>(
Error(withMessage: "Could not find the start state"),
Error(withMessage: "No start state defined"),
Runtime.ParserRuntime.create(program: program)))
}
@Test func test_simple_local_element_variable_declaration() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
bool b = false;
string s = "testing";
true;
false;
transition reject;
}
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
// This seems awkward to me!
// TODO: Is there a better way?
guard case Common.Result.Ok(let (state_result, execution_result)) = runtime.run(input: Packet())
else {
assert(false)
}
// There should be 1 scope.
#expect(execution_result.scopes.count == 1)
guard let scope = execution_result.scopes.current else {
assert(false)
}
// We should be in the accept state.
#expect(state_result == Lang.reject)
// There are two variables declared.
#expect(scope.count == 2)
// Check the names/values of the variables in scope.
let b = try #require(scope.lookup(identifier: Identifier(name: "b")))
let s = try #require(scope.lookup(identifier: Identifier(name: "s")))
#expect(b.value_type.eq(rhs: P4BooleanValue(withValue: false)))
#expect(s.value_type.eq(rhs: P4StringValue(withValue: "\"testing\"")))
}
@Test func test_simple_parser_with_transition_select_expression() async throws {
let simple_parser_declaration = """
parser main_parser() {
@@ -148,18 +93,14 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
#expect(#RequireOkResult(program.find_parser(withName: Identifier(name: "main_parser"))))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
guard case Common.Result.Ok(let (state_result, _)) = runtime.run(input: Packet())
else {
assert(false)
}
#expect(state_result == Lang.accept)
}
@@ -176,16 +117,10 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
#expect(#RequireOkResult(program.find_parser(withName: Identifier(name: "main_parser"))))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
guard case Common.Result.Ok(let (state_result, _)) = runtime.run(input: Packet())
else {
assert(false)
}
#expect(state_result == Lang.reject)
}
}
+173
View File
@@ -0,0 +1,173 @@
// 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 Foundation
import Lang
import Macros
import Runtime
import SwiftTreeSitter
import Testing
import TreeSitter
import TreeSitterP4
@testable import Parser
@Test func test_simple_local_element_variable_declaration() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
bool b = false;
string s = "testing";
true;
false;
true;
transition reject;
}
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
#expect(execution_result.scopes.count == 1)
guard let scope = execution_result.scopes.current else {
assert(false)
}
// We should be in the accept state.
#expect(state_result == Lang.reject)
// There are two variables declared.
#expect(scope.count == 2)
// Check the names/values of the variables in scope.
let b = try #require(scope.lookup(identifier: Identifier(name: "b")))
let s = try #require(scope.lookup(identifier: Identifier(name: "s")))
#expect(b.value_type.eq(rhs: P4BooleanValue(withValue: false)))
#expect(s.value_type.eq(rhs: P4StringValue(withValue: "\"testing\"")))
}
@Test func test_simple_scope() async throws {
let simple_parser_declaration = """
parser main_parser() {
state starts {
bool where_to = false;
int va = 5;
transition accept;
}
state start {
bool where_to = true;
transition select (where_to) {
false: reject;
true: starts;
};
}
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
#expect(state_result == Lang.accept)
#expect(execution_result.scopes.count == 1)
let scope = try! #require(execution_result.scopes.current)
#expect(scope.count == 2)
let va = try #require(scope.lookup(identifier: Identifier(name: "va")))
let where_to = try #require(scope.lookup(identifier: Identifier(name: "where_to")))
#expect(where_to.value_type.eq(rhs: P4BooleanValue(withValue: false)))
#expect(va.value_type.eq(rhs: P4IntValue(withValue: 5)))
}
@Test func test_simple_scope2() async throws {
let simple_parser_declaration = """
parser main_parser() {
state starts {
bool where_to = false;
int va = 5;
transition accept;
}
state start {
bool where_to = true;
where_to = false;
transition select (where_to) {
false: reject;
true: starts;
};
}
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 2)
#expect(state_result == Lang.reject)
#expect(execution_result.scopes.count == 1)
let scope = try! #require(execution_result.scopes.current)
#expect(scope.count == 1)
let where_to = try #require(scope.lookup(identifier: Identifier(name: "where_to")))
#expect(where_to.value_type.eq(rhs: P4BooleanValue(withValue: false)))
}
@Test func test_simple_assignment() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
bool where_to = true;
string where_from = "here";
where_to = false;
where_from = "there";
transition select (true) {
false: reject;
true: accept;
};
}
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(state_result == Lang.accept)
#expect(execution_result.scopes.count == 1)
let scope = try! #require(execution_result.scopes.current)
#expect(scope.count == 2)
let where_to = try #require(scope.lookup(identifier: Identifier(name: "where_to")))
#expect(where_to.value_type.eq(rhs: P4BooleanValue(withValue: false)))
let where_from = try #require(scope.lookup(identifier: Identifier(name: "where_from")))
#expect(where_from.value_type.eq(rhs: P4StringValue(withValue: "\"there\"")))
}