Skip to content

Commit 2abeb8b

Browse files
committed
Now the zerolang supprot muilteline with
1 parent 3a98341 commit 2abeb8b

File tree

2 files changed

+96
-33
lines changed

2 files changed

+96
-33
lines changed

ZeroLang.py

Lines changed: 96 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -462,32 +462,32 @@ def __init__(self, cases, else_case):
462462

463463

464464
class ForNode:
465-
def __init__(self, var_name_tok, start_value_node, end_value_node, step_value_node, body_node):
465+
def __init__(self, var_name_tok, start_value_node, end_value_node, step_value_node, body_node,should_return_null):
466466
self.var_name_tok = var_name_tok
467467
self.start_value_node = start_value_node
468468
self.end_value_node = end_value_node
469469
self.step_value_node = step_value_node
470-
470+
self.should_return_null = should_return_null
471471
self.pos_start = self.var_name_tok.pos_start
472472
self.pos_end = self.var_name_tok.pos_end
473473
self.body_node = body_node
474474

475475

476476
class WhileNode:
477-
def __init__(self, condition_node, body_node):
477+
def __init__(self, condition_node, body_node,should_return_null):
478478
self.condition_node = condition_node
479479
self.body_node = body_node
480-
480+
self.should_return_null = should_return_null
481481
self.pos_start = self.condition_node.pos_start
482482
self.pos_end = self.body_node.pos_end
483483

484484

485485
class FuncDefNode:
486-
def __init__(self, var_name_tok, arg_name_toks, body_node):
486+
def __init__(self, var_name_tok, arg_name_toks, body_node, should_return_null):
487487
self.var_name_tok = var_name_tok
488488
self.arg_name_toks = arg_name_toks
489489
self.body_node = body_node
490-
490+
self.should_return_null = should_return_null
491491
if self.var_name_tok:
492492
self.pos_start = self.var_name_tok.pos_start
493493
elif len(self.arg_name_toks) > 0:
@@ -1108,15 +1108,32 @@ def for_expr(self):
11081108
f"Expected 'THEN'"
11091109
))
11101110

1111+
if self.current_tok.type ==TT_NEWLINE:
1112+
res.register_advancement()
1113+
self.advance()
1114+
1115+
body=res.register(self.statements())
1116+
if res.error: return res
1117+
1118+
if not self.current_tok.matches(TT_KEYWORD, 'end'):
1119+
return res.failure(InvalidSyntaxError(
1120+
self.current_tok.pos_start, self.current_tok.pos_end,
1121+
f"Expected 'end'"
1122+
))
1123+
res.register_advancement()
1124+
self.advance()
1125+
#TODO:Change the function setting
1126+
return res.success(ForNode(var_name_tok=var_name, start_value_node=start_value, end_value_node=end_value,
1127+
step_value_node=step_value, body_node=body,should_return_null=True))
1128+
11111129
res.register_advancement()
11121130
self.advance()
11131131

11141132
body = res.register(self.expr())
11151133
if res.error: return res
1116-
1134+
#TODO:change the function setting
11171135
return res.success(ForNode(var_name_tok=var_name, start_value_node=start_value, end_value_node=end_value,
1118-
step_value_node=step_value, body_node=body))
1119-
1136+
step_value_node=step_value, body_node=body,should_return_null=False))
11201137
def while_expr(self):
11211138
res = ParseResult()
11221139

@@ -1138,13 +1155,31 @@ def while_expr(self):
11381155
f"Expected 'then'"
11391156
))
11401157

1158+
1159+
11411160
res.register_advancement()
11421161
self.advance()
11431162

1144-
body = res.register(self.expr())
1145-
if res.error: return res
1163+
if self.current_tok.type ==TT_NEWLINE:
1164+
res.register_advancement()
1165+
self.advance()
1166+
1167+
body=res.register(self.statements())
1168+
if res.error: return res
1169+
1170+
if not self.current_tok.matches(TT_KEYWORD, 'end'):
1171+
return res.failure(InvalidSyntaxError(
1172+
self.current_tok.pos_start, self.current_tok.pos_end,
1173+
f"Expected 'end'"
1174+
))
1175+
res.register_advancement()
1176+
self.advance()
11461177

1147-
return res.success(WhileNode(condition, body))
1178+
return res.success(WhileNode(condition,body,True))
1179+
1180+
body=res.register(self.expr())
1181+
if res.error: return res
1182+
return res.success(WhileNode(condition,body,False))
11481183

11491184
def expr(self):
11501185
res = ParseResult()
@@ -1269,24 +1304,49 @@ def func_def(self):
12691304
res.register_advancement()
12701305
self.advance()
12711306

1272-
if self.current_tok.type != TT_ARROW:
1273-
return res.failure(InvalidSyntaxError(
1274-
self.current_tok.pos_start,
1275-
self.current_tok.pos_end,
1276-
f"Expected '->' "
1277-
))
1278-
res.register_advancement()
1279-
self.advance()
1280-
node_to_return = res.register(self.expr())
1281-
if res.error: return res
1307+
if self.current_tok.type == TT_ARROW:
1308+
res.register_advancement()
1309+
self.advance()
12821310

1283-
return res.success(
1284-
FunDefNode(
1311+
body = res.register(self.expr())
1312+
if res.error: return res
1313+
return res.success(FuncDefNode(
12851314
var_name_tok,
12861315
arg_name_toks,
1287-
node_to_return
1316+
body,
1317+
False
1318+
))
1319+
1320+
if self.current_tok.type == TT_ARROW or self.current_tok.type == TT_NEWLINE:
1321+
res.register_advancement()
1322+
self.advance()
1323+
1324+
body = res.register(self.statements())
1325+
if res.error: return res
1326+
1327+
if not self.current_tok.matches(TT_KEYWORD, 'end'):
1328+
return res.failure(InvalidSyntaxError(
1329+
self.current_tok.pos_start, self.current_tok.pos_end,
1330+
f"Expected 'end'"
1331+
))
1332+
res.register_advancement()
1333+
self.advance()
1334+
1335+
1336+
1337+
1338+
res.register_advancement()
1339+
self.advance()
1340+
if res.error: return res
1341+
1342+
return res.success(
1343+
FunDefNode(
1344+
var_name_tok,
1345+
arg_name_toks,
1346+
body,
1347+
True
1348+
)
12881349
)
1289-
)
12901350

12911351

12921352
class RTResult:
@@ -1662,10 +1722,11 @@ def check_and_populate_args(self, arg_names, args, exec_ctx):
16621722

16631723

16641724
class Function(BaseFunction):
1665-
def __init__(self, name, body_node, arg_names):
1725+
def __init__(self, name, body_node, arg_names,should_return_null):
16661726
super().__init__(name)
16671727
self.body_node = body_node
16681728
self.arg_names = arg_names
1729+
self.should_return_null = should_return_null
16691730

16701731
def execute(self, args):
16711732
res = RTResult()
@@ -1674,10 +1735,10 @@ def execute(self, args):
16741735
res.register(self.check_and_populate_args(self.arg_names, args, exec_ctx=exec_ctx))
16751736
value = res.register(interpreter.visit(self.body_node, exec_ctx))
16761737
if res.error: return res
1677-
return res.success(value)
1738+
return res.success(Number.null if self.should_return_null else value.value)
16781739

16791740
def copy(self):
1680-
copy = Function(self.name, self.body_node, self.arg_names)
1741+
copy = Function(self.name, self.body_node, self.arg_names,self.should_return_null)
16811742
copy.set_context(self.context)
16821743
copy.set_pos(self.pos_start, self.pos_end)
16831744
return copy
@@ -2058,19 +2119,21 @@ def visit_IfNode(self, node, context):
20582119
return res.success(expr_value)
20592120

20602121
if node.else_case:
2061-
else_value = res.register(self.visit(node.else_case, context))
2122+
expr,should_return_null=node.else_case
2123+
expr_value = res.register(self.visit(expr, context))
20622124
if res.error: return res
2063-
return res.success(else_value)
2125+
return res.success(Number.null if should_return_null else expr_value)
20642126

20652127
return res.success(None)
20662128

20672129
def visit_FunDefNode(self, node, context):
2068-
2130+
#Here we are not sure about return
2131+
should_return_null=True
20692132
res = RTResult()
20702133
func_name = node.var_name_tok.value if node.var_name_tok else None
20712134
body_node = node.body_node
20722135
arg_names = [arg_name.value for arg_name in node.arg_name_toks]
2073-
func_value = Function(func_name, body_node, arg_names).set_context(context).set_pos(
2136+
func_value = Function(func_name, body_node, arg_names,should_return_null).set_context(context).set_pos(
20742137
node.pos_start, node.pos_end
20752138
)
20762139
if node.var_name_tok:
2.73 KB
Binary file not shown.

0 commit comments

Comments
 (0)