Exemplo n.º 1
0
    def initFunction(self, fnNode, functionIndex, statements, functionType):
        fnNode.itsFunctionType = functionType
        fnNode.addChildToBack(statements)
        functionCount = fnNode.getFunctionCount()
        if (functionCount != 0):
            fnNode.itsNeedsActivation = True
            ## for-while
            i = 0
            while (i != functionCount):
                fn = fnNode.getFunctionNode(i)
                if (fn.getFunctionType() == FunctionNode.FUNCTION_EXPRESSION_STATEMENT):
                    name = fn.getFunctionName()
                    if name is not None and (len(name) != 0):
                        fnNode.removeParamOrVar(name)
                i += 1
        if (functionType == FunctionNode.FUNCTION_EXPRESSION):
            name = fnNode.getFunctionName()
            if (name is not None) and \
                            len(name) != 0 \
                            and (not fnNode.hasParamOrVar(name)):

                if (fnNode.addVar(name) == ScriptOrFnNode.DUPLICATE_CONST):
                    self.parser.addError("msg.const.redecl", name)
                setFn = Node(Token.EXPR_VOID, Node(Token.SETNAME, Node.newString(Token.BINDNAME, name), Node(Token.THISFN)))
                statements.addChildrenToFront(setFn)
        lastStmt = statements.getLastChild()
        if lastStmt is None or (lastStmt.getType() != Token.RETURN):
            statements.addChildToBack(Node(Token.RETURN))
        result = Node.newString(Token.FUNCTION, fnNode.getFunctionName())
        result.putIntProp(Node.FUNCTION_PROP, functionIndex)
        return result
Exemplo n.º 2
0
 def createAssignment(self, assignType, left, right):
     left = self.makeReference(left)
     if left is None:
         self.parser.reportError("msg.bad.assign.left")
         return right
     assignOp = 0
     if assignType == Token.ASSIGN:
         return self.simpleAssignment(left, right)
     elif assignType == Token.ASSIGN_BITOR:
         assignOp = Token.BITOR
     elif assignType == Token.ASSIGN_BITXOR:
         assignOp = Token.BITXOR
     elif assignType == Token.ASSIGN_BITAND:
         assignOp = Token.BITAND
     elif assignType == Token.ASSIGN_LSH:
         assignOp = Token.LSH
     elif assignType == Token.ASSIGN_RSH:
         assignOp = Token.RSH
     elif assignType == Token.ASSIGN_URSH:
         assignOp = Token.URSH
     elif assignType == Token.ASSIGN_ADD:
         assignOp = Token.ADD
     elif assignType == Token.ASSIGN_SUB:
         assignOp = Token.SUB
     elif assignType == Token.ASSIGN_MUL:
         assignOp = Token.MUL
     elif assignType == Token.ASSIGN_DIV:
         assignOp = Token.DIV
     elif assignType == Token.ASSIGN_MOD:
         assignOp = Token.MOD
     else:
         raise Kit.codeBug()
     nodeType = left.getType()
     if nodeType == Token.NAME:
         s = left.getString()
         opLeft = Node.newString(Token.NAME, s)
         op = Node(assignOp, opLeft, right)
         lvalueLeft = Node.newString(Token.BINDNAME, s)
         return Node(Token.SETNAME, lvalueLeft, op)
     elif nodeType in (Token.GETPROP,
                       Token.GETELEM):
         obj = left.getFirstChild()
         id = left.getLastChild()
         type = Token.SETPROP_OP if (nodeType == Token.GETPROP) else Token.SETELEM_OP
         opLeft = Node(Token.USE_STACK)
         op = Node(assignOp, opLeft, right)
         return Node(type, obj, id, op)
     elif nodeType == Token.GET_REF:
         ref = left.getFirstChild()
         self.checkMutableReference(ref)
         opLeft = Node(Token.USE_STACK)
         op = Node(assignOp, opLeft, right)
         return Node(Token.SET_REF_OP, ref, op)
     raise Kit.codeBug()
Exemplo n.º 3
0
 def createForIn(self, loop,
                       lhs,
                       obj,
                       body,
                       isForEach):
     type = lhs.getType()
     lvalue = None#Node()
     if (type == Token.VAR):
         lastChild = lhs.getLastChild()
         if (lhs.getFirstChild() != lastChild):
             self.parser.reportError("msg.mult.index")
         lvalue = Node.newString(Token.NAME, lastChild.getString())
     else:
         lvalue = self.makeReference(lhs)
         if lvalue is None:
             self.parser.reportError("msg.bad.for.in.lhs")
             return obj
     localBlock = Node(Token.LOCAL_BLOCK)
     initType = Token.ENUM_INIT_VALUES if isForEach else Token.ENUM_INIT_KEYS
     init = Node(initType, obj)
     init.putProp(Node.LOCAL_BLOCK_PROP, localBlock)
     cond = Node(Token.ENUM_NEXT)
     cond.putProp(Node.LOCAL_BLOCK_PROP, localBlock)
     id = Node(Token.ENUM_ID)
     id.putProp(Node.LOCAL_BLOCK_PROP, localBlock)
     newBody = Node(Token.BLOCK)
     assign = self.simpleAssignment(lvalue, id)
     newBody.addChildToBack(Node(Token.EXPR_VOID, assign))
     newBody.addChildToBack(body)
     loop = self.createWhile(loop, cond, newBody)
     loop.addChildToFront(init)
     if (type == Token.VAR):
         loop.addChildToFront(lhs)
     localBlock.addChildToBack(loop)
     return localBlock
Exemplo n.º 4
0
 def createUnary(self, nodeType, child):
     childType = child.getType()
     if nodeType == Token.DELPROP:
         n = None#Node()
         if (childType == Token.NAME):
             child.setType(Token.BINDNAME)
             left = child
             right = Node.newString(child.getString())
             n = Node(nodeType, left, right)
         else:
             if (childType == Token.GETPROP) or (childType == Token.GETELEM):
                 left = child.getFirstChild()
                 right = child.getLastChild()
                 child.removeChild(left)
                 child.removeChild(right)
                 n = Node(nodeType, left, right)
             else:
                 if (childType == Token.GET_REF):
                     ref = child.getFirstChild()
                     child.removeChild(ref)
                     n = Node(Token.DEL_REF, ref)
                 else:
                     n = Node(Token.TRUE)
         return n
     elif nodeType == Token.TYPEOF:
         if (childType == Token.NAME):
             child.setType(Token.TYPEOFNAME)
             return child
     elif nodeType == Token.BITNOT:
         if (childType == Token.NUMBER):
             value = ScriptRuntime.toInt32(child.getDouble())
             child.setDouble(~value)
             return child
     elif nodeType == Token.NEG:
         if (childType == Token.NUMBER):
             child.setDouble(-child.getDouble())
             return child
     elif nodeType == Token.NOT:
         status = self.isAlwaysDefinedBoolean(child)
         if (status != 0):
             type = 0
             if (status == self.ALWAYS_TRUE_BOOLEAN):
                 type = Token.FALSE
             else:
                 type = Token.TRUE
             if (childType == Token.TRUE) or (childType == Token.FALSE):
                 child.setType(type)
                 return child
             return Node(type)
     return Node(nodeType, child)
Exemplo n.º 5
0
 def createString(self, string):
     return Node.newString(string)
Exemplo n.º 6
0
 def createName(self, name):
     self.checkActivationName(name, Token.NAME)
     return Node.newString(Token.NAME, name)