js/jasminesnake/__main__.py

61 lines
1.5 KiB
Python

from jasminesnake import __version__, __snake__
from antlr4 import *
from .lex import JavaScriptLexer, JavaScriptParser
import argparse
import colorama
arg_parser = argparse.ArgumentParser(
description="Jasmine Snake, another JS interpreter in Python",
epilog="I hope you don't use it, **especially** in production.",
)
arg_parser.add_argument("--snake", action="store_true", help="Print a snake")
args = arg_parser.parse_args()
JSL = JavaScriptLexer.JavaScriptLexer
JSP = JavaScriptParser.JavaScriptParser
class WriteTreeListener(ParseTreeListener):
def visitTerminal(self, node: TerminalNode):
print("Visit Terminal: " + str(node) + " - " + repr(node))
def main():
colorama.init()
print("Jasmine Snake v{version}".format(version=__version__))
if args.snake:
print(colorama.Style.DIM + __snake__ + colorama.Style.RESET_ALL)
print(
colorama.Fore.BLACK
+ colorama.Back.YELLOW
+ "Don't tread on me!"
+ colorama.Back.RESET
+ colorama.Fore.RESET
)
print()
input_stream = InputStream("var a;\n{a=2+a;}")
lexer = JSL(input_stream)
stream = CommonTokenStream(lexer)
stream.fill()
for token in stream.tokens:
print("Token: {}".format(str(token)))
parser = JSP(stream)
print("Created parsers")
tree = parser.program()
print(tree.toStringTree(parser.ruleNames))
# ParseTreeWalker.DEFAULT.walk(WriteTreeListener(), tree)
if __name__ == "__main__":
main()