Files
centvrion/cent
2026-04-01 14:45:48 +02:00

53 lines
1.4 KiB
Python
Executable File

#! /home/nikolaj/.pyenv/shims/python
"""
Usage:
cent (-h|--help)
cent (-i|-c) FILE
Options:
-h --help Print this help screen
-i Run the interpreter
-c Run the compiler
FILE The file to compile/interpret
"""
import sys
from docopt import docopt
from rply.errors import LexingError
from centvrion.errors import CentvrionError
from centvrion.lexer import Lexer
from centvrion.parser import Parser
from centvrion.ast_nodes import Program
def main():
args = docopt(__doc__)
file_path = args["FILE"]
with open(file_path, "r", encoding="utf-8") as file_pointer:
program_text = file_pointer.read() + "\n"
lexer = Lexer().get_lexer()
parser = Parser()
try:
tokens = lexer.lex(program_text)
program = parser.parse(tokens)
except LexingError as e:
pos = e.source_pos
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}")
if isinstance(program, Program):
if args["-i"]:
try:
program.eval()
except CentvrionError as e:
sys.exit(f"CENTVRION error: {e}")
else:
raise Exception("Compiler not implemented")
else:
raise Exception("Output not of type 'Program'", type(program))
if __name__ == "__main__":
main()