コード例 #1
0
        public static Block Parse(String body)
        {
            var block = new Block();
            var lexer = new Lexer(body);

            while (lexer.HasNext())
            {
                // Conditional
                if (lexer.PeekToken() == "if")
                {
                    lexer.SkipToken();      // Absorb if
                    Expression condition = new Expression(lexer.ReadBlock("(", ")"));
                    Block b = Block.Parse(lexer.ReadBlock("{", "}"));
                    IfStatement statement = new IfStatement(condition, b);
                    block.Statements.Add(statement);
                }
                else if(lexer.PeekToken(2) == "=")
                {
                    var type = lexer.ReadToken();
                    var name = lexer.ReadToken();
                    lexer.SkipToken();
                    var value = lexer.ReadUntil(";");
                    var variableDefinition = new VariableDefinition(name, type, value);
                    block.Statements.Add(variableDefinition);
                }
                else
                {
                    string str = lexer.ReadUntil(";");
                    block.Statements.Add( Expression.Parse(str) );
                    lexer.SkipToken();  // Skip ;
                }
            }

            return block;
        }
コード例 #2
0
        private void ParseClassBody(String body)
        {
            Lexer lexer = new Lexer(body);

            while (lexer.HasNext())
            {
                if (lexer.PeekToken(2) == ";") // Member definition without initial value
                {
                    var member = CreateMember(lexer.ReadToken(), lexer.ReadToken());
                    lexer.SkipToken(); // ;
                    Members.Add(member);
                }
                if (lexer.PeekToken(2) == "=") // Member definition with initial value
                {
                    var member = CreateMember(lexer.ReadToken(), lexer.ReadToken(), lexer.ReadBlock("=", ";"));
                    Members.Add(member);
                }
                if (lexer.PeekToken(2) == "(") // Function definition
                {
                    Function function = Function.Parse(
                        lexer.ReadToken(), // return type
                        lexer.ReadToken(), // name
                        lexer.ReadBlock("(", ")"), // arguments
                        lexer.ReadBlock("{", "}")); // body
                    Functions.Add(function);
                }
            }
        }