示例#1
0
    def addDefaultGlobalFunctions(t:TACgen):
        def f1(ob: list):
            eprint(ob[0])
            return [LexName.NULL, ""]

        def f2(ob: list):
            fromBuffer = ob[0]
            bufferizedStack = ob[1]

            if fromBuffer:
                string = bufferizedStack.pop(0)
            else:
                string = input()
                bufferizedStack.append(string)
            return [LexName.STRING, string]

        def f3(ob: list):
            return [LexName.STRING, str(ob[0])]

        def f4(ob: list):
            string = ob[0]
            try:
                i = int(string)
            except ValueError:
                i = 0
            return [LexName.INT, i]

        def f5(ob: list):
            string = ob[0]
            index = ob[1]
            try:
                char = string[index]
            except Exception:
                char = None
            return [LexName.STRING, char]

        t.addGlobalFunction(TFunction([LexName.NULL, False], Token.get(LexName.PRINT),"Global",
                                      [FnParameter(LexName.STRING, "ob")], f1))
        t.addGlobalFunction(TFunction([LexName.STRING, False], Token.get(LexName.INPUT),"Global",
                                      [], f2))
        t.addGlobalFunction(TFunction([LexName.STRING, False], "toStr", "Global",
                                      [FnParameter(LexName.STRING, "ob")], f3))
        t.addGlobalFunction(TFunction([LexName.INT, False], "toInt", "Global",
                                      [FnParameter(LexName.STRING, "ob")], f4))
        t.addGlobalFunction(TFunction([LexName.STRING, False], "charAt", "Global",
                                      [FnParameter(LexName.STRING, "ob"), FnParameter(LexName.INT, "i")], f5))
示例#2
0
    def interpretBlock(self, label: str):
        self.callStack.append(self.generateScope(label))
        self.currentBlock = self.callStack.getLast()
        self.executedStatements.append("Call stack:" + str(self.callStack))
        if self.debug:
            print(self.executedStatements.getLast())
        i = -1
        while True:
            i += 1
            if i >= self.currentBlock.statements.__len__():
                # return after CALLBLOCK/CALL to previous spot
                i = self.doReturn()
                if i == -1:
                    break
                i+=1
            # pre-evaluation
            if i >= self.currentBlock.statements.__len__():
                # return after CALLBLOCK/CALL to previous spot
                i = self.doReturn()
                if i == -1:
                    break
                i+=1
            try:
                # found end
                st = self.currentBlock.statements[i]
            except IndexError as e:
                print(e,"at",i)

                break
            string = ""
            for reg in self.currentBlock.registers.returnItemsInOrder():
                string+=str(reg)
            self.executedStatements.append(str(i).zfill(3)+":"+str(st)+string)
            if self.debug:
                print(self.executedStatements.getLast())
            operation = st.operation
            if operation in [Tnames.CALLBLOCK,Tnames.CALL]:
                scope = None
                if operation == Tnames.CALL:
                    argumentCount = self.stack.pop().value
                    fetched = self.fetchFunction(st.tuple[1])
                    if argumentCount != fetched.parameters.__len__():
                        raise SemanticException("Function Parameter amount miss-match")
                    # global function
                    if self.globalFunctions.containsKey(fetched.name):
                        variables = []
                        for var in range(0,fetched.parameters.__len__()):
                            variables.insert(0,self.stack.pop().value)
                        start = datetime.datetime.now()
                        if fetched.name==Token.get(LexName.INPUT):
                            variables.append(self.fromBuffer)
                            variables.append(self.inputBuffer)

                        retVal = fetched.execute(variables)
                        end = datetime.datetime.now()
                        if fetched.name==Token.get(LexName.INPUT):
                            self.inputDelay+= end - start
                        systemReg = Reg("_sysReg")
                        systemReg.t = retVal[0]         # set type
                        systemReg.value = retVal[1]     # set value
                        self.stack.append(systemReg)
                    else:
                        scope = self.doFunction(self.callStack.getLast(), st.tuple[1])
                else:
                    scope = Operation.generateScope(self.scopes.get(st.tuple[1]))
                if scope is not None:
                    self.currentBlock.returnIndex = i
                    self.callStack.append(scope)
                    self.currentBlock = self.callStack.getLast()
                    self.executedStatements.append("Call stack:"+str(self.callStack))
                    if self.debug:
                        print(self.executedStatements.getLast())
                    i = -1
            elif operation in [Tnames.JUMPZ,Tnames.JUMP]:
                boolVal = True
                if operation == Tnames.JUMPZ:
                    reg = self.setOrGetRegister(st.tuple[2])
                    boolVal = reg.booleanValue()
                if (operation==Tnames.JUMP) or (not boolVal):
                    index = self.doJump(st.tuple[1])
                    i = index

            elif operation == Tnames.RETURN:
                fetched = self.fetchFunction(st.tuple[2])
                val = self.stack.getLast()
                if val.tp() != fetched.tp():
                    if val.tp() == LexName.NULL:
                        raise SemanticException("Function " + st.tuple[2] + " " + "returned nothing")
                    raise SemanticException("Function "+st.tuple[2]+" "+"returned different "+val.tp()+" declared"+fetched.tp())
                i = self.doReturn(st.tuple[1])
                if i == -1:
                    break
            else:
                #Non block jumping command
                self.evaluate(st)
示例#3
0
    try:
        i = int(string)
    except ValueError:
        i = 0
    return [LexName.INT,i]

def f5(ob:list):
    string = ob[0]
    index = ob[1]
    try:
        char = string[index]
    except Exception:
        char = None
    return [LexName.STRING,char]

t.addGlobalFunction(TFunction([LexName.NULL, False],Token.get(LexName.PRINT),"Global", [FnParameter(LexName.STRING, "ob")],f1))
t.addGlobalFunction(TFunction([LexName.STRING, False],Token.get(LexName.INPUT),"Global", [],f2))
t.addGlobalFunction(TFunction([LexName.STRING, False],"toStr","Global", [FnParameter(LexName.STRING, "ob")],f3))
t.addGlobalFunction(TFunction([LexName.INT, False],"toInt","Global", [FnParameter(LexName.STRING, "ob")],f4))
t.addGlobalFunction(TFunction([LexName.STRING, False],"charAt","Global",
                              [FnParameter(LexName.STRING, "ob"),FnParameter(LexName.INT, "i")],f5))
t.parseRoot(root)
print("\n\n")
for k in t.getStatementsAsString():
    print(k.__str__())
print("_________")
'''
for k in TACgen.getTabbedStatements(t.getOrderedListOfStatements(t.rootLabel)):
    print(k.__str__())
'''
for label in t.scopes.keyOrder: