public static AbstractSyntaxTree Parse(Token[] stream) { Queue <Token> tokens = new Queue <Token>(stream); AST.Function main = ParseFunction(tokens); AST.Program program = new AST.Program(main); AbstractSyntaxTree ast = new AbstractSyntaxTree(program); return(ast); }
public static AST.Function ParseFunction(Queue <Token> tokens) { Token token = tokens.Dequeue(); if (!Match(token, Token.TokenType.Keyword, Syntax.GetKeyword(Syntax.Keyword.Integer))) { Fail(token); } token = tokens.Dequeue(); if (token.Type != Token.TokenType.Identifier) { Fail(token); } string identifier = token.Value; token = tokens.Dequeue(); if (token.Type != Token.TokenType.OpenParenthesis) { Fail(token); } token = tokens.Dequeue(); if (token.Type != Token.TokenType.CloseParenthesis) { Fail(token); } token = tokens.Dequeue(); if (token.Type != Token.TokenType.OpenBrace) { Fail(token); } AST.Function function = new AST.Function(identifier, ParseStatement(tokens)); token = tokens.Dequeue(); if (token.Type != Token.TokenType.CloseBrace) { Fail(token); } return(function); }
static void WriteFunction(StreamWriter writer, AST.Function function) { writer.Write(Assembly.format_function, function.Identifier); WriteStatement(writer, function.Body); }