Skip to content

Commit 29c82b5

Browse files
committed
Fix bugs of func def
1 parent 871e709 commit 29c82b5

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

ZeroLang.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,24 +1246,20 @@ def func_def(self):
12461246
))
12471247
res.register_advancement()
12481248
self.advance()
1249+
12491250
var_name_tok = None
12501251
if self.current_tok.type == TT_IDENTIFIER:
12511252
var_name_tok = self.current_tok
12521253
res.register_advancement()
12531254
self.advance()
1255+
12541256
if self.current_tok.type != TT_LPAREN:
12551257
return res.failure(InvalidSyntaxError(
12561258
self.current_tok.pos_start,
12571259
self.current_tok.pos_end,
12581260
f"Expected '(' "
12591261
))
1260-
else:
1261-
if self.current_tok.type != TT_LPAREN:
1262-
return res.failure(InvalidSyntaxError(
1263-
self.current_tok.pos_start,
1264-
self.current_tok.pos_end,
1265-
f"Expected identifier or '(' "
1266-
))
1262+
12671263
res.register_advancement()
12681264
self.advance()
12691265

@@ -1280,27 +1276,21 @@ def func_def(self):
12801276
return res.failure(InvalidSyntaxError(
12811277
self.current_tok.pos_start,
12821278
self.current_tok.pos_end,
1283-
f"Expected 'identifier' "
1279+
f"Expected identifier"
12841280
))
1285-
12861281
arg_name_toks.append(self.current_tok)
12871282
res.register_advancement()
12881283
self.advance()
1284+
12891285
if self.current_tok.type != TT_RPAREN:
12901286
return res.failure(InvalidSyntaxError(
12911287
self.current_tok.pos_start,
12921288
self.current_tok.pos_end,
1293-
f"Expected , or ')' "
1289+
f"Expected ')'"
12941290
))
1295-
else:
1296-
if self.current_tok.type != TT_RPAREN:
1297-
return res.failure(InvalidSyntaxError(
1298-
self.current_tok.pos_start,
1299-
self.current_tok.pos_end,
1300-
f"Expected identifier or ')' "
1301-
))
1302-
res.register_advancement()
1303-
self.advance()
1291+
1292+
res.register_advancement()
1293+
self.advance()
13041294

13051295
if self.current_tok.type == TT_ARROW:
13061296
res.register_advancement()
@@ -1315,7 +1305,7 @@ def func_def(self):
13151305
False
13161306
))
13171307

1318-
if self.current_tok.type == TT_ARROW or self.current_tok.type == TT_NEWLINE:
1308+
if self.current_tok.type == TT_NEWLINE:
13191309
res.register_advancement()
13201310
self.advance()
13211311

@@ -1330,18 +1320,18 @@ def func_def(self):
13301320
res.register_advancement()
13311321
self.advance()
13321322

1333-
res.register_advancement()
1334-
self.advance()
1335-
if res.error: return res
1323+
return res.success(FuncDefNode(
1324+
var_name_tok,
1325+
arg_name_toks,
1326+
body,
1327+
True
1328+
))
13361329

1337-
return res.success(
1338-
FunDefNode(
1339-
var_name_tok,
1340-
arg_name_toks,
1341-
body,
1342-
True
1343-
)
1344-
)
1330+
return res.failure(InvalidSyntaxError(
1331+
self.current_tok.pos_start,
1332+
self.current_tok.pos_end,
1333+
f"Expected '->' or newline"
1334+
))
13451335

13461336

13471337
class RTResult:
@@ -1729,9 +1719,11 @@ def execute(self, args):
17291719
interpreter = Interpreter()
17301720
exec_ctx = self.generate_new_context()
17311721
res.register(self.check_and_populate_args(self.arg_names, args, exec_ctx=exec_ctx))
1722+
if res.error: return res
17321723
value = res.register(interpreter.visit(self.body_node, exec_ctx))
17331724
if res.error: return res
1734-
return res.success(Number.null if self.should_return_null else value.value)
1725+
# return res.success(Number.null if self.should_return_null else value)
1726+
return res.success(value)
17351727

17361728
def copy(self):
17371729
copy = Function(self.name, self.body_node, self.arg_names, self.should_return_null)
@@ -2142,7 +2134,7 @@ def visit_IfNode(self, node, context):
21422134

21432135
return res.success(None)
21442136

2145-
def visit_FunDefNode(self, node, context):
2137+
def visit_FuncDefNode(self, node, context):
21462138
# Here we are not sure about return
21472139
should_return_null = True
21482140
res = RTResult()
-723 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)