A fast implementation of the Nix expression language
Rev. | 376831c93fc2872a6134b52fd5bec158805e195f |
---|---|
Tamaño | 1,690 octetos |
Tiempo | 2024-06-10 07:57:27 |
Autor | Corbin |
Log Message | parser: Add weird let-expression and rec-expression.
|
import sys
from heap import defaultScope, InfiniteLoop, MissingAttr, WrongType
from parser import parse, MissingVar, NotAnExpr, ParseError
def entryPoint(argv):
if len(argv) < 2:
print "Usage: rix <file.nix>"
return 1
path = argv[1]
with open(path, "rb") as handle: expr = handle.read()
try:
ast = parse(expr)
print "AST:", ast
print "Pretty:", ast.pretty()
obj = ast.compile(defaultScope)
print "Heap object:", obj
print "Unevaluated object:", obj.asStr()
obj = obj.evaluate()
print "Evaluated object:", obj.asStr()
return 0
except ParseError as e:
print "Error while parsing"
token = e.token
print "Unexpected token:", token.gettokentype(), "(%s)" % token.getstr()
pos = token.getsourcepos()
if pos is None: print "No position information"
else: print "Error at line %d, column %d" % (pos.lineno, pos.colno)
return 1
except NotAnExpr as e:
print "Error while compiling"
print "Input syntax was not an expr, but:", e.cls
return 1
except MissingVar as e:
print "Error while compiling"
print "Name was missing:", e.name
return 1
except InfiniteLoop as e:
print "Error while executing: infinite loop"
return 1
except MissingAttr as e:
print "Error while executing"
print "Attr was missing:", e.attr
return 1
except WrongType as e:
print "Error while executing"
print "Type error:", e.message
return 1
def target(*args): return entryPoint, None
if __name__ == "__main__": entryPoint(sys.argv)