void IIndexableVariable.EmitLoad(Block block, Expression[] expr)
        {
            if (resolveContext.ParameterType.IsArrayType)
            {
                if (resolveContext.ParameterType.Rank > 1)
                {
                    block.Method.Generator.Emit(OpCodes.Ldarg, ParameterIndex);

                    foreach (Expression e in expr)
                    {
                        e.Emit();
                    }

                    block.Method.Generator.Emit(OpCodes.Ldelem);
                }
                else
                {
                    //Exception
                }
            }
            else
            {
                //Find indexer
            }
        }
        void IIndexableVariable.EmitStore(Block block, Expression[] expr)
        {
            foreach (Expression e in expr)
            {
                e.Emit();
            }

            block.Method.Generator.Emit(getOpCode(), Assigner.GetMethodInfo());
        }
 public void EmitLoad(Block block)
 {
     if (IsShared)
     {
         block.Method.Generator.Emit(OpCodes.Ldsfld, FieldBuilder);
     }
     else
     {
         block.Method.Generator.Emit(OpCodes.Ldfld, FieldBuilder);
     }
 }
 public void EmitStore(Block block)
 {
     if (IsShared)
     {
         block.Method.Generator.Emit(OpCodes.Stsfld, FieldBuilder);
     }
     else
     {
         block.Method.Generator.Emit(OpCodes.Stfld, FieldBuilder);
     }
 }
        public Block(MethodDef method, Block parent)
            : base(method, parent)
        {
            statements = new List<Statement>();

            if (parent == null)
            {
                localScope = new LocalScope(this, null);
            }
            else
            {
                localScope = new LocalScope(this, parent.localScope);
            }
        }
        public LocalScope(Block block, LocalScope parent)
        {
            Block = block;

            Parent = parent;

            Parameters = new List<Parameter>();
            Locals = new List<Local>();

            if (parent == null)
            {
                if (Method.Parameters.Count() != 0)
                {
                    Parameters.AddRange(Method.Parameters);
                }
            }
        }
 public ReturnStatement(Block block)
     : base(block)
 {
 }
        private Statement ParseWhileStmt()
        {
            tokenQueue.Dequeue();

            Expression expr;

            Statement inlineStmt;
            TokenInfo token;

            bool flag = true;
            bool inElse;

            WhileStatement whileStatement = new WhileStatement(block);
            statement = whileStatement;

            expr = ParseCompExpr();
            whileStatement.SetCondition(expr);

            ExpectEndOfStatementOrEat();

            block = whileStatement;

            while (flag)
            {
                token = this.Tokenizer.PeekToken();

                switch (token.Token)
                {
                    case Token.EOL:
                        GetNextToken();
                        break;

                    case Token.EOF:
                        GetNextToken();
                        flag = false;
                        break;

                    case Token.End:
                        GetNextToken();
                        token = this.Tokenizer.PeekToken();
                        if (token.Is(Token.While))
                        {
                            GetNextToken();
                            flag = false;
                        }
                        else
                        {
                            //Unexpected
                        }
                        break;
                
                    default:
                        inlineStmt = ParseStatement();
                        whileStatement.AddStatement(inlineStmt);

                        statement = whileStatement;
                        break;
                }
            }

            block = whileStatement.ContainingBlock;

            return whileStatement;
        }
        private void ParseField()
        {
            #region Old

            //bool b = Expect(Token.LeftSquareBracket);

            //TokenInfo token = Tokenizer.PeekToken();

            //if (token.Is(Token.Identifier) || token.Is(Token.ReservedWord))
            //{

            //}

            //if (Expect(Token.As))
            //{
            //    token = Tokenizer.PeekToken();

            //    if (Expect(Token.Identifier))
            //    {

            //    }
            //    else
            //    {

            //    }
            //}
            //else
            //{

            //}

            //if (Expect(Token.Equality))
            //{
            //    token = Tokenizer.PeekToken();

            //    if (Expect(Token.Identifier))
            //    {

            //    }
            //    else
            //    {

            //    }
            //}
            //else
            //{

            //}

            #endregion

            TokenInfo token;

            Modifier modifiers;
            SimpleName name;
            FullNamedExpression type;
            Expression expr;

            modifiers = ParseMethodModifiers();

            token = tokenQueue.Dequeue();

            name = null;
            type = null;
            expr = null;


            if (token.Is(Token.Identifier))
            {
                name = new SimpleName(statement, token.Value,
                                      new SourceData(token.GetSpan(token), SourceFile, statementSymbols));

                if (EatOptional(Token.As))
                {
                    type = ParseFullName();
                }

                TokenInfo token3 = Tokenizer.PeekToken();

                if (EatOptional(Token.Equality))
                {
                    EatOptional(Token.EOL);

                    //Set parse context to initializer
                    MethodDef initializer;

                    if ((modifiers & Modifier.Shared) == Modifier.Shared)
                    {
                        initializer = (MethodDef) this.type.GetTypeInitializer();
                    }
                    else
                    {
                        initializer = (MethodDef) this.type.GetDefaultConstructor();
                    }

                    //Initalize context
                    method = initializer;
                    block = method.Body;
                    var assignStmt = new ExpressionStatement(initializer.Body);
                    statement = assignStmt;

                    //Parse initialization expression
                    expr = ParseExpression();

                    //Build initialization expression statement
                    assignStmt.SetExpression(
                        new BinaryExpression(assignStmt, Operator.Assign,
                                             new VariableAccess(assignStmt,
                                                                new SimpleName(assignStmt, name, null), null),
                                             expr, null));

                    //Add statement
                    initializer.Body.AddStatement(assignStmt);

                    //Reset context to null
                    method = null;
                    block = null;
                    statement = null;

                    if (expr == null)
                    {
                        Report.AddItem(VBErrors.ExpressionExpected, SourceFile,
                                       token3.GetSpan(token3), statementSymbols);
                    }
                }

                TokenInfo token4 = Tokenizer.PeekToken();

                if (token4.Is(Token.EOL))
                {
                    //GetNextToken(); //*
                    resetStmtTokens();
                }
                else
                {
                    ExpectEndOfStatementOrEat();
                }
            }
            else
            {
                GetNextToken();
                TokenInfo token2 = Tokenizer.PeekToken();
                Report.AddItem(VBErrors.IdentifierExpected, SourceFile,
                               new SourceSpan(token.GetSourceLocation(), token2.GetSourceLocation()), statementSymbols);
                EatToEOL();
            }

            var fieldDef = new FieldDef(this.type, name, type, modifiers);
            fieldDef.SetInitalizationExpression(expr);

            ((ClassStructOrModuleDef) this.type).AddField(fieldDef);
        }
        private Block CloseBlock()
        {
            //if (this.block.Parent == null)
            //    throw new InternalCompilerException("Block stack is empty.");

            Block block = this.block;
            this.block = this.block.ContainingBlock;

            return block;
        }
        ///// <summary>
        ///// Parse a REPL-statement.
        ///// </summary>
        ///// <returns></returns>
        //public Statement ParseReplStatement()
        //{
        //    TokenInfo token;
        //    Statement statement = null;

        //    token = Tokenizer.PeekToken();

        //    if (token.Is(Token.EOF))
        //    {
        //        GetNextToken();
        //    }
        //    else
        //    {
        //        tokenQueue.Enqueue(token);

        //        switch (token.Token)
        //        {
        //            //case Token.Sub:
        //            //    GetNextToken();
        //            //    return new MethodDefStatement(ParseSubroutine(), new SourceData(sourceFile );

        //            //case Token.Function:
        //            //    GetNextToken();
        //            //    return new MethodDefStatement(ParseFunction());

        //            case Token.EOL:
        //                tokenQueue.Dequeue();
        //                GetNextToken();
        //                break;

        //            default:
        //                tokenQueue.Dequeue();
        //                statement = ParseStatement();
        //                break;
        //        }
        //    }

        //    if (statement == null)
        //    {
        //        statement = new ErrorStatement(null);
        //    }

        //    SourceLocation loc = token.GetSourceLocation();
        //    //statement.SetLoc(loc);

        //    return statement;
        //}

        //private TopLevelBlock ParseBlock(Token end)
        //{
        //    TopLevelBlock block;
        //    Statement statement;
        //    bool flag;
        //    TokenInfo token;

        //    block = new TopLevelBlock();
        //    statement = null;
        //    flag = true;

        //    while (flag)
        //    {
        //        token = this.GetNextToken();

        //        // Has the end of file been reached?
        //        if (token.Is(Token.EOF))
        //            break;

        //        //Push first token on the token stack
        //        this.tokenStack.Enqueue(token);

        //        switch (token.Token)
        //        {
        //                // Declaration statement
        //            case Token.Dim:
        //                statement = this.state_DeclareStmt();
        //                break;

        //                // If statement
        //            case Token.If:
        //                statement = this.ParseIfStmt();
        //                break;

        //                // For statement
        //            case Token.For:
        //                this.Report.FeatureNotImplemented("For statement", token);
        //                goto default;

        //                // Return statement
        //            case Token.Return:
        //                this.Report.FeatureNotImplemented("Return statement", token);
        //                goto default;

        //                // Could be an en of this block
        //            case Token.End:
        //                flag = !this.ParseEndStmt(end);
        //                if (flag) 
        //                    continue; 
        //                else 
        //                    break;

        //                // Empty line
        //            case Token.EOL:
        //                this.tokenStack.Dequeue();
        //                continue;

        //                // Error: Invalid statement
        //            default:
        //                this.tokenStack.Dequeue();
        //                this.Report.Warning("Invalid statement.", token.SourceFile, token.GetSourceLocation(), null);
        //                ExpectOrEatToEOL();
        //                break;
        //        }

        //        // If faulty statement was found and null was returned then emit error statement
        //        if (statement == null) statement = new ErrorStatement();

        //        // Set source location
        //        SourceLocation loc = token.GetSourceLocation();
        //        statement.SetLoc(loc);       

        //        // Add statement to block collection
        //        block.AddStatement(statement);
        //    }

        //    return block;
        //}

        private Statement ParseIfStmt()
        {
            tokenQueue.Dequeue();

            Expression expr;

            Statement inlineStmt;
            TokenInfo token;

            bool flag = true;
            bool inElse;
            
            IfStatement ifStatement = new IfStatement(block);
            statement = ifStatement;

            expr = ParseCompExpr();
            ifStatement.SetCondition(expr);

            Expect(Token.Then);

            ExpectEndOfStatementOrEat();
            
            block = ifStatement;

            //flag = ParseBlock(Token.If);

            while(flag)
            {
                token = this.Tokenizer.PeekToken();

                switch (token.Token)
                {
                    case Token.EOL:
                        GetNextToken();
                        break;

                    case Token.EOF:
                        GetNextToken();
                        flag = false;
                        break;

                    case Token.End:
                        GetNextToken();
                        token = this.Tokenizer.PeekToken();
                        if (token.Is(Token.If))
                        {
                            GetNextToken();
                            flag = false;
                        }
                        else
                        {
                            //Unexpected
                        }
                        break;
                     
                    case Token.Else:
                        GetNextToken();
                        EatOptional(Token.EOL);
                        token = this.Tokenizer.PeekToken();
                        if (token.Is(Token.If))
                        {
                            var tmp1 = ParseIfStmt();
                            ifStatement.SetElse(tmp1);

                            statement = ifStatement;
                        }
                        else
                        {
                            ParseBlock(Token.If);
                            var tmp1 = CloseBlock();
                            ifStatement.SetElse(tmp1);

                            statement = ifStatement;
                        }
                        break;

                    default:
                        inlineStmt = ParseStatement();
                        ifStatement.AddStatement(inlineStmt);

                        statement = ifStatement;
                        break;
                }
            }

            block = ifStatement.ContainingBlock;

            return ifStatement;
        }
        void IIndexableVariable.EmitStore(Block block, Expression expr)
        {
            if (resolveContext.ParameterType.IsArrayType)
            {
                block.Method.Generator.Emit(OpCodes.Ldarg, ParameterIndex);

                expr.Emit();

                block.Method.Generator.Emit(OpCodes.Stelem);
            }
        }
        private bool ParseBlock(Token end)
        {
            TokenInfo token;
            Statement statement = null;
            bool flag = true;

            block = new ScopeBlock(method, block);

            while (flag)
            {
                token = Tokenizer.PeekToken();

                if (token.Is(Token.EOF))
                {
                    GetNextToken();
                    return false;
                }

                tokenQueue.Enqueue(token);

                if (token.Is(Token.EOL))
                {
                    tokenQueue.Dequeue();
                    GetNextToken();
                    continue;
                }

                switch (token.Token)
                {
                    case Token.End:
                        GetNextToken();

                        flag = !ParseEndStmt(end);
                        if (flag)
                            continue;
                        else
                            goto end;

                        //case Token.EOL:
                        //    tokenQueue.Dequeue();
                        //    GetNextToken();
                        //    continue;

                    case Token.SingleQuote:
                        TokenList list;
                        ParseComment(out list);
                        //CommentStatement stmt = new CommentStatement(block);
                        //stmt.SetTokenList(list);
                        //statement = stmt;
                        //break;
                        continue;

                    case Token.DoubleQuote:
                        SourceLocation slocEnd = skipCharStringLiteral();
                        var span = new SourceSpan(token.GetSourceLocation(), slocEnd);
                        Report.AddItem(VBErrors.SyntaxError, SourceFile, span, statementSymbols);
                        EatToEOL();
                        resetStmtTokens();
                        //statement = null;
                        continue; //break;

                    default:
                        tokenQueue.Dequeue();
                        statement = ParseStatement();
                        resetStmtTokens();
                        break;
                }

                if (statement == null)
                {
                    statement = new ErrorStatement(block);
                }

                // Set source location
                SourceLocation loc = token.GetSourceLocation();
                //statement.SetLoc(loc);

                // Add statement to block collection
                block.AddStatement(statement);
            }

            end:

            return true;
        }
        public void EmitStore(Block block, Expression[] expr, Expression assignExpr)
        {
            EmitLoad(block);

            foreach (Expression e in expr)
            {
                e.Emit();
            }
            assignExpr.Emit();
            block.Method.Generator.Emit(OpCodes.Stelem);
        }
 void IIndexableVariable.EmitLoad(Block block, Expression expr)
 {
     expr.Emit();
     block.Method.Generator.Emit(getOpCode(), Accessor.GetMethodInfo());
 }
Example #16
0
 public void EmitStore(Block block, Expression[] expr)
 {
     int index = 0;
     EmitLoad(block);
     block.Method.Generator.Emit(OpCodes.Stelem, index);
 }
 public ErrorStatement(Block block)
     : base(block)
 {
 }
Example #18
0
 public Local(Block block, int index, string name, FullNamedExpression type)
     : this(block, index, name)
 {
     Type = type;
 }
 public WhileStatement(Block block)
     : base(block.Method, block)
 {
 }
 void IVariable.EmitLoad(Block block)
 {
     Method.Generator.Emit(OpCodes.Ldarg, (short) ParameterIndex);
 }
 public DeclareStatement(Block block)
     : base(block)
 {
 }
 void IVariable.EmitStore(Block block)
 {
     block.Method.Generator.Emit(getOpCode(), Assigner.GetMethodInfo());
 }
Example #23
0
 public void EmitStore(Block block, Expression expr)
 {
     throw new NotImplementedException();
 }
 void IVariable.EmitStore(Block block)
 {
     Method.Generator.Emit(OpCodes.Starg, (short) ParameterIndex);
 }
        public void EmitLoad(Block block, Expression[] expr)
        {
            EmitLoad(block);

            foreach (Expression e in expr)
            {
                e.Emit();
            }
            block.Method.Generator.Emit(OpCodes.Ldelem);
            //Multi-dimensional arrays?
        }
Example #26
0
 public void EmitLoad(Block block)
 {
     block.Method.Generator.Emit(OpCodes.Ldloc, LocalIndex);
 }
 void IIndexableVariable.EmitStore(Block block, Expression expr)
 {
     throw new NotSupportedException();
 }
Example #28
0
 public void EmitStore(Block block)
 {
     block.Method.Generator.Emit(OpCodes.Stloc, LocalIndex);
 }
Example #29
0
 public Local(Block block, int index, string name)
 {
     Block = block;
     LocalIndex = index;
     Name = name;
 }
 void IVariable.EmitLoad(Block block)
 {
     block.Method.Generator.Emit(getOpCode(), Accessor.GetMethodInfo());
 }