Exemplo n.º 1
0
 public DataType VisitExp(ExpStatement e)
 {
     if (e.Expression != null)
     {
         e.Expression.Accept(this);
     }
     return(DataType.Cont);
 }
Exemplo n.º 2
0
        public void VisitExp(ExpStatement e)
        {
            if (e.Expression is AssignExp ass)
            {
                if (ass.Dst is Identifier idDst &&
                    idDst.Name != "__slots__")
                {
                    var(dt, nmspcs) = types.TranslateTypeOf(idDst);
                    gen.EnsureImports(nmspcs);
                    gensym.EnsureLocalVariable(idDst.Name, dt, false);
                }

                if (ass.Dst is ExpList dstTuple)
                {
                    if (ass.Src is ExpList srcTuple)
                    {
                        EmitTupleToTupleAssignment(dstTuple.Expressions, srcTuple.Expressions);
                    }
                    else
                    {
                        var rhsTuple = ass.Src !.Accept(xlat);
                        EmitTupleAssignment(dstTuple.Expressions, rhsTuple);
                    }
                    return;
                }
                CodeExpression?rhs = ass.Src?.Accept(xlat);
                var            lhs = ass.Dst.Accept(xlat);
                if (gen.CurrentMember != null)
                {
                    if (ass.op == Op.Assign)
                    {
                        if (rhs != null)
                        {
                            gen.Assign(lhs, rhs);
                        }
                    }
                    else
                    {
                        gen.SideEffect(e.Expression.Accept(xlat));
                    }
                }
                else
                {
                    if (ass.Dst is Identifier id)
                    {
                        ClassTranslator_GenerateField(id, xlat, ass);
                    }
                    else
                    {
                        //$TODO: declarations
                        if (rhs != null)
                        {
                            EnsureClassConstructor().Statements.Add(
                                new CodeAssignStatement(lhs, rhs));
                        }
                    }
                }
                return;
            }
            else if (e.Expression is Ellipsis)
            {
                return;
            }
            if (gen.CurrentMember != null)
            {
                var ex = e.Expression.Accept(xlat);
                gen.SideEffect(ex);
            }
            else
            {
                var ex = e.Expression.Accept(xlat);
                EnsureClassConstructor().Statements.Add(
                    new CodeExpressionStatement(ex));
            }
        }
Exemplo n.º 3
0
        public void VisitExp(ExpStatement e)
        {
            var ass = e.Expression as AssignExp;
            if (ass != null)
            {
                var idDst = ass.Dst as Identifier;
                if (idDst != null)
                    EnsureLocalVariable(idDst.Name, new CodeTypeReference(typeof(object)), false);

                var dstTuple = ass.Dst as ExpList;
                if (dstTuple != null)
                {
                    var srcTuple = ass.Src as ExpList;
                    if (srcTuple != null)
                    {
                        EmitTupleToTupleAssignment(dstTuple.Expressions, srcTuple.Expressions);
                    }
                    else
                    {
                        var rhsTuple = ass.Src.Accept(xlat);
                        EmitTupleAssignment(dstTuple.Expressions, rhsTuple);
                    }
                    return;
                }
                var rhs = ass.Src.Accept(xlat);
                var lhs = ass.Dst.Accept(xlat);
                if (gen.CurrentMethod != null)
                {
                    if (ass.op == Op.Assign)
                    {
                        gen.Assign(lhs, rhs);
                    }
                    else
                    {
                        gen.SideEffect(e.Expression.Accept(xlat));
                    }
                }
                else
                {
                    var id = ass.Dst as Identifier;
                    if (id != null)
                    {
                        ClassTranslator_GenerateField(id, xlat, ass);
                    }
                    else
                    {
                        EnsureClassConstructor().Statements.Add(
                            new CodeAssignStatement(lhs, rhs));
                    }
                }
                return;
            }
            if (gen.CurrentMethod != null)
            {
                var ex = e.Expression.Accept(xlat);
                gen.SideEffect(ex);
            }
            else
            {
                var ex = e.Expression.Accept(xlat);
                EnsureClassConstructor().Statements.Add(
                    new CodeExpressionStatement( e.Expression.Accept(xlat)));
            }
        }
