From e59ec59b027f921a6bfbe5014b15c2a802445ada Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Wed, 29 Nov 2023 16:01:41 +0100 Subject: pkg/ast: support expressions with ast.Type So far they have the following grammar: OP = "==", "!=", "&" value-expr = value-expr OP value-expr | factor factor = "(" and-expr ")" | integer | identifier | string Operators are left associative, e.g. A & B & C is the same as (A & B) & C. Further restrictions will be imposed in pkg/compiler. This will help implement conditionally included fields. --- pkg/ast/ast.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'pkg/ast/ast.go') diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index 2458c1245..2726af602 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -252,15 +252,35 @@ func (n *Int) GetName() string { return n.Ident } +type Operator int + +const ( + OperatorCompareEq = iota + 1 + OperatorCompareNeq + OperatorBinaryAnd +) + +type BinaryExpression struct { + Pos Pos + Operator Operator + Left *Type + Right *Type +} + +func (n *BinaryExpression) Info() (Pos, string, string) { + return n.Pos, "binary-expression", "" +} + type Type struct { Pos Pos - // Only one of Value, Ident, String is filled. - Value uint64 - ValueFmt IntFmt - Ident string - String string - StringFmt StrFmt - HasString bool + // Only one of Value, Ident, String, Expression is filled. + Value uint64 + ValueFmt IntFmt + Ident string + String string + StringFmt StrFmt + HasString bool + Expression *BinaryExpression // Parts after COLON (for ranges and bitfields). Colon []*Type // Sub-types in []. -- cgit mrf-deployment