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/testdata/all.txt | 19 +++++++++++++++++++ pkg/ast/testdata/errors.txt | 10 +++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) (limited to 'pkg/ast/testdata') diff --git a/pkg/ast/testdata/all.txt b/pkg/ast/testdata/all.txt index c9569840c..d04fd4992 100644 --- a/pkg/ast/testdata/all.txt +++ b/pkg/ast/testdata/all.txt @@ -8,3 +8,22 @@ incdir strflags0 = "foo", strflags1 strflags1 = "bar" + +expressions { + f0 int8 (if[value[X] & Y]) + f1 int8 (if[X & Y == Z]) + f2 int8 (if[X & Y & Z == value[X] & A]) + f3 int8 (if[X & (A == B) & Z != C]) +} + +condFields { + mask int8 +# Simple expressions work. + f0 int16 (if[val[mask] == SOME_CONST]) +# Conditions and other attributes work together. + f1 int16 (out, if[val[mask] == SOME_CONST]) +# Test some more complex expressions. + f2 int16 (out, if[val[mask] & SOME_CONST == OTHER_CONST]) + f3 int16 (out, if[val[mask] & SOME_CONST & OTHER_CONST == val[mask] & CONST_X]) + f4 int16 (out, if[val[mask] & SOME_CONST]) +} diff --git a/pkg/ast/testdata/errors.txt b/pkg/ast/testdata/errors.txt index 266babf8f..b3d9e7f52 100644 --- a/pkg/ast/testdata/errors.txt +++ b/pkg/ast/testdata/errors.txt @@ -11,7 +11,7 @@ meta foo, bar ### unexpected ',', expecting '\n' int_flags0 = 0, 0x1, 0xab int_flags1 = 123ab0x ### bad integer "123ab0x" -int_flags1 == 0, 1 ### unexpected '=', expecting int, identifier, string +int_flags1 == 0, 1 ### unexpected ==, expecting '(', '{', '[', '=' int_flags = 0, "foo" ### unexpected string, expecting int, identifier int_flags2 = ' ### char literal is not terminated int_flags3 = 'a ### char literal is not terminated @@ -67,6 +67,14 @@ s3 { f1 int8 } [attribute[1, "foo"], another[and[another]]] +sCondFieldsError1 { + f0 int16 (out, if[val[mask] SOME_CONST == val[mask]]) ### unexpected identifier, expecting ']' +} ### unexpected '}', expecting comment, define, include, resource, identifier + +sCondFieldsError2 { + f5 int16 (out, if[val[mask] & == val[mask]]) ### unexpected ==, expecting int, identifier, string +} ### unexpected '}', expecting comment, define, include, resource, identifier + type mybool8 int8 type net_port proc[1, 2, int16be] type mybool16 ### unexpected '\n', expecting '[', identifier -- cgit mrf-deployment