public override string go() { Lexems.Lexema l = getToken(); string s=""; if (l == null) return null; string type = l.GetType().Name; switch (type) { case "OpenBracket": { Lexems.Lexema local=null; while((local=getToken())!=null&&local.GetType().Name!="CloseBracket") { this.lexems.Add(local); } if(local==null) { addError(new CloseBracketMissed(),0); } Expression e=new Expression(this); string res= e.go(); calculations = e.calculations; this.result="cx"; if (res != null) { s = res + "\tmov cx," + e.result+"\n"; } } break; default: { do { this.lexems.Add(l); } while ((l = getToken()) != null); BinaryOperator op=new BinaryOperator(this); s = op.go(); calculations = op.calculations; this.result = "cx"; s += "\tmov cx," + op.result+"\n"; } break; } return s; }
public override string go() { string register = "cx"; string loop = ""; string label = "LOOP" + root.loopCount.ToString() + ":"; int loopCountQ = root.loopCount++; string header = label; Assignment a=new Assignment(this.parent); string assignment =a.go() ; header+= "\tmov "+register+"," + a.indetifier.Data+"\n"; Lexems.Lexema l = getToken(); if (l == null || l.GetType().Name != "To") { addError(new ToKeyWordMissed(), 0); if (l != null) this.lexems.Add(l); } while ((l = getToken()) != null) { this.lexems.Add(l); } int position = m_tokenPosition; Expression e = new Expression(this); string expression=e.go(); string condition = "\txor "+register+"," + e.result +"\n\tdec "+e.result+ "\n\tjnz " + label+"\n"; m_tokenPosition = position; loop += ";----------Begin loop " + (loopCountQ).ToString() + "----------------\n"; loop += assignment+"\n"; loop += expression; loop += header; loop += "\t\tpush "+register+"\n"; loop += "\t\tpush " + e.result + "\n"; loop += e.calculations; loop += "\t\tpop "+register+"\n"; loop += "\t\tpop " + e.result + "\n"; loop += condition; loop += ";----------End loop " + (loopCountQ ).ToString() + "----------------\n"; return loop; }
public override string go() { string s = ""; Lexems.Lexema l = getToken(); string type = ""; if (l != null) { type = l.GetType().Name; } else { return null; } switch (type) { case "Identifire": { wasIndetifier = true; if (hasId(l.Data)) { indetifier.Data = l.Data; } else { addError(new UndefindedIndefier(l.Data), 0); ; } Lexems.Lexema lt = null; while ((lt = getToken()) != null && lt.GetType().Name != "Endl") { this.lexems.Add(lt); } if (lt == null) { addError(new EndlMissedError(), 0); } Assignment a=new Assignment(this); a.indetifier = this.indetifier; a.wasIndetifier = true; string ass = a.go(); if (ass != null) { s += ass; } } break; case "BinOperEqual": { if (wasIndetifier) { s += "\tmov " + indetifier.Data + ",ax"; } else { addError(new IndetifierMissedError(), 0); } Lexems.Lexema lt = null; while ((lt = getToken()) != null && lt.GetType().Name != "Endl") { this.lexems.Add(lt); } if (lt == null&&!wasIndetifier) { addError(new EndlMissedError(), 0); } Expression e=new Expression(this); string result = e.go(); if (result != null) { s = result + "\tmov ax," + e.result + "\n" + s; } } break; case "For": if (wasIndetifier) { addError(new UnexpectedIndefier(this.indetifier.Data), 0); } return new ForTo(this.parent).go(); /* case "Print": if (wasIndetifier) { addError(new UnexpectedIndefier(this.indetifier.Data), 0); } return new Print(this.parent).go(); */ default: { if (wasIndetifier) { this.lexems.Insert(0, indetifier); } Lexems.Lexema lt = null; while ((lt = getToken()) != null && lt.GetType().Name != "Endl") { this.lexems.Add(lt); } if (lt == null) { addError(new EndlMissedError(), 0); } Expression e = new Expression(this); string result = e.go(); if (result != null) { s = result; } } break; } return s; }