Skip to content

Commit 321c508

Browse files
committed
Some new functions
1 parent 0b58d36 commit 321c508

File tree

2 files changed

+154
-11
lines changed

2 files changed

+154
-11
lines changed

__pycache__/basic.cpython-312.pyc

9.07 KB
Binary file not shown.

basic.py

Lines changed: 154 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from operator import index
2+
13
from jinja2.runtime import new_context
24
from numba.typed.listobject import new_list
5+
import os
36

47
TT_FLOAT = 'FLOAT'
58
TT_IDENTIFIER = 'IDENTIFIER'
@@ -1449,31 +1452,34 @@ def __repr__(self):
14491452

14501453

14511454
class BuiltInFunction(BaseFunction):
1452-
def __init__(self):
1453-
super().__init__()
1455+
def __init__(self, name):
1456+
super().__init__(name)
14541457

14551458
def execute(self, args):
14561459
res = RTResult()
14571460
exec_ctx = self.generate_new_context()
14581461
method_name = f"execute_{self.name}"
14591462
method = getattr(self, method_name, self.no_visit_method)
14601463

1461-
res.register(self.check_and_populate_args(method.arg_namse, args, exec_ctx))
1464+
res.register(self.check_and_populate_args(method.arg_names, args, exec_ctx))
14621465
if res.error: return res
14631466

14641467
return_value = res.register(method(exec_ctx))
14651468
if res.error: return res
14661469
return res.success(return_value)
14671470

1468-
def no_visit_method(self, node, context):
1469-
raise Exception(f"No execute_{self.name} method deined")
1471+
def no_visit_method(self, exec_ctx):
1472+
raise Exception(f"No execute_{self.name} method defined")
14701473

14711474
def copy(self):
14721475
copy = BuiltInFunction(self.name)
14731476
copy.set_context(self.context)
14741477
copy.set_pos(self.pos_start, self.pos_end)
14751478
return copy
14761479

1480+
def __repr__(self):
1481+
return f"<Built-in function {self.name}>"
1482+
14771483
def execute_print(self, exec_ctx):
14781484
print(str(exec_ctx.symbol_table.get("value")))
14791485
return RTResult().success(Number.null)
@@ -1489,11 +1495,126 @@ def execute_input(self, exec_ctx):
14891495
text = input()
14901496
return RTResult().success(String(text))
14911497

1492-
execute_input().arg_names = []
1498+
execute_input.arg_names = []
14931499

1500+
def execute_input_int(self, exec_ctx):
1501+
while True:
1502+
value = input()
1503+
try:
1504+
value = int(value)
1505+
break
1506+
except ValueError:
1507+
print(f"'{value}' should be int , try again")
1508+
return RTResult().success(Number(value))
14941509

1495-
def __repr__(self):
1496-
return f"<Built-in function {self.name}>"
1510+
execute_input_int.arg_names = []
1511+
1512+
def execute_clear(self, exec_ctx):
1513+
os.system("cls" if os.name == "nt" else "clear")
1514+
return RTResult().success(Number.null)
1515+
1516+
execute_clear.arg_names = []
1517+
1518+
def execute_is_number(self, exec_ctx):
1519+
is_number = isinstance(exec_ctx.symbol_table.get("value"), Number)
1520+
return RTResult().success(Number.true if is_number else Number.false)
1521+
1522+
execute_is_number.arg_names = ["value"]
1523+
1524+
def execute_is_string(self, exec_ctx):
1525+
is_string = isinstance(exec_ctx.symbol_table.get("value"), String)
1526+
return RTResult().success(Number.true if is_string else Number.false)
1527+
1528+
execute_is_string.arg_names = ["value"]
1529+
1530+
def execute_is_function(self, exec_ctx):
1531+
is_func = isinstance(exec_ctx.symbol_table.get("value"), Function)
1532+
return RTResult().success(Number.true if is_func else Number.false)
1533+
1534+
execute_is_function.arg_names = ["value"]
1535+
1536+
def execute_is_list(self, exec_ctx):
1537+
is_list = isinstance(exec_ctx.symbol_table.get("value"), List)
1538+
return RTResult().success(Number.true if is_list else Number.false)
1539+
1540+
execute_is_list.arg_names = ["value"]
1541+
1542+
def execute_append(self, exec_ctx):
1543+
list_ = exec_ctx.symbol_table.get("list")
1544+
value = exec_ctx.symbol_table.get("value")
1545+
1546+
if not isinstance(list_, List):
1547+
return RTResult().failure(RTError(
1548+
self.pos_start, self.pos_end,
1549+
"First argument must be list",
1550+
exec_ctx
1551+
))
1552+
list_.elements.append(value)
1553+
return RTResult().success(Number.null)
1554+
1555+
execute_append.arg_names = ["list", "value"]
1556+
1557+
def execute_pop(self, exec_ctx):
1558+
list_ = exec_ctx.symbol_table.get("list")
1559+
index = exec_ctx.symbol_table.get("index")
1560+
1561+
if not isinstance(list_, List):
1562+
return RTResult().failure(RTError(
1563+
self.pos_start, self.pos_end,
1564+
"First argument must be list",
1565+
exec_ctx
1566+
))
1567+
1568+
if not isinstance(index, Number):
1569+
return RTResult().failure(RTError(
1570+
self.pos_start, self.pos_end,
1571+
"Second argument must be int",
1572+
exec_ctx
1573+
))
1574+
try:
1575+
element = list_.elements.pop(index.value)
1576+
except:
1577+
return RTResult().failure(RTError(
1578+
self.pos_start, self.pos_end,
1579+
"Out of index!!!",
1580+
exec_ctx
1581+
))
1582+
return RTResult().success(element)
1583+
1584+
execute_pop.arg_names = ["list", "index"]
1585+
1586+
def execute_extend(self, exec_ctx):
1587+
listA = exec_ctx.symbol_table.get("listA")
1588+
listB = exec_ctx.symbol_table.get("listB")
1589+
if not isinstance(listA, List):
1590+
return RTResult().failure(RTError(
1591+
self.pos_start, self.pos_end,
1592+
"First argument must be list",
1593+
exec_ctx
1594+
))
1595+
if not isinstance(listB, List):
1596+
return RTResult().failure(RTError(
1597+
self.pos_start, self.pos_end,
1598+
"Second argument must be list",
1599+
exec_ctx
1600+
))
1601+
listA.elements.extend(listB.elements)
1602+
return RTResult().success(Number.null)
1603+
1604+
execute_extend.arg_names = ["listA", "listB"]
1605+
1606+
BuiltInFunction.print = BuiltInFunction("print")
1607+
BuiltInFunction.print_ret = BuiltInFunction("print_ret")
1608+
BuiltInFunction.input = BuiltInFunction("input")
1609+
BuiltInFunction.input_int = BuiltInFunction("input_int")
1610+
BuiltInFunction.clear = BuiltInFunction("clear")
1611+
BuiltInFunction.is_number = BuiltInFunction("is_number")
1612+
BuiltInFunction.is_string = BuiltInFunction("is_string")
1613+
BuiltInFunction.is_list = BuiltInFunction("is_list")
1614+
BuiltInFunction.is_function = BuiltInFunction("is_function")
1615+
BuiltInFunction.append = BuiltInFunction("append")
1616+
BuiltInFunction.pop = BuiltInFunction("pop")
1617+
BuiltInFunction.extend = BuiltInFunction("extend")
14971618

