示例#1
0
def packFieldDef(fieldResult, className, indent):
    global nameIDX
    #  ['(', [['>', 'me', ['CID'], [':', 'tag']], '<=>', [[[['hasTag']], '=', [[[[[[[['54321'], []], []], []], []], []], []]]]]], ')']
    coFactuals = None
    if fieldResult[0] == '(':  # Reorganize Cofactuals if they are here
        coFactuals = fieldResult[1][2]
        fieldResult = fieldResult[1][0]

    fieldDef = {}
    argList = []
    paramList = []
    innerDefs = []
    optionalTags = None
    isNext = False
    if (fieldResult.isNext): isNext = True
    if (fieldResult.owner): owner = fieldResult.owner
    else: owner = 'me'
    isAllocated = False

    if (fieldResult.fieldType):
        fieldType = fieldResult.fieldType[0]
        if not isinstance(fieldType, str) and (fieldType[0] == '['
                                               or fieldType[0] == '{'):
            #print("FIELDTYPE is an inline SEQ or ALT")
            if fieldType[0] == '{': fieldList = fieldType[1:-1]
            elif fieldType[0] == '[': fieldList = fieldType[1]
            for innerField in fieldList:
                innerFieldDef = packFieldDef(innerField, className,
                                             indent + '    ')
                innerDefs.append(innerFieldDef)
    else:
        fieldType = None

    isAContainer = False
    if (fieldResult.arraySpec):
        arraySpec = fieldResult.arraySpec
        isAContainer = True
        #print("         ****Old ArraySpec found: ")
    else:
        arraySpec = None

    if (fieldResult.containerSpec):
        containerSpec = fieldResult.containerSpec
        isAContainer = True
    else:
        containerSpec = None

    varOwner = owner
    if isAContainer:
        if "owner" in arraySpec:
            varOwner = arraySpec['owner']
        else:
            varOwner = 'me'

    if (fieldResult.nameAndVal):
        nameAndVal = fieldResult.nameAndVal
        #print("nameAndVal = ", nameAndVal.dump())

        if (nameAndVal.fieldName):
            fieldName = nameAndVal.fieldName
            #print("FIELD NAME", fieldName)
        else:
            fieldName = None

        if (nameAndVal.allocDoubleColon):
            if varOwner == 'me' or varOwner == 'we':
                print(
                    "Error: unable to allocate variable with owner me or we: ",
                    fieldName)
                exit(1)
            else:
                isAllocated = True

        if (nameAndVal.givenValue):
            givenValue = nameAndVal.givenValue
        elif (nameAndVal.funcBody):
            [funcBodyOut,
             funcTextVerbatim] = extractFuncBody(fieldName,
                                                 nameAndVal.funcBody)
            givenValue = [funcBodyOut, funcTextVerbatim]
            #print("\n\n[funcBodyOut, funcTextVerbatim] ", givenValue)
        elif (nameAndVal.rValueVerbatim):
            givenValue = ['', nameAndVal.rValueVerbatim[1]]
        else:
            givenValue = None

        if (nameAndVal.argListTag):
            for argSpec in nameAndVal.argList:
                argList.append(
                    packFieldDef(argSpec.fieldDef, className, indent + "    "))
        else:
            argList = None

        if 'parameters' in nameAndVal:
            if ('deprecateDoubleColon' in nameAndVal):
                print(
                    "            ***deprecated doubleColon in nameAndVal at: ",
                    fieldName)
                exit(1)

            if (str(nameAndVal.parameters) == "['(']"): prmList = {}
            else: prmList = nameAndVal.parameters[1]
            for param in prmList:
                paramList.append(param)
            if (isAllocated == False
                ):  # use a constructor instead of assignment
                paramList.append("^&useCtor//8")
        else:
            paramList = None

        if (nameAndVal.optionalTag):
            optionalTags = extractTagDefs(nameAndVal.tagDefList)
    else:
        givenValue = None
        fieldName = None

    if (fieldResult.flagDef):
        cdlog(3, "FLAG: {}".format(fieldResult))
        if (arraySpec):
            cdErr("Lists of flags are not allowed.\n")
            exit(2)
        fieldDef = progSpec.packField(className, False, owner, 'flag',
                                      arraySpec, containerSpec, fieldName,
                                      None, paramList, givenValue, isAllocated)
    elif (fieldResult.modeDef):
        cdlog(3, "MODE: {}".format(fieldResult))
        modeList = fieldResult.modeList
        if (arraySpec):
            cdErr("Lists of modes are not allowed.\n")
            exit(2)
        fieldDef = progSpec.packField(className, False, owner, 'mode',
                                      arraySpec, containerSpec, fieldName,
                                      None, paramList, givenValue, isAllocated)
        fieldDef['typeSpec']['enumList'] = modeList
    elif (fieldResult.constStr):
        if fieldName == None:
            fieldName = "constStr" + str(nameIDX)
            nameIDX += 1
        if (len(fieldResult) > 1 and fieldResult[1] == '[opt]'):
            arraySpec = {
                'datastructID': 'opt'
            }
            if (len(fieldResult) > 3 and fieldResult[3] != ''):
                fieldName = fieldResult[3]
        givenValue = fieldResult.constStr[1:-1]
        fieldDef = progSpec.packField(className, True, 'const', 'string',
                                      arraySpec, containerSpec, fieldName,
                                      None, paramList, givenValue, isAllocated)
    elif (fieldResult.constNum):
        cdlog(3, "CONST Num: {}".format(fieldResult))
        if fieldName == None:
            fieldName = "constNum" + str(nameIDX)
            nameIDX += 1
        fieldDef = progSpec.packField(className, True, 'const', 'int',
                                      arraySpec, containerSpec, fieldName,
                                      None, paramList, givenValue, isAllocated)
    elif (fieldResult.nameVal):
        cdlog(3, "NameAndVal: {}".format(fieldResult))
        fieldDef = progSpec.packField(className, None, None, None, arraySpec,
                                      containerSpec, fieldName, argList,
                                      paramList, givenValue, isAllocated)
    elif (fieldResult.fullFieldDef):
        fieldTypeStr = str(fieldType)[:50]
        cdlog(
            3, "FULL FIELD: {}".format(
                str([
                    isNext, owner, fieldTypeStr + '... ', arraySpec,
                    containerSpec, fieldName
                ])))
        fieldDef = progSpec.packField(className, isNext, owner, fieldType,
                                      arraySpec, containerSpec, fieldName,
                                      argList, paramList, givenValue,
                                      isAllocated)
    else:
        cdErr("Error in packing FieldDefs: {}".format(fieldResult))
        exit(1)
    if len(innerDefs) > 0:
        fieldDef['innerDefs'] = innerDefs
    if coFactuals != None:
        fieldDef['coFactuals'] = coFactuals
    if optionalTags != None:
        fieldDef['optionalTags'] = optionalTags
    return fieldDef
示例#2
0
def Write_fieldExtracter(classes, ToStructName, field, memObjFields, VarTagBase, VarName, advancePtr, indent, level, logLvl):
    debugTmp=False # Erase this line
    VarTag=VarTagBase+str(level)
    ###################   G a t h e r   N e e d e d   I n f o r m a t i o n
    global  globalFieldCount
    global  globalTempVarIdx
    S=''
    fieldName  = field['fieldName']
    fieldIsNext= field['isNext']
    fieldValue = field['value']
    typeSpec   = field['typeSpec']
    fieldType  = progSpec.getFieldType(typeSpec)
    fieldOwner =typeSpec['owner']
    fromIsEmbeddedAlt = (not isinstance(fieldType, str) and fieldType[0]=='[')
    fromIsEmbeddedSeq = (not isinstance(fieldType, str) and fieldType[0]=='{')
    fromIsEmbedded    = fromIsEmbeddedAlt or fromIsEmbeddedSeq

    if(fieldIsNext!=True): return '' # This field isn't in the parse stream.

    [memObj, memVersionName]=fetchMemVersion(classes, ToStructName)

    toField = progSpec.fetchFieldByName(memObjFields, fieldName)
    if(toField==None):
        #print "   TOFIELD == None", fieldName
        # Even tho there is no LVAL, we need to move the cursor. Also, there could be a co-factual.
        toFieldType = progSpec.TypeSpecsMinimumBaseType(classes, typeSpec)
        toTypeSpec=typeSpec
        toFieldOwner="me"
    else:
        toTypeSpec   = toField['typeSpec']
        toFieldType  = progSpec.getFieldType(toTypeSpec)
        toFieldOwner = progSpec.getInnerContainerOwner(toTypeSpec)

        if debugTmp:
            print('        toFieldType:', toFieldType)

    LHS_IsPointer=progSpec.typeIsPointer(toTypeSpec)

   # print "        CONVERTING:", fieldName, str(toFieldType)[:100]+'... ', str(typeSpec)[:100]+'... '
   # print "            TOFieldTYPE1:", str(toField)[:100]
   # print "            TOFieldTYPE :", toFieldOwner, toFieldType
   # print "       fieldValue:",ToStructName, fieldType, fieldValue
    cdlog(logLvl, "FIELD {}: '{}'".format(fieldName, str(fieldValue)))

    fields=[]
    fromIsStruct=progSpec.isStruct(fieldType)
    toIsStruct=progSpec.isStruct(toFieldType)
    ToIsEmbedded = toIsStruct and (toFieldType[0]=='[' or toFieldType[0]=='{')
    [fromIsALT, fields] = progSpec.isAltStruct(classes, fieldType)
    fromIsOPT =False
    fromIsList=False
    toIsList  =False
    if progSpec.isAContainer(typeSpec):
        datastructID = progSpec.getDatastructID(typeSpec)
        if datastructID=='opt': fromIsOPT=True;
        else: fromIsList=True

    if progSpec.isAContainer(toTypeSpec):
        if datastructID != 'opt': toIsList=True

    if debugTmp:
        print('        fromIsOPT:', fromIsOPT)
        print('        fromIsList:', fromIsList)
        print('        toIsList:', toIsList)
        print('        fromIsStruct:', fromIsStruct)
        print('        toIsStruct:', toIsStruct)
        print('        fieldType:', fieldType)
        print('        ToIsEmbedded:', ToIsEmbedded)
        print('        ToStructName:', ToStructName)
        print('        memVersionName:', memVersionName, "\n")
    ###################   W r i t e   L V A L   R e f e r e n c e
    finalCodeStr=''
    CodeLVAR_Alloc=''
    CODE_LVAR_v2=''
    if VarName=='' or VarName=='memStruct':  # Default to the target argument name
        #if VarName=='': print "        VARNAME was ''; FIELDNAME:", fieldName
        VarName='memStruct'
        if(fieldName==None): # Field hasn't a name so in memory it's a cofactual or this is a parser marker.
            globalFieldCount+=1
            # We need two versions in case this is set in a function instead of assignment
            CODE_LVAR_v2 = 'S'+str(globalFieldCount)
            CodeLVAR_Alloc='    me string: '+CODE_LVAR_v2
            CODE_LVAR = CodeLVAR_Alloc
            if debugTmp: print('        CODE_LVARS:', CODE_LVAR)
        else:
            CODE_LVAR = VarName+'.'+fieldName
            if fieldName=='inf': CODE_LVAR = VarName
            CODE_LVAR_v2 = CODE_LVAR
    else:
        CODE_LVAR = VarName
        CODE_LVAR_v2 = CODE_LVAR

    ###################   W r i t e   R V A L   C o d e
    CODE_RVAL=''
    objName=''
    humanIDType= VarTag+' ('+str(fieldName)+' / '+str(fieldValue) + ' / '+str(fieldType)[:40] +')'
    humanIDType=humanIDType.replace('"', "'")
    #print humanIDType

    if advancePtr:
        S+=indent+VarTag+' <- getNextStateRec('+VarTag+')\n'
        # UNCOMMENT FOR DEGUG: S+='    docPos('+str(level)+', '+VarTag+', "Get Next in SEQ for: '+humanIDType+'")\n'


    if fieldOwner=='const'and (toField == None):
        #print'CONSTFIELDVALUE("'+fieldValue+'")\n'
        finalCodeStr += indent + 'tmpStr'+' <- makeStr('+VarTag+"<LVL_SUFFIX>"+'.child)\n'

    else:
        if toIsStruct:
            if debugTmp: print('        toFieldType:', toFieldType)
            if not ToIsEmbedded:
                objName=toFieldType[0]
                if  progSpec.typeIsInteger(objName):
                    strFieldType = fieldType[0]
                    if(strFieldType == "BigInt"):
                        CODE_RVAL='makeStr('+VarTag+'.child'+')'
                    elif(strFieldType == "HexNum"):
                        CODE_RVAL='makeHexInt('+VarTag+'.child'+')'
                    elif(strFieldType == "BinNum"):
                        CODE_RVAL='makeBinInt('+VarTag+'.child'+')'
                    else:
                        CODE_RVAL='makeStr('+VarTag+'.child'+')'
                    toIsStruct=False; # false because it is really a base type.
                elif (objName=='ws' or objName=='wsc' or objName=='quotedStr1' or objName=='quotedStr2' or objName=='CID' or objName=='UniID'
                      or objName=='printables' or objName=='toEOL' or objName=='alphaNumSeq'):
                    CODE_RVAL='makeStr('+VarTag+'.child'+')'
                    toIsStruct=False; # false because it is really a base type.
                else:
                    #print "toObjName:", objName, memVersionName, fieldName
                    [toMemObj, toMemVersionName]=fetchMemVersion(classes, objName)
                    if toMemVersionName==None:
                        # make alternate finalCodeStr. Also, write the extractor that extracts toStruct fields to memVersion of this
                        childStr = ".child"
                        if fromIsOPT:
                            childStr += ".next"
                        finalCodeStr=(indent + CodeLVAR_Alloc + '\n' +indent+'    '+getFunctionName(fieldType[0], memVersionName)+'('+VarTag+"<LVL_SUFFIX>"+childStr+', memStruct)\n')
                        objSpec = progSpec.findSpecOf(classes[0], objName, 'string')
                        ToFields=objSpec['fields']
                        FromStructName=objName
                        Write_Extracter(classes, ToStructName, FromStructName, logLvl+1)
                    else:
                        fromFieldTypeCID = fieldType[0].replace('::', '_')
                        toFieldTypeCID = toMemVersionName.replace('::', '_')
                        #print "FUNC:", getFunctionName(fromFieldTypeCID, toFieldTypeCID)
                        if fromFieldTypeCID != toFieldTypeCID:
                            Write_Extracter(classes, toFieldTypeCID, fromFieldTypeCID, logLvl+1)
                        finalCodeStr=indent + CodeLVAR_Alloc + '\n' +indent+'    '+getFunctionName(fromFieldTypeCID, toFieldTypeCID)+'('+VarTag+"<LVL_SUFFIX>"+'.child, '+CODE_LVAR_v2+')\n'
            else: pass

        else:
            CODE_RVAL = CodeRValExpr(toFieldType, VarTag, "")


    #print "CODE_RVAL:", CODE_RVAL

    ###################   H a n d l e   o p t i o n a l   a n d   r e p e t i t i o n   a n d   a s s i g n m e n t   c a s e s
    gatherFieldCode=''
    if fromIsList and toIsList:
        CODE_RVAL='tmpVar'
        globalFieldCount +=1
        childRecName='SRec' + str(globalFieldCount)
        gatherFieldCode+='\n'+indent+'\nour stateRec: '+childRecName+' <- '+VarTag+'.child.next'
        gatherFieldCode+='\n'+indent+'while('+childRecName+'){\n'
        if fromIsALT:
          #  print "ALT-#1"
            gatherFieldCode+=Write_ALT_Extracter(classes, fieldType[0], fields, childRecName, '', 'tmpVar', indent+'    ', level)

        elif fromIsStruct and toIsStruct:
            gatherFieldCode+='\n'+indent+toFieldOwner+' '+progSpec.baseStructName(toFieldType[0])+': tmpVar'
            if toFieldOwner!='me':
                gatherFieldCode+='\n'+indent+'Allocate('+CODE_RVAL+')'
            #print "##### FUNCT:", getFunctionName(fieldType[0], fieldType[0])
            gatherFieldCode+='\n'+indent+getFunctionName(fieldType[0], toFieldType[0])+'('+childRecName+'.child, tmpVar)\n'

        else:
            CODE_RVAL = CodeRValExpr(toFieldType, childRecName, ".next")

        # Now code to push the chosen alternative into the data field# This is a LIST, not an OPT:
        gatherFieldCode+='\n'+indent+CODE_LVAR+'.pushLast('+CODE_RVAL+')'

        gatherFieldCode+=indent+'    '+childRecName+' <- getNextStateRec('+childRecName+')\n'
        # UNCOMMENT FOR DEGUG: S+= '    docPos('+str(level)+', '+VarTag+', "Get Next in LIST for: '+humanIDType+'")\n'


        gatherFieldCode+='\n'+indent+'}\n'
        if(fromIsOPT):
            print("Handle when the optional item is a list.");
            exit(2)
    else:
        if toIsList: print("Error: parsing a non-list to a list is not supported.\n"); exit(1);
        levelSuffix=''
        assignerCode=''
        oldIndent=indent
        if (fromIsOPT):
            setTrueCode=''
            assignerCode+='\n'+indent+'if('+VarTag+'.child.next' +' == NULL){'
            if toFieldOwner=='me':
                if debugTmp: print('        toFieldOwner:', toFieldOwner)
                ## if fieldName==None and a model of fromFieldType has no cooresponding model But we are in EXTRACT_ mode:
                        ## Make a special form of Extract_fromFieldType_to_ToFieldType()
                        ## Call that function instead of the one in Code_LVAR
                # First, create a new flag field
                if fieldName==None:
                    fieldName="TEMP"+str(globalTempVarIdx)
                    globalTempVarIdx += globalTempVarIdx
                newFieldsName=fieldName   #'has_'+fieldName
                fieldDef=progSpec.packField(ToStructName, False, 'me', 'flag', None, None, newFieldsName, None, None, None, False)
                progSpec.addField(classes[0], memVersionName, 'struct', fieldDef)

                # Second, generate the code to set the flag
                assignerCode+='\n'+indent+'    '+VarName+'.'+newFieldsName+' <- false'
                setTrueCode += VarName+'.'+newFieldsName+' <- true'
            elif LHS_IsPointer: # If owner is my, our or their
                assignerCode+='\n'+indent+'    '+CODE_LVAR+' <- NULL'
            else:
                print("ERROR: OPTional fields must not be '"+toFieldOwner+"'.\n")
                exit(1)
            assignerCode+='\n'+indent+'} else {\n'
            levelSuffix='.child.next'
            indent+='    '
            assignerCode+=indent+setTrueCode+'\n'


        if fromIsALT or fromIsEmbeddedAlt:
            if(fromIsEmbeddedAlt):
               # print "ALT-#2"
                assignerCode+=Write_ALT_Extracter(classes, ToStructName, field['innerDefs'], VarTagBase, levelSuffix, VarName, indent+'    ', level+1, logLvl+1)
            else:
              #  print "ALT-#3"
                assignerCode+=Write_ALT_Extracter(classes, fieldType[0], fields, VarTagBase, levelSuffix, VarName+'X', indent+'    ', level, logLvl+1)
                assignerCode+=indent+CODE_LVAR+' <- '+(VarName+'X')+"\n"
        elif fromIsEmbeddedSeq:
            globalFieldCount +=1
            childRecNameBase='childSRec' + str(globalFieldCount)
            childRecName=childRecNameBase+str(level)
            assignerCode+='\n'+indent+'our stateRec: '+childRecName+' <- '+VarTag+levelSuffix+'.child\n'
            for innerField in field['innerDefs']:
                assignerCode+=Write_fieldExtracter(classes, ToStructName, innerField, memObjFields, childRecNameBase, '', True, '    ', level, logLvl+1)
        elif fromIsStruct and toIsStruct:
            assignerCode+=finalCodeStr.replace("<LVL_SUFFIX>", levelSuffix);
            if debugTmp: print('        assignerCode:', assignerCode)
        else:
           # if toFieldOwner == 'const': print "Error: Attempt to extract a parse to const field.\n"; exit(1);
            if CODE_RVAL!="":
                if LHS_IsPointer:
                    assignerCode+='        '+CODE_LVAR+' <deep- '+CODE_RVAL+"\n"
                else: assignerCode+='        '+CODE_LVAR+' <- '+CODE_RVAL+"\n"
            elif finalCodeStr!="": assignerCode+=finalCodeStr.replace("<LVL_SUFFIX>", levelSuffix);

        if (fromIsOPT):
            indent=oldIndent
            assignerCode += indent+'}\n'
            #print '######################\n'+assignerCode, memVersionName, '\n'
           # exit(2)
        gatherFieldCode = assignerCode
    #print ("##########################\n",S,"\n#####################################\n")
    if LHS_IsPointer: # LVAL is a pointer and should be allocated or cleared.
        S+= indent + 'AllocateOrClear(' +CODE_LVAR +')\n'

    S+=gatherFieldCode
    #print "ASSIGN_CODE", S
 #   if debugTmp: exit(2)
    return S
