void VarAssignment(string varName) { int type, reg; Obj obj = tab.Find(varName); if (obj.kind != var) { SemErr("Must assign to a variable"); } if (tab.typeIsStruct(obj.type.id)) { SemErr("Can only assign to primitave fields"); } Expect(35); Expr(out reg, out type); if (type != obj.getTypeId()) { SemErr("Must be equal types, " + obj.getTypeId() + ':' + type); } if (obj.level == 0) { gen.StoreGlobal(reg, obj.adr, varName); } else { gen.StoreLocal(reg, tab.curLevel - obj.level, obj.adr, varName); } Expect(27); }
void ConstExpr(out int type, out int value) { string name; type = undef; value = -1; if (la.kind == 1) { Get(); type = integer; value = Convert.ToInt32(t.val); } else if (la.kind == 5) { Get(); Expect(1); type = integer; value = -(Convert.ToInt32(t.val)); } else if (la.kind == 20) { Get(); type = boolean; value = 1; } else if (la.kind == 21) { Get(); type = boolean; value = 0; } else if (la.kind == 2) { Ident(out name); Obj obj = tab.Find(name); if (obj.kind == constant) { type = obj.getTypeId(); value = obj.compileTimeValue; } else { SemErr("Must be constant"); } } else { SynErr(55); } }
void SwitchStat() { string name; int reg, oReg, cReg; int type; int caseLabel, statementLabel; int endLabel = gen.NewLabel(); Expect(43); Ident(out name); while (la.kind == 36) { StructField(name, out name); } Obj obj = tab.Find(name); tab.OpenSubScope(endLabel); Expect(29); while (la.kind == 44) { caseLabel = gen.NewLabel(); gen.Label(caseLabel); Console.WriteLine(";Load in the Case primary"); Get(); Primary(out reg, out type); Expect(25); gen.ClearRegisters(); oReg = gen.GetRegister(); cReg = gen.GetRegister(); //move generated primary value to cReg gen.MoveRegister(cReg, reg); //load obj value into oReg Console.WriteLine(";Load in Switch obj "); if (obj.level == 0) { gen.LoadGlobal(oReg, obj.adr, name); } else { gen.LoadLocal(oReg, tab.curLevel - obj.level, obj.adr, name); } if (type != obj.getTypeId()) { SemErr("Yo, don't mix n' match the types"); } //Compare oReg to cReg Console.WriteLine(";Comapare Switch obj with Case primary"); gen.RelOp(Op.EQU, oReg, cReg); gen.BranchFalse(caseLabel + 2); //jump to next case, +2 to skip statementLabel //Gen statement labels so optional break can skip the comparisons statementLabel = gen.NewLabel(); gen.Label(statementLabel); //....statements while (StartOf(3)) { Stat(); } Console.WriteLine(";Jump to next set of statements"); gen.Branch(statementLabel + 2); } Console.WriteLine(";Default labels whether default present or not"); caseLabel = gen.NewLabel(); gen.Label(caseLabel); statementLabel = gen.NewLabel(); gen.Label(statementLabel); if (la.kind == 45) { Get(); Expect(25); while (StartOf(3)) { Stat(); } } Expect(30); gen.Label(endLabel); tab.CloseSubScope(); }