🐐 Small fixes
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,5 +2,8 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
|
|
||||||
|
examples/*
|
||||||
|
!examples/*.cent
|
||||||
|
|
||||||
.claude/
|
.claude/
|
||||||
CLAUDE.md
|
CLAUDE.md
|
||||||
112
cent
112
cent
@@ -1,16 +1,16 @@
|
|||||||
#! /home/nikolaj/.pyenv/shims/python
|
#! /usr/bin/env python
|
||||||
"""
|
"""
|
||||||
Usage:
|
Usage:
|
||||||
cent (-h|--help)
|
cent (-h|--help)
|
||||||
cent -i FILE
|
cent -i FILE
|
||||||
cent -c [--keep-c] FILE
|
cent -c [-k|--keep-c] FILE
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h --help Print this help screen
|
-h --help Print this help screen
|
||||||
-i Run the interpreter
|
-i Run the interpreter
|
||||||
-c Run the compiler
|
-c Run the compiler
|
||||||
--keep-c Keep the generated C file alongside the binary
|
-k --keep-c Keep the generated C file alongside the binary
|
||||||
FILE The file to compile/interpret
|
FILE The file to compile/interpret
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
@@ -27,56 +27,56 @@ from centvrion.ast_nodes import Program
|
|||||||
from centvrion.compiler.emitter import compile_program
|
from centvrion.compiler.emitter import compile_program
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = docopt(__doc__)
|
args = docopt(__doc__)
|
||||||
file_path = args["FILE"]
|
file_path = args["FILE"]
|
||||||
with open(file_path, "r", encoding="utf-8") as file_pointer:
|
with open(file_path, "r", encoding="utf-8") as file_pointer:
|
||||||
program_text = file_pointer.read() + "\n"
|
program_text = file_pointer.read() + "\n"
|
||||||
|
|
||||||
lexer = Lexer().get_lexer()
|
lexer = Lexer().get_lexer()
|
||||||
parser = Parser()
|
parser = Parser()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tokens = lexer.lex(program_text)
|
tokens = lexer.lex(program_text)
|
||||||
program = parser.parse(tokens)
|
program = parser.parse(tokens)
|
||||||
except LexingError as e:
|
except LexingError as e:
|
||||||
pos = e.source_pos
|
pos = e.source_pos
|
||||||
char = program_text[pos.idx] if pos.idx < len(program_text) else "?"
|
char = program_text[pos.idx] if pos.idx < len(program_text) else "?"
|
||||||
sys.exit(f"CENTVRION error: Invalid character {char!r} at line {pos.lineno}, column {pos.colno}")
|
sys.exit(f"CENTVRION error: Invalid character {char!r} at line {pos.lineno}, column {pos.colno}")
|
||||||
|
|
||||||
if isinstance(program, Program):
|
if isinstance(program, Program):
|
||||||
if args["-i"]:
|
if args["-i"]:
|
||||||
try:
|
try:
|
||||||
program.eval()
|
program.eval()
|
||||||
except CentvrionError as e:
|
except CentvrionError as e:
|
||||||
sys.exit(f"CENTVRION error: {e}")
|
sys.exit(f"CENTVRION error: {e}")
|
||||||
else:
|
|
||||||
c_source = compile_program(program)
|
|
||||||
runtime_c = os.path.join(
|
|
||||||
os.path.dirname(__file__),
|
|
||||||
"centvrion", "compiler", "runtime", "cent_runtime.c"
|
|
||||||
)
|
|
||||||
out_path = os.path.splitext(file_path)[0]
|
|
||||||
if args["--keep-c"]:
|
|
||||||
tmp_path = out_path + ".c"
|
|
||||||
with open(tmp_path, "w") as f:
|
|
||||||
f.write(c_source)
|
|
||||||
subprocess.run(
|
|
||||||
["gcc", "-O2", tmp_path, runtime_c, "-o", out_path],
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".c", delete=False, mode="w") as tmp:
|
|
||||||
tmp.write(c_source)
|
|
||||||
tmp_path = tmp.name
|
|
||||||
try:
|
|
||||||
subprocess.run(
|
|
||||||
["gcc", "-O2", tmp_path, runtime_c, "-o", out_path],
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
os.unlink(tmp_path)
|
|
||||||
else:
|
else:
|
||||||
raise Exception("Output not of type 'Program'", type(program))
|
c_source = compile_program(program)
|
||||||
|
runtime_c = os.path.join(
|
||||||
|
os.path.dirname(__file__),
|
||||||
|
"centvrion", "compiler", "runtime", "cent_runtime.c"
|
||||||
|
)
|
||||||
|
out_path = os.path.splitext(file_path)[0]
|
||||||
|
if args["--keep-c"]:
|
||||||
|
tmp_path = out_path + ".c"
|
||||||
|
with open(tmp_path, "w") as f:
|
||||||
|
f.write(c_source)
|
||||||
|
subprocess.run(
|
||||||
|
["gcc", "-O2", tmp_path, runtime_c, "-o", out_path],
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
with tempfile.NamedTemporaryFile(suffix=".c", delete=False, mode="w") as tmp:
|
||||||
|
tmp.write(c_source)
|
||||||
|
tmp_path = tmp.name
|
||||||
|
try:
|
||||||
|
subprocess.run(
|
||||||
|
["gcc", "-O2", tmp_path, runtime_c, "-o", out_path],
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
os.unlink(tmp_path)
|
||||||
|
else:
|
||||||
|
raise Exception("Output not of type 'Program'", type(program))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user