14981619

14991620
class SymbolTable:
@@ -1603,7 +1724,7 @@ def visit_VarAccessNode(self, node, context):
16031724
context
16041725
))
16051726

1606-
value = value.copy().set_pos(node.pos_start, node.pos_end)
1727+
value.set_pos(node.pos_start, node.pos_end).set_context(context)
16071728
return res.success(value)
16081729

16091730
def visit_VarAssignNode(self, node, context):
@@ -1708,23 +1829,45 @@ def visit_FunDefNode(self, node, context):
17081829
def visit_CallNode(self, node, context):
17091830
res = RTResult()
17101831
args = []
1832+
1833+
# 不再 copy(),保留原函数引用
17111834
value_to_call = res.register(self.visit(node.node_to_call, context))
17121835
if res.error: return res
1713-
value_to_call = value_to_call.copy().set_pos(node.pos_start, node.pos_end)
1836+
value_to_call.set_pos(node.pos_start, node.pos_end).set_context(context)
17141837

17151838
for arg_node in node.arg_nodes:
17161839
args.append(res.register(self.visit(arg_node, context)))
17171840
if res.error: return res
17181841

17191842
return_value = res.register(value_to_call.execute(args))
17201843
if res.error: return res
1721-
return res.success(return_value)
1844+
1845+
# 返回值可以 copy,因为它是新创建的,不会影响上下文
1846+
return res.success(return_value.copy().set_pos(node.pos_start, node.pos_end).set_context(context))
17221847

17231848

17241849
global_symbol_table = SymbolTable()
17251850
global_symbol_table.set("NULL", Number.null)
17261851
global_symbol_table.set("False", Number.false)
17271852
global_symbol_table.set("True", Number.true)
1853+
global_symbol_table.set("NULL", Number.null)
1854+
global_symbol_table.set("FALSE", Number.false)
1855+
global_symbol_table.set("TRUE", Number.true)
1856+
# global_symbol_table.set("MATH_PI", Number.math_PI)
1857+
global_symbol_table.set("print", BuiltInFunction.print)
1858+
global_symbol_table.set("print_ret", BuiltInFunction.print_ret)
1859+
global_symbol_table.set("input", BuiltInFunction.input)
1860+
global_symbol_table.set("input_int", BuiltInFunction.input_int)
1861+
global_symbol_table.set("clear", BuiltInFunction.clear)
1862+
global_symbol_table.set("cls", BuiltInFunction.clear)
1863+
global_symbol_table.set("is_sum", BuiltInFunction.is_number)
1864+
global_symbol_table.set("is_str", BuiltInFunction.is_string)
1865+
global_symbol_table.set("is_list", BuiltInFunction.is_list)
1866+
global_symbol_table.set("is_fun", BuiltInFunction.is_function)
1867+
global_symbol_table.set("append", BuiltInFunction.append)
1868+
global_symbol_table.set("pop", BuiltInFunction.pop)
1869+
global_symbol_table.set("exetend", BuiltInFunction.extend)
1870+
17281871

17291872

17301873
def run(fn, text):

0 commit comments

Comments
 (0)