示例#1
0
    public override CType CheckType()
    {
        var t1 = exp1.CheckType();
        var t2 = exp2.CheckType();

        return(t1 == CType.Double || t2 == CType.Double
            ? CType.Double
            : t1);
    }
示例#2
0
    public override CType CheckType()
    {
        var idType  = Compiler.variables[id];
        var expType = exp.CheckType();

        if (idType != expType && !(idType == CType.Double && expType == CType.Int))
        {
            new Error($"Cannot assign {expType} to {idType}");
        }

        return(idType);
    }
示例#3
0
    public override void GenCode()
    {
        var t = exp.CheckType();

        if (t == CType.Double)
        {
            EmitCode("call class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Globalization.CultureInfo::get_InvariantCulture()");
            EmitCode("ldstr \"{0:0.000000}\"");
        }

        exp.GenCode();

        string type = "";

        switch (t)
        {
        case CType.Bool:
            type = "bool";
            break;

        case CType.Int:
            type = "int32";
            break;

        case CType.Double:
            type = "float64";
            EmitCode("box [mscorlib]System.Double");
            EmitCode("call string [mscorlib]System.String::Format(class [mscorlib]System.IFormatProvider, string, object)");
            EmitCode("call void [System.Console]System.Console::Write(string)");
            return;
        }

        EmitCode("call void [System.Console]System.Console::Write({0})", type);
    }
示例#4
0
    public RelationNode(
        SyntaxNode ex1,
        SyntaxNode ex2,
        string op,
        string opName,
        bool canBeBool)
        : base(ex1, ex2, op)
    {
        var t1 = ex1.CheckType();
        var t2 = ex2.CheckType();

        if (canBeBool)
        {
            if (t1 != t2 &&
                !(t1 == CType.Double && t2 == CType.Int) &&
                !(t1 == CType.Int && t2 == CType.Double))
            {
                new Error($"Attempted {opName} check on a {t1} and {t2}");
            }
        }
        else
        {
            if (t1 == CType.Bool || t2 == CType.Bool)
            {
                new Error($"Attempted {opName} check on a non number type");
            }
        }
    }
示例#5
0
 public CalculationNode(SyntaxNode ex1, SyntaxNode ex2, string op, string opName)
     : base(ex1, ex2, op)
 {
     if (ex1.CheckType() == CType.Bool ||
         ex2.CheckType() == CType.Bool)
     {
         new Error($"Attempted {opName} on a non number type");
     }
 }
示例#6
0
    public BitNegNode(SyntaxNode ex)
    {
        if (ex.CheckType() != CType.Int)
        {
            new Error("Attempted bit negation on a non int type");
        }

        exp = ex;
    }
示例#7
0
    public NegNode(SyntaxNode ex)
    {
        if (ex.CheckType() == CType.Bool)
        {
            new Error("Attempted negation on a non number type");
        }

        exp = ex;
    }
示例#8
0
    public override void GenCode()
    {
        exp.GenCode();

        if (exp.CheckType() != CType.Double)
        {
            EmitCode("conv.r8");
        }
    }
示例#9
0
    public BoolNegNode(SyntaxNode ex)
    {
        if (ex.CheckType() != CType.Bool)
        {
            new Error("Attempted logical negation on a non bool type");
        }

        exp = ex;
    }
示例#10
0
    public IfNode(SyntaxNode con, SyntaxNode stmnt)
    {
        if (con.CheckType() != CType.Bool)
        {
            new Error("If condition must return bool");
        }

        condition = con;
        statement = stmnt;
        label     = Compiler.NextLabel();
    }
示例#11
0
    public LogicalNode(SyntaxNode ex1, SyntaxNode ex2, string op, string opName)
        : base(ex1, ex2, op)
    {
        if (ex1.CheckType() != CType.Bool ||
            ex2.CheckType() != CType.Bool)
        {
            new Error($"Attempted {opName} on a non bool type");
        }

        label = Compiler.NextLabel();
    }
示例#12
0
    public IfElseNode(
        SyntaxNode con,
        SyntaxNode ifStmnt,
        SyntaxNode elseStmnt)
    {
        if (con.CheckType() != CType.Bool)
        {
            new Error("If condition must return bool");
        }

        condition     = con;
        ifStatement   = ifStmnt;
        elseStatement = elseStmnt;
        ifLabel       = Compiler.NextLabel();
        elseLabel     = Compiler.NextLabel();
    }
示例#13
0
    public AssignNode(string i, SyntaxNode e)
    {
        if (!Compiler.variables.ContainsKey(i))
        {
            throw new ErrorException($"Attempted to assign to undeclared variable {i}");
        }

        var idType  = Compiler.variables[i];
        var expType = e.CheckType();

        if (idType != expType && !(idType == CType.Double && expType == CType.Int))
        {
            new Error($"Cannot assign {expType} to {idType}");
        }

        id  = i;
        exp = e;
    }
示例#14
0
 public SemicolonNode(SyntaxNode exp)
 {
     exp.CheckType();
     expression = exp;
 }
示例#15
0
 public override CType CheckType()
 {
     return(exp.CheckType());
 }