Skip to content

Commit 4c31f49

Browse files
committed
Implement comma separating expressions
1 parent 8d9a4be commit 4c31f49

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

shivyc/parser/declaration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import shivyc.parser.utils as p
66
import shivyc.token_kinds as token_kinds
77
import shivyc.tree.nodes as nodes
8-
from shivyc.parser.expression import parse_expression
8+
from shivyc.parser.expression import parse_assignment
99
from shivyc.parser.utils import (add_range, ParserError, match_token, token_is,
1010
raise_error)
1111

@@ -36,7 +36,7 @@ def parse_declaration(index):
3636
if token_is(index, token_kinds.equals):
3737
# Parse initializer expression
3838
# Currently, only simple initializers are supported
39-
expr, index = parse_expression(index + 1)
39+
expr, index = parse_assignment(index + 1)
4040
inits.append(expr)
4141
else:
4242
inits.append(None)

shivyc/parser/expression.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
@add_range
1111
def parse_expression(index):
1212
"""Parse expression."""
13-
# TODO: Support expressions separated by commas
14-
return parse_assignment(index)
13+
return parse_series(
14+
index, parse_assignment,
15+
{token_kinds.comma: expr_nodes.MultiExpr})
1516

1617

1718
@add_range
@@ -153,7 +154,7 @@ def parse_postfix(index):
153154
return expr_nodes.FuncCall(cur, args, tok), index + 1
154155

155156
while True:
156-
arg, index = parse_expression(index)
157+
arg, index = parse_assignment(index)
157158
args.append(arg)
158159

159160
if token_is(index, token_kinds.comma):

shivyc/tree/expr_nodes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ def make_il(self, il_code, symbol_table, c):
9999
return lvalue.val(il_code)
100100

101101

102+
class MultiExpr(_RExprNode):
103+
"""Expression that is two expressions joined by comma."""
104+
105+
def __init__(self, left, right, op):
106+
"""Initialize node."""
107+
self.left = left
108+
self.right = right
109+
self.op = op
110+
111+
def make_il(self, il_code, symbol_table, c):
112+
"""Make code for this node."""
113+
self.left.make_il(il_code, symbol_table, c)
114+
return self.right.make_il(il_code, symbol_table, c)
115+
116+
102117
class Number(_RExprNode):
103118
"""Expression that is just a single number."""
104119

tests/feature_tests/expr_comma.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int main() {
2+
int a = (3, 5);
3+
int *p = (10, &a);
4+
5+
if(a != 5) return 1;
6+
if(p != &a) return 2;
7+
}

0 commit comments

Comments
 (0)