Created
October 23, 2021 12:55
-
-
Save skejeton/a28ea31f9e8a76a0cea45a89309a7fe5 to your computer and use it in GitHub Desktop.
GARBAGESCRIPT
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
instructions = [] | |
f = open("input.grs", "r") | |
src = str(f.read()) | |
f.close() | |
def var_emit(line): | |
name = line[1:].strip() | |
instructions.append(["MAKEVAR", name]) | |
def load_emit(line): | |
val = line[1:].strip() | |
instructions.append(["LOAD", val]) | |
def call_emit(line): | |
instructions.append(["CALL", line[1:].strip()]) | |
def defn_emit(line): | |
instructions.append(["DEFN"]) | |
def return_emit(line): | |
instructions.append(["RETURN"]) | |
newsrc = "" | |
# preporcess | |
for line in src.replace("::", "\n").split("\n"): | |
line = line.strip() | |
if len(line) < 1: | |
continue | |
if line[0] == '#': | |
l = line[1:].split(" ") | |
if l[0].startswith("use"): | |
f = open(l[1].strip(), "r") | |
newsrc += f.read()+"\n" | |
f.close() | |
else: | |
newsrc += line + "\n" | |
src = newsrc | |
line_no = 1 | |
for line in src.replace("::", "\n").split("\n"): | |
line = line.strip() | |
if len(line) < 1: | |
continue | |
if line[0] == '\\': | |
var_emit(line) | |
elif line[0] == '\'': | |
load_emit(line) | |
elif line[0] == '%': | |
instructions.append(["LABEL", line[1:].strip()]) | |
elif line[0] == '$': | |
call_emit(line) | |
elif line.startswith("?"): | |
instructions.append(["IF", line[1:].strip()]) | |
elif line.startswith("{"): | |
defn_emit(line) | |
elif line.startswith("}") or line.startswith("return"): | |
return_emit(line) | |
elif line[0] == '*': | |
continue | |
else: | |
raise Exception("Invalid prefix at line {}".format(line_no)) | |
line_no += 1 | |
decls = {} | |
fns = {} | |
load = None | |
stack = [] | |
pos = 0 | |
for instr in instructions: | |
if instr[0] == "LOAD": | |
load = instr[1] | |
if instr[0] == "DEFN": | |
fns[load.strip()] = pos+1 | |
pos += 1 | |
load = None | |
#print(instructions) | |
pos = fns["main"] | |
while pos < len(instructions): | |
instr = instructions[pos] | |
#print(">>", pos, "\t", instr, stack) | |
if instr[0] == "LOAD": | |
load = instr[1] | |
elif instr[0] == "MAKEVAR": | |
decls[instr[1]] = load | |
elif instr[0] == "IF": | |
if load: | |
pos += 1 | |
continue | |
npos = pos | |
for instruc in instructions[pos:]: | |
if instruc[0] == "LABEL" and instruc[1] == instr[1]: | |
pos = npos | |
break | |
npos += 1 | |
else: | |
raise Exception("label "+instr[1]+" not found!") | |
elif instr[0] == "RETURN": | |
if len(stack) > 0: | |
pos = stack.pop() | |
else: | |
break | |
elif instr[0] == "CALL": | |
if instr[1] == "assembly": | |
exec(load) | |
else: | |
stack.append(pos) | |
pos = fns[instr[1]] | |
continue | |
pos += 1 | |
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
* This prints something * | |
#use std.grs | |
'main :: { | |
$false :: ?if | |
'Hello world :: $print | |
%if :: ?if | |
'Hello world :: $print | |
%if | |
} | |
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
* Printing * | |
'print :: { | |
\arg | |
*--------------------------------* | |
'print(decls['arg']) :: $assembly | |
} | |
* Discards * | |
'$ :: { | |
'load = None :: $assembly | |
} | |
'true :: { | |
'load = True :: $assembly | |
} | |
'false :: { | |
'load = False :: $assembly | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment