Skip to content

Commit

Permalink
Support global char init and logical/equality ops
Browse files Browse the repository at this point in the history
- Refactor read_numeric_sconstant function:
  - Support character initialization.
  - Support brackets with primary values only.
  - Rename to read_primary_constant for clarity.

- Refactor eval_expression_imm:
  - Add support for logical and equality operators:
    - OP_log_and, OP_log_or, OP_eq, OP_neq.

- Add tests:
  - Global character initialization.
  - Global initialization with logical and equality operations.
  • Loading branch information
AW-AlanWu committed Dec 18, 2024
1 parent 882c3ad commit 279ad0c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1828,18 +1828,24 @@ bool read_body_assignment(char *token,
return false;
}

int read_numeric_sconstant()
int read_primary_constant()
{
/* return signed constant */
int isneg = 0, res;
char buffer[10];
if (lex_accept(T_minus))
isneg = 1;
if (lex_peek(T_numeric, buffer))
if (lex_accept(T_open_bracket)) {
res = read_primary_constant();
lex_expect(T_close_bracket);
} else if (lex_peek(T_numeric, buffer)) {
res = read_numeric_constant(buffer);
else
lex_expect(T_numeric);
} else if (lex_peek(T_char, buffer)) {
res = buffer[0];
lex_expect(T_char);
} else
error("Invalid value after assignment");
lex_expect(T_numeric);
if (isneg)
return (-1) * res;
return res;
Expand Down Expand Up @@ -1879,6 +1885,18 @@ int eval_expression_imm(opcode_t op, int op1, int op2)
case OP_rshift:
res = op1 >> op2;
break;
case OP_log_and:
res = op1 && op2;
break;
case OP_log_or:
res = op1 || op2;
break;
case OP_eq:
res = op1 == op2;
break;
case OP_neq:
res = op1 != op2;
break;
case OP_lt:
res = op1 < op2 ? 1 : 0;
break;
Expand Down Expand Up @@ -1929,7 +1947,7 @@ bool read_global_assignment(char *token)
int val_stack[10];
int op_stack_index = 0, val_stack_index = 0;
int operand1, operand2;
operand1 = read_numeric_sconstant();
operand1 = read_primary_constant();
op = get_operator();
/* only one value after assignment */
if (op == OP_generic) {
Expand All @@ -1955,7 +1973,7 @@ bool read_global_assignment(char *token)
eval_ternary_imm(operand1, token);
return true;
}
operand2 = read_numeric_sconstant();
operand2 = read_primary_constant();
next_op = get_operator();
if (next_op == OP_generic) {
/* only two operands, apply and return */
Expand Down Expand Up @@ -2013,7 +2031,7 @@ bool read_global_assignment(char *token)
} while (op_stack_index > 0 && same_op == 0);
}
/* push next operand on stack */
val_stack[val_stack_index++] = read_numeric_sconstant();
val_stack[val_stack_index++] = read_primary_constant();
/* push operator on stack */
op_stack[op_stack_index++] = op;
op = get_operator();
Expand Down
23 changes: 23 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1249,4 +1249,27 @@ int main()
return 0;
}
EOF

# global character initialization
try_ 198 << EOF
char ch1 = 'A';
char ch2 = ('B');
char ch3 = (('C'));
int main()
{
return ch1 + ch2 + ch3;
}
EOF

# global initialization with logical and equality operation
try_ 4 << EOF
int b1 = 1 && 1;
int b2 = 1 || 0;
int b3 = 1 == 1;
int b4 = 1 != 2;
int main()
{
return b1 + b2 + b3 + b4;
}
EOF
echo OK

0 comments on commit 279ad0c

Please sign in to comment.