public static IADNode DoWhileLoop() { MainFSM.GetNextToken(); var result = new ADDoWhileLoop(); if (MainFSM.GetNextToken().Type != TokenType.leftCBType) { ParserFunctions.SyntaxError("Byl ocekavan znak \'{\'"); } ParserFunctions.STablesStack.Push(new STable()); result.Body = ParserFunctions.statement_list(); if (MainFSM.GetNextToken().Type != TokenType.rightCBType) { ParserFunctions.SyntaxError("Byl ocekavan znak \'}\'"); } ParserFunctions.STablesStack.Pop(); if (MainFSM.GetNextToken().Type != TokenType.whileType) { ParserFunctions.SyntaxError("Bylo ocekavano klicove slovo while"); } if (MainFSM.GetNextToken().Type != TokenType.leftRBType) { ParserFunctions.SyntaxError("Byl ocekavan znak \'(\'"); } result.Condition = PrecedenceSyntaxAnalysis.Precedence(TokenType.rightRBType); if (MainFSM.GetNextToken().Type != TokenType.rightRBType) { ParserFunctions.SyntaxError("Byl ocekavan znak \')\'"); } if (MainFSM.GetNextToken().Type != TokenType.semiType) { ParserFunctions.SyntaxError("Byl ocekavan znak \';\'"); } return(result); }
public static IEnumerable <string> decl_arg() { Token token = MainFSM.GetNextToken(); while (token.Type != TokenType.rightRBType) { if (token.Type == TokenType.idType) { yield return(token.Attribute); } else if (token.Type == TokenType.comType) { yield return(next_decl_arg()); } token = MainFSM.GetNextToken(); } }
public static IADNode Return() { if (MainFSM.GetNextToken().Type != TokenType.returnType) { ParserFunctions.SyntaxError("Bylo ocekavano klicove slovo \'return\'"); } var result = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType); if (MainFSM.GetNextToken().Type != TokenType.semiType) { ParserFunctions.SyntaxError("Byl ocekavan znak \';\'"); } return(new ADReturn { Expression = result }); }
public static void decl_type(ADVariable variable) { if (MainFSM.PeekNextToken().Type == TokenType.leftABType) { MainFSM.GetNextToken(); variable.Type = ADVariable.VarType.array; variable.STRecord.Type = STType.array; var arrayDimension = new ADArrayDimension() { ValuesCount = value() }; variable.ArrayDimensions.Add(arrayDimension); if (MainFSM.GetNextToken().Type != TokenType.rightABType) { SyntaxError("Byl ocekavan znak \']\'"); } decl_type(variable); } }
public static void ArrayValuesDeclaration(ADVariable array, int dimensionIndex) { if (array.ArrayDimensions.Count > 0 && array.ArrayDimensions.Count < (dimensionIndex + 1)) { ParserFunctions.SemanticError("Dane pole nema tolik dimenzi."); } array.Type = ADVariable.VarType.array; if (MainFSM.GetNextToken().Type == TokenType.leftCBType) { while (MainFSM.PeekNextToken().Type != TokenType.rightCBType) { array.ArrayDimensions[dimensionIndex].Values.Add ( PrecedenceSyntaxAnalysis.Precedence(TokenType.rightCBType) ); if (MainFSM.PeekNextToken().Type == TokenType.comType) { MainFSM.GetNextToken(); } } if (MainFSM.GetNextToken().Type != TokenType.rightCBType) { ParserFunctions.SyntaxError("Byl ocekavan znak \'}\'"); } if (MainFSM.PeekNextToken().Type == TokenType.comType) { ArrayValuesDeclaration(array, dimensionIndex++); } } else { ParserFunctions.SyntaxError("Byl ocekavan znak \'{\'"); } }
public static IADNode InnerStatements() { MainFSM.GetNextToken(); ParserFunctions.STablesStack.Push(new STable()); var result = ParserFunctions.statement_list(); if (MainFSM.GetNextToken().Type != TokenType.rightCBType) { ParserFunctions.SyntaxError("Byl ocekavan znak \'}\'"); } ParserFunctions.STablesStack.Pop(); if (MainFSM.PeekNextToken().Type == TokenType.semiType) { MainFSM.GetNextToken(); } return(new ADInnerStatements() { Statements = result }); }
public static IADNode st_main() { var token = MainFSM.GetNextToken(); if (token.Type != TokenType.idType) { SyntaxError("Byl ocekavan identifikator funkce."); } if (STSearch(token.Attribute, true) != null) { SemanticError($"Funkce nebo globalni promenna se jmenem {token.Attribute} je j*z deklarovana."); } ADFunctionDeclaration fceDeclaration = new ADFunctionDeclaration() { Name = token.Attribute }; var newRecord = new STRecord() { Access = STAccess.global, Name = token.Attribute, Type = STType.function, Function = fceDeclaration }; FunctionsST.Records.Add(newRecord); if (MainFSM.GetNextToken().Type != TokenType.leftRBType) { SyntaxError("Byl ocekavan znak \'(\'"); } var arguments = decl_arg(); var symbolTable = new STable(); foreach (var arg in arguments) { var stRecord = new STRecord() { Access = STAccess.local, Name = arg, Type = STType.variable }; symbolTable.Records.Add(stRecord); fceDeclaration.Arguments.Add(new ADVariable { Name = arg, Type = ADVariable.VarType.variable, STRecord = stRecord }); } STablesStack.Push(symbolTable); if (MainFSM.GetNextToken().Type != TokenType.leftCBType) { SyntaxError("Byl ocekavan znak \'{\'"); } fceDeclaration.Body = statement_list(true); if (MainFSM.GetNextToken().Type != TokenType.rightCBType) { SyntaxError("Byl ocekavan znak \'}\'"); } STablesStack.Pop(); return(fceDeclaration); }
public static IADNode statement() { var nextToken = MainFSM.PeekNextToken(); IADNode result = null; switch (nextToken.Type) { case TokenType.longType: var list = new ADVariableDeclarations(); StatementHelpers.VariableDeclaration(ref list); list.Variables.Reverse(); result = list; break; case TokenType.starType: result = new ADStatementExpression { Expression = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType) }; if (MainFSM.GetNextToken().Type != TokenType.semiType) { SyntaxError("Byl ocekavan znak \';\'"); } break; case TokenType.idType: case TokenType.incrType: case TokenType.decrType: if (MainFSM.PeekNextToken(2).Type == TokenType.leftRBType) { var fceToken = MainFSM.GetNextToken(); result = fce_call(fceToken.Attribute); if (MainFSM.GetNextToken().Type != TokenType.semiType) { SyntaxError("Byl ocekavan znak \';\'"); } } else { result = new ADStatementExpression { Expression = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType) }; if (MainFSM.GetNextToken().Type != TokenType.semiType) { SyntaxError("Byl ocekavan znak \';\'"); } } break; case TokenType.ifType: result = StatementHelpers.Condition(); break; case TokenType.forType: result = StatementHelpers.ForLoop(); break; case TokenType.whileType: result = StatementHelpers.WhileLoop(); break; case TokenType.doType: result = StatementHelpers.DoWhileLoop(); break; case TokenType.returnType: result = StatementHelpers.Return(); break; case TokenType.breakType: result = StatementHelpers.Break(); break; case TokenType.continueType: result = StatementHelpers.Continue(); break; case TokenType.leftCBType: result = StatementHelpers.InnerStatements(); break; default: SyntaxError("Byl ocekavan jeden z nasledujici klicovych slov nebo znaku: long, *, identifikator, if, for, while, do, return, break, continue"); break; } return(result); }
public static IEnumerable <ADVariable> glob_decl() { if (MainFSM.GetNextToken().Type != TokenType.longType) { SyntaxError("Byla ocekavana deklarace globalni promenne nebo funkce."); } var id = MainFSM.GetNextToken(); if (id.Type != TokenType.idType) { SyntaxError("Byl ocekavan identifikator promenne."); } var variable = new ADVariable() { Name = id.Attribute }; variable.STRecord = new STRecord { Access = STAccess.global, Name = variable.Name, Type = STType.variable, Address = variable.Name }; STablesStack.Peek().Records.Add(variable.STRecord); global_decl_type(variable); if (variable.ArrayDimensions.Count > 0) { variable.Type = ADVariable.VarType.array; } else { variable.Type = ADVariable.VarType.variable; } global_assign(variable); yield return(variable); List <ADVariable> variableList = new List <ADVariable>(); variableList.Add(variable); while (true) { variable = global_variable_list(); if (variable != null) { variableList.Add(variable); yield return(variable); } else { foreach (var item in variableList) { if (item.Value == null) { item.Value = variableList.Last().Value; } } break; } } if (MainFSM.GetNextToken().Type != TokenType.semiType) { SyntaxError("Byl ocekavan znak \';\'"); } yield break; }
private static Term GetNextTerm(TokenType endType, int numOfLeftB, int numOfRightB, ref Stack <Term> SS, bool decl = false) { var token = MainFSM.GetNextToken(); TermType type = TermType.error; Term newTerm = new Term(); #region Term type by token switch (token.Type) { case TokenType.exrType: type = TermType.not; break; case TokenType.decNumberType: case TokenType.hexNumberType: case TokenType.octNumberType: case TokenType.charType: case TokenType.idType: case TokenType.longType: case TokenType.sizeofType: case TokenType.stringType: type = TermType.id; break; case TokenType.asgnType: type = TermType.asgn; break; case TokenType.procType: type = TermType.mod; break; case TokenType.ampType: type = TermType.binAnd; break; case TokenType.andType: type = TermType.logAnd; break; case TokenType.starType: type = TermType.mul; break; case TokenType.plusType: type = TermType.plus; break; case TokenType.incrType: type = TermType.inc; break; case TokenType.minusType: type = TermType.minus; break; case TokenType.decrType: type = TermType.dec; break; case TokenType.divType: type = TermType.div; break; case TokenType.tElseType: type = TermType.cElse; break; case TokenType.leftRBType: type = TermType.leftBr; break; case TokenType.rightRBType: type = TermType.rightBr; break; case TokenType.lessType: type = TermType.less; break; case TokenType.leftType: type = TermType.left; break; case TokenType.lessEqType: type = TermType.lessEq; break; case TokenType.eqEqType: type = TermType.eq; break; case TokenType.grtType: type = TermType.grt; break; case TokenType.grtEqType: type = TermType.grtEq; break; case TokenType.rightType: type = TermType.right; break; case TokenType.tIfType: type = TermType.cCond; break; case TokenType.xorType: type = TermType.xor; break; case TokenType.bOrType: type = TermType.binOr; break; case TokenType.lOrType: type = TermType.logOr; break; case TokenType.plusEqType: type = TermType.plusEq; break; case TokenType.minusEqType: type = TermType.minusEq; break; case TokenType.multEqType: type = TermType.mulEq; break; case TokenType.divEqType: type = TermType.divEq; break; case TokenType.procEq: type = TermType.modEq; break; case TokenType.rightEqType: type = TermType.rightEq; break; case TokenType.leftEqType: type = TermType.leftEq; break; case TokenType.ampEqType: type = TermType.andEq; break; case TokenType.xorEqType: type = TermType.xorEq; break; case TokenType.bOrEqType: type = TermType.orEq; break; case TokenType.bNotType: type = TermType.compl; break; case TokenType.exrEqType: type = TermType.negEq; break; case TokenType.leftABType: type = TermType.leftABtype; break; } #endregion if ((endType == TokenType.rightCBType || endType == TokenType.rightRBType) && token.Type == TokenType.comType && (numOfLeftB == numOfRightB)) { type = TermType.end; } if (endType == TokenType.semiType && decl && token.Type == TokenType.comType) { type = TermType.end; } if (token.Type == endType && (numOfLeftB == numOfRightB)) { type = TermType.end; } if (token.Type == TokenType.leftCBType) { MainFSM.Index--; var array = new ADVariable() { Type = ADVariable.VarType.array }; array.ArrayDimensions.Add(new ADArrayDimension()); StatementHelpers.ArrayValuesDeclaration(array, 0); type = TermType.id; array.STRecord = new STRecord { Access = STAccess.local, Name = array.Name, Type = STType.array }; newTerm.Expression = array; newTerm.STRecord = array.STRecord; var declaration = new ADVariableDeclarations(); declaration.Variables.Add(array); (AnonymousDeclarations as List <IADNode>).Add(declaration); } if (type != TermType.error) { if (token.Type == TokenType.decNumberType || token.Type == TokenType.hexNumberType || token.Type == TokenType.octNumberType || token.Type == TokenType.charType) { string newId = Guid.NewGuid().ToString("N"); newTerm.STRecord.Name = newId; newTerm.STRecord.Value = token.Attribute; newTerm.STRecord.Type = STType.constant; } else if (token.Type == TokenType.stringType) { string newId = Guid.NewGuid().ToString("N"); newTerm.STRecord.Name = newId; newTerm.STRecord.Value = token.Attribute; newTerm.STRecord.Type = STType.str; newTerm.STRecord.Access = STAccess.local; var array = new ADVariable { STRecord = newTerm.STRecord, Name = newId, Type = ADVariable.VarType.array }; if (MainFSM.PeekNextToken().Type == TokenType.leftABType) { var arrayValue = new ADArrayValue() { STRecord = newTerm.STRecord }; arrayValue.ComputeIndexes(); newTerm.Expression = arrayValue; } var declaration = new ADVariableDeclarations(); declaration.Variables.Add(array); (AnonymousDeclarations as List <IADNode>).Add(declaration); } else if (token.Type == TokenType.idType) { var stRecord = ParserFunctions.STSearch(token.Attribute); if (stRecord == null) { if (MainFSM.PeekNextToken().Type == TokenType.leftRBType) { stRecord = ParserFunctions.STablesStack.Peek().Records.Where(m => m.Name == token.Attribute).FirstOrDefault(); if (stRecord == null) { stRecord = new STRecord() { Name = token.Attribute, Type = STType.function }; } } if (stRecord == null) { ParserFunctions.SemanticError($"Promenna \'{token.Attribute}\' nebyla deklarovana."); } } if (stRecord.Type == STType.function) { if (SS.Peek().Type == TermType.reference) { stRecord.Address = stRecord.Name; var variable = new ADVariable() { Name = stRecord.Name, STRecord = stRecord, Type = ADVariable.VarType.function }; newTerm.Expression = variable; } else { var fceCall = ParserFunctions.fce_call(token.Attribute); fceCall.STRecord = stRecord; newTerm.Expression = fceCall; } } newTerm.STRecord = stRecord; } else if (token.Type == TokenType.sizeofType) { if (MainFSM.GetNextToken().Type != TokenType.leftRBType) { ParserFunctions.SyntaxError("Byl ocekavan znak \'(\'"); } if (MainFSM.GetNextToken().Type != TokenType.longType) { ParserFunctions.SyntaxError("Bylo ocekavano klicove slovo \'long\'"); } if (MainFSM.GetNextToken().Type != TokenType.rightRBType) { ParserFunctions.SyntaxError("Byl ocekavan znak \')\'"); } newTerm.Expression = new ADSizeOfValue(); type = TermType.id; } newTerm.Type = type; return(newTerm); } else { ParserFunctions.SyntaxError("Chyba ve vyrazu."); return(null); } }
public static void VariableDeclaration(ref ADVariableDeclarations list, bool recursive = false) { if (!recursive) { if (MainFSM.GetNextToken().Type != TokenType.longType) { ParserFunctions.SyntaxError("Bylo ocekavano klicove slovo \'long\'"); } } var token = MainFSM.GetNextToken(); if (token.Type != TokenType.idType) { ParserFunctions.SyntaxError("Byl ocekavan identifikator"); } if (ParserFunctions.STSearch(token.Attribute, true) != null) { ParserFunctions.SemanticError($"Promenna {token.Attribute} byla j*z deklarovana"); } var newRecord = new STRecord() { Name = token.Attribute, Access = STAccess.local }; ParserFunctions.STablesStack.Peek().Records.Add(newRecord); var variableDeclaration = new ADVariable() { Name = token.Attribute, STRecord = newRecord, Type = ADVariable.VarType.variable }; ParserFunctions.decl_type(variableDeclaration); if (MainFSM.PeekNextToken().Type == TokenType.comType) { MainFSM.GetNextToken(); VariableDeclaration(ref list, true); } if (MainFSM.PeekNextToken().Type == TokenType.semiType) { list.Variables.Add(variableDeclaration); if (!recursive) { MainFSM.GetNextToken(); } return; } if (MainFSM.GetNextToken().Type != TokenType.asgnType) { ParserFunctions.SyntaxError("Byl ocekavan znak \';\' nebo \'=\'"); } if (MainFSM.PeekNextToken().Type == TokenType.leftCBType) { variableDeclaration.STRecord.Type = STType.array; if (variableDeclaration.ArrayDimensions.Count == 0) { variableDeclaration.ArrayDimensions.Add(new ADArrayDimension()); } ArrayValuesDeclaration(variableDeclaration, 0); } else { variableDeclaration.STRecord.Type = STType.variable; variableDeclaration.Value = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType, true); } if (MainFSM.PeekNextToken().Type == TokenType.comType) { MainFSM.GetNextToken(); VariableDeclaration(ref list, true); } if (!recursive) { if (MainFSM.GetNextToken().Type != TokenType.semiType) { ParserFunctions.SyntaxError("Byl ocekavan znak \';\'"); } } list.Variables.Add(variableDeclaration); }