def analyze_sentenceWhile(self, overlookSet = [const.R_BRACE]): downOverlookSet = [const.R_BRACE] self.overlookSetAdd(downOverlookSet, const.R_PARENTHESIS) V_sentenceWhile = VN.create(const.SENTENCE_LOOP) V_sentenceWhile.append(self.pointer) self.getsym() whileBRInstruction = None whileHeadLab = Instruction.generateLab() if self.pointer.isL_Parenthesis(): V_sentenceWhile.append(self.pointer) self.getsym() # 为while头部设置lab标签 self.instructionStream.setLab(whileHeadLab) V_sentenceWhile.append(self.analyze_condition(downOverlookSet)) if self.pointer.isR_Parenthesis(): V_sentenceWhile.append(self.pointer) self.getsym() # 条件分析结束后加入BRF指令 whileBRInstruction = Instruction(Instruction.BRF, Instruction.generateLab()) self.instructionStream.append(whileBRInstruction) V_sentenceWhile.append(self.analyze_sentence(overlookSet)) # while中的语句分析结束后,加入BR指令,直接跳转回while头部 self.emit(Instruction(Instruction.BR, whileHeadLab)) else: self.error(Error.GA_MISS_R_PARENTHESIS) self.overlookToMarks(downOverlookSet) else: self.error(Error.GA_MISS_L_PARENTHESIS, self.pointer.previous) self.overlookToMarks(downOverlookSet) # while分析结束后,在其后的第一条语句添加lab标记 if whileBRInstruction: self.instructionStream.setLab(whileBRInstruction.operator) return self.checkEmpty(V_sentenceWhile)
def analyze_sentenceIf(self, overlookSet = [const.R_BRACE]): downOverlookSet = overlookSet[:] self.overlookSetAdd(downOverlookSet, const.R_PARENTHESIS) self.overlookSetAdd(downOverlookSet, const.ELSE) ifSentenceOverlookSet = overlookSet[:] self.overlookSetAdd(ifSentenceOverlookSet, const.ELSE) V_sentenceIf = VN.create(const.SENTENCE_CONDITION) V_sentenceIf.append(self.pointer) self.getsym() if self.pointer.isL_Parenthesis(): V_sentenceIf.append(self.pointer) self.getsym() V_sentenceIf.append(self.analyze_condition(downOverlookSet)) if self.pointer.isR_Parenthesis(): # 分析完条件,加入比较跳转指令 headBRFInstruction = Instruction(Instruction.BRF, Instruction.generateLab()) self.emit(headBRFInstruction) V_sentenceIf.append(self.pointer) self.getsym() if not self.pointer.isR_Else(): V_sentenceIf.append(self.analyze_sentence(ifSentenceOverlookSet)) else: self.error(Error.GA_MISS_SENTENCE, self.pointer.previous, 'Gramma Analysis Error: A sentence between IF and ELSE is expected.') if self.pointer.isR_Else(): V_sentenceIf.append(self.pointer) self.getsym() # 在if标记后的语句分析结束后,Else分析开始前,添加一条BR指令, # 用于跳转到Else分析结束后的第一条指令 # 如果没有else部分,这条指令不存在 headBRInstruction = Instruction(Instruction.BR, Instruction.generateLab()) self.emit(headBRInstruction) # if标记之后的语句分析结束后,加入lab标记, # 使得在Else分析出的语句中的第一条语句获得lab标记 self.instructionStream.setLab(headBRFInstruction.operator) V_sentenceIf.append(self.analyze_sentence(overlookSet)) # 对于存在Else分析的情况,在Else结束后的第一条语句后面添加BR标记 self.instructionStream.setLab(headBRInstruction.operator) else: # if标记之后的语句分析结束后,加入lab标记, # 在没有else标记的情况下,if分析后的第一条语句也要加入lab标记 self.instructionStream.setLab(headBRFInstruction.operator) else : self.error(Error.GA_MISS_R_PARENTHESIS) self.overlookToMarks(overlookSet) else: self.error(Error.GA_MISS_L_PARENTHESIS, self.pointer.previous) self.overlookToMarks(overlookSet) return V_sentenceIf