aboutsummaryrefslogtreecommitdiffstats
path: root/prog
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2023-12-05 14:46:00 +0100
committerAleksandr Nogikh <nogikh@google.com>2024-02-19 11:54:01 +0000
commited571339c6ff5ed764283737a0aa68451085e84d (patch)
treef809f9dcd1782d7b281d2bb6b60fb3be5fa8704f /prog
parente59ec59b027f921a6bfbe5014b15c2a802445ada (diff)
pkg/compiler: support if[expr] attributes
The expression may either include integers/consts or reference other fields in the structure via value[field1:field2:field3]. The fields on this path must all belong to structures and must not have any if conditions themselves. For unions, mandate that the last field has no conditions (it will be the default one). For structs, convert conditional fields into fields of a union type of the following form: anonymous_union [ value T (if[expression]) void void ]
Diffstat (limited to 'prog')
-rw-r--r--prog/types.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/prog/types.go b/prog/types.go
index ce02d2d21..73fa3a972 100644
--- a/prog/types.go
+++ b/prog/types.go
@@ -72,6 +72,7 @@ type Field struct {
Type
HasDirection bool
Direction Dir
+ Condition Expression
}
func (f *Field) Dir(def Dir) Dir {
@@ -81,6 +82,62 @@ func (f *Field) Dir(def Dir) Dir {
return def
}
+type Expression interface {
+ fmt.GoStringer
+ ForEachValue(func(*Value))
+ Clone() Expression
+}
+
+type BinaryOperator int
+
+const (
+ OperatorCompareEq BinaryOperator = iota
+ OperatorCompareNeq
+ OperatorBinaryAnd
+)
+
+type BinaryExpression struct {
+ Operator BinaryOperator
+ Left Expression
+ Right Expression
+}
+
+func (bo BinaryExpression) GoString() string {
+ return fmt.Sprintf("&prog.BinaryExpression{%#v,%#v,%#v}", bo.Operator, bo.Left, bo.Right)
+}
+
+func (bo BinaryExpression) ForEachValue(cb func(*Value)) {
+ bo.Left.ForEachValue(cb)
+ bo.Right.ForEachValue(cb)
+}
+
+func (bo *BinaryExpression) Clone() Expression {
+ return &BinaryExpression{
+ Operator: bo.Operator,
+ Left: bo.Left.Clone(),
+ Right: bo.Right.Clone(),
+ }
+}
+
+type Value struct {
+ // If Path is empty, Value is to be used.
+ Value uint64
+ // Path to the field.
+ Path []string
+}
+
+func (v Value) GoString() string {
+ return fmt.Sprintf("&prog.Value{%#v,%#v}", v.Value, v.Path)
+}
+
+func (v *Value) ForEachValue(cb func(*Value)) {
+ cb(v)
+}
+
+func (v *Value) Clone() Expression {
+ return &Value{v.Value, append([]string{}, v.Path...)}
+}
+
type BinaryFormat int
const (