compiler, language, runtime: Separate Parser Type From Instances
In P4, parsers are considered types. Those parsers are instantiated. The instantiated parsers are values. Previously, gp4 treated a parser type and a parser value as identical. This PR makes that difference clear _and_ sets the stage for the future. TODO: Make the same distinction between control and action types and values. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -18,8 +18,8 @@
|
||||
import Common
|
||||
import Foundation
|
||||
import Macros
|
||||
import P4Runtime
|
||||
import P4Lang
|
||||
import P4Runtime
|
||||
import SwiftTreeSitter
|
||||
import Testing
|
||||
import TreeSitter
|
||||
@@ -39,20 +39,30 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
|
||||
])))
|
||||
withValue: P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)),
|
||||
P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
|
||||
program: program, withGlobalValues: test_values))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_array_access_invalid_type() async throws {
|
||||
@@ -67,12 +77,15 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Int()))
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"), withValue: (P4QualifiedType(P4Int()), .none))
|
||||
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
withMessage: "{49, 22}: Failed to parse a statement element: {65, 2}: ta does not name an array type"
|
||||
withMessage:
|
||||
"{49, 22}: Failed to parse a statement element: {65, 2}: ta does not name an array type"
|
||||
),
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
)
|
||||
@@ -90,21 +103,29 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))
|
||||
])))
|
||||
withValue: P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)),
|
||||
P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
|
||||
program: program, withGlobalValues: test_values))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
|
||||
#expect(state_result == P4Lang.reject)
|
||||
}
|
||||
|
||||
@Test func test_array_access3() async throws {
|
||||
@@ -118,20 +139,29 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
withValue: P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)),
|
||||
P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
|
||||
program: program, withGlobalValues: test_values))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_array_access4() async throws {
|
||||
@@ -145,20 +175,28 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
withValue: P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)),
|
||||
P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
|
||||
program: program, withGlobalValues: test_values))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_array_access_nested() async throws {
|
||||
@@ -173,24 +211,39 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))))
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: (
|
||||
P4QualifiedType(
|
||||
P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))),
|
||||
.none
|
||||
))
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
let nested = P4Value(P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [P4Value(P4IntValue(withValue: 5)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
|
||||
let nested = P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [
|
||||
P4Value(P4IntValue(withValue: 5)), P4Value(P4IntValue(withValue: 2)),
|
||||
P4Value(P4IntValue(withValue: 3)),
|
||||
]))
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), withValue: [nested])))
|
||||
withValue: P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))),
|
||||
withValue: [nested])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
|
||||
program: program, withGlobalValues: test_values))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_array_set() async throws {
|
||||
@@ -206,20 +259,30 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
withValue: P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)),
|
||||
P4Value(P4IntValue(withValue: 3)),
|
||||
])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
|
||||
program: program, withGlobalValues: test_values))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_array_set_nested() async throws {
|
||||
@@ -235,22 +298,38 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_declarations = VarTypeScopes().enter()
|
||||
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))))
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: (
|
||||
P4QualifiedType(
|
||||
P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))),
|
||||
.none
|
||||
))
|
||||
|
||||
var test_values = VarValueScopes().enter()
|
||||
|
||||
let nested = P4Value(P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))]))
|
||||
let nested = P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Int()),
|
||||
withValue: [
|
||||
P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)),
|
||||
P4Value(P4IntValue(withValue: 3)),
|
||||
]))
|
||||
|
||||
test_values = test_values.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), withValue: [nested])))
|
||||
withValue: P4Value(
|
||||
P4ArrayValue(
|
||||
withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))),
|
||||
withValue: [nested])))
|
||||
|
||||
let program = try #UseOkResult(
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
|
||||
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
|
||||
program: program, withGlobalValues: test_values))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user