//parsing by the format functionname-(arguments){} public override void Parse(TokensStack sTokens) { Args = new List <Expression>(); Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected identifier name, received " + tName, tName); } FunctionName = ((Identifier)tName).Name; Token t = sTokens.Pop(); //( while (sTokens.Count > 0) //) { if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name == ')') { break; } if (sTokens.Count < 1) { throw new SyntaxErrorException("Early termination ", t); } Expression tExpression = Expression.Create(sTokens); tExpression.Parse(sTokens); Args.Add(tExpression); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } Token tEnd = sTokens.Pop();//} }
public override void Parse(TokensStack sTokens) { Globals = new List <VarDeclaration>(); while ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name == "var") { VarDeclaration global = new VarDeclaration(); global.Parse(sTokens); Globals.Add(global); } Functions = new List <Function>(); while (sTokens.Count > 0) { if (!(sTokens.Peek() is Statement) || ((Statement)sTokens.Peek()).Name != "function") { throw new SyntaxErrorException("Expected function", sTokens.Peek()); } Function f = new Function(); f.Parse(sTokens); Functions.Add(f); } if (Functions.Count > 0) { Function f = Functions[Functions.Count - 1]; Functions.RemoveAt(Functions.Count - 1); Main = f; } else { throw new Exception("Missing main method"); } }
//This function creates the correct Expression type based on the top most element in the stack. public static Expression Create(TokensStack sTokens) { if (sTokens.Count < 3) { throw new SyntaxErrorException("Expected expression" + sTokens.Count + " --- > 1", null); } Token tFirst = sTokens.Peek(0); Token tSecond = sTokens.Peek(1); Token tThird = sTokens.Peek(2); if (tFirst is Operator) { return(new UnaryOperatorExpression()); } if ((tFirst is Parentheses) && (((Parentheses)tFirst).Name == '(')) { return(new BinaryOperationExpression()); } if ((tFirst is Identifier) && (tSecond is Parentheses) && (((Parentheses)tSecond).Name == '(') && !(tThird is Operator)) { return(new FunctionCallExpression()); } //if (tFirst.Type == Token.TokenType.ID && tSecond.Name == "[") //we are not supporting arrays // return new ArrayExpression(); if (tFirst is Identifier) { return(new VariableExpression()); } if (tFirst is Number) { return(new NumericExpression()); } throw new SyntaxErrorException("Expected expression" + tFirst.ToString() + "-----> 2", tFirst); }
public override void Parse(TokensStack sTokens) { Globals = new List <VarDeclaration>(); while ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name == "var") { VarDeclaration global = new VarDeclaration(); global.Parse(sTokens); Globals.Add(global); } Main = new Function(); Main.Parse(sTokens); Functions = new List <Function>(); while (sTokens.Count > 0) { if (!(sTokens.Peek() is Statement) || ((Statement)sTokens.Peek()).Name != "function") { throw new SyntaxErrorException("Expected function", sTokens.Peek()); } Function f = new Function(); f.Parse(sTokens); Functions.Add(f); throw new Exception(string.Join("\n", f.ToString())); } // throw new Exception(string.Join("\n", Functions.ToArray().ToString())); }
//parsing by the format let variableName=expression public override void Parse(TokensStack sTokens) { if (sTokens.Count < 5) { throw new SyntaxErrorException("Early termination ", sTokens.LastPop); } Token tLet = sTokens.Pop(); if (!(tLet is Statement) || ((Statement)tLet).Name != "let") { throw new SyntaxErrorException("Expected let statment , received " + tLet, tLet); } Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected identifier name, received " + tName, tName); } Variable = ((Identifier)tName).Name; Token tEqual = sTokens.Pop(); if (!(tEqual is Operator) || ((Operator)tEqual).Name != '=') { throw new SyntaxErrorException("Expected = , received " + tEqual, tEqual); } Value = Expression.Create(sTokens); Value.Parse(sTokens); if (!(sTokens.Peek() is Separator) || ((Separator)sTokens.Peek()).Name != ';') { throw new SyntaxErrorException("Expected ; , received ", sTokens.Peek()); } sTokens.Pop(); }
public override void Parse(TokensStack sTokens) { Token tWhile = sTokens.Pop(); if (!(tWhile is Statement) || !((Statement)tWhile).Name.Equals("while")) { throw new SyntaxErrorException("$Expected while", tWhile); } Token con_open = sTokens.Pop(); if (!(con_open is Parentheses) || ((Parentheses)con_open).Name != '(') { throw new SyntaxErrorException("$Expected (", con_open); } Term = Expression.Create(sTokens); Term.Parse(sTokens); /*while (!(sTokens.Peek() is Parentheses)) * { * sTokens.Pop(); * }*/ Token con_close = sTokens.Pop(); if (!(con_close is Parentheses) || ((Parentheses)con_close).Name != ')') { throw new SyntaxErrorException("$Expected )", con_close); } Token while_open = sTokens.Pop(); if (!(while_open is Parentheses) || ((Parentheses)while_open).Name != '{') { throw new SyntaxErrorException("$Expected {", while_open); } Body = new List <StatetmentBase>(); while (sTokens.Count > 0 & !(sTokens.Peek() is Parentheses)) { StatetmentBase statetmentBase = StatetmentBase.Create(sTokens.Peek()); statetmentBase.Parse(sTokens); Body.Add(statetmentBase); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } Token while_close = sTokens.Pop(); if (!(while_close is Parentheses) || ((Parentheses)while_close).Name != '}') { throw new SyntaxErrorException("$Expected }", while_close); } }
public override void Parse(TokensStack sTokens) { Token tFunc = sTokens.Pop(); if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function") { throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc); } Token tType = sTokens.Pop(); if (!(tType is VarType)) { throw new SyntaxErrorException("Expected var type, received " + tType, tType); } ReturnType = VarDeclaration.GetVarType(tType); Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected function name, received " + tType, tType); } Name = ((Identifier)tName).Name; Token t = sTokens.Pop(); //( while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) //) { if (sTokens.Count < 3) { throw new SyntaxErrorException("Early termination ", t); } Token tArgType = sTokens.Pop(); Token tArgName = sTokens.Pop(); VarDeclaration vc = new VarDeclaration(tArgType, tArgName); Args.Add(vc); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } t = sTokens.Pop(); //) t = sTokens.Pop(); //{ while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var")) { VarDeclaration local = new VarDeclaration(); local.Parse(sTokens); Locals.Add(local); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); Body.Add(s); } Token tEnd = sTokens.Pop();//} }
public override void Parse(TokensStack sTokens) { Args = new List <Expression>(); //function name Token token = sTokens.Pop(); if (!(token is Identifier)) { throw new SyntaxErrorException("Expected function name Identifier, received " + token, token); } FunctionName = ((Identifier)token).Name; //check for '(' token = sTokens.Pop(); if (!(token is Parentheses) || ((Parentheses)token).Name != '(') { throw new SyntaxErrorException("Expected '(' for function args, received " + token, token); } //define args until closing ')' for args token = sTokens.Peek(); while (!(token is Parentheses) || (((Parentheses)token).Name != ')')) { Expression expression = Expression.Create(sTokens); expression.Parse(sTokens); Args.Add(expression); //expect additional arg if theres a comma if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { token = sTokens.Pop(); if (((Separator)token).Name != ',') { throw new SyntaxErrorException(@"Expected , (comma), received " + token, token); } } token = sTokens.Peek(); } //check for ')' token = sTokens.Pop(); if (!(token is Parentheses) || ((Parentheses)token).Name != ')') { throw new SyntaxErrorException("Expected ')' for function args, received " + token, token); } }
//parsing by the format operand-expression-expression public override void Parse(TokensStack sTokens) { Token t = sTokens.Pop(); //( if (sTokens.Count < 3 || !(sTokens.Peek(0) is Operator)) { throw new SyntaxErrorException("Expected Operator, recieved ", sTokens.Peek(0)); } Operator = ((Operator)sTokens.Pop()).Name.ToString(); Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); t = sTokens.Pop(); //) }
public override void Parse(TokensStack sTokens) { Token tFun = sTokens.Pop(); if (!(tFun is Identifier)) { throw new SyntaxErrorException("$Expected identifier", tFun); } else { this.FunctionName = ((Identifier)tFun).ToString(); } Token fun_open = sTokens.Pop(); if (!(fun_open is Parentheses) || ((Parentheses)fun_open).Name != '(') { throw new SyntaxErrorException("$Expected (", fun_open); } Args = new List <Expression>(); // problematic scope! while (sTokens.Count > 0 && !((sTokens.Peek() is Parentheses) && ((Parentheses)sTokens.Peek()).Name == ')')) { Expression arg = Expression.Create(sTokens); arg.Parse(sTokens); Args.Add(arg); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } Token fun_close = sTokens.Pop(); if (!(fun_close is Parentheses) || ((Parentheses)fun_close).Name != ')') { throw new SyntaxErrorException("$Expected )" + fun_close.ToString() + " " + sTokens.Pop().ToString() + " " + sTokens.Pop().ToString() + " " + sTokens.Pop().ToString(), fun_close); } //Token tEnd = sTokens.Pop(); // if (!(tEnd is Separator) || ((Separator)tEnd).Name != ';') // throw new SyntaxErrorException("$Expected ;", tEnd); }
public override void Parse(TokensStack sTokens) { Args = new List <Expression>(); Token tFuncName = sTokens.Pop(); if (!(tFuncName is Identifier)) { throw new SyntaxErrorException("Expected Identifier var, recived " + tFuncName, tFuncName); } FunctionName = tFuncName.ToString(); Token tParentheses = sTokens.Pop(); if (!(tParentheses is Parentheses) && (tParentheses).ToString().Equals("(")) { throw new SyntaxErrorException("Expected Parentheses '(', recived " + tParentheses, tParentheses); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses && (sTokens.Peek(0)).ToString().Equals(")"))) { if (sTokens.Peek() is Separator) { Token tComa = sTokens.Pop(); if (!(tComa.ToString().Equals(","))) { throw new SyntaxErrorException("Expected Seperator ',', recived " + tComa, tComa); } } else { Expression eArg = Expression.Create(sTokens); eArg.Parse(sTokens); Args.Add(eArg); } //else // throw new SyntaxErrorException("Expectes Expressions Args or Comas, recived " + sTokens.Peek(0), sTokens.Peek(0)); } if (sTokens.Count < 1) { throw new Exception("Code Terminated"); } Token eParentheses = sTokens.Pop(); if (!(eParentheses is Parentheses) && (eParentheses).ToString().Equals(")")) { throw new SyntaxErrorException("Expected Parentheses ')', recived " + eParentheses, eParentheses); } }
public override void Parse(TokensStack sTokens) { /* * "(" + Operator + " " + Operand1 + " " + Operand2 + ")" */ Token t; StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(") { t = sTokens.Pop(); //( } else { throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Operator) { Token tOperator = sTokens.Pop(); //Operator Operator = ((Operator)tOperator).Name.ToString(); } else { throw new SyntaxErrorException("Expected Operator received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); //Operand1 Operand1 = Expression.Create(sTokens); if (Operand1 == null) { Token tOp1 = new Token(); throw new SyntaxErrorException("Invalid Expression Exception", tOp1); } Operand1.Parse(sTokens); //Operand2 Operand2 = Expression.Create(sTokens); if (Operand2 == null) { Token tOp2 = new Token(); throw new SyntaxErrorException("Invalid Expression Exception", tOp2); } Operand2.Parse(sTokens); StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")") { t = sTokens.Pop(); //) } else { throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } }
public override void Parse(TokensStack sTokens) { Body = new List <StatetmentBase>(); if (sTokens.Count < 6) { throw new SyntaxErrorException("Early termination ", sTokens.LastPop); } Token tWhile = sTokens.Pop(); if (!(tWhile is Statement) || ((Statement)tWhile).Name != "while") { throw new SyntaxErrorException("Expected while statment , received " + tWhile, tWhile); } Token t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != '(') { throw new SyntaxErrorException("Expected ( , received ", t); } Expression pTerm = (Expression.Create(sTokens)); pTerm.Parse(sTokens); Term = pTerm; t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != ')') { throw new SyntaxErrorException("Expected ) , received ", t); } t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != '{') { throw new SyntaxErrorException("Expected { , received ", t); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); Body.Add(s); } Token tEnd = sTokens.Pop();//} }
public override void Parse(TokensStack sTokens) { Token token; token = sTokens.Pop(); if (!(token is Identifier)) { throw new SyntaxErrorException($"Expected an identifier but got {token}", token); } FunctionName = ((Identifier)token).Name; token = sTokens.Pop(); // '(' if (!(token is Parentheses) || ((Parentheses)token).Name != '(') { throw new SyntaxErrorException($"Expected a '(' but saw '{token}'", token); } Args = new List <Expression>(); while (sTokens.Count > 0 && (!(sTokens.Peek() is Parentheses) || ((Parentheses)sTokens.Peek()).Name != ')')) { Expression expression = Create(sTokens); expression.Parse(sTokens); Args.Add(expression); //If there is a comma, then there is another argument if (sTokens.Count > 0 && sTokens.Peek() is Separator) //, { token = sTokens.Pop(); // ',' if (!(token is Separator) || ((Separator)token).Name != ',') { throw new SyntaxErrorException($"Expected a ',' but saw '{token}'", token); } } } token = sTokens.Pop(); // ')' if (!(token is Parentheses) || ((Parentheses)token).Name != ')') { throw new SyntaxErrorException($"Expected a ')' but saw '{token}'", token); } }
public override void Parse(TokensStack sTokens) { Globals = new List <VarDeclaration>(); while ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name == "var") { VarDeclaration global = new VarDeclaration(); global.Parse(sTokens); Globals.Add(global); } /* * function int main(){ * varDecl * Statment* * Return Statment * } */ Main = new Function(); Main.Parse(sTokens); Functions = new List <Function>(); if (Main.Name != "main") { throw new Exception("The first func must be main"); } if (Main.Args.Count() > 0) { throw new Exception("the main func recieves no Arguments"); } while (sTokens.Count > 0) { if (!(sTokens.Peek() is Statement) || ((Statement)sTokens.Peek()).Name != "function") { throw new SyntaxErrorException("Expected function", sTokens.Peek()); } Function f = new Function(); f.Parse(sTokens); Functions.Add(f); } }
public override void Parse(TokensStack sTokens) { if (sTokens.Count <= 0) { throw new SyntaxErrorException("Expected let Statement", new Token()); } else { Token t1 = sTokens.Pop(); if ((sTokens.Peek() is Identifier)) { Variable = ((Identifier)sTokens.Pop()).Name; } else { throw new SyntaxErrorException("missing identifier type", sTokens.Pop()); } if (sTokens.Peek() is Operator && ((Operator)sTokens.Peek()).Name.Equals('=')) { Token t2 = sTokens.Pop(); } else { throw new SyntaxErrorException("missing =", sTokens.Pop()); } Value = Expression.Create(sTokens); if (Value != null) { Value.Parse(sTokens); } else { throw new SyntaxErrorException("Bad exp", sTokens.Pop()); } Token t3 = sTokens.Pop();//; } }
protected static void ParseStatements(TokensStack sTokens, List <StatetmentBase> body) { Token token = sTokens.Pop(); // '{' if (!(token is Parentheses) || ((Parentheses)token).Name != '{') { throw new SyntaxErrorException($"Expected a '{{' but saw '{token}'", token); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { token = sTokens.Peek(); StatetmentBase statetment = Create(token); statetment.Parse(sTokens); body.Add(statetment); } token = sTokens.Pop(); // '}' if (!(token is Parentheses) || ((Parentheses)token).Name != '}') { throw new SyntaxErrorException($"Expected a '}}' but saw '{token}'", token); } }
public override void Parse(TokensStack sTokens) { if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('(')) { Token t1 = sTokens.Pop(); } else { throw new SyntaxErrorException("missing ( ", sTokens.Peek()); } if (sTokens.Peek() is Operator) { Operator = "" + ((Operator)sTokens.Pop()).Name; } else { throw new SyntaxErrorException("no operator", sTokens.Peek()); } Operand1 = Expression.Create(sTokens); if (Operand1 != null) { Operand1.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Ope1", new Token()); } Operand2 = Expression.Create(sTokens); if (Operand2 != null) { Operand2.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Ope2", new Token()); } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals(')')) { Token t2 = sTokens.Pop(); } else { throw new SyntaxErrorException("misssing (", sTokens.Pop()); } }
public override void Parse(TokensStack sTokens) { Args = new List <Expression>(); if (sTokens.Count <= 0) { throw new SyntaxErrorException("Missing function", new Token()); } else { if (!(sTokens.Peek() is Identifier)) { throw new SyntaxErrorException("Missing name of func", sTokens.Pop()); } else { FunctionName = ((Identifier)sTokens.Pop()).Name; } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('(')) { Token t1 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing (", sTokens.Pop()); } while (sTokens.Count > 0 && (((!(sTokens.Peek() is Parentheses)) || ((sTokens.Peek() is Parentheses) && ((Parentheses)sTokens.Peek()).Name.Equals('('))))) //maybe need check casting { Expression E1 = Expression.Create(sTokens); if (E1 != null) { E1.Parse(sTokens); Args.Add(E1); } else { throw new SyntaxErrorException("Bad Exp", new Token()); } if (sTokens.Peek() is Separator && ((Separator)sTokens.Peek()).Name.Equals(',')) { sTokens.Pop(); } } sTokens.Pop(); ToString(); } }
public List <LetStatement> ParseAssignments(List <string> lLines) { List <LetStatement> lParsed = new List <LetStatement>(); List <Token> lTokens = Tokenize(lLines); TokensStack sTokens = new TokensStack(); for (int i = lTokens.Count - 1; i >= 0; i--) { sTokens.Push(lTokens[i]); } while (sTokens.Count > 0) { LetStatement ls = new LetStatement(); ls.Parse(sTokens); lParsed.Add(ls); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } return(lParsed); }
public override void Parse(TokensStack sTokens) { Token token; token = sTokens.Pop(); // 'if' if (!(token is Statement) && ((Statement)token).Name != "if") { throw new SyntaxErrorException($"Expected 'if' but got {token}", token); } token = sTokens.Pop(); // '(' if (!(token is Parentheses) || ((Parentheses)token).Name != '(') { throw new SyntaxErrorException($"Expected a '(' but saw '{token}'", token); } Expression term = Expression.Create(sTokens); term.Parse(sTokens); Term = term; token = sTokens.Pop(); // ')' if (!(token is Parentheses) || ((Parentheses)token).Name != ')') { throw new SyntaxErrorException($"Expected a ')' but saw '{token}'", token); } DoIfTrue = new List <StatetmentBase>(); ParseStatements(sTokens, DoIfTrue); token = sTokens.Peek(); // maybe "else" if (token is Statement && ((Statement)token).Name == "else") { sTokens.Pop(); DoIfFalse = new List <StatetmentBase>(); ParseStatements(sTokens, DoIfFalse); } }
//This is an example of the implementation of the Parse method public override void Parse(TokensStack sTokens) { //We check that the first token is "function" Token tFunc = sTokens.Pop(); if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function") { throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc); } //Now there should be the return type. We pop it from the stack, check for errors, and then set the field Token tType = sTokens.Pop(); if (!(tType is VarType)) { throw new SyntaxErrorException("Expected var type, received " + tType, tType); } ReturnType = VarDeclaration.GetVarType(tType); //Next is the function name Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected function name, received " + tType, tType); } Name = ((Identifier)tName).Name; //After the name there should be opening paranthesis for the arguments Token t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != '(') { throw new SyntaxErrorException($"Expected a '(' but saw '{t}'", t); } //Now we extract the arguments from the stack until we see a closing parathesis while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))//) { //For each argument there should be a type, and a name if (sTokens.Count < 3) { throw new SyntaxErrorException("Early termination ", t); } Token tArgType = sTokens.Pop(); Token tArgName = sTokens.Pop(); VarDeclaration vc = new VarDeclaration(tArgType, tArgName); Args.Add(vc); //If there is a comma, then there is another argument if (sTokens.Count > 0 && sTokens.Peek() is Separator) //, { t = sTokens.Pop(); if (!(t is Separator) || ((Separator)t).Name != ',') { throw new SyntaxErrorException($"Expected a ',' but saw '{t}'", t); } } } //Now we pop out the ) and the {. Note that you need to check that the stack contains the correct symbols here. t = sTokens.Pop();//) if (!(t is Parentheses) || ((Parentheses)t).Name != ')') { throw new SyntaxErrorException($"Expected a ')' but saw '{t}'", t); } t = sTokens.Pop();//{ if (!(t is Parentheses) || ((Parentheses)t).Name != '{') { throw new SyntaxErrorException($"Expected a '{{' but saw '{t}'", t); } //Now we parse the list of local variable declarations while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var")) { VarDeclaration local = new VarDeclaration(); //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration local.Parse(sTokens); Locals.Add(local); } //Now we parse the list of statements while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { //We create the correct Statement type (if, while, return, let) based on the top token in the stack StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); //And call the Parse method of the statement to parse the different parts of the statement s.Parse(sTokens); Body.Add(s); } bool hasReturn = false; if (Body.Count > 0) { StatetmentBase lastStatetment = Body[Body.Count - 1]; Body.RemoveAt(Body.Count - 1); if (lastStatetment is ReturnStatement) { Return = (ReturnStatement)lastStatetment; hasReturn = true; } else { hasReturn = false; } } //Need to check here that the last statement is a return statement //Finally, the function should end with } Token tEnd = sTokens.Pop();//} if (!hasReturn) { throw new SyntaxErrorException("Missing a return statement.", tEnd); } if (!(tEnd is Parentheses) || ((Parentheses)tEnd).Name != '}') { throw new SyntaxErrorException($"Expected a '}}' but saw '{t}'", t); } }
public override void Parse(TokensStack sTokens) { Body = new List <StatetmentBase>(); if (sTokens.Count <= 0) { throw new SyntaxErrorException("Missing while", new Token()); } else { Token t1 = sTokens.Pop(); if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('(')) { Token t2 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing (", sTokens.Pop()); } Term = Expression.Create(sTokens); if (Term != null) { Term.Parse(sTokens); } else { throw new SyntaxErrorException("Bad exp", new Token()); } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals(')')) { Token t3 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing )", sTokens.Pop()); } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('{')) { Token t4 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing {", sTokens.Pop()); } while (/*sTokens.Count > 0 && (((sTokens.Peek() is Parentheses) && !(((Parentheses)sTokens.Peek()).Name.Equals('}'))) || (!(sTokens.Peek() is Parentheses)))*/ sTokens.Count > 0 && ((!(sTokens.Peek() is Parentheses)))) { StatetmentBase st = StatetmentBase.Create(sTokens.Peek()); if (st != null) { st.Parse(sTokens); Body.Add(st); } else { throw new SyntaxErrorException("illegal statement", new Token()); } } if (sTokens.Count > 0 && ((sTokens.Peek() is Parentheses))) { Token t5 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing }", sTokens.Pop()); } } }
public override void Parse(TokensStack sTokens) { /* * Function Type ID( args ){ * varDec* * Statments* * Return Statment * } * * check more errors */ Token t; StackIsNotEmpty(sTokens); Token tFunc = sTokens.Pop(); if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function") { throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc); } StackIsNotEmpty(sTokens); Token tType = sTokens.Pop(); if (!(tType is VarType)) { throw new SyntaxErrorException("Expected var type, received " + tType, tType); } ReturnType = VarDeclaration.GetVarType(tType); StackIsNotEmpty(sTokens); Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected function name, received " + tType, tType); } Name = ((Identifier)tName).Name; StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(") { t = sTokens.Pop(); //( } else { throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { if (sTokens.Count < 3) { throw new SyntaxErrorException("Early termination ", t); } Token tArgType = sTokens.Pop(); Token tArgName = sTokens.Pop(); VarDeclaration vc = new VarDeclaration(tArgType, tArgName); Args.Add(vc); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")") { t = sTokens.Pop(); //) } else { throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "{") { t = sTokens.Pop(); //{ } else { throw new SyntaxErrorException("Expected { Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var")) { VarDeclaration local = new VarDeclaration(); local.Parse(sTokens); Locals.Add(local); } StackIsNotEmpty(sTokens); while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); Body.Add(s); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "}") { t = sTokens.Pop(); //} } else { throw new SyntaxErrorException("Expected } Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } }
public override void Parse(TokensStack sTokens) { DoIfTrue = new List <StatetmentBase>(); DoIfFalse = new List <StatetmentBase>(); if (sTokens.Count < 5) { throw new SyntaxErrorException("Early termination ", sTokens.LastPop); } Token tIF = sTokens.Pop(); if (!(tIF is Statement) || ((Statement)tIF).Name != "if") { throw new SyntaxErrorException("Expected while statment , received " + tIF, tIF); } Token t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != '(') { throw new SyntaxErrorException("Expected ( , received ", t); } Term = Expression.Create(sTokens); Term.Parse(sTokens); t = sTokens.Pop(); //) if (!(t is Parentheses) || ((Parentheses)t).Name != ')') { throw new SyntaxErrorException("Expected ) , received ", t); } t = sTokens.Pop(); //{ if (!(t is Parentheses) || ((Parentheses)t).Name != '{') { throw new SyntaxErrorException("Expected { , received ", t); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); DoIfTrue.Add(s); } t = sTokens.Pop(); //} if (sTokens.Count > 3 && sTokens.Peek() is Statement && ((Statement)sTokens.Peek()).Name == "else") { t = sTokens.Pop(); if (!(sTokens.Peek() is Parentheses)) { throw new SyntaxErrorException("Expected { , received ", sTokens.Peek()); } if (((Parentheses)sTokens.Peek()).Name != '{') { throw new SyntaxErrorException("Expected { , received ", sTokens.Peek()); } t = sTokens.Pop(); //{ while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); DoIfFalse.Add(s); } Token tEnd = sTokens.Pop(); } }
public override void Parse(TokensStack sTokens) { DoIfTrue = new List <StatetmentBase>(); DoIfFalse = new List <StatetmentBase>(); Token tIf = sTokens.Pop(); if (!(tIf.ToString().Equals("if"))) { throw new SyntaxErrorException("Expected if statment, recived " + tIf, tIf); } Token termS_Parentheses = sTokens.Pop(); if (!(termS_Parentheses is Parentheses) && !(termS_Parentheses).ToString().Equals("(")) { throw new SyntaxErrorException("Expected Parentheses '(', recived " + termS_Parentheses, termS_Parentheses); } Term = Expression.Create(sTokens); Term.Parse(sTokens); Token termE_Parentheses = sTokens.Pop(); if (!(termE_Parentheses is Parentheses) && !(termE_Parentheses).ToString().Equals(")")) { throw new SyntaxErrorException("Expected Parentheses ')', recived " + termE_Parentheses, termE_Parentheses); } Token tS_Parentheses = sTokens.Pop(); if (!(tS_Parentheses is Parentheses) && !(tS_Parentheses).ToString().Equals("{")) { throw new SyntaxErrorException("Expected Parentheses '{', recived " + tS_Parentheses, tS_Parentheses); } while (!(sTokens.Peek(0) is Parentheses && ((Parentheses)sTokens.Peek(0)).Name == '}')) { Token tStatment = sTokens.Peek(); StatetmentBase statetment = StatetmentBase.Create(tStatment); statetment.Parse(sTokens); DoIfTrue.Add(statetment); } Token tE_Parentheses = sTokens.Pop(); if (!(tE_Parentheses is Parentheses) && !(tE_Parentheses).ToString().Equals("}")) { throw new SyntaxErrorException("Expected Parentheses '}', recived " + tE_Parentheses, tE_Parentheses); } if (sTokens.Peek(0).ToString().Equals("else")) // else statment { Token tElse = sTokens.Pop(); Token elseS_Parentheses = sTokens.Pop(); if (!(elseS_Parentheses is Parentheses) && !(elseS_Parentheses).ToString().Equals("{")) { throw new SyntaxErrorException("Expected Parentheses '{', recived " + elseS_Parentheses, elseS_Parentheses); } while (!(sTokens.Peek(0) is Parentheses && (sTokens.Peek(0)).ToString().Equals("}"))) { Token tStatment = sTokens.Peek(); StatetmentBase statetment = StatetmentBase.Create(tStatment); statetment.Parse(sTokens); DoIfFalse.Add(statetment); } Token elseE_Parentheses = sTokens.Pop(); if (!(elseE_Parentheses is Parentheses) && !(elseE_Parentheses).ToString().Equals("}")) { throw new SyntaxErrorException("Expected Parentheses '}', recived " + elseE_Parentheses, elseE_Parentheses); } } }
public override void Parse(TokensStack sTokens) { Token tWhile = sTokens.Pop(); if (!(tWhile is Statement) || ((Statement)tWhile).Name != "if") { throw new SyntaxErrorException("Expected function received: " + tWhile, tWhile); } Token tPar = sTokens.Pop(); if (!(tPar is Parentheses) || ((Parentheses)tPar).Name != '(') { throw new SyntaxErrorException("Expected var type, received " + tPar, tPar); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { Expression local = Expression.Create(sTokens); //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration local.Parse(sTokens); Term = local; } Token tPar1 = sTokens.Pop(); if (!(tPar1 is Parentheses) || ((Parentheses)tPar1).Name != ')') { throw new SyntaxErrorException("Expected function received: " + tPar1, tPar1); } Token tPar2 = sTokens.Pop(); if (!(tPar2 is Parentheses) || ((Parentheses)tPar1).Name != '{') { throw new SyntaxErrorException("Expected function received: " + tPar2, tPar2); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase state = StatetmentBase.Create(sTokens.Peek()); //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration state.Parse(sTokens); DoIfTrue.Add(state); } Token tPar3 = sTokens.Pop(); if (!(tPar3 is Parentheses) || ((Parentheses)tPar1).Name != '}') { throw new SyntaxErrorException("Expected function received: " + tPar3, tPar3); } if (sTokens.Count > 0 && sTokens.Peek() is Statement && ((Statement)sTokens.Pop()).Name.Equals("else")) { Token tPar4 = sTokens.Pop(); if (!(tPar4 is Parentheses) || ((Parentheses)tPar4).Name != '{') { throw new SyntaxErrorException("Expected function received: " + tPar4, tPar4); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase state = StatetmentBase.Create(sTokens.Peek()); //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration state.Parse(sTokens); DoIfFalse.Add(state); } Token tPar5 = sTokens.Pop(); if (!(tPar5 is Parentheses) || ((Parentheses)tPar1).Name != '}') { throw new SyntaxErrorException("Expected function received: " + tPar5, tPar5); } } }
public override void Parse(TokensStack sTokens) { DoIfTrue = new List <StatetmentBase>(); DoIfFalse = new List <StatetmentBase>(); if (((Statement)sTokens.Peek()).Name.Equals("if")) { Token tIf = sTokens.Pop(); if (!(tIf is Statement) || !((Statement)tIf).Name.Equals("if")) { throw new SyntaxErrorException("$Expected if", tIf); } Token con_open = sTokens.Pop(); if (!(con_open is Parentheses) || ((Parentheses)con_open).Name != '(') { throw new SyntaxErrorException("$Expected (", con_open); } Term = Expression.Create(sTokens); Term.Parse(sTokens); /* while (!(sTokens.Peek() is Parentheses)) * { * sTokens.Pop(); * }*/ Token con_close = sTokens.Pop(); if (!(con_close is Parentheses) || ((Parentheses)con_close).Name != ')') { throw new SyntaxErrorException("$Expected )", con_close); } Token open_if = sTokens.Pop(); if (!(open_if is Parentheses) || ((Parentheses)open_if).Name != '{') { throw new SyntaxErrorException("$Expected {", open_if); } while (sTokens.Count > 0 & !(sTokens.Peek() is Parentheses)) { StatetmentBase statetmentBase = StatetmentBase.Create(sTokens.Peek()); statetmentBase.Parse(sTokens); DoIfTrue.Add(statetmentBase); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } Token close_if = sTokens.Pop(); if (!(close_if is Parentheses) || ((Parentheses)close_if).Name != '}') { throw new SyntaxErrorException("$Expected }", close_if); } if ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name.Equals("else")) { Token tElse = sTokens.Pop(); if (!(tElse is Statement) || !((Statement)tElse).Name.Equals("else")) { throw new SyntaxErrorException("$Expected else" + tElse.ToString(), tElse); } Token open_else = sTokens.Pop(); // not must if (!(open_else is Parentheses) || ((Parentheses)open_else).Name != '{') { throw new SyntaxErrorException("$Expected {", open_else); } while (sTokens.Count > 0 & !(sTokens.Peek() is Parentheses)) { StatetmentBase statetmentBase = StatetmentBase.Create(sTokens.Peek()); statetmentBase.Parse(sTokens); DoIfFalse.Add(statetmentBase); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } Token close_else = sTokens.Pop(); if (!(close_else is Parentheses) || ((Parentheses)close_else).Name != '}') { throw new SyntaxErrorException("$Expected }", close_else); } } } }
public override void Parse(TokensStack sTokens) { DoIfTrue = new List <StatetmentBase>(); DoIfFalse = new List <StatetmentBase>(); if (sTokens.Count <= 0) { throw new SyntaxErrorException("Missing if", new Token()); } else { Token t1 = sTokens.Pop(); if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('(')) { Token t2 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing (", sTokens.Pop()); } Term = Expression.Create(sTokens); if (Term != null) { Term.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Exp", sTokens.Pop()); } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals(')')) { Token t3 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing )", sTokens.Pop()); } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('{')) { Token t4 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing {", sTokens.Pop()); } while (sTokens.Count > 0 && ((!(sTokens.Peek() is Parentheses)))) { StatetmentBase st = StatetmentBase.Create(sTokens.Peek()); //maybe need fix if (st != null) { st.Parse(sTokens); DoIfTrue.Add(st); } else { throw new SyntaxErrorException("illegal exp", sTokens.Pop()); } } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('}')) { Token t3 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing }", sTokens.Pop()); } if (sTokens.Peek() is Statement && ((Statement)sTokens.Peek()).Name.Equals("else")) { Token t4 = sTokens.Pop(); if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('{')) { Token t5 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing (", sTokens.Pop()); } while (/*sTokens.Count > 0 && (((sTokens.Peek() is Parentheses) && ((Parentheses)sTokens.Peek()).Name.Equals('}')) || (!(sTokens.Peek() is Parentheses)))*/ sTokens.Count > 0 && ((!(sTokens.Peek() is Parentheses))))/// { StatetmentBase st2 = StatetmentBase.Create(sTokens.Peek()); if (st2 != null) { st2.Parse(sTokens); DoIfFalse.Add(st2); } else { throw new SyntaxErrorException("illegal exp", sTokens.Pop()); } } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('}')) { Token t6 = sTokens.Pop(); } else { throw new SyntaxErrorException("Missing }", sTokens.Pop()); } } else { } } }
public override void Parse(TokensStack sTokens) { /* * 'while' '(' expression ')' '{' statement* '}' */ Body = new List <StatetmentBase>(); Token t; StackIsNotEmpty(sTokens); if (sTokens.Peek() is Statement && sTokens.Peek().ToString() == "while") { Token tStartWhile = sTokens.Pop(); //while } else { throw new SyntaxErrorException("Expected while statment received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(") { t = sTokens.Pop(); //( } else { throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); while ((sTokens.Count > 0 && !(sTokens.Peek() is Parentheses) || (sTokens.Peek().ToString() != ")"))) { if (sTokens.Count < 3) { throw new SyntaxErrorException("Early termination ", t); } Term = Expression.Create(sTokens); if (Term == null) { t = new Token(); throw new SyntaxErrorException("Invalid Exception", t); } Term.Parse(sTokens); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")") { t = sTokens.Pop(); //) } else { throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "{") { t = sTokens.Pop(); //{ } else { throw new SyntaxErrorException("Expected { Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); Body.Add(s); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "}") { t = sTokens.Pop(); //} } else { throw new SyntaxErrorException("Expected } Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } }