Skip to content

Commit b460bce

Browse files
committed
break implementation. Thanks to Vladimir Miklashevich
1 parent 2b5f00c commit b460bce

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

tools/brus16_dsl.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ def trans_expr(env, node):
113113
raise SyntaxError(ast.unparse(node))
114114

115115

116-
def trans_if(env, test, true, false):
116+
def trans_if(env, test, true, false, end_label):
117117
test = trans_expr(env, test)
118-
true = trans_block(env, true)
119-
false = trans_block(env, false)
118+
true = trans_block(env, true, end_label)
119+
false = trans_block(env, false, end_label)
120120
L1, L2 = next(new_label), next(new_label)
121121
return [*test,
122122
('JZ', L1),
@@ -129,17 +129,17 @@ def trans_if(env, test, true, false):
129129

130130
def trans_while(env, test, body):
131131
test = trans_expr(env, test)
132-
body = trans_block(env, body)
133-
L1, L2 = next(new_label), next(new_label)
132+
L1, end_label = next(new_label), next(new_label)
133+
body = trans_block(env, body, end_label)
134134
return [('LABEL', L1),
135135
*test,
136-
('JZ', L2),
136+
('JZ', end_label),
137137
*body,
138138
('JMP', L1),
139-
('LABEL', L2)]
139+
('LABEL', end_label)]
140140

141141

142-
def trans_stmt(env, node):
142+
def trans_stmt(env, node, end_label):
143143
match node:
144144
case ast.Assign([ast.Name(name)], expr):
145145
return trans_store(env, name, expr)
@@ -152,11 +152,13 @@ def trans_stmt(env, node):
152152
return trans_stmt(env, ast.Assign([target],
153153
ast.BinOp(target, op, val)))
154154
case ast.If(test, true, false):
155-
return trans_if(env, test, true, false)
155+
return trans_if(env, test, true, false, end_label)
156156
case ast.While(test, body, []):
157157
return trans_while(env, test, body)
158158
case ast.Return(None):
159159
return [('RET', None)]
160+
case ast.Break() if end_label is not None:
161+
return [('JMP', end_label)]
160162
case ast.Return(val):
161163
return [*trans_expr(env, val), ('RET', None)]
162164
case ast.Expr(ast.Call() as call):
@@ -167,8 +169,8 @@ def trans_stmt(env, node):
167169
raise SyntaxError(ast.unparse(node))
168170

169171

170-
def trans_block(env, block):
171-
return sum([trans_stmt(env, stmt) for stmt in block], [])
172+
def trans_block(env, block, end_label=None):
173+
return sum([trans_stmt(env, stmt, end_label) for stmt in block], [])
172174

173175

174176
def replace_locs(locs, asm):

0 commit comments

Comments
 (0)