|
2 | 2 | import os |
3 | 3 | from types import new_class |
4 | 4 |
|
| 5 | +from altair import value |
| 6 | + |
5 | 7 | TT_FLOAT = 'FLOAT' |
6 | 8 | TT_IDENTIFIER = 'IDENTIFIER' |
7 | 9 | TT_KEYWORD = 'KEYWORD' |
@@ -1767,28 +1769,48 @@ def __repr__(self): |
1767 | 1769 | def execute_print(self, exec_ctx): |
1768 | 1770 | print(str(exec_ctx.symbol_table.get("value"))) |
1769 | 1771 | return RTResult().success(Number.null) |
| 1772 | + |
1770 | 1773 | execute_print.arg_names = ["value"] |
1771 | 1774 |
|
| 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"] |
1772 | 1786 |
|
1773 | | - def execute_add(self, exec_ctx): |
1774 | | - value1 =exec_ctx.symbol_table.get("value1") |
1775 | | - value2 =exec_ctx.symbol_table.get("value2") |
1776 | 1787 |
|
| 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") |
1777 | 1800 | if not isinstance(value1, Number) or not isinstance(value2, Number): |
1778 | 1801 | return RTResult().failure(RTError( |
1779 | 1802 | self.pos_start, self.pos_end, |
1780 | 1803 | "both of arguments must be numbers", |
1781 | 1804 | exec_ctx.context |
1782 | 1805 | )) |
1783 | | - res =value1.added_to(value2) |
| 1806 | + res = value1.added_to(value2) |
1784 | 1807 |
|
1785 | 1808 | if res[1]: |
1786 | 1809 | return RTResult.failure(res[1]) |
1787 | 1810 | return RTResult().success(res[0]) |
1788 | 1811 |
|
1789 | 1812 | execute_add.arg_names = ["value1", "value2"] |
1790 | 1813 |
|
1791 | | - |
1792 | 1814 | def execute_print_ret(self, exec_ctx): |
1793 | 1815 | return RTResult().success(String(str(exec_ctx.symbol_table.get("value")))) |
1794 | 1816 |
|
@@ -1921,6 +1943,8 @@ def execute_extend(self, exec_ctx): |
1921 | 1943 | BuiltInFunction.pop = BuiltInFunction("pop") |
1922 | 1944 | BuiltInFunction.extend = BuiltInFunction("extend") |
1923 | 1945 | BuiltInFunction.add = BuiltInFunction("add") |
| 1946 | +BuiltInFunction.abs = BuiltInFunction("abs") |
| 1947 | +BuiltInFunction.max=BuiltInFunction("max") |
1924 | 1948 |
|
1925 | 1949 |
|
1926 | 1950 | class SymbolTable: |
@@ -2186,6 +2210,10 @@ def visit_CallNode(self, node, context): |
2186 | 2210 | global_symbol_table.set("pop", BuiltInFunction.pop) |
2187 | 2211 | global_symbol_table.set("exetend", BuiltInFunction.extend) |
2188 | 2212 | 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 | + |
2189 | 2217 |
|
2190 | 2218 |
|
2191 | 2219 | def run(fn, text): |
|
0 commit comments