Open Akuli opened 1 year ago
A toy repl made in python, based on your crepl(That repo is really great)!
""" REPL for jou """
from os import getcwd, mkdir
from subprocess import call
from shutil import rmtree
_TEMPLATE = """\
# This is a source file generated by REPL
%(imports)s
def main() -> int:
%(source)s
return 0
"""
def run(command, **kwargs):
status = call(command, **kwargs)
if status != 0:
print("Exited with status" + ' ' + str(status))
return status
def repl():
imports = ""
sources = ""
flag = True
try:
mkdir("repltemp")
except:
pass
print("Welcome to Jou REPL!")
try:
while flag:
try:
source = input(">>> ")
except KeyboardInterrupt:
print("\nInterrupt, type exit to quit.")
continue
if source == "exit":
flag = False
continue
if source.lstrip().startswith("import"):
imports += source + '\n'
continue
_SOURCE = _TEMPLATE % {
'imports': imports,
'source': source,
}
with open(getcwd() + "\\repltemp\\source.jou", "w", encoding = "utf-8") as sf:
sf.write(_SOURCE)
jou_args = ("jou" + ' ' + getcwd() + '\\repltemp\\source.jou')
if run(jou_args) == 0:
print()
finally:
rmtree("repltemp")
if __name__ == "__main__":
repl()
I don't know why, but a REPL for a compiled language seems kinda cursed.
I think it would be possible to make a REPL with LLVM. However, I don't think I want to implement it into the compiler I currently have, because currently a compiler error exits the entire process. Maybe if I write a self-hosted compiler some day.