//**************************************************************************************************** private void AnalyseHeader(string str) { Parser header_p = new Parser(str); if (!header_p.MoveNext()) throw new SyntaxErrorException("ќшибка в заголовке функции"); if ((header_p.Current as System.String) != m_name) throw new SyntaxErrorException("»м¤ функции не совпадает с именем файла"); if ((!header_p.MoveNext()) || ((header_p.Current as String) != "[")) throw new SyntaxErrorException("ќшибка в заголовке функции"); if ((!header_p.MoveNext())) throw new SyntaxErrorException("ќшибка в заголовке функции"); if ((header_p.Current as System.String != "]")) { string readstr; while (true) { readstr = (header_p.Current as System.String); if (!Parser.IsID(readstr)) throw new SyntaxErrorException("ќшибка в заголовке функции"); m_args.Add(readstr); if (!header_p.MoveNext()) throw new SyntaxErrorException("ќшибка в заголовке функции"); readstr = (header_p.Current as System.String); if (readstr == ",") { if (!header_p.MoveNext()) throw new SyntaxErrorException("ќшибка в заголовке функции"); } else if (readstr == "]") break; else throw new SyntaxErrorException("ќшибка в заголовке функции"); } } if (header_p.MoveNext()) throw new SyntaxErrorException("ќшибка в заголовке функции"); if (m_args.IndexOf("result") >= 0) throw new SyntaxErrorException("ѕараметр функции не может иметь им¤ \"result\""); }
//**************************************************************************************************** private void Analyse(Parser p) { try { LinkedList l = new LinkedList(); while (p.MoveNext()) l.Add(p.Current); OPZ(l); } catch (CalcException ex) { throw ex; } catch { throw new SyntaxErrorException("Синтаксическая ошибка в выражении"); } }
//**************************************************************************************************** public static IOperator CompileOperator(string str) { Parser p = new Parser(str); if (!p.HasMore()) { return new EmptyCommand(); } String pstr = p.GetString(); p.MoveNext(); string firsttoken = (p.Current as String); if (firsttoken == "for") { try { return ParseForStatement(p.GetString()); } catch (SyntaxErrorException ex) { throw ex; } catch (Exception ex) { throw new SyntaxErrorException(ex.Message); } } int posa = pstr.IndexOf(":="); if (posa >= 0) { int cq = 0; for (int iq = 0; iq < posa; iq++) if (pstr[iq] == '\"') cq++; if (cq%2 == 0) { try { if (posa == 0) throw new SyntaxErrorException("Синтаксическая ошибка"); try { if (pstr[posa - 1] == '}') { int posob = pstr.IndexOf('{'); if ((posob < 0) || (posob > posa)) throw new SyntaxErrorException("Синтаксическая ошибка"); return new AssignCommand(pstr.Substring(0, posob), pstr.Substring(posob + 1, posa - posob - 2), pstr.Substring(posa + 2)); } else { return new AssignCommand(pstr.Substring(0, posa), pstr.Substring(posa + 2)); } } catch { throw new SyntaxErrorException("Синтаксическая ошибка"); } } catch (CalcException ex) { throw new SyntaxErrorException(ex.Message); } } } try { if (firsttoken == "clear") { if (!p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); Command cc = new ClearCommand(p.Current as String); if (p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); return cc; } else if (firsttoken == "next") { if (p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); return new NextOperator(); } else if (firsttoken == "else") { if (p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); return new ElseOperator(); } else if (firsttoken == "endif") { if (p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); return new EndifOperator(); } else if (firsttoken == "loop") { if (p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); return new LoopOperator(); } else if (firsttoken == "return") { if (p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); return new ReturnOperator(); } else if (firsttoken == "error") { if (p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); return new ErrorOperator(); } Expression expr = new Expression(p); if (firsttoken == "print") return new PrintCommand(expr); else if (firsttoken == "println") return new PrintLnCommand(expr); else if (firsttoken == "call") return new CallCommand(expr); else if (firsttoken == "while") return new WhileOperator(expr); else if (firsttoken == "if") return new IfOperator(expr); else if (firsttoken == "elseif") return new ElseifOperator(expr); else throw new SyntaxErrorException("Синтаксическая ошибка"); } catch (SyntaxErrorException ex) { throw ex; } catch (Exception ex) { throw new SyntaxErrorException(ex.Message); } }
//**************************************************************************************************** public static Command CompileCommand(string str) { Parser p = new Parser(str); if (!p.HasMore()) { return new EmptyCommand(); } String pstr = p.GetString(); int posa = pstr.IndexOf(":="); if (posa >= 0) { int cq = 0; for (int iq = 0; iq < posa; iq++) if (pstr[iq] == '\"') cq++; if (cq%2 == 0) { try { if (posa == 0) throw new SyntaxErrorException("Синтаксическая ошибка"); try { if (pstr[posa - 1] == '}') { int posob = pstr.IndexOf('{'); if ((posob < 0) || (posob > posa)) throw new SyntaxErrorException("Синтаксическая ошибка"); return new AssignCommand(pstr.Substring(0, posob), pstr.Substring(posob + 1, posa - posob - 2), pstr.Substring(posa + 2)); } else { return new AssignCommand(pstr.Substring(0, posa), pstr.Substring(posa + 2)); } } catch { throw new SyntaxErrorException("Синтаксическая ошибка"); } } catch (CalcException ex) { throw new SyntaxErrorException(ex.Message); } } } p.MoveNext(); string firsttoken = (p.Current as String); try { if (firsttoken == "clear") { if (!p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); Command cc = new ClearCommand(p.Current as String); if (p.MoveNext()) throw new SyntaxErrorException("Синтаксическая ошибка"); return cc; } if (firsttoken == "print") { Expression expr = new Expression(p); return new PrintCommand(expr); } else if (firsttoken == "println") { Expression expr = new Expression(p); return new PrintLnCommand(expr); } else if (firsttoken == "call") { Expression expr = new Expression(p); return new CallCommand(expr); } else { p.Reset(); Expression expr1 = new Expression(p); return new PrintLnCommand(expr1); } } catch (SyntaxErrorException ex) { throw ex; } catch (Exception ex) { throw new SyntaxErrorException(ex.Message); } }
//**************************************************************************************************** public Expression(Parser p) { Analyse(p); }
//**************************************************************************************************** public Expression(String str) { Parser p = new Parser(str); Analyse(p); }