Skip to content

Commit

Permalink
Merge pull request #87 from Thunno/reduces
Browse files Browse the repository at this point in the history
Add some reducing commands
  • Loading branch information
nayakrujul authored May 6, 2023
2 parents 294a277 + aaaec78 commit 0f0c056
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
26 changes: 25 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ Usage: `Ƙ CODE ;`. Push the first `a` positive integers where the result of `CO

# `Ʋ` (Cumulative reduce by)

Usage: `Ʋ CODE ;`. Cumulatively reduce `a` by `CODE`
Usage: `Ʋ CODE ;`. Cumulative reduce (scanl) `a` by `CODE`

***

Expand Down Expand Up @@ -1971,4 +1971,28 @@ Usage: `µµ CODE ;`. Start recursive list generation, executing `CODE` to get e

* `any a`, `any b`: push `b ^ a`

***

# `µƲ` (Single function reduce by)

Reduce (foldl) the elements of `a` by the next command.

***

# `µɼ` (Single function right reduce by)

Right reduce (foldr) the elements of `a` by the next command.

***

# `µƇ` (Single function right cumulative reduce by)

Right cumulative reduce (scanr) the elements of `a` by the next command.

***

# `µʋ` (Right reduce by)

Usage: `Ʋ CODE ;`. Right reduce (foldr) `a` by `CODE`

***
Binary file modified src/__pycache__/test_run.cpython-39.pyc
Binary file not shown.
Binary file modified src/thunno2/__pycache__/interpreter.cpython-39.pyc
Binary file not shown.
Binary file modified src/thunno2/__pycache__/lexer.cpython-39.pyc
Binary file not shown.
66 changes: 66 additions & 0 deletions src/thunno2/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,72 @@ def run(code, *, n, iteration_index):
ctx.stack.push(r)
else:
ctx.stack.push([])
elif desc == "single function reduce by":
a = next(ctx.stack.rmv(1))
x = listify(a)
old_stack = Stack(copy.deepcopy(list(ctx.stack).copy()))
func = info
if func != Void:
if x:
r = [x.pop(0)]
for i, j in enumerate(x):
ctx.stack = Stack(copy.deepcopy(old_stack))
ctx.stack.push(r[-1])
ctx.stack.push(j)
for k in func():
r.append(k)
ctx.stack.push(r[-1])
else:
ctx.stack.push([])
elif desc == "single function right reduce by":
a = next(ctx.stack.rmv(1))
x = listify(a)[::-1]
old_stack = Stack(copy.deepcopy(list(ctx.stack).copy()))
func = info
if func != Void:
if x:
r = [x.pop(0)]
for i, j in enumerate(x):
ctx.stack = Stack(copy.deepcopy(old_stack))
ctx.stack.push(r[-1])
ctx.stack.push(j)
for k in func():
r.append(k)
ctx.stack.push(r[-1])
else:
ctx.stack.push([])
elif desc == "single function right cumulative reduce by":
a = next(ctx.stack.rmv(1))
x = listify(a)[::-1]
old_stack = Stack(copy.deepcopy(list(ctx.stack).copy()))
func = info
if func != Void:
if x:
r = [x.pop(0)]
for i, j in enumerate(x):
ctx.stack = Stack(copy.deepcopy(old_stack))
ctx.stack.push(r[-1])
ctx.stack.push(j)
for k in func():
r.append(k)
ctx.stack.push(r)
else:
ctx.stack.push([])
elif desc == "right reduce by":
a = next(ctx.stack.rmv(1))
x = listify(a)[::-1]
old_stack = Stack(copy.deepcopy(list(ctx.stack).copy()))
if x:
r = [x.pop(0)]
for i, j in enumerate(x):
ctx.stack = Stack(copy.deepcopy(old_stack))
ctx.stack.push(r[-1])
ctx.stack.push(j)
run(info, n=j, iteration_index=i)
r.append(next(ctx.stack.rmv(1)))
ctx.stack.push(r[-1])
else:
ctx.stack.push([])
elif desc == "for loop":
a = next(ctx.stack.rmv(1))
x = listify(a)
Expand Down
30 changes: 30 additions & 0 deletions src/thunno2/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,36 @@ def tokenise(code, expected_end=""):
ret.append((y, "recursive environment", r))
elif y == "µ£":
ret.append((y, "print each", 0))
elif y == "µƲ":
index += 1
cmd = code[index]
if cmd in DIGRAPHS:
index += 1
cmd += code[index]
func = get_a_function(cmd)
ret.append((char + cmd, "single function reduce by", func))
elif y == "µɼ":
index += 1
cmd = code[index]
if cmd in DIGRAPHS:
index += 1
cmd += code[index]
func = get_a_function(cmd)
ret.append((char + cmd, "single function right reduce by", func))
elif y == "µƇ":
index += 1
cmd = code[index]
if cmd in DIGRAPHS:
index += 1
cmd += code[index]
func = get_a_function(cmd)
ret.append(
(char + cmd, "single function right cumulative reduce by", func)
)
elif y == "µʋ":
i, r = tokenise(code[index + 1 :], expected_end=";")
index += i
ret.append((y, "right reduce by", r))
else:
ret.append((y, "digraph", get_a_function(y)))
except:
Expand Down

0 comments on commit 0f0c056

Please sign in to comment.