示例#3
0
            else: prmList=nameAndVal.parameters[1]
            for param in prmList:
                paramList.append(param)
        else: paramList=None

        if(nameAndVal.optionalTag): optionalTags=extractTagDefs(nameAndVal.tagDefList)
    else:
        givenValue=None;
        fieldName=None;



    if(fieldResult.flagDef):
        cdlog(3,"FLAG: {}".format(fieldResult))
        if(arraySpec): cdErr("Lists of flags are not allowed.\n"); exit(2);
        fieldDef=progSpec.packField(className, False, owner, 'flag', arraySpec, containerSpec, fieldName, None, paramList, givenValue, isAllocated)
    elif(fieldResult.modeDef):
        cdlog(3,"MODE: {}".format(fieldResult))
        modeList=fieldResult.modeList
        if(arraySpec): cdErr("Lists of modes are not allowed.\n"); exit(2);
        fieldDef=progSpec.packField(className, False, owner, 'mode', arraySpec, containerSpec, fieldName, None, paramList, givenValue, isAllocated)
        fieldDef['typeSpec']['enumList']=modeList
    elif(fieldResult.constStr):
        if fieldName==None: fieldName="constStr"+str(nameIDX); nameIDX+=1;
        if(len(fieldResult)>1 and fieldResult[1]=='[opt]'):
            arraySpec={'datastructID': 'opt'};
            if(len(fieldResult)>3 and fieldResult[3]!=''):
                fieldName=fieldResult[3]
        givenValue=fieldResult.constStr[1:-1]
        fieldDef=progSpec.packField(className, True, 'const', 'string', arraySpec, containerSpec, fieldName, None, paramList, givenValue, isAllocated)
    elif(fieldResult.constNum):