-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f6dff0
commit 2995705
Showing
18 changed files
with
42,709 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from thunno2 import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from thunno2.tokens import * | ||
from thunno2.lexer import * | ||
|
||
|
||
def auto_explain(code, indent=0, comment_indent=0, tkn=True, post_indent=0): | ||
if tkn: | ||
_, lexed = tokenise(code) | ||
else: | ||
lexed = code | ||
ret = "" | ||
index = 0 | ||
for chars, info, other in lexed: | ||
if "single function" in info or info in ( | ||
"outer product", | ||
"apply to x", | ||
"apply to y", | ||
"execute without popping", | ||
"apply to every nth item", | ||
): | ||
if len(chars) == 2: | ||
i = 1 | ||
elif len(chars) == 4: | ||
i = 2 | ||
else: | ||
if chars[-2] in DIGRAPHS: | ||
i = 2 | ||
else: | ||
i = 1 | ||
ret += ( | ||
" " * (index + indent) | ||
+ chars[:-i] | ||
+ (len(code) - index - len(chars) + i) * " " | ||
+ post_indent * " " | ||
+ " # " | ||
+ " " * comment_indent | ||
+ info | ||
+ ":\n" | ||
+ " " * (index + indent + len(chars) - i) | ||
+ chars[-i:] | ||
+ (len(code) - index - len(chars)) * " " | ||
+ post_indent * " " | ||
+ " # " | ||
+ " " * comment_indent | ||
) | ||
ret += other.keywords[0] + "\n" | ||
else: | ||
ret += ( | ||
" " * (index + indent) | ||
+ chars | ||
+ (len(code) - index - len(chars)) * " " | ||
+ post_indent * " " | ||
+ " # " | ||
+ " " * comment_indent | ||
) | ||
if isinstance(other, list): | ||
ret += info + ":\n" | ||
ret += auto_explain( | ||
code[index + 1 :], index + indent + 1, comment_indent + 1 | ||
) | ||
elif info == "if statement": | ||
t, f = other | ||
ret += info + ":\n" | ||
ret += auto_explain( | ||
t, | ||
index + indent + 1, | ||
comment_indent + 1, | ||
False, | ||
len(code) - len("".join(x[0] for x in t)) - index - 1, | ||
) | ||
index += len("".join(x[0] for x in t)) + 1 | ||
if f: | ||
ret += ( | ||
" " * (index + indent) | ||
+ ":" | ||
+ (len(code) - index - 1) * " " | ||
+ post_indent * " " | ||
+ " # " | ||
+ " " * comment_indent | ||
+ "else:\n" | ||
) | ||
ret += auto_explain( | ||
f, | ||
index + indent + 1, | ||
comment_indent + 1, | ||
False, | ||
len(code) - len("".join(x[0] for x in f)) - index - 1, | ||
) | ||
index += len("".join(x[0] for x in f)) + 1 | ||
elif info not in ("command", "digraph"): | ||
ret += info + "\n" | ||
else: | ||
ret += ( | ||
dict([(j, i) for i, j in full_list[::-1]])[chars].replace("_", " ") | ||
+ "\n" | ||
) | ||
index += len(chars) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class Canvas: | ||
def __init__(self): | ||
self.board = [] | ||
self.x, self.y = 0, 0 | ||
self.len_x, self.len_y = 0, 0 | ||
|
||
def __str__(self): | ||
return "\n".join(map("".join, self.board)) | ||
|
||
def add_xy(self, d): | ||
x, y = [(0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)][d] | ||
self.x += x | ||
self.y += y | ||
|
||
def extend(self, direction): | ||
if direction == 0: | ||
self.board = [[" " for _ in range(self.len_x)], *self.board] | ||
self.len_y += 1 | ||
elif direction == 1: | ||
self.board = [[*row, " "] for row in self.board] | ||
self.len_x += 1 | ||
elif direction == 2: | ||
self.board = [*self.board, [" " for _ in range(self.len_x)]] | ||
self.len_y += 1 | ||
elif direction == 3: | ||
self.board = [[" ", *row] for row in self.board] | ||
self.len_x += 1 | ||
|
||
def decide_extend(self): | ||
if self.y < 0: | ||
self.y = 0 | ||
self.extend(0) | ||
if self.x >= self.len_x: | ||
self.extend(1) | ||
if self.y >= self.len_y: | ||
self.extend(2) | ||
if self.x < 0: | ||
self.x = 0 | ||
self.extend(3) | ||
|
||
def draw(self, text, dirs): | ||
for c, d in zip(text, dirs): | ||
self.add_xy(d) | ||
self.decide_extend() | ||
self.board[self.y][self.x] = c | ||
|
||
def clear(self): | ||
self.board = [] | ||
self.x, self.y = 0, 0 | ||
self.len_x, self.len_y = 0, 0 | ||
|
||
|
||
canvas = Canvas() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# This codepage is exactly the same as the Jelly codepage | ||
# (https://github.com/DennisMitchell/jellylanguage/blob/master/jelly/interpreter.py#L7) | ||
|
||
"""The 256-character Single Byte Character Set for encoding Thunno 2 programs (https://en.wikipedia.org/wiki/SBCS)""" | ||
|
||
CODEPAGE = ( | ||
r"""¡¢£¤¥¦©¬®µ½¿€ÆÇÐÑ×ØŒÞßæçðıȷñ÷øœþ !"#$%&'()*+,-./0123456789:;<=>?""" | ||
r"""@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~¶""" | ||
r"""°¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾ƁƇƊƑƓƘⱮƝƤƬƲȤɓƈɗƒɠɦƙɱɲƥʠɼʂƭʋȥẠḄḌẸḤỊḲḶṂṆỌṚṢṬỤṾẈỴẒȦḂ""" | ||
r"""ĊḊĖḞĠḢİĿṀṄȮṖṘṠṪẆẊẎŻạḅḍẹḥịḳḷṃṇọṛṣṭ§Äẉỵẓȧḃċḋėḟġḣŀṁṅȯṗṙṡṫẇẋẏż«»‘’“”""" | ||
) | ||
|
||
assert len(CODEPAGE) == 256 | ||
|
||
|
||
def codepage_index(*chars): | ||
for char in chars: | ||
if char in CODEPAGE: | ||
yield CODEPAGE.index(char) | ||
else: | ||
yield -1 | ||
|
||
|
||
def utf8_to_thunno2(string): | ||
ret = "" | ||
for char in string: | ||
if ord(char) < 256: | ||
ret += CODEPAGE[ord(char)] | ||
else: | ||
ret += char | ||
return ret | ||
|
||
|
||
def thunno2_to_utf8(string): | ||
ret = "" | ||
for char in string: | ||
if char in CODEPAGE: | ||
ret += chr(CODEPAGE.index(char)) | ||
else: | ||
ret += char | ||
return ret |
Oops, something went wrong.