Skip to content

Commit dd401d2

Browse files
author
Andy Chu
committed
[refactor] Make the structure of the Transformer clearer.
Related to issue #387.
1 parent 49b0649 commit dd401d2

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

oil_lang/expr_to_ast.py

+45-43
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class Transformer(object):
4646
Other:
4747
frontend/parse_lib.py (turn on print_parse_tree)
4848
49+
Public methods:
50+
Expr, OilAssign
51+
atom, trailer, etc. are private, named after productions in grammar.pgen2.
4952
"""
5053
def __init__(self, gr):
5154
# type: (Grammar) -> None
@@ -70,7 +73,7 @@ def _AssocBinary(self, children):
7073
assert isinstance(op.tok, syntax_asdl.token)
7174
return expr.Binary(op.tok, self.Expr(left), right)
7275

73-
def _Trailer(self, base, p_trailer):
76+
def trailer(self, base, p_trailer):
7477
# type: (expr_t, PNode) -> expr_t
7578
children = p_trailer.children
7679
op_tok = children[0].tok
@@ -105,47 +108,6 @@ def _Trailer(self, base, p_trailer):
105108

106109
raise AssertionError(op_tok)
107110

108-
def OilAssign(self, pnode):
109-
# type: (PNode) -> command__OilAssign
110-
"""Transform an Oil assignment statement."""
111-
typ = pnode.typ
112-
children = pnode.children
113-
114-
if typ == grammar_nt.oil_var:
115-
# assign: lvalue_list [type_expr] '=' testlist (';' | '\n')
116-
117-
#log('len(children) = %d', len(children))
118-
119-
lvalue = self.Expr(children[0]) # could be a tuple
120-
#log('lvalue %s', lvalue)
121-
122-
n = len(children)
123-
if n == 4:
124-
op_tok = children[1].tok
125-
rhs = children[2]
126-
elif n == 5:
127-
# TODO: translate type expression
128-
op_tok = children[2].tok
129-
rhs = children[3]
130-
131-
else:
132-
raise AssertionError(n)
133-
134-
# The caller should fill in the keyword token.
135-
# TODO: type expression
136-
return command.OilAssign(None, lvalue, op_tok, self.Expr(rhs))
137-
138-
if typ == grammar_nt.oil_setvar:
139-
# oil_setvar: lvalue_list (augassign | '=') testlist (Op_Semi | Op_Newline)
140-
lvalue = self.Expr(children[0]) # could be a tuple
141-
op_tok = children[1].tok
142-
rhs = children[2]
143-
return command.OilAssign(None, lvalue, op_tok, self.Expr(rhs))
144-
145-
nt_name = self.number2symbol[typ]
146-
raise AssertionError(
147-
"PNode type %d (%s) wasn't handled" % (typ, nt_name))
148-
149111
def atom(self, children):
150112
# type: (List[PNode]) -> expr_t
151113
"""Handles alternatives of 'atom' where there is more than one child."""
@@ -245,7 +207,7 @@ def Expr(self, pnode):
245207
for i in xrange(1, n):
246208
pnode = children[i]
247209
tok = pnode.tok
248-
base = self._Trailer(base, pnode)
210+
base = self.trailer(base, pnode)
249211

250212
return base
251213

@@ -332,3 +294,43 @@ def Expr(self, pnode):
332294
else:
333295
raise AssertionError(tok.id)
334296

297+
def OilAssign(self, pnode):
298+
# type: (PNode) -> command__OilAssign
299+
"""Transform an Oil assignment statement."""
300+
typ = pnode.typ
301+
children = pnode.children
302+
303+
if typ == grammar_nt.oil_var:
304+
# assign: lvalue_list [type_expr] '=' testlist (';' | '\n')
305+
306+
#log('len(children) = %d', len(children))
307+
308+
lvalue = self.Expr(children[0]) # could be a tuple
309+
#log('lvalue %s', lvalue)
310+
311+
n = len(children)
312+
if n == 4:
313+
op_tok = children[1].tok
314+
rhs = children[2]
315+
elif n == 5:
316+
# TODO: translate type expression
317+
op_tok = children[2].tok
318+
rhs = children[3]
319+
320+
else:
321+
raise AssertionError(n)
322+
323+
# The caller should fill in the keyword token.
324+
# TODO: type expression
325+
return command.OilAssign(None, lvalue, op_tok, self.Expr(rhs))
326+
327+
if typ == grammar_nt.oil_setvar:
328+
# oil_setvar: lvalue_list (augassign | '=') testlist (Op_Semi | Op_Newline)
329+
lvalue = self.Expr(children[0]) # could be a tuple
330+
op_tok = children[1].tok
331+
rhs = children[2]
332+
return command.OilAssign(None, lvalue, op_tok, self.Expr(rhs))
333+
334+
nt_name = self.number2symbol[typ]
335+
raise AssertionError(
336+
"PNode type %d (%s) wasn't handled" % (typ, nt_name))

0 commit comments

Comments
 (0)