private static SyntaxTree Expression() { SyntaxTree temp; if (CurrentToken.TType == TokenType.OPERATOR && CurrentToken.TokenValue == "-") { temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.OPERATOR, CurrentToken.TokenValue); match(CurrentToken.TType, CurrentToken.TokenValue); temp.AddChild(Term()); } else { temp = Term(); } while (CurrentToken.TType == TokenType.OPERATOR && (CurrentToken.TokenValue == "+" || CurrentToken.TokenValue == "-")) { SyntaxTree newtemp = SyntaxTree.MakeNode(SyntaxTreeNodeType.OPERATOR, CurrentToken.TokenValue); match(CurrentToken.TType, CurrentToken.TokenValue); newtemp.AddChild(temp); newtemp.AddChild(Term()); temp = newtemp; } return(temp); }
private static SyntaxTree Conditions() { SyntaxTree temp; if (CurrentToken.TType == TokenType.LPARAN) { match(TokenType.LPARAN, "("); temp = Conditions(); match(TokenType.RPARAN, ")"); } else { temp = TestExpression(); } while (CurrentToken.TType == TokenType.LOGICAL_OPERATOR && CurrentToken.TokenValue != "!") { SyntaxTree newtemp = SyntaxTree.MakeNode(SyntaxTreeNodeType.OPERATOR, CurrentToken.TokenValue); match(TokenType.LOGICAL_OPERATOR, CurrentToken.TokenValue); newtemp.AddChild(temp); if (CurrentToken.TType == TokenType.LPARAN) { temp = Conditions(); } else { temp = TestExpression(); } newtemp.AddChild(temp); temp = newtemp; } return(temp); }
private static SyntaxTree IfKeyWord() { SyntaxTree temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.STRUCTURE, "if"); match(TokenType.KEYWORD, "if"); match(TokenType.LPARAN, "("); temp.AddChild(Conditions()); match(TokenType.RPARAN, ")"); if (CurrentToken.TType == TokenType.LCURBRACKET) { temp.AddChild(StatementBlock2()); } else { temp.AddChild(Statement()); } if (CurrentToken.TType == TokenType.KEYWORD && CurrentToken.TokenValue == "else") { match(TokenType.KEYWORD, "else"); if (CurrentToken.TType == TokenType.LCURBRACKET) { temp.AddChild(StatementBlock2()); } else { temp.AddChild(Statement()); } } return(temp); }
private static SyntaxTree ForKeyWord() { SyntaxTree temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.STRUCTURE, "for"); match(TokenType.KEYWORD, "for"); match(TokenType.LPARAN, "("); temp.AddChild(AssignmentExpression()); match(TokenType.SEMICOLON, ";"); temp.AddChild(Conditions()); match(TokenType.SEMICOLON, ";"); temp.AddChild(AssignmentExpression()); match(TokenType.RPARAN, ")"); if (CurrentToken.TType == TokenType.LCURBRACKET) { temp.AddChild(StatementBlock2()); } else { temp.AddChild(Statement()); } return(temp); }
private static SyntaxTree Factor() { SyntaxTree temp; switch (CurrentToken.TType) { case TokenType.LPARAN: match(TokenType.LPARAN, "("); temp = Expression(); match(TokenType.RPARAN, ")"); break; case TokenType.NUMBER: temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.NUMBER, CurrentToken.TokenValue); match(CurrentToken.TType, CurrentToken.TokenValue); break; case TokenType.IDENTIFIER: temp = Identifier(); //SyntaxTree.MakeNode (SyntaxTreeNodeType.IDENTIFIER,CurrentToken.TokenValue); //match(CurrentToken.TType,CurrentToken.TokenValue ); break; case TokenType.FUNCTION: temp = FunctionCall(); break; default: Error(); temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.OTHER, "ERROR"); break; } while ((CurrentToken.TokenValue == "^" || CurrentToken.TokenValue == "%") && CurrentToken.TType == TokenType.OPERATOR) { SyntaxTree newtemp = SyntaxTree.MakeNode(SyntaxTreeNodeType.OPERATOR, CurrentToken.TokenValue); match(CurrentToken.TType, CurrentToken.TokenValue); newtemp.AddChild(temp); if (CurrentToken.TType == TokenType.LPARAN) { match(TokenType.LPARAN, "("); newtemp.AddChild(Expression()); match(TokenType.RPARAN, ")"); } else { newtemp.AddChild(Factor()); } temp = newtemp; } return(temp); }
private static SyntaxTree Term() { SyntaxTree temp = Factor(); while (CurrentToken.TType == TokenType.OPERATOR && (CurrentToken.TokenValue == "*" || CurrentToken.TokenValue == "/")) { SyntaxTree newtemp = SyntaxTree.MakeNode(SyntaxTreeNodeType.OPERATOR, CurrentToken.TokenValue); match(CurrentToken.TType, CurrentToken.TokenValue); newtemp.AddChild(temp); newtemp.AddChild(Factor()); temp = newtemp; } return(temp); }
private static SyntaxTree Directive() { match(TokenType.DIRECTIVE, "#PARALLEL"); SyntaxTree temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.BLOCK, "ParallelBlock"); temp.AddChild(Statement()); while (CurrentToken.TType != TokenType.DIRECTIVE) { temp.AddChild(Statement()); } match(TokenType.DIRECTIVE, "#ENDPARALLEL"); return(temp); }
private static SyntaxTree Identifier() { SyntaxTree temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.IDENTIFIER, CurrentToken.TokenValue); match(TokenType.IDENTIFIER, CurrentToken.TokenValue); if (CurrentToken.TType == TokenType.LSQUARE) { match(TokenType.LSQUARE, "["); temp.AddChild(Expression()); match(TokenType.COMMA, ","); temp.AddChild(Expression()); match(TokenType.RSQUARE, "]"); } return(temp); }
private static SyntaxTree StatementBlock() { SyntaxTree temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.BLOCK, "StatementBlock"); while (CurrentToken.TType != TokenType.END) { if (CurrentToken.TType == TokenType.DIRECTIVE) { temp.AddChild(Directive()); } else { temp.AddChild(Statement()); } } return(temp); }
private static SyntaxTree StatementBlock2() { SyntaxTree temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.BLOCK, "StatementBlock"); match(TokenType.LCURBRACKET, "{"); while (CurrentToken.TType != TokenType.RCURBRACKET) { if (CurrentToken.TType == TokenType.DIRECTIVE) { temp.AddChild(Directive()); } else { temp.AddChild(Statement()); } } match(TokenType.RCURBRACKET, "}"); return(temp); }
private static SyntaxTree MatrixRow(ref int nrow) { match(TokenType.LSQUARE, "["); int tnrow = 0; SyntaxTree temp = SyntaxTree.MakeNode(SyntaxTreeNodeType.OTHER, "ROW"); if (CurrentToken.TType != TokenType.NUMBER) { Error(); } SyntaxTree newtemp = SyntaxTree.MakeNode(SyntaxTreeNodeType.NUMBER, CurrentToken.TokenValue); match(TokenType.NUMBER, CurrentToken.TokenValue); if (CurrentToken.TType == TokenType.COLON) { double st = 0, inc = 1, ed = 0; SyntaxTree newtemp2 = SyntaxTree.MakeNode(SyntaxTreeNodeType.OTHER, ":"); newtemp2.AddChild(newtemp); st = double.Parse(newtemp.NodeValue); match(TokenType.COLON, ":"); if (CurrentToken.TType != TokenType.NUMBER) { Error(); } newtemp = SyntaxTree.MakeNode(SyntaxTreeNodeType.NUMBER, CurrentToken.TokenValue); match(TokenType.NUMBER, CurrentToken.TokenValue); if (CurrentToken.TType == TokenType.COLON) { match(TokenType.COLON, ":"); if (CurrentToken.TType != TokenType.NUMBER) { Error(); } newtemp2.AddChild(newtemp); inc = double.Parse(newtemp.NodeValue); newtemp2.AddChild(SyntaxTree.MakeNode(SyntaxTreeNodeType.NUMBER, CurrentToken.TokenValue)); ed = double.Parse(CurrentToken.TokenValue); match(TokenType.NUMBER, CurrentToken.TokenValue); } else { newtemp2.AddChild(SyntaxTree.MakeNode(SyntaxTreeNodeType.NUMBER, "1")); ed = double.Parse(newtemp.NodeValue); newtemp2.AddChild(newtemp); } temp.AddChild(newtemp2); tnrow = (int)((ed - st) / inc) + 1; } else { tnrow++; temp.AddChild(newtemp); while (CurrentToken.TType == TokenType.NUMBER) { temp.AddChild(SyntaxTree.MakeNode(SyntaxTreeNodeType.NUMBER, CurrentToken.TokenValue)); match(CurrentToken.TType, CurrentToken.TokenValue); tnrow++; } } if (tnrow == 0) { Error(); } if (nrow > 0 && tnrow != nrow) { Error(); } else { nrow = tnrow; } match(TokenType.RSQUARE, "]"); return(temp); }