public static SyntaxNode Parse(CobaltParser parser) { if (parser.Match(TokenClass.IDENTIFIER, "if", TokenClass.OPEN_PARAN, "(")) { Statement ret = new Statement(); parser.Match(TokenClass.IDENTIFIER, TokenClass.OPEN_PARAN, typeof(Expression), TokenClass.CLOSE_PARAN, typeof(Statement)); return ret; } else if (parser.Match(TokenClass.IDENTIFIER, "while", TokenClass.OPEN_PARAN, "(")) { Statement ret = new Statement(); parser.Match(TokenClass.IDENTIFIER, TokenClass.OPEN_PARAN, typeof(Expression), TokenClass.CLOSE_PARAN, typeof(Statement)); return ret; } else if (parser.Match(TokenClass.IDENTIFIER, TokenClass.ASSIGN)) { Statement stn = new Statement(); parser.Term(stn, typeof(Identifier), TokenClass.ASSIGN, typeof(Expression), TokenClass.SEMI_COLON); return stn; } else if (parser.Match(TokenClass.OPEN_BRACKET)) { CodeBlock block = new CodeBlock(); parser.ReadToken(); while (parser.PeekToken() != null && parser.PeekToken().Class != TokenClass.CLOSE_BRACKET) block.AddLeave(Statement.Parse(parser)); parser.ReadToken(); return block; } return null; }
public static SyntaxNode Parse(CobaltParser parser) { ArgList list = new ArgList(); while (parser.PeekToken().Class != Lexer.TokenClass.CLOSE_PARAN) { list.AddLeave(Expression.Parse(parser)); if (parser.PeekToken().Class != Lexer.TokenClass.CLOSE_PARAN && parser.PeekToken().Class != Lexer.TokenClass.COMMA) throw new Exception("Oh now"); } parser.ReadToken(); return list; }
private static SyntaxNode ParseLevel0BinOperation(CobaltParser parser) { SyntaxNode left = ParseLevel1BinOperation(parser); if (parser.PeekToken() != null && parser.PeekToken().Class == TokenClass.BIN_OP && getPrecedence(parser.PeekToken().Value.ToString()) == 0) { string oper = parser.ReadToken().Value.ToString(); BinOp ret = new BinOp(BinaryOperation.BOOL_OR); ret.AddLeave(left); ret.AddLeave(ParseLevel0BinOperation(parser)); return ret; } return left; }
private static SyntaxNode ParseValue(CobaltParser parser) { if (parser.PeekToken().Class == TokenClass.IDENTIFIER) return new Identifier(parser.ReadToken().Value.ToString()); else if (parser.PeekToken().Class == TokenClass.CONSTANT) return new Constant((long)parser.ReadToken().Value); else if (parser.PeekToken().Class == TokenClass.OPEN_PARAN) { parser.ReadToken(); SyntaxNode ret = ParseLevel0BinOperation(parser); if (parser.ReadToken().Class != TokenClass.CLOSE_PARAN) throw new Exception("Expected )"); return ret; } else { Console.WriteLine(parser.PeekToken().Class); return null; } }
private static SyntaxNode ParseLevel6BinOperation(CobaltParser parser) { SyntaxNode left = ParseValue(parser); if (parser.PeekToken() != null && parser.PeekToken().Class == TokenClass.BIN_OP && getPrecedence(parser.PeekToken().Value.ToString()) == 6) { string oper = parser.ReadToken().Value.ToString(); Console.WriteLine(oper); BinOp ret = new BinOp(oper == "*" ? BinaryOperation.MUL : BinaryOperation.DIV); ret.AddLeave(left); ret.AddLeave(ParseLevel6BinOperation(parser)); return ret; } return left; }
private static SyntaxNode ParseLevel2BinOperation(CobaltParser parser) { SyntaxNode left = ParseLevel3BinOperation(parser); if (parser.PeekToken() != null && parser.PeekToken().Class == TokenClass.BIN_OP && getPrecedence(parser.PeekToken().Value.ToString()) == 2) { string oper = parser.ReadToken().Value.ToString(); BinOp ret = new BinOp(oper == "==" ? BinaryOperation.EQUAL : BinaryOperation.NOT_EQUAL); ret.AddLeave(left); ret.AddLeave(ParseLevel2BinOperation(parser)); return ret; } return left; }