private Statement statement() { if (TokenListItem < TokenList.Count()) { lookahead = (int)TokenList[TokenListItem].tag; switch (lookahead) { case (int)TokenTags.OPENBRACKET: match((int)TokenTags.OPENBRACKET); List <Statement> stateArr = new List <Statement>(); while (lookahead != (int)TokenTags.CLOSEBRACKET) { if ((TokenListItem) >= TokenList.Count()) { break; } Statement foo = statement(); stateArr.Add(foo); } match((int)TokenTags.CLOSEBRACKET); return(new CompoundStatement(stateArr.ToArray())); case (int)TokenTags.FUNCTION: match((int)TokenTags.FUNCTION); int idindx2 = match((int)TokenTags.ID); match((int)TokenTags.OPENCIRKBRACKET); string funcname = ((Word)TokenList[idindx2]).lexeme; List <Expression> func_parameters = new List <Expression>(); while (lookahead != (int)TokenTags.CLOSECIRKBRACKET) { int tokindx = match((int)TokenTags.ID); string forfunctionID = ((Word)TokenList[tokindx]).lexeme; if (IDs[forfunctionID] != null) { throw new Exception("Existed ID can`t be a parameter for your function"); } //~~~ //~~~ я передаватиму у сворену юзером функію захардкодений параметр, значення якого при виклику заміниться //~~~ IDExpr megacrutch = new IDExpr(forfunctionID, IDs); megacrutch.SetValue(new NumbExpr(-666)); func_parameters.Add(megacrutch); if (lookahead != (int)TokenTags.COMMA) { break; } else { match((int)TokenTags.COMMA); } } match((int)TokenTags.CLOSECIRKBRACKET); Statement body = statement(); UserFunctionStatement userFunctionStatement = new UserFunctionStatement(funcname, func_parameters.ToArray(), body, IDs, functions); userFunctionStatement.SetTree(null); return(userFunctionStatement); case (int)TokenTags.IF: match((int)TokenTags.IF); match((int)TokenTags.OPENCIRKBRACKET); Expression ifcondition = expression(); match((int)TokenTags.CLOSECIRKBRACKET); Statement ifaction = statement(); IfStatement ifStatement = new IfStatement(ifcondition, ifaction); ifStatement.SetTree(ifcondition); return(ifStatement); case (int)TokenTags.WHILE: match((int)TokenTags.WHILE); match((int)TokenTags.OPENCIRKBRACKET); Expression condition = expression(); match((int)TokenTags.CLOSECIRKBRACKET); Statement action = statement(); WhileStatement whileStatement = new WhileStatement(condition, action); whileStatement.SetTree(condition); return(whileStatement); case (int)TokenTags.RETURN: match((int)TokenTags.RETURN); Expression retexpr = expression(); match((int)TokenTags.DOTCOMMA); ReturnStatement RetState = new ReturnStatement(retexpr); RetState.SetTree(retexpr); return(RetState); case (int)TokenTags.LET: match((int)TokenTags.LET); int idindx = match((int)TokenTags.ID); match((int)TokenTags.ASSIGN); Expression val = expression(); match((int)TokenTags.DOTCOMMA); string id = Convert.ToString(((Word)TokenList[idindx]).lexeme); LetStatement LetState = new LetStatement(id, val, IDs); LetState.SetTree(val); return(LetState); case ((int)TokenTags.DOTCOMMA): match((int)TokenTags.DOTCOMMA); EmptyStatement emptyStatement = new EmptyStatement(); emptyStatement.SetTree(null); return(emptyStatement); case ((int)TokenTags.ID): int idindxx = match((int)TokenTags.ID); string idname = Convert.ToString(((Word)TokenList[idindxx]).lexeme); match((int)TokenTags.ASSIGN); Expression newval = expression(); match((int)TokenTags.DOTCOMMA); AssignStatement assignStatement = new AssignStatement(idname, newval, IDs); assignStatement.SetTree(newval); return(assignStatement); default: Expression onlyexp = expression(); match((int)TokenTags.DOTCOMMA); ExpressionStatement expressionStatement = new ExpressionStatement(onlyexp); expressionStatement.SetTree(onlyexp); return(expressionStatement); } } string error = "Fatal error: don`t fitted statement"; throw new Exception(error); }
private Expression expression() { Expression expr = null; if (TokenListItem < TokenList.Count()) { lookahead = (int)TokenList[TokenListItem].tag; switch (lookahead) { case (int)TokenTags.BOPERATOR: int operator_indx = match((int)TokenTags.BOPERATOR); Expression eNumb = expression(); if (eNumb.Type != ExpressionTypes.Number) { throw new Exception("Must be a number"); } switch (((Word)TokenList[operator_indx]).lexeme) { case "+": return(new NumbExpr(Convert.ToDouble(eNumb.Value()))); case "-": return(new NumbExpr((-1) * Convert.ToDouble(eNumb.Value()))); default: throw new Exception("Invalid expression term '" + ((Word)TokenList[operator_indx]).lexeme + "'"); } case (int)TokenTags.NUMBER: match((int)TokenTags.NUMBER); Expression eNumb1 = new NumbExpr(((Numb)TokenList[TokenListItem - 1]).value); if (lookahead == (int)TokenTags.STROPERATOR) { throw new Exception("Number operator expected;"); } if (lookahead == (int)TokenTags.BOPERATOR) { return(matchBOPERATOR(eNumb1)); } else { return(eNumb1); } case (int)TokenTags.LITERAL: match((int)TokenTags.LITERAL); Expression litr1 = new StrExpr(((Word)TokenList[TokenListItem - 1]).lexeme); if (lookahead == (int)TokenTags.BOPERATOR) { throw new Exception("String operator expected;"); } if (lookahead == (int)TokenTags.STROPERATOR) { return(matchSTROPERATOR(litr1)); } else { return(litr1); } case (int)TokenTags.ID: int IDindx = match((int)TokenTags.ID); string str1 = ((Word)TokenList[IDindx]).lexeme; IDExpr id1 = new IDExpr(str1, IDs); if (lookahead == (int)TokenTags.BOPERATOR) { return(matchBOPERATOR(id1)); } if (lookahead == (int)TokenTags.STROPERATOR) { return(matchSTROPERATOR(id1)); } return(id1); case (int)TokenTags.OPENCIRKBRACKET: match((int)TokenTags.OPENCIRKBRACKET); expr = expression(); match((int)TokenTags.CLOSECIRKBRACKET); if (lookahead == (int)TokenTags.BOPERATOR) { return(matchBOPERATOR(expr)); } if (lookahead == (int)TokenTags.STROPERATOR) { return(matchSTROPERATOR(expr)); } return(expr); case (int)TokenTags.CALL: match((int)TokenTags.CALL); int FunkNameTokeIndx = match((int)TokenTags.ID); List <Expression> FunkParams = new List <Expression>(); match((int)TokenTags.OPENCIRKBRACKET); while (lookahead != (int)TokenTags.CLOSECIRKBRACKET) { Expression param1 = expression(); FunkParams.Add(param1); if (lookahead != (int)TokenTags.CLOSECIRKBRACKET) { match((int)TokenTags.COMMA); } else { break; } } match((int)TokenTags.CLOSECIRKBRACKET); string FunkName = ((Word)TokenList[FunkNameTokeIndx]).lexeme + "#" + Convert.ToString(FunkParams.Count()); Expression func = (matchFunk(FunkName)).Copy(); ((Function)func).SetParameters(FunkParams.ToArray()); return(func); default: string err = "Syntax Error: not fited expression"; //MessageBox.Show(err); throw new Exception(err); } } else { string err = "Out of range"; //MessageBox.Show(err); throw new Exception(err); } }