Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
 static void WriteFunction(StreamWriter writer, AST.Function function)
 {
     writer.Write(Assembly.format_function, function.Identifier);
     WriteStatement(writer, function.Body);
 }