Skip to content

Commit c857577

Browse files
committed
Refactoring expression parsing
1 parent b75b448 commit c857577

3 files changed

Lines changed: 33 additions & 36 deletions

File tree

src/expression/expr_lexer.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ static expr_op_t op_lookup(etoken t, const char *s)
3333
/* TODO: move to expr_variable.h */
3434
static int var_lookup(etoken tok, const char *s, int len)
3535
{
36+
if ('_' == *s) {
37+
tok->gen.flags |= VAR_MUTED;
38+
++s;
39+
--len;
40+
}
3641
if ('t' != *s || '_' != *(s+1))
3742
tok->toktype = TOK_VAR;
3843
else if (len > 2) {
@@ -241,11 +246,9 @@ static int expr_lex(const char *str, int idx, etoken tok)
241246
case ';':
242247
tok->toktype = TOK_SEMICOLON;
243248
return ++idx;
244-
case '_':
245-
tok->toktype = TOK_MUTED;
246-
return ++idx;
247249
default:
248-
if (!isalpha(c)) {
250+
/* '_' could be the start of a muted variable */
251+
if ('_' != c && !isalpha(c)) {
249252
int len = op_lookup(tok, str+i);
250253
if (len) {
251254
return idx + len;

src/expression/expr_parser.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ typedef struct _temp_var_cache {
6161

6262
#define ASSIGN_MASK (TOK_VAR | TOK_OPEN_SQUARE | TOK_COMMA | TOK_CLOSE_SQUARE | TOK_CLOSE_CURLY \
6363
| TOK_OPEN_CURLY | TOK_NEGATE | TOK_LITERAL | TOK_COLON)
64-
#define OBJECT_TOKENS (TOK_VAR | TOK_LITERAL | TOK_FN | TOK_VFN | TOK_MUTED | TOK_NEGATE \
65-
| TOK_OPEN_PAREN | TOK_OPEN_SQUARE | TOK_OP_UNARY | TOK_TT | TOK_HASH)
64+
#define OBJECT_TOKENS (TOK_VAR | TOK_LITERAL | TOK_FN | TOK_VFN | TOK_NEGATE | TOK_OPEN_PAREN \
65+
| TOK_OPEN_SQUARE | TOK_OP_UNARY | TOK_TT | TOK_HASH)
6666
#define JOIN_TOKENS (TOK_OP | TOK_OP_UNARY | TOK_CLOSE_PAREN | TOK_CLOSE_SQUARE | TOK_CLOSE_CURLY \
6767
| TOK_COMMA | TOK_COLON | TOK_SEMICOLON | TOK_FN_DOT)
6868

@@ -138,7 +138,7 @@ int expr_parser_build_stack(mpr_expr expr, const char *str,
138138
int i, lex_idx = 0, allow_toktype = 0x2FFFFF;;
139139

140140
/* TODO: use bitflags instead? */
141-
uint8_t assigning = 0, is_const = 1, out_assigned = 0, muted = 0, vectorizing = 0;
141+
uint8_t assigning = 0, is_const = 1, out_assigned = 0, vectorizing = 0;
142142
uint8_t lambda_allowed = 0, reduce_types = 0;
143143
uint8_t decorating_var = 0;
144144
uint8_t vec_len_ctx = 0;
@@ -155,7 +155,7 @@ int expr_parser_build_stack(mpr_expr expr, const char *str,
155155
{FAIL_IF(!str[lex_idx], "No expression found.");}
156156

157157
assigning = 1;
158-
allow_toktype = TOK_VAR | TOK_TT | TOK_OPEN_SQUARE | TOK_MUTED | TOK_OP_UNARY;
158+
allow_toktype = TOK_VAR | TOK_TT | TOK_OPEN_SQUARE | TOK_OP_UNARY;
159159

160160
/* Find lowest and highest signal types */
161161
for (i = 0; i < num_src; i++) {
@@ -216,10 +216,6 @@ int expr_parser_build_stack(mpr_expr expr, const char *str,
216216
}
217217
}
218218
switch (tok.toktype) {
219-
case TOK_MUTED:
220-
muted = 1;
221-
allow_toktype = TOK_VAR | TOK_TT;
222-
break;
223219
case TOK_LITERAL:
224220
/* push to output stack */
225221
estack_push(out, &tok);
@@ -402,8 +398,6 @@ int expr_parser_build_stack(mpr_expr expr, const char *str,
402398
}
403399
vec_len_ctx = tok.gen.vec_len;
404400
tok.var.vec_idx = 0;
405-
if (muted)
406-
tok.gen.flags |= VAR_MUTED;
407401

408402
/* timetag tokens have type double */
409403
if (tok.toktype == TOK_TT) {
@@ -423,7 +417,6 @@ int expr_parser_build_stack(mpr_expr expr, const char *str,
423417
allow_toktype |= TOK_VFN_DOT;
424418
if (tok.var.idx != VAR_Y || out_assigned > 1)
425419
allow_toktype |= JOIN_TOKENS;
426-
muted = 0;
427420
break;
428421
}
429422
case TOK_FN:

src/expression/expr_token.h

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,37 @@ enum etoken_type {
2828
TOK_VFN_DOT = 0x00000040, /* Dot vector function */
2929
TOK_RFN = 0x00000080, /* Reduce function */
3030
TOK_OPEN_PAREN = 0x00000100,
31-
TOK_MUTED = 0x00000200,
32-
TOK_OPEN_SQUARE = 0x00000400,
33-
TOK_OPEN_CURLY = 0x00000800,
34-
TOK_CLOSE_PAREN = 0x00001000,
35-
TOK_CLOSE_SQUARE = 0x00002000,
36-
TOK_CLOSE_CURLY = 0x00004000,
37-
TOK_VAR = 0x00008000,
38-
TOK_VAR_NUM_INST = 0x00008001,
39-
TOK_VAR_INST_IDX = 0x00008002,
40-
TOK_DOLLAR = 0x00010000,
41-
TOK_HASH = 0x00020000,
42-
TOK_OP = 0x00040000,
43-
TOK_OP_UNARY = 0x00080000,
44-
TOK_COMMA = 0x00100000,
45-
TOK_COLON = 0x00200000,
46-
TOK_SEMICOLON = 0x00400000,
47-
TOK_VECTORIZE = 0x00800000,
48-
TOK_TT = 0x01000000, /* NTP Timestamp */
49-
TOK_ASSIGN = 0x02000000,
31+
TOK_OPEN_SQUARE = 0x00000200,
32+
TOK_OPEN_CURLY = 0x00000400,
33+
TOK_CLOSE_PAREN = 0x00000800,
34+
TOK_CLOSE_SQUARE = 0x00001000,
35+
TOK_CLOSE_CURLY = 0x00002000,
36+
TOK_VAR = 0x00004000,
37+
TOK_VAR_NUM_INST = 0x00004001,
38+
TOK_VAR_INST_IDX = 0x00004002,
39+
TOK_DOLLAR = 0x00008000,
40+
TOK_HASH = 0x00010000,
41+
TOK_OP = 0x00020000,
42+
TOK_OP_UNARY = 0x00040000,
43+
TOK_COMMA = 0x00080000,
44+
TOK_COLON = 0x00100000,
45+
TOK_SEMICOLON = 0x00200000,
46+
TOK_VECTORIZE = 0x00400000,
47+
TOK_TT = 0x00800000, /* NTP Timestamp */
48+
TOK_ASSIGN = 0x01000000,
5049
TOK_ASSIGN_OP,
5150
TOK_ASSIGN_USE,
5251
TOK_ASSIGN_CONST, /* Const assignment (does not require input) */
5352
TOK_ASSIGN_TT, /* Assign to NTP timestamp */
5453
TOK_ASSIGN_TT_OP, /* Assign to NTP timestamp */
55-
TOK_COPY_FROM = 0x04000000, /* Copy from stack */
54+
TOK_COPY_FROM = 0x02000000, /* Copy from stack */
5655
TOK_MOVE, /* Move stack */
5756
TOK_LAMBDA,
5857
TOK_LOOP_START,
5958
TOK_LOOP_END,
6059
TOK_SP_ADD, /* Stack pointer offset */
6160
TOK_REDUCING,
62-
TOK_END = 0x10000000
61+
TOK_END = 0x04000000
6362
};
6463

6564
struct generic_type {
@@ -625,6 +624,8 @@ static void etoken_print(etoken tok, expr_var_t *vars, int show_locks)
625624
printf(" tlock");
626625
if (TOK_ASSIGN & tok->toktype && tok->gen.flags & CLEAR_STACK)
627626
printf(" clear");
627+
if (tok->gen.flags & VAR_MUTED)
628+
printf(" muted");
628629
}
629630
}
630631
#endif /* TRACE_PARSE || TRACE_EVAL */

0 commit comments

Comments
 (0)