js/jasminesnake/__main__.py

61 lines
1.5 KiB
Python
Raw Normal View History

2020-04-15 16:45:22 +10:00
from jasminesnake import __version__, __snake__
2020-04-15 16:15:56 +10:00
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:
2020-04-15 16:45:22 +10:00
print(colorama.Style.DIM + __snake__ + colorama.Style.RESET_ALL)
2020-04-15 16:15:56 +10:00
print(
colorama.Fore.BLACK
+ colorama.Back.YELLOW
+ "Don't tread on me!"
+ colorama.Back.RESET
+ colorama.Fore.RESET
)
print()
2020-04-15 16:32:57 +10:00
input_stream = InputStream("var a;\n{a=2+a;}")
2020-04-15 16:15:56 +10:00
lexer = JSL(input_stream)
stream = CommonTokenStream(lexer)
2020-04-15 16:32:57 +10:00
stream.fill()
for token in stream.tokens:
print("Token: {}".format(str(token)))
2020-04-15 16:15:56 +10:00
parser = JSP(stream)
print("Created parsers")
tree = parser.program()
2020-04-15 16:32:57 +10:00
print(tree.toStringTree(parser.ruleNames))
# ParseTreeWalker.DEFAULT.walk(WriteTreeListener(), tree)
2020-04-15 16:15:56 +10:00
if __name__ == "__main__":
main()