Ejemplo n.º 1
0
def main(argv=sys.argv):
    inpath, outpath = argv[1:]
    with open(inpath, 'rb') as inf:
        char_stream = antlr3.ANTLRInputStream(inf)
    lexer = ZasLexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = ZasParser(tokens)
    r = parser.program()
    t = r.tree
    #print t.toStringTree()
    nodes = antlr3.tree.CommonTreeNodeStream(t)
    nodes.setTokenStream(tokens)
    assembler = ZasAssembler()
    walker = ZasWalker(nodes, assembler)
    walker.program()
    assembler.finalize()
    zcode = []
    for secname in ('data', 'rodata', 'text'):
        zcode.append(assembler.sections[secname].getvalue())
    zcode = ''.join(zcode)[0x40:]
    header = ZHeader()
    header.version = 5
    header.initpc = assembler.start
    header.globals = assembler.globals
    header.statmem = assembler.sections['rodata'].base
    header.himem = assembler.sections['text'].base
    header.filesz = len(zcode) + 0x40
    with open(outpath, 'wb') as outf:
        outf.write(str(header))
        outf.write(zcode)
    return 0
Ejemplo n.º 2
0
	def compile(self, srcfile, base_dir, output_dir):
		#fp = codecs.open(sys.argv[1], 'r', 'utf-8')
		fp = open(srcfile, 'r')
		char_stream = antlr3.ANTLRInputStream(fp)
		lexer = ExprLexer(char_stream)
		tokens = antlr3.CommonTokenStream(lexer)

		parser = ExprParser(tokens)
		r = parser.prog()

		# this is the root of the AST
		root = r.tree
		#print (root.toStringTree())
		#print '-------'

		nodes = antlr3.tree.CommonTreeNodeStream(root)
		nodes.setTokenStream(tokens)
		from Eval import Eval
		eval = Eval(nodes)

		#######################################
		head, tail = os.path.split(srcfile)

		if not os.path.exists(output_dir):
			os.mkdir(output_dir)
		if not os.path.exists(output_dir + '/__init__.py'):
			fp = open(output_dir + '/__init__.py', 'w')
			fp.close()

		dstfile = os.path.normpath(output_dir + '/' + tail.split('.')[0] + '.py')
		#print 'compile: %-30s=> %s' % (srcfile, dstfile)

		cpy = CpyBuilder(dstfile, base_dir, output_dir)
		eval.prog(cpy)
		return dstfile
Ejemplo n.º 3
0
def parseFSM(path):
    char_stream = antlr3.ANTLRInputStream(open(path))
    lexer = FsmlLexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = FsmlParser(tokens)
    parser.fsm()
    return parser.fsmObject
Ejemplo n.º 4
0
 def parse_str(self, content):
     stream = antlr3.ANTLRInputStream(StringIO.StringIO(content))
     lexer = CoreLexer(stream)
     tokens = antlr3.CommonTokenStream(lexer)
     parser = CoreParser(tokens)
     ast = parser.program()
     return ast.tree
Ejemplo n.º 5
0
def parse(file):
    fp = open(file)
    char_stream = antlr3.ANTLRInputStream(fp)
    lexer = GDBMILexer.GDBMILexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = GDBMIParser.GDBMIParser(tokens)
    fp.close()
    
    return lexer, tokens, parser 
Ejemplo n.º 6
0
 def parse(self, filename):
     'parse an file into an ast'
     if not os.path.exists(filename):
         raise Exception('file not found: %s.' % (filename))
     with open(filename) as f:
         stream = antlr3.ANTLRInputStream(f)
         lexer = CoreLexer(stream)
         tokens = antlr3.CommonTokenStream(lexer)
         parser = CoreParser(tokens)
         ast = parser.program()
         return ast.tree
Ejemplo n.º 7
0
    def execute(self, argv):
        options, args = self.parseOptions(argv)

        self.setUp(options)

        if options.interactive:
            while True:
                try:
                    input = eval(input(">>> "))
                except (EOFError, KeyboardInterrupt):
                    self.stdout.write("\nBye.\n")
                    break

                inStream = antlr3.ANTLRStringStream(input)
                self.parseStream(options, inStream)

        else:
            if options.input is not None:
                inStream = antlr3.ANTLRStringStream(options.input)

            elif len(args) == 1 and args[0] != '-':
                inStream = antlr3.ANTLRFileStream(args[0],
                                                  encoding=options.encoding)

            else:
                inStream = antlr3.ANTLRInputStream(self.stdin,
                                                   encoding=options.encoding)

            if options.profile:
                try:
                    import cProfile as profile
                except ImportError:
                    import profile

                profile.runctx('self.parseStream(options, inStream)',
                               globals(), locals(), 'profile.dat')

                import pstats
                stats = pstats.Stats('profile.dat')
                stats.strip_dirs()
                stats.sort_stats('time')
                stats.print_stats(100)

            elif options.hotshot:
                import hotshot

                profiler = hotshot.Profile('hotshot.dat')
                profiler.runctx('self.parseStream(options, inStream)',
                                globals(), locals())

            else:
                self.parseStream(options, inStream)
Ejemplo n.º 8
0
def parse(filename):
    'parse a file into an ast'
    if not os.path.exists(filename):
        raise Exception('file not found: %s.' % (filename))
    with open(filename) as f:
        stream = antlr3.ANTLRInputStream(f)
        lexer = CoreLexer(stream)
        tokens = antlr3.CommonTokenStream(lexer)
        parser = CoreParser(tokens)
        ast = parser.program()
        for combinator in ast.tree.combinators():
            symtab[combinator.name()] = combinator
        return ast.tree
Ejemplo n.º 9
0
def parse():
    char_stream = antlr3.ANTLRInputStream(sys.stdin, encoding='utf-8')
    lexer = ExprLexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = ExprParser(tokens)
    r = parser.prog()
    root = r.tree
    nodes = antlr3.tree.CommonTreeNodeStream(root)
    walker = Eval.Eval(nodes)

    try:
        walker.prog()
    except ReturnValue, v:
        if isinstance(v.getValue(), str) or isinstance(v.getValue(), unicode):
            print v.getValue().encode('utf-8')
        else:
            print v.getValue()
Ejemplo n.º 10
0
 def parse_input(self, input):
     """Parse from a file-like object."""
     return self._parse(antlr3.ANTLRInputStream(input))
Ejemplo n.º 11
0
 def load_from_stream(self, stream):
     """Load a fuzzy system from FCL stream."""
     return self.__load(antlr3.ANTLRInputStream(stream))
Ejemplo n.º 12
0
# vim:fileencoding=gbk

import sys
import antlr3
import antlr3.tree
from ExprLexer import ExprLexer
from ExprParser import ExprParser
from TreeExpr import TreeExpr

istream = antlr3.ANTLRInputStream(sys.stdin)
lexer = ExprLexer(istream)
parser = ExprParser(antlr3.CommonTokenStream(lexer))
nstream = antlr3.tree.CommonTreeNodeStream(parser.prog().getTree())
walker = TreeExpr(nstream)
walker.prog()
Ejemplo n.º 13
0
 def load_from_stream(self,stream):
     return self.__load(antlr3.ANTLRInputStream(stream))