Skip to content

Commit 07adb41

Browse files
authored
Merge pull request #173 from AW-AlanWu/master
Support global character initialization and initialization with logical and equality operators
2 parents 882c3ad + 279ad0c commit 07adb41

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/parser.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,18 +1828,24 @@ bool read_body_assignment(char *token,
18281828
return false;
18291829
}
18301830

1831-
int read_numeric_sconstant()
1831+
int read_primary_constant()
18321832
{
18331833
/* return signed constant */
18341834
int isneg = 0, res;
18351835
char buffer[10];
18361836
if (lex_accept(T_minus))
18371837
isneg = 1;
1838-
if (lex_peek(T_numeric, buffer))
1838+
if (lex_accept(T_open_bracket)) {
1839+
res = read_primary_constant();
1840+
lex_expect(T_close_bracket);
1841+
} else if (lex_peek(T_numeric, buffer)) {
18391842
res = read_numeric_constant(buffer);
1840-
else
1843+
lex_expect(T_numeric);
1844+
} else if (lex_peek(T_char, buffer)) {
1845+
res = buffer[0];
1846+
lex_expect(T_char);
1847+
} else
18411848
error("Invalid value after assignment");
1842-
lex_expect(T_numeric);
18431849
if (isneg)
18441850
return (-1) * res;
18451851
return res;
@@ -1879,6 +1885,18 @@ int eval_expression_imm(opcode_t op, int op1, int op2)
18791885
case OP_rshift:
18801886
res = op1 >> op2;
18811887
break;
1888+
case OP_log_and:
1889+
res = op1 && op2;
1890+
break;
1891+
case OP_log_or:
1892+
res = op1 || op2;
1893+
break;
1894+
case OP_eq:
1895+
res = op1 == op2;
1896+
break;
1897+
case OP_neq:
1898+
res = op1 != op2;
1899+
break;
18821900
case OP_lt:
18831901
res = op1 < op2 ? 1 : 0;
18841902
break;
@@ -1929,7 +1947,7 @@ bool read_global_assignment(char *token)
19291947
int val_stack[10];
19301948
int op_stack_index = 0, val_stack_index = 0;
19311949
int operand1, operand2;
1932-
operand1 = read_numeric_sconstant();
1950+
operand1 = read_primary_constant();
19331951
op = get_operator();
19341952
/* only one value after assignment */
19351953
if (op == OP_generic) {
@@ -1955,7 +1973,7 @@ bool read_global_assignment(char *token)
19551973
eval_ternary_imm(operand1, token);
19561974
return true;
19571975
}
1958-
operand2 = read_numeric_sconstant();
1976+
operand2 = read_primary_constant();
19591977
next_op = get_operator();
19601978
if (next_op == OP_generic) {
19611979
/* only two operands, apply and return */
@@ -2013,7 +2031,7 @@ bool read_global_assignment(char *token)
20132031
} while (op_stack_index > 0 && same_op == 0);
20142032
}
20152033
/* push next operand on stack */
2016-
val_stack[val_stack_index++] = read_numeric_sconstant();
2034+
val_stack[val_stack_index++] = read_primary_constant();
20172035
/* push operator on stack */
20182036
op_stack[op_stack_index++] = op;
20192037
op = get_operator();

tests/driver.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,4 +1249,27 @@ int main()
12491249
return 0;
12501250
}
12511251
EOF
1252+
1253+
# global character initialization
1254+
try_ 198 << EOF
1255+
char ch1 = 'A';
1256+
char ch2 = ('B');
1257+
char ch3 = (('C'));
1258+
int main()
1259+
{
1260+
return ch1 + ch2 + ch3;
1261+
}
1262+
EOF
1263+
1264+
# global initialization with logical and equality operation
1265+
try_ 4 << EOF
1266+
int b1 = 1 && 1;
1267+
int b2 = 1 || 0;
1268+
int b3 = 1 == 1;
1269+
int b4 = 1 != 2;
1270+
int main()
1271+
{
1272+
return b1 + b2 + b3 + b4;
1273+
}
1274+
EOF
12521275
echo OK

0 commit comments

Comments
 (0)