Пример #1
0
def main(argv):
    """
    Source: https://www.tutorialspoint.com/python/python_command_line_arguments.htm
    :param argv:
    :return:
    """
    inputFileName = ''
    outputFileName = ''
    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["ifile=", "ofile="])
    except getopt.GetoptError:
        print('test.py -i <inputfile> -o <outputfile>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('test.py -i <inputfile> -o <outputfile>')
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputFileName = arg
        elif opt in ("-o", "--ofile"):
            outputFileName = arg
    inputFile = open(inputFileName, "r")
    sourceCode = inputFile.read()
    compiler = Compiler(sourceCode)
    compiler.parse()
    compiler.compile()
    code = compiler.build()
    print(code)
Пример #2
0
def compile(s=None, **options):
    # Update arguments from the argument cache, and vice versa
    global SavedTest, SavedOptions, CallNo
    if s is None:
        try:
            s = SavedTest
        except NameError:
            raise "The first call to compile must specify a source string."
    options_ = globals().get('SavedOptions', {})
    options_.update(options)
    options = options_
    if s:
        SavedTest = s
    if options:
        SavedOptions = options
    reloadSystem()
    from Compiler import Compiler
    c = Compiler(**options)
    from org.openlaszlo.sc.parser import ParseException
    try:
        CallNo += 1
        #bytes = c.compile(('#file "interactive input %d"\n#line 1\n' % CallNo) + s)
        bytes = c.compile(s)
    except ParseException, e:
        raise `e`
Пример #3
0
    def compile(self,
                source=None,
                file=None,
                moduleName=None,
                mainMethodName='respond'):
        """Compile the template. This method is automatically called by __init__
        when __init__ is fed a file or source string."""

        from Compiler import Compiler

        if file and type(file) in StringTypes and not moduleName and \
           re.match(r'[a-zA-Z_][a-zA-Z_0-9]*$', file):
            moduleName = os.path.splitext(os.path.split(file)[1])[0]
        elif not moduleName:
            moduleName = 'GenTemplate'

        self._fileMtime = None
        self._fileDirName = None
        self._fileBaseName = None
        if file and type(file) in StringTypes:
            file = self.serverSidePath(file)
            self._fileMtime = os.path.getmtime(file)
            self._fileDirName, self._fileBaseName = os.path.split(file)
        self._filePath = file

        compiler = Compiler(
            source,
            file,
            moduleName=moduleName,
            mainMethodName=mainMethodName,
            templateObj=self,
            settings=self._compilerSettings,
        )
        compiler.compile()
        self._generatedModuleCode = str(compiler)
        self._generatedClassCode = str(
            compiler._finishedClassIndex[moduleName])

        compiler._templateObj = None
        compiler.__dict__ = {}
        del compiler
Пример #4
0
	def buildOneProj(self, proj):
		print 'building \033[93m%s\033[0m, type \033[92m%s\033[0m' % (proj['Name'],proj['BuildType'])
		build_type = proj['BuildType']
		obj_dir = '/'.join([self.projDir, proj['ObjDir']])
		bin_dir = '/'.join([self.projDir, proj['BinDir']])
		if 'SharedLib' == build_type:
			bin_path = ('/'.join([self.projDir, proj['BinDir'], proj['Name']+'.so']))
		else:
			bin_path = ('/'.join([self.projDir, proj['BinDir'], proj['Name']+'.exe']))

		if not os.path.exists(obj_dir):
			os.makedirs(obj_dir)

		if not os.path.exists(bin_dir):
			os.makedirs(bin_dir)

		if BuildTool=='clang':
			cmd_gen = ClangCommandGenerator()
		elif BuildTool=='msvc':
			cmd_gen = MSVCCommandGenerator()
		elif BuildTool=='ndk':
			cmd_gen = NDKCommandGenerator()

		compiler 	= Compiler(cmd_gen)
		linker 		= Linker(cmd_gen)


		objs = []
		src_path = ''
		for src in proj['Source']:
			src_path = self.projDir+'/'+src
			obj_path = '/'.join([self.projDir, proj['ObjDir'], src+'.obj'])
			compiler.compile(obj_path, src_path, proj['CXX_FLAGS'], proj['C_FLAGS'])
			objs.append(obj_path)

		print '\033[92mLink Stage ...\033[0m'
		if 'SharedLib' == build_type:
			linker.link('-shared', bin_path, objs, proj['LINK_FLAGS'])
		else:
			linker.link('', bin_path, objs, proj['LINK_FLAGS'])
Пример #5
0
    def compile(self, source=None, file=None,
                moduleName=None,
                mainMethodName='respond'):
        
        """Compile the template. This method is automatically called by __init__
        when __init__ is fed a file or source string."""
        
        from Compiler import Compiler
        
        if file and type(file) in StringTypes and not moduleName and \
           re.match(r'[a-zA-Z_][a-zA-Z_0-9]*$', file):
            moduleName = os.path.splitext(os.path.split(file)[1])[0]
        elif not moduleName:
            moduleName='GenTemplate'

        self._fileMtime = None
        self._fileDirName = None
        self._fileBaseName = None
        if file and type(file) in StringTypes:
            file = self.serverSidePath(file)
            self._fileMtime = os.path.getmtime(file)
            self._fileDirName, self._fileBaseName = os.path.split(file)
        self._filePath = file
                    
        compiler = Compiler(source, file,
                            moduleName=moduleName,
                            mainMethodName=mainMethodName,
                            templateObj=self,
                            settings=self._compilerSettings,
                            )
        compiler.compile()
        self._generatedModuleCode = str(compiler)
        self._generatedClassCode = str(compiler._finishedClassIndex[moduleName])

        compiler._templateObj = None
        compiler.__dict__ = {}
        del compiler
Пример #6
0
def test(keys=undefined, index=undefined, **options):
    """Run the tests."""
    reloadSystem()
    tests = collectTests(keys, index, saveArguments=true)
    from Compiler import Compiler
    runConstraintTests()
    for test in tests:
        c = Compiler(**options)
        try:
            from jarray import array
            bytes = c.compile(test)
            array(bytes, 'b')
        except:
            print "During compilation of %r" % test
            raise
Пример #7
0
 def evaluate(self, ind, **kwargs):
     fitness = 0
     guess = ind.phenotype
     compiler = Compiler()
     program = compiler.compile(guess)
     initial_board = Board(self.path, "init")
     final_board = Board(self.path, "end")
     lift = Lift(self.path)
     game = GameOperations(initial_board, final_board, program, lift)
     game.runProgram()
     fitness += 0 if game.checkVictory(
     ) else self.check_difference_with_neighborhood(game.finalBoard,
                                                    game.board)
     fitness += 20 if lift.liftedBlock != 0 else 0
     return fitness
Пример #8
0
def compile_file(input_file, output_file, run, minify_file):
    print("Compiling file '%s'..." % input_file)

    with open(input_file, "rb") as f:
        code = f.read().decode("utf8")

    brainfuck_code = Compiler.compile(code)
    brainfuck_code += "\n"

    if minify_file:
        brainfuck_code = Minify.minify(brainfuck_code)

    with open(output_file, "wt") as f:
        f.write(brainfuck_code)

    print("Compiled successfully to '%s'" % output_file)

    if run:
        print("Running compiled code...")
        Interpreter.brainfuck(brainfuck_code)
Пример #9
0
def write():
    """Write the test source to tests.as, and the swf to sc.swf.
    The intent is that Flash compiles tests.as -> flash.swf,
    and the flasms of sc.swf and flash.swf can be compared."""
    tests = collectTests(None, flashOnly=true)
    reloadSystem()
    # put the functions first, since Flash will compile them first
    def isFn(s): return s.startswith('function')
    def insertFunctionComment(s):
        pos = s.find('{')
        return s[:pos+1] + 'src = %r;' %s + s[pos+1:]
    tests = [insertFunctionComment(s) for s in tests if isFn(s)] + \
            ['src = %r\n%s' % (s,s)
             for s in tests if not isFn(s)]
    text = '\n'.join(tests) + '\n'
    f = open('tests.as', 'w')
    f.write(text)
    f.close()
    from Compiler import Compiler
    c = Compiler(flashCompilerCompatability=true)
    bytes = c.compile(text)
    writeMovie(bytes, 'sc.swf')
Пример #10
0
class ArduinoCompilerUploader:

	def __init__(self, pathToMain):
		#Get path on mac
		if platform.system() == 'Darwin':
			# logging.debug('self.pathToMain');
			# logging.debug(self.pathToMain);
			# logging.debug('PWD=');
			# logging.debug(os.environ.get('PWD'));
			#logging.debug('PYTHONPATH=');
			#logging.debug(os.environ.get('PYTHONPATH'));
			# logging.debug('ENVIRON=');
			# logging.debug(os.environ);
			if os.environ.get('PYTHONPATH') != None:
				self.pathToMain = os.environ.get('PYTHONPATH')
			else:
				self.pathToMain = pathToMain
		elif platform.system() == 'Windows' or platform.system() == 'Linux':
			self.pathToMain = pathToMain

		if platform.system() == 'Linux':
			self.pathToSketchbook = expanduser("~").decode('latin1')+'/Arduino'
		elif platform.system() == 'Windows' or platform.system() == 'Darwin':
			self.pathToSketchbook = expanduser("~").decode('latin1')+'/Documents/Arduino'

		self.pathToSketchbook = self.pathToSketchbook.decode('latin1')

		self.pathToArduinoDir = pathToMain+'/res/arduino/'
		self.uploader = Uploader(pathToMain)
		self.compiler = Compiler(pathToMain)
		self.boardSettings = defaultdict(BoardConfig)
		self.parseBoardSettings(self.pathToMain+"/res/boards.txt")
		self.board = 'uno'
		self.port = None

	def setBoard (self, board):
		self.board = board
		return self.searchPort()
	def setPort (self,port=''):
		self.port = port
	def getPort (self):
		return self.port
	def getBoard(self):
		return self.board

	def getBoardBaudRate(self):
		return self.boardSettings[self.board].getBaudRate()

	def getBoardMCU(self):
		return self.boardSettings[self.board].getMCU()

	def getBoardFCPU(self):
		return self.boardSettings[self.board].getFCPU()

	def parseBoardSettings (self, path):
		file=open(path,'r')
		#Split the file using the separator in boards.txt to separate the config of the different boards
		a = file.read().split('\n\n##############################################################\n\n');
		a.pop(0) #Remove first element which is only a url
		for line in a:
			boardName = line.split('.')[0]
			boardConfig = line.split('\n')
			boardConfig= [i.split('=')for i in boardConfig]
			boardConfig.pop(0) #Remove empty first element
			self.boardSettings[boardName]=BoardConfig(boardConfig)

	def getAvailablePorts (self):
		if platform.system() =='Windows':
			from serial.tools.list_ports import comports
			comPorts = list(comports())
			availablePorts = []
			for port in comPorts:
				if not 'Bluetooth' in port[1]: #discard bluetooth ports
					availablePorts.append(port[0])
		elif platform.system() =='Darwin':
			if self.board == 'uno':
				availablePorts = glob.glob('/dev/tty.usbmodem*')
			elif self.board == 'bt328':
				availablePorts = glob.glob('/dev/tty.usbserial-*')
			else:
				availablePorts = glob.glob('/dev/tty*')

		elif platform.system() =='Linux':
			if self.board == 'uno':
				availablePorts = glob.glob('/dev/ttyACM*')
			elif self.board == 'bt328':
				availablePorts = glob.glob('/dev/ttyUSB*')
			else:
				availablePorts = glob.glob('/dev/tty*')
		return availablePorts


	def searchPort (self):
		availablePorts = self.getAvailablePorts()
		if len(availablePorts) <=0:
			return []
		ports = []
		for port in availablePorts:
			args = "-P "+port+" -p "+ self.getBoardMCU() +" -b "+ self.getBoardBaudRate()+" -c arduino"
			output, err = callAvrdude(args);
			if 'Device signature =' in output or 'Device signature =' in err:
				ports.append(port)
		if len(ports)==1:
			self.setPort(ports[0])
		return ports



	def compile (self, code):
		return self.compiler.compile( code, self.getBoard() or 'uno', self.pathToArduinoDir, self.pathToSketchbook, self.getBoardMCU(), self.getBoardFCPU())

	def upload (self, code):
		compilationErrorReport = self.compile(code)
		if compilationErrorReport['status'] == 'OK':
			uploadErrorReport = self.uploader.upload(code, self.getPort(), self.getBoard(), self.getBoardMCU(), self.getBoardBaudRate(), self.pathToMain, self.pathToSketchbook)
			return uploadErrorReport
		else:
			return compilationErrorReport
Пример #11
0
from Compiler import Compiler

comp = Compiler()

comp.fetchLibrary()
comp.compile()
comp.output()
Пример #12
0
def main():
    # record time of start
    beginTime = time.time()

    # read entrypoint
    with open(config.__fileinput__, "rb") as inpf:
        raw = inpf.read().decode()

    # preprocess
    lex = Lexer(config.__fileinput__, raw)
    firstTokens = lex.getTokens()
    del lex
    pp = PreProcessor(firstTokens)
    totals = pp.process()

    # no compilation needed, just output preprocessed file
    if (config.__preprocessonly__):

        output = "".join([t.reverse() for t in totals])
        with open(config.__fileoutput__, "wb") as f:
            f.write(output.encode())

        return 0

    del pp
    # global compilation
    c = Compiler()
    config.GlobalCompiler = c

    try:
        c.compile(totals)
    except Error as e:
        fatalThrow(e)

    # function compilation
    c.finalize()

    if (c.panicmode):
        print("Compilation terminated due to errors.")
        exit(1)

    # feed to template
    asm = "%s" % fileTemplate
    asm = asm.replace("%%HEAP%%", c.heap)
    if (not config.DO_DEBUG and config.__tonasm__):
        c.text = re.sub(";.*", "", c.text)

    asm = asm.replace("%%ALIGN%%", str(16 if not config.__Osize__ else 0))

    asm = asm.replace("%%TEXT%%", c.text)
    asm = asm.replace("%%CEXTERNS%%", config.__CEXTERNS__)
    asm = asm.replace("%%CONSTANTS%%", c.constants)
    asm = asm.replace("%%MACROTEXT%%", config.__macrotext__)
    asm = asm.replace("%%INIT%%", c.inittext)
    asm = asm.replace("%%FINI%%", c.fini)
    del c
    # cleanup

    if (config.__tonasm__):  # extra cleanup so the output looks good
        asm = asm.replace("  ", " ").replace("  ", ' ')
        while ("\n " in asm):
            asm = asm.replace("\n ", "\n")
        while ("\n\n" in asm):
            asm = asm.replace("\n\n", "\n")
        asm = asm.replace("\n", "\n\t")

        asm = re.sub("\n\t.*:", asm_labelRepl, asm)

    # linking, and running

    with open(config.__fileoutput__ + ".asm", "wb") as f:
        f.write(asm.encode())

    os.system(assemble(config.__fileoutput__))
    if (config.__executable__):

        # setup path
        if config.__win__:
            os.environ["PATH"] += os.pathsep + \
                os.pathsep.join([f"{config.compilepath}\\windows\\gcc\\bin"])
            os.environ["PATH"] += os.pathsep + \
                os.pathsep.join([f"{config.compilepath}\\windows\\gcc"])
            os.environ["PATH"] += os.pathsep + os.pathsep.join(
                [f"{config.compilepath}\\windows\\gcc\\x86_64-w64-mingw32"])
            os.environ["PATH"] += os.pathsep + \
                os.pathsep.join(
                [f"{config.compilepath}\\windows\\gcc\\opt\\bin"])
            os.environ["PATH"] += os.pathsep + os.pathsep.join([
                f"{config.compilepath}\\windows\\gcc\\x86_64-w64-mingw32\\bin"
            ])

        os.system(link(config.__fileoutput__, config.__fileoutput__))
        os.remove(config.__fileoutput__ + ".o")
    else:
        os.system(linkonly(config.__fileoutput__ + ".o",
                           config.__fileoutput__))

    if (not config.__tonasm__):
        os.remove(config.__fileoutput__ + ".asm")
    if config.__verbose__:
        print("Compiled and Linked symbols in %s s" %
              (time.time() - beginTime))
Пример #13
0
class ArduinoCompilerUploader:
    def __init__(self, pathToMain):
        self.pathToSketchbook = ""
        # Get path on mac
        if platform.system() == "Darwin":
            # logging.debug('self.pathToMain');
            # logging.debug(self.pathToMain);
            # logging.debug('PWD=');
            # logging.debug(os.environ.get('PWD'));
            # logging.debug('PYTHONPATH=');
            # logging.debug(os.environ.get('PYTHONPATH'));
            # logging.debug('ENVIRON=');
            # logging.debug(os.environ);
            if os.environ.get("PYTHONPATH") != None:
                self.pathToMain = os.environ.get("PYTHONPATH")
            else:
                self.pathToMain = pathToMain
        elif platform.system() == "Windows" or platform.system() == "Linux":
            self.pathToMain = pathToMain

        if platform.system() == "Linux":
            self.pathToSketchbook = expanduser("~").decode("latin1") + "/Arduino"
        elif platform.system() == "Darwin":
            self.pathToSketchbook = base.sys_path.get_document_path() + "/Arduino"
        elif platform.system() == "Windows":
            self.pathToSketchbook = (
                os.path.dirname(os.path.dirname(os.path.dirname(base.sys_path.get_tmp_path()))) + "/Documents/Arduino"
            )

        self.pathToArduinoDir = pathToMain + "/res/arduino/"
        self.uploader = Uploader(pathToMain)
        self.compiler = Compiler(pathToMain)
        self.boardSettings = defaultdict(BoardConfig)
        self.parseBoardSettings(self.pathToMain + "/res/boards.txt")
        self.board = "uno"
        self.port = None

    def setBoard(self, board):
        self.board = board
        return self.searchPort()

    def setPort(self, port=""):
        self.port = port

    def getPort(self):
        return self.port

    def getBoard(self):
        return self.board

    def getBoardBaudRate(self):
        return self.boardSettings[self.board].getBaudRate()

    def getBoardMCU(self):
        return self.boardSettings[self.board].getMCU()

    def getBoardFCPU(self):
        return self.boardSettings[self.board].getFCPU()

    def parseBoardSettings(self, path):
        file = open(path, "r")
        # Split the file using the separator in boards.txt to separate the config of the different boards
        a = file.read().split("\n\n##############################################################\n\n")
        a.pop(0)  # Remove first element which is only a url
        for line in a:
            boardName = line.split(".")[0]
            boardConfig = line.split("\n")
            boardConfig = [i.split("=") for i in boardConfig]
            boardConfig.pop(0)  # Remove empty first element
            self.boardSettings[boardName] = BoardConfig(boardConfig)

    def getAvailablePorts(self):
        availablePorts = []

        if platform.system() == "Windows":
            from serial.tools.list_ports import comports

            comPorts = list(comports())
            for port in comPorts:
                if not "Bluetooth" in port[1]:  # discard bluetooth ports
                    availablePorts.append(port[0])
        elif platform.system() == "Darwin":
            darwinPorts = glob.glob("/dev/tty.*")

            for port in darwinPorts:
                if not "Bluetooth" in port:  # discard bluetooth ports
                    availablePorts.append(port)

        elif platform.system() == "Linux":
            availablePorts = glob.glob("/dev/ttyACM*")
            availablePorts += glob.glob("/dev/ttyUSB*")
        return availablePorts

    def searchPort(self):
        availablePorts = self.getAvailablePorts()
        if len(availablePorts) <= 0:
            return []
        ports = []
        for port in availablePorts:
            args = "-P " + port + " -p " + self.getBoardMCU() + " -b " + self.getBoardBaudRate() + " -c arduino"
            output, err = callAvrdude(args)
            if "Device signature =" in output or "Device signature =" in err:
                ports.append(port)
        if len(ports) == 1:
            self.setPort(ports[0])
        return ports

    def compile(self, code):
        return self.compiler.compile(
            code,
            self.getBoard() or "uno",
            self.pathToArduinoDir,
            self.pathToSketchbook,
            self.getBoardMCU(),
            self.getBoardFCPU(),
        )

    def upload(self, code):
        compilationErrorReport = self.compile(code)
        if compilationErrorReport["status"] == "OK":
            uploadErrorReport = self.uploader.upload(
                code,
                self.getPort(),
                self.getBoard(),
                self.getBoardMCU(),
                self.getBoardBaudRate(),
                self.pathToMain,
                self.pathToSketchbook,
            )
            return uploadErrorReport
        else:
            return compilationErrorReport
Пример #14
0
from Compiler import Compiler
from Paths import testScriptPath

if __name__ == '__main__':
    c = Compiler(testScriptPath)
    c.compile()