Exemplo n.º 4
0
        public void VisitExp(ExpStatement e)
        {
            if (e.Expression is AssignExp ass)
            {
                if (ass.Dst is Identifier idDst)
                {
                    gensym.EnsureLocalVariable(idDst.Name, new CodeTypeReference(typeof(object)), false);
                }

                if (ass.Dst is ExpList dstTuple)
                {
                    if (ass.Src is ExpList srcTuple)
                    {
                        EmitTupleToTupleAssignment(dstTuple.Expressions, srcTuple.Expressions);
                    }
                    else
                    {
                        var rhsTuple = ass.Src.Accept(xlat);
                        EmitTupleAssignment(dstTuple.Expressions, rhsTuple);
                    }
                    return;
                }
                var rhs = ass.Src.Accept(xlat);
                var lhs = ass.Dst.Accept(xlat);
                if (gen.CurrentMember != null)
                {
                    if (ass.op == Op.Assign)
                    {
                        gen.Assign(lhs, rhs);
                    }
                    else
                    {
                        gen.SideEffect(e.Expression.Accept(xlat));
                    }
                }
                else
                {
                    if (ass.Dst is Identifier id)
                    {
                        ClassTranslator_GenerateField(id, xlat, ass);
                    }
                    else
                    {
                        EnsureClassConstructor().Statements.Add(
                            new CodeAssignStatement(lhs, rhs));
                    }
                }
                return;
            }
            if (gen.CurrentMember != null)
            {
                var ex = e.Expression.Accept(xlat);
                gen.SideEffect(ex);
            }
            else
            {
                var ex = e.Expression.Accept(xlat);
                EnsureClassConstructor().Statements.Add(
                    new CodeExpressionStatement(e.Expression.Accept(xlat)));
            }
        }
Exemplo n.º 5
0
 public void VisitExp(ExpStatement e)
 {
     // forces any pending indentation to be emitted
     // since expressions are unaware of indenting writers.
     w.Write("");
     e.Expression.Write(writer);
 }
Exemplo n.º 6
0
 public SymbolTable VisitExp(ExpStatement e)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 7
0
Arquivo: AST.cs Projeto: chenzuo/blue
    // Resolve all the fields in this type. Only class/struct should call
    // this. 
    void FixFields(ISemanticResolver s, ICLRtypeProvider provider)
    {
        Debug.Assert(!IsInterface);
        
        int cInstance = 0;
        int cStatic = 0;

        foreach(FieldDecl f in m_alFields)
        {
            f.ResolveMember(m_symbol, s, provider);
            //f.Symbol.SetInfo(provider);

            if (f.InitialExp != null)
                if (f.Mods.IsStatic) 
                    cStatic++; 
                else 
                    cInstance++;
        }


        Statement [] stmtStatic = new Statement[cStatic];
        Statement [] stmtInstance = new Statement[cInstance];

        cStatic = 0;
        cInstance = 0;
        // Fields can have assignments. Make 2 helper functions to do
        // assignment for static & instance fields
        foreach(FieldDecl f in m_alFields)
        {
            if (f.InitialExp != null)
            {           
                Statement stmt = new ExpStatement(new AssignStmtExp(
                    new SimpleObjExp(new Identifier(f.Name, f.Location)), f.InitialExp));

                if (f.Mods.IsStatic)
                {
                    stmtStatic[cStatic] = stmt;
                    cStatic++;
                } 
                else 
                {
                    if (IsStruct)
                    {
                        //ThrowError_NoFieldInitForStructs(s, f);
                        ThrowError(SymbolError.NoFieldInitForStructs(f));
                    }
                    
                    stmtInstance[cInstance] = stmt;
                    cInstance ++;
                }
            } // end has initializer expression
        }

        Debug.Assert(cStatic == stmtStatic.Length);
        Debug.Assert(cInstance == stmtInstance.Length);

        // Create methods to initialize the static & instance fields.
        // Then the ctors can call these methods
        if (cStatic != 0)
        {
            Modifiers mods = new Modifiers();
            mods.SetStatic();
            mods.SetPrivate();
            
            m_nodeStaticInit = new MethodDecl(
                new Identifier(".StaticInit", this.Location), 
                new ResolvedTypeSig(typeof(void), s),                    
                new ParamVarDecl[0],
                new BlockStatement(null, stmtStatic),                     
                mods
                );

            //AddMethodToList(m_nodeStaticInit);
        } 
        if (cInstance != 0)
        {   
            Modifiers mods = new Modifiers();
            mods.SetPrivate();
            
            m_nodeInstanceInit = new MethodDecl(
                new Identifier(".InstanceInit", this.Location),                     
                new ResolvedTypeSig(typeof(void), s),
                new ParamVarDecl[0],
                new BlockStatement(null, stmtInstance),                     
                mods
                );
            AddMethodToList(m_nodeInstanceInit);
        }        
    } // end fields
Exemplo n.º 8
0
    // Do the real work
    protected void ParseStatementOrLocal_Helper(out Statement s, out LocalVarDecl v)
    {   
        s = null;
        v = null;
        
        // For each statement, we know which type based off the first token.
        // Expect for an identifier, in which case it could be a few things.
        Token t = m_lexer.PeekNextToken();
        
        #if false
        // Skip past any ';' (as empty statements)
        while(t.TokenType == Token.Type.cSemi)
        {
            ConsumeNextToken();
            t = m_lexer.PeekNextToken();            
        }
        #endif
                       
        if (IsStartOfExp(t))
        {
            FileRange f = BeginRange();
            
            // This could be either an expression or a type
            Exp e = ParseExp();
            t = m_lexer.PeekNextToken();

            
            // Case 1 - Var declaration:
            // If an identifier follows, then we just read a type and this is
            // a var declaration:
            // Type id ';'
            // Type id '=' exp ';'
            if (t.TokenType == Token.Type.cId)
            {
                TypeSig tSig  = this.ConvertExpToType(e);

                Identifier id = ReadExpectedIdentifier();
                
                v = new LocalVarDecl(id, tSig);
                                
                // Check for optional assignment (if there's an '=' after the name)
                Token t3 = m_lexer.PeekNextToken();
                if (t3.TokenType == Token.Type.cAssign)
                {
                    ConsumeNextToken();                     // '='
                    Exp eRHS = ParseExp();                  // exp                
                    ReadExpectedToken(Token.Type.cSemi);    // ';'
                    
                    SimpleObjExp oleft = new SimpleObjExp(id);
                    StatementExp se = new AssignStmtExp(oleft, eRHS);
                    s = new ExpStatement(se);
                    
                    se.SetLocation(EndRange(f));
                } else {                
                    ReadExpectedToken(Token.Type.cSemi);    // ';'
                }


                
                return;
            } // end decl case

            // Case 2 - label declaration
            else if (t.TokenType == Token.Type.cColon)
            {                
                SimpleObjExp o2 = e as SimpleObjExp;
                if (o2 != null)
                {
                    ConsumeNextToken(); // ':'
                    s = new LabelStatement(o2.Name);
                    return; // skip reading a ';'
                } 
                
                ThrowError(new ParserErrorException(Code.cBadLabelDef, t.Location, 
                    "Bad label definition (labels must be a single identifier)"));                                
            } // end case for label decls
                        
            // Expect a StatementExp
            else if (t.TokenType == Token.Type.cSemi) {
                ReadExpectedToken(Token.Type.cSemi);
                
                // Else we must be a StatementExp
                StatementExp se = e as StatementExp;
                if (se == null)
                    //this.ThrowError_ExpectedStatementExp(e.Location);
                    ThrowError(E_ExpectedStatementExp(e.Location));
                
                se.SetLocation(EndRange(f));
                s = new ExpStatement(se);            
                return;
            }
    
            ThrowError(E_UnexpectedToken(t));
        } // end start of expressions
        
        switch(t.TokenType)
        {
        // Empty statement
        case Token.Type.cSemi:
            ConsumeNextToken();
            s = new EmptyStatement();
            break;

        // Return -> 'return' ';'
        //         | 'return' exp ';'
        case Token.Type.cReturn:
            {
                ConsumeNextToken();
                
                t = m_lexer.PeekNextToken();
                Exp e = null;
                if (t.TokenType != Token.Type.cSemi) 
                {
                    e = ParseExp();                    
                }
                ReadExpectedToken(Token.Type.cSemi);
        
                s = new ReturnStatement(e);                
            }        
            break;
            
        // Note that the semi colons are included inthe stmt            
        // IfSmt -> 'if' '(' exp ')' stmt:then 
        // IfSmt -> 'if' '(' exp ')' stmt:then 'else' stmt:else
        case Token.Type.cIf:
        {
            ConsumeNextToken(); // 'if'
            ReadExpectedToken(Token.Type.cLParen);
            Exp exp = ParseExp();            
            ReadExpectedToken(Token.Type.cRParen);
            
            Statement sThen = ParseStatement();
            Statement sElse = null;
            
            Token t2 = m_lexer.PeekNextToken();
            if (t2.TokenType == Token.Type.cElse) 
            {
                ConsumeNextToken(); // 'else'
                sElse = ParseStatement();                
            }
            
            s = new IfStatement(exp, sThen, sElse);        
        }
            break;
            
        case Token.Type.cSwitch:
            s = ParseSwitchStatement();
            break;            
        
        // Throw an expression
        // ThrowStmt -> 'throw' objexp    
        case Token.Type.cThrow:
        {
            ConsumeNextToken(); // 'throw'
            Exp oe = null;
            if (m_lexer.PeekNextToken().TokenType != Token.Type.cSemi)
            {
                oe = ParseExp();            
            }
            ReadExpectedToken(Token.Type.cSemi);
            
            s = new ThrowStatement(oe);
        }
            break;
        
        // try-catch-finally
        case Token.Type.cTry:
            s = ParseTryCatchFinallyStatement();
            break;
        
        // while loop
        // 'while' '(' exp ')' stmt            
        case Token.Type.cWhile:
        {
            ConsumeNextToken(); // 'while'
            ReadExpectedToken(Token.Type.cLParen);

            Exp e = ParseExp();

            ReadExpectedToken(Token.Type.cRParen);

            Statement body = ParseStatement();

            s = new WhileStatement(e, body);


        }
            break;

        // do loop
        // 'do' stmt 'while' '(' exp ')' ';'
        case Token.Type.cDo:
        {
            ConsumeNextToken(); // 'do'
            Statement body = ParseStatement();
            ReadExpectedToken(Token.Type.cWhile);

            ReadExpectedToken(Token.Type.cLParen);
            Exp e = ParseExp();
            ReadExpectedToken(Token.Type.cRParen);

            ReadExpectedToken(Token.Type.cSemi);

            s = new DoStatement(e, body);

        }
            break;

        // goto
        // 'goto' id:label ';'
        case Token.Type.cGoto:
        {
            ConsumeNextToken();                             // 'goto'
            Identifier id = ReadExpectedIdentifier();       // id:label
            ReadExpectedToken(Token.Type.cSemi);            // ';'

            s = new GotoStatement(id);
        }
            break;

        // break
        // 'break' ';'
        case Token.Type.cBreak:
            ConsumeNextToken();
            ReadExpectedToken(Token.Type.cSemi);
            s = new BreakStatement();
            break;

        // Continue
        // 'continue' ';'
        case Token.Type.cContinue:
            ConsumeNextToken();
            ReadExpectedToken(Token.Type.cSemi);
            s = new ContinueStatement();
            break;
            
        // For-loop            
        case Token.Type.cFor:
            s = ParseForStatement();
            break;

        // For-each
        // -> 'foreach' '(' Type id 'in' exp:collection ')' stmt
        case Token.Type.cForEach:
            s = ParseForeachStatement();
            break;
            
        // BlockStatement - can be nested inside each other
        // start with a  '{', no terminating semicolon
        case Token.Type.cLCurly:
            {
                s = ParseStatementBlock();                
            }
            break;
            
        default:
            ThrowError(E_UnexpectedToken(t)); // unrecognized statement
            break;
        
        } // end switch
        
        // Must have come up with something
        Debug.Assert(s != null || v != null);    
    }