⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expast.cs

📁 charp compiler
💻 CS
📖 第 1 页 / 共 5 页
字号:
    {
        Debug.Assert(left != null);
            
        m_filerange = left.Location;
    
        m_left = left;        
        m_op = op;
    }

    protected Exp m_left;    
    protected UnaryOp m_op;

    public Exp Left
    {
        get { return m_left; }
    }            
    
    public UnaryOp Op
    {
        get { return m_op; }
    }
    
    // Nothing to resolve for literal expressions
    protected override Exp ResolveExpAsRight(ISemanticResolver s)
    {
        ResolveExpAsRight(ref m_left, s);

        CalcCLRType(s);
        
        return this;
    }

    // Dump as XML
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("UnaryExp");
        o.WriteAttributeString("op", m_op.ToString());
        m_left.Dump(o);
        o.WriteEndElement();
    }         

    // Code generation
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);        
    }

    // Determine the CLR type of this expression
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        switch(m_op)
        {
            case UnaryOp.cNegate:
                return typeof(int);
            case UnaryOp.cNot:
                return typeof(bool);

            case UnaryOp.cPreInc:
                return typeof(int);

            case UnaryOp.cPostInc:
                return typeof(int);

            case UnaryOp.cPreDec:
                return typeof(int);

            case UnaryOp.cPostDec:
                return typeof(int);
        
            default:
                Debug.Assert(false, "Illegal unary operator:" + m_op.ToString());
                return null;
        }
        
    }
  
#region Checks    
    //-----------------------------------------------------------------------------
    // Debugging check
    //-----------------------------------------------------------------------------
    public override void DebugCheck(ISemanticResolver s)
    {
        CalcCLRType(s);  
        m_left.DebugCheck(s);
    }
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        char ch = '?';
        switch(m_op)
        {
            case UnaryOp.cNegate: ch = '-'; break;
            case UnaryOp.cNot: ch = '!'; break;
        }
        sb.Write(ch);
        this.Left.ToSource(sb);
    }
#endregion    
}

#endregion

#region Literal Expressions
//-----------------------------------------------------------------------------    
// Literals - constant expressions
//-----------------------------------------------------------------------------        

/// <summary>
/// Abstract base class for all literals (ints, strings, chars, null, etc)
/// </summary>
public abstract class LiteralExp : Exp
{    
    public LiteralExp()
    {
#if DEBUG
        m_fResolved = false;
#endif
    }

    // Nothing to resolve for literal expressions
    protected override Exp ResolveExpAsRight(ISemanticResolver s)
    {
        CalcCLRType(s);
#if DEBUG
        m_fResolved = true;
#endif
        return this;
    }
    
    
    public override void DebugCheck(ISemanticResolver s)
    {   
        Debug.Assert(Exp.CanBeNullType(this) || this.CLRType != null);
#if DEBUG
        Debug.Assert(m_fResolved);
#endif      
    }

// We're supposed to resolve all expressions. But if we forget to resolve
// a literal, things will still conveniently work out, and so we won't notice it.
// But if we don't resolve a literal, then we could have missed any expression.
// So add a debug-only check here to make sure literals are resolved.
#if DEBUG
    bool m_fResolved;
#endif

    // Generating the address of a literal is ok. We just have to
    // create a temporary local to provide backing storage
    public override void GenerateAddrOf(CodeGen.EmitCodeGen gen)
    {
        gen.GenerateAddrOf(this);
    }        
}

//-----------------------------------------------------------------------------
// Literal expression
//-----------------------------------------------------------------------------
/// <summary>
/// Node representing the 'null' keyword literal.
/// </summary>
public class NullExp : LiteralExp
{
    public NullExp(FileRange location) 
    {
        m_filerange = location;
    }

    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        return null;
    }

    // Dump as XML
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("NullExp");        
        o.WriteEndElement();
    }        
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("null");
    }

    // Code generation
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);
    }  
    
     
}

//-----------------------------------------------------------------------------        
// literal integer    
//-----------------------------------------------------------------------------        

/// <summary>
/// Node representing an System.Int32
/// </summary>
public class IntExp : LiteralExp
{
    public IntExp(int i, FileRange location) 
    {
        m_filerange = location;
        m_value = i;        
    }
    
    // Determine the CLR type of the expression
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {        
        return typeof(int);
    }
    
    int m_value;
    
    public int Value
    {
        get { return m_value; }
    }    
    
    // Dump as XML
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("IntExp");
        o.WriteAttributeString("value", m_value.ToString());                        
        o.WriteEndElement();
    }   
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("{0}i", m_value);
    }     

    // Code generation
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);
    }
}

//-----------------------------------------------------------------------------
// literal boolean
//-----------------------------------------------------------------------------
public class StringExp : LiteralExp
{
    public StringExp(string s, FileRange location) 
    {
        m_filerange = location;
        m_stValue = s;        
    }

    // Determine the CLR type of the expression
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {        
        return typeof(string);
    }
    
    string m_stValue;

    public string
    Value
    {
        get 
        { 
            return m_stValue; 
        }
    }    

    // Dump as XML
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("StringExp");
        o.WriteAttributeString("value", Value);
        o.WriteEndElement();
    }        
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("@\"{0}\"", m_stValue);
    }

    // Code generation
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);
    }
}

//-----------------------------------------------------------------------------
// literal boolean
//-----------------------------------------------------------------------------
public class BoolExp : LiteralExp
{
    public BoolExp(bool f, FileRange location) 
    {
        m_filerange = location;
        m_fValue = f;        
    }

    // Determine the CLR type of the expression
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        return typeof(bool);
    }

    bool m_fValue;

    public bool
    Value
    {
        get 
        { 
            return m_fValue; 
        }
    }    

    // Dump as XML
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("BoolExp");
        o.WriteAttributeString("value", m_fValue ? "true" : "false");                        
        o.WriteEndElement();
    }        

    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("{0}b", m_fValue ? "true" : "false");
    }

    // Code generation
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);
    }
 
}

#if false
//-----------------------------------------------------------------------------
// literal double precision floating point number
//-----------------------------------------------------------------------------
public class DoubleExp : LiteralExp
{
    public DoubleExp(double d, FileRange location) 
    {
        m_filerange = location;
        m_dValue = d;        
    }

    // Determine the CLR type of the expression
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {        
        return typeof(double);        
    }

    double m_dValue;

    public double
    Value
    {
        get 
        { 
            return m_dValue; 
        }
    }    

    // Dump as XML
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("DoubleExp");
        o.WriteAttributeString("value", m_dValue.ToString());
        o.WriteEndElement();
    }        

    // Code generation
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);
    }
  
}
#endif

//-----------------------------------------------------------------------------
// literal Char
//-----------------------------------------------------------------------------
public class CharExp : LiteralExp
{
    public CharExp(char ch, FileRange location) 
    {
        m_filerange = location;
        m_chValue = ch;        
    }

    // Determine the CLR type of the expression
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        return typeof(char);
    }

    char m_chValue;

    public char Value
    {
        get 
        { 
            return m_chValue; 
        }
    }    

    // Dump as XML
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("CharExp");
        o.WriteAttributeString("value", m_chValue.ToString());
        o.WriteEndElement();
    }        
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("'{0},{1}'", m_chValue, (int) m_chValue);
    }

    // Code generation
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);
    }

}
#endregion

} // namespace AST

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -