예제 #1
0
        public AssignOp(SyntaxTree r, string name, int ln)
        {
            line         = ln;
            rightExpr    = r;
            variableName = string.Concat("_", name);

            if (!declaredVariables.ContainsKey(variableName))
            {
                SetError($"Variable {name} undeclared", line);
            }
            rightExpr.CheckType();
            TypeT varType = declaredVariables[variableName];

            if (varType == TypeT.IntT || varType == TypeT.BoolT)
            {
                if (varType != rightExpr.type)
                {
                    SetError($"Wrong type in assigment - expected {TypeToStr(varType)}, was {TypeToStr(rightExpr.type)}", line);
                }
            }
            else if (varType == TypeT.DoubleT)
            {
                if (rightExpr.type != TypeT.DoubleT && rightExpr.type != TypeT.IntT)
                {
                    SetError($"Wrong type in assigment - expected {TypeToStr(varType)}, was {TypeToStr(rightExpr.type)}", line);
                }
            }
        }
예제 #2
0
        public override void GenCode()
        {
            TypeT varType = declaredVariables[variableName];

            Compiler.EmitCode("call string [mscorlib]System.Console::ReadLine()");
            Compiler.EmitCode("ldloca {0}", variableName);
            switch (varType)
            {
            case TypeT.IntT:
                Compiler.EmitCode("call bool [mscorlib]System.Int32::TryParse(string, int32&)");
                break;

            case TypeT.DoubleT:
                Compiler.EmitCode("call bool [mscorlib]System.Double::TryParse(string, float64&)");
                break;

            case TypeT.BoolT:
                Compiler.EmitCode("call bool [mscorlib]System.Boolean::TryParse(string, bool&)");
                break;

            default:
                SetError("internal gencode error", line);
                break;
            }
            Compiler.EmitCode("pop");
        }
예제 #3
0
    public static void DeclareNewVariable(string id, Tokens token, int ln)
    {
        string name = string.Concat("_", id);

        if (declaredVariables.ContainsKey(name))
        {
            SetError("Variable already declared", ln);
            return;
        }
        TypeT type = token == Tokens.Bool ? TypeT.BoolT : (token == Tokens.Int ? TypeT.IntT : TypeT.DoubleT);

        declaredVariables.Add(name, type);
    }
예제 #4
0
        public override TypeT CheckType()
        {
            expression.CheckType();
            TypeT argType = expression.type;

            type = TypeT.VoidT;
            switch (kind)
            {
            case Tokens.Minus:
                if (argType == TypeT.IntT || argType == TypeT.DoubleT)
                {
                    type = argType;
                }
                break;

            case Tokens.BitNeg:
                if (argType == TypeT.IntT)
                {
                    type = argType;
                }
                break;

            case Tokens.LogicNeg:
                if (argType == TypeT.BoolT)
                {
                    type = argType;
                }
                break;

            case Tokens.Int:
                if (argType != TypeT.VoidT)
                {
                    type = TypeT.IntT;
                }
                break;

            case Tokens.Double:
                if (argType != TypeT.VoidT)
                {
                    type = TypeT.DoubleT;
                }
                break;

            default:
                SetError("internal gencode error", line);
                break;
            }

            return(type);
        }
예제 #5
0
        T2K()
        {
            Type_g = new TypeG[12];
            Type_t = new TypeT[12];

            Diam       = new int[4];
            G_max      = new float[4];
            G_pcnt_max = new byte[4];
            G_pcnt_min = new byte[4];

            F_max  = new float[2];
            Weight = new float[2];

            SysConN = new SysCon[4];
        }
예제 #6
0
    public static void SetTypesOnStack(TypeT type1, TypeT type2)
    {
        TypeT type = (type1 == TypeT.IntT && type2 == TypeT.IntT) ? TypeT.IntT : TypeT.DoubleT;

        if (type1 != type)
        {
            Compiler.EmitCode("stloc temp");
            Compiler.EmitCode("conv.r8");
            Compiler.EmitCode("ldloc temp");
        }
        if (type2 != type)
        {
            Compiler.EmitCode("conv.r8");
        }
    }
예제 #7
0
        public ReadOp(string name, int ln)
        {
            variableName = string.Concat("_", name);
            line         = ln;

            if (!declaredVariables.ContainsKey(variableName))
            {
                SetError($"Variable {name} undeclared", line);
            }
            TypeT varType = declaredVariables[variableName];

            if (varType != TypeT.IntT && varType != TypeT.BoolT && varType != TypeT.DoubleT)
            {
                SetError($"Wrong variable type in read operation", line);
            }
        }
예제 #8
0
    public static string TypeToStr(TypeT type)
    {
        switch (type)
        {
        case TypeT.IntT:
            return("int32");

        case TypeT.DoubleT:
            return("float64");

        case TypeT.BoolT:
            return("bool");

        default:
            return("void");
        }
    }
예제 #9
0
        private void ValidateTypes(TypeT argType, Tokens token)
        {
            switch (token)
            {
            case Tokens.Minus:
                if (argType != TypeT.IntT && argType != TypeT.DoubleT)
                {
                    SetError($"Wrong argument type in unary miuns operation - expected int or double, was {TypeToStr(argType)}", line);
                }
                break;

            case Tokens.BitNeg:
                if (argType != TypeT.IntT)
                {
                    SetError($"Wrong argument type in bit negation operation - expected int, was {TypeToStr(argType)}", line);
                }
                break;

            case Tokens.LogicNeg:
                if (argType != TypeT.BoolT)
                {
                    SetError($"Wrong argument type in logical negation operation - expected bool, was {TypeToStr(argType)}", line);
                }
                break;

            case Tokens.Int:
                if (argType == TypeT.VoidT)
                {
                    SetError($"Wrong argument type in cast to int operation", line);
                }
                break;

            case Tokens.Double:
                if (argType == TypeT.VoidT)
                {
                    SetError($"Wrong argument type in cast to double operation", line);
                }
                break;

            default:
                SetError("internal gencode error", line);
                break;
            }
        }
예제 #10
0
        public override void GenCode()
        {
            TypeT varType = declaredVariables[variableName];

            rightExpr.CheckType();
            rightExpr.GenCode();

            if (varType == TypeT.IntT || varType == TypeT.BoolT)
            {
                Compiler.EmitCode("stloc {0}", variableName);
            }

            if (varType == TypeT.DoubleT)
            {
                if (rightExpr.type == TypeT.IntT)
                {
                    Compiler.EmitCode("conv.r8");
                }
                Compiler.EmitCode("stloc {0}", variableName);
            }
            Compiler.EmitCode("ldloc {0}", variableName);
        }