Skip to content

Commit

Permalink
Some simple refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
atzlt committed Dec 14, 2022
1 parent d7e99b6 commit 7ebbbff
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 51 deletions.
4 changes: 4 additions & 0 deletions docs/The Built-In Table.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ They're the same as Python's builtin functions.

## More Things

There are ***lots*** of built-in callers in this category, and the ones listed here are only _some_ of them. I may
complete this list later, but for now, you can refer to `interpreter/buitins.py` to see the full implementation. It
shouldn't be very hard to understand.

### List and String Manipulation

| Caller | Usage | Function |
Expand Down
63 changes: 63 additions & 0 deletions functions/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import random
from copy import deepcopy as copy

from interpreter.util_classes import Token, Env, Caller, ReturnValue


def v(x):
return [el[1] for el in x]


def product(x):
p = 1
for a in x:
p *= a
return p


def sort(x):
x = list(x)
x.sort()
return x


def pop(x, t):
c = x[0][1][-1]
t.set(x[0][0], x[0][1][:-1])
return ReturnValue(c)


def pop_at(x, t):
c = x[0][1][x[1][1]]
t.set(x[0][0], x[0][1][:x[1][1]] + x[0][1][x[1][1] + 1:])
return ReturnValue(c)


def def_new_caller(param_list: list[str], code: list[Token]):
call_id = str(random.randint(0, 65535))
suffix = "$call_arg$" + call_id
for i in range(len(code)):
if code[i].kind == Token.ID and code[i].value in param_list:
code[i].value += suffix

def run_new_caller(param: list, old_table: Env):
from interpreter.interpret import Interpreter
param = v(param)
table = Env()
table.prev = old_table
for j in range(len(param_list)):
table.def_here(param_list[j] + suffix, param[j])
ret = Interpreter(code, table, None).run()
return ReturnValue(ret)

return Caller(
len(param_list),
run_new_caller
)


def map_over(f: Caller, old_list: list, table: Env):
return [
copy(f).add_args([(None, i)]).enclose().resolve(table).ret
for i in old_list
]
53 changes: 4 additions & 49 deletions interpreter/builtins.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,10 @@
import random
from copy import deepcopy as copy
from fractions import Fraction
from math import *
from interpreter.util_classes import Caller, Env, Token, C, R

from interpreter.util_classes import C, R
from functions.num_theory import *
from primality import primality as prime
def v(x):
return [el[1] for el in x]
def product(x):
p = 1
for a in x:
p *= a
return p
def sort(x):
x = list(x)
x.sort()
return x
def pop(x, t):
c = x[0][1][-1]
t.set(x[0][0], x[0][1][:-1])
return R(c)
def pop_at(x, t):
c = x[0][1][x[1][1]]
t.set(x[0][0], x[0][1][:x[1][1]] + x[0][1][x[1][1] + 1:])
return R(c)
def def_new_caller(param_list: list[str], code: list[Token]):
call_id = str(random.randint(0, 65535))
suffix = "$call_arg$" + call_id
for i in range(len(code)):
if code[i].kind == Token.ID and code[i].value in param_list:
code[i].value += suffix

def run_new_caller(param: list, old_table: Env):
from interpreter.interpret import Interpreter
param = v(param)
table = Env()
table.prev = old_table
for j in range(len(param_list)):
table.force_def(param_list[j] + suffix, param[j])
ret = Interpreter(code, table, None).run()
return R(ret)

return Caller(
len(param_list),
run_new_caller
)
def map_over(f: Caller, old_list: list, table: Env):
return [
copy(f).add_args([(None, i)]).enclose().resolve(table).ret
for i in old_list
]
from functions.basic import *

BUILTINS_TABLE = Env(table={
"<-": C(2, lambda x, t: R(t.s(x[0][0], x[1][1]))),
":=": C(2, lambda x, t: R(t.d(x[0][0], x[1][1]))),
Expand Down
4 changes: 2 additions & 2 deletions interpreter/util_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get(self, key: str):
else:
return None

def force_def(self, key: str, val):
def def_here(self, key: str, val):
self.table[key] = val

def set(self, key: str, val, origin=True):
Expand All @@ -64,7 +64,7 @@ def set(self, key: str, val, origin=True):
return False

s = set
d = force_def
d = def_here


class Caller:
Expand Down

0 comments on commit 7ebbbff

Please sign in to comment.