Skip to content

Commit 677a175

Browse files
committed
Some built-in function update
1 parent 29c82b5 commit 677a175

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

ZeroLang.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import os
33
from types import new_class
44

5+
from altair import value
6+
57
TT_FLOAT = 'FLOAT'
68
TT_IDENTIFIER = 'IDENTIFIER'
79
TT_KEYWORD = 'KEYWORD'
@@ -1767,28 +1769,48 @@ def __repr__(self):
17671769
def execute_print(self, exec_ctx):
17681770
print(str(exec_ctx.symbol_table.get("value")))
17691771
return RTResult().success(Number.null)
1772+
17701773
execute_print.arg_names = ["value"]
17711774

1775+
def execute_abs(self, exec_ctx):
1776+
value1 = exec_ctx.symbol_table.get("value1")
1777+
if not isinstance(value1, Number):
1778+
return RTResult().failure(RTError(
1779+
self.pos_start, self.pos_end,
1780+
f"{value1} must be a number",
1781+
self.context
1782+
))
1783+
res = Number(abs(value1.value))
1784+
return RTResult().success(res)
1785+
execute_abs.arg_names = ["value1"]
17721786

1773-
def execute_add(self, exec_ctx):
1774-
value1 =exec_ctx.symbol_table.get("value1")
1775-
value2 =exec_ctx.symbol_table.get("value2")
17761787

1788+
def execute_max(self,exec_ctx):
1789+
value1 = exec_ctx.symbol_table.get("value1")
1790+
value2 = exec_ctx.symbol_table.get("value2")
1791+
res = Number(max(value1.value, value2.value))
1792+
return RTResult().success(res)
1793+
1794+
execute_max.arg_names = ["value1","value2"]
1795+
1796+
1797+
def execute_add(self, exec_ctx):
1798+
value1 = exec_ctx.symbol_table.get("value1")
1799+
value2 = exec_ctx.symbol_table.get("value2")
17771800
if not isinstance(value1, Number) or not isinstance(value2, Number):
17781801
return RTResult().failure(RTError(
17791802
self.pos_start, self.pos_end,
17801803
"both of arguments must be numbers",
17811804
exec_ctx.context
17821805
))
1783-
res =value1.added_to(value2)
1806+
res = value1.added_to(value2)
17841807

17851808
if res[1]:
17861809
return RTResult.failure(res[1])
17871810
return RTResult().success(res[0])
17881811

17891812
execute_add.arg_names = ["value1", "value2"]
17901813

1791-
17921814
def execute_print_ret(self, exec_ctx):
17931815
return RTResult().success(String(str(exec_ctx.symbol_table.get("value"))))
17941816

@@ -1921,6 +1943,8 @@ def execute_extend(self, exec_ctx):
19211943
BuiltInFunction.pop = BuiltInFunction("pop")
19221944
BuiltInFunction.extend = BuiltInFunction("extend")
19231945
BuiltInFunction.add = BuiltInFunction("add")
1946+
BuiltInFunction.abs = BuiltInFunction("abs")
1947+
BuiltInFunction.max=BuiltInFunction("max")
19241948

19251949

19261950
class SymbolTable:
@@ -2186,6 +2210,10 @@ def visit_CallNode(self, node, context):
21862210
global_symbol_table.set("pop", BuiltInFunction.pop)
21872211
global_symbol_table.set("exetend", BuiltInFunction.extend)
21882212
global_symbol_table.set("add", BuiltInFunction.add)
2213+
global_symbol_table.set("abs", BuiltInFunction.abs)
2214+
global_symbol_table.set("max", BuiltInFunction.max)
2215+
2216+
21892217

21902218

21912219
def run(fn, text):
1.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)