Пример #1
0
        public override TypeT CheckType()
        {
            left.CheckType();
            right.CheckType();

            type = left.type == TypeT.IntT && right.type == TypeT.IntT ? TypeT.IntT : TypeT.DoubleT;
            return(type);
        }
Пример #2
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);
        }
Пример #3
0
        public override void GenCode()
        {
            expr.CheckType();


            switch (expr.type)
            {
            case TypeT.IntT:
                expr.GenCode();
                Compiler.EmitCode("call void [mscorlib]System.Console::Write(int32)");
                break;

            case TypeT.DoubleT:

                Compiler.EmitCode("call class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Globalization.CultureInfo::get_InvariantCulture()");
                Compiler.EmitCode("ldstr \"{0:0.000000}\"");
                expr.GenCode();
                Compiler.EmitCode("box [mscorlib]System.Double");
                Compiler.EmitCode("call string [mscorlib]System.String::Format(class [mscorlib]System.IFormatProvider,string,object)");
                Compiler.EmitCode("call void [mscorlib]System.Console::Write(string)");
                break;

            case TypeT.BoolT:
                expr.GenCode();
                Compiler.EmitCode("call void [mscorlib]System.Console::Write(bool)");
                break;

            default:
                SetError("internal gencode error", line);
                break;
            }
            Compiler.EmitCode("");
        }
Пример #4
0
        public SingleExprInstruction(SyntaxTree e, int ln)
        {
            expr = e;
            line = ln;

            expr.CheckType();
        }
Пример #5
0
 public override char CheckType()
 {
     left.CheckType();
     right.CheckType();
     type = left.type == 'i' && right.type == 'i' ? 'i' : 'r';
     return(type);
 }
Пример #6
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);
                }
            }
        }
Пример #7
0
 public override char CheckType()
 {
     if (toWrite.CheckType() == 'e')
     {
         return('e');
     }
     return('o');
 }
Пример #8
0
 public ExprList(SyntaxTree s, SyntaxTree el, int ln)
 {
     line     = ln;
     exprList = el;
     stmt     = s;
     stmt.CheckType();
     exprList.CheckType();
 }
Пример #9
0
 public override char CheckType()
 {
     exp.CheckType();
     if (id[0] == '@' && exp.type != 'i')
     {
         throw new ErrorException($"  line {line,3}:  semantic error - cannot convert real to int", false);
     }
     return(' ');
 }
Пример #10
0
        public UnaryOp(SyntaxTree e, Tokens k, int ln)
        {
            expression = e;
            kind       = k;
            line       = ln;

            expression.CheckType();
            ValidateTypes(expression.type, k);
        }
Пример #11
0
        public ProgramTree(SyntaxTree el, int ln)
        {
            line     = ln;
            exprList = el;

            if (exprList != null)
            {
                exprList.CheckType();
            }
        }
Пример #12
0
            public override char CheckType()
            {
                char type1 = additiveExpression.CheckType();

                if (type1 == 'I')
                {
                    type1 = FindIndentType(((Leaf)additiveExpression).value);
                }
                char type2 = relationalExpression.CheckType();

                if (type2 == 'I')
                {
                    type2 = FindIndentType(((Leaf)relationalExpression).value);
                }
                if (type1 == 'e' || type2 == 'e')
                {
                    return('e');
                }
                if (op.Equals("==") || op.Equals("!="))
                {
                    if ((type1 == 'b' && type2 != type1) || (type2 == 'b' && type1 != type2))
                    {
                        CallAnError(line_number, "Incorrect types");
                        return('e');
                    }
                }
                else
                {
                    if (type1 == 'b' || type2 == 'b')
                    {
                        CallAnError(line_number, "Incorrect types");
                        return('e');
                    }
                }
                if (type1 != type2)
                {
                    if (type1 == 'd')
                    {
                        relationalExpression = new IntToDouble(relationalExpression);
                        type2 = relationalExpression.CheckType();
                    }
                    else
                    {
                        additiveExpression = new IntToDouble(additiveExpression);
                        type1 = additiveExpression.CheckType();
                    }
                    if (type1 != type2)
                    {
                        CallAnError(line_number, "Incorrect type. Both arguments of operand must be of the same type.");
                        return('e');
                    }
                }
                type = 'b';
                return('b');
            }
Пример #13
0
        public WriteExprOp(SyntaxTree e, int l)
        {
            expr = e;
            line = l;

            expr.CheckType();
            if (expr.type != TypeT.IntT && expr.type != TypeT.BoolT && expr.type != TypeT.DoubleT)
            {
                SetError($"Wrong argument type in write operation - expected int, double or bool.", line);
            }
        }
Пример #14
0
 public override char CheckType()
 {
     if (child.CheckType() != 'e')
     {
         return('o');
     }
     else
     {
         return('e');
     }
 }
Пример #15
0
        public IfOp(SyntaxTree c, SyntaxTree s, int ln)
        {
            line      = ln;
            condition = c;
            stmt      = s;

            condition.CheckType();
            if (condition.type != TypeT.BoolT)
            {
                SetError($"Wrong condition type in if statement- expected {TypeToStr(TypeT.BoolT)}, was {TypeToStr(condition.type)}", line);
            }
        }
Пример #16
0
            public override char CheckType()
            {
                if (!variables.ContainsKey(ident))
                {
                    CallAnError(line_number, "Undeclared variable");
                    return('e');
                }
                Var_type variable_type = variables[ident];

                type = assignExpression.CheckType();
                if (type == 'I')
                {
                    type = FindIndentType(((Leaf)assignExpression).value);
                }
                switch (variable_type)
                {
                case Var_type.real:
                    if (type == 'b')
                    {
                        CallAnError(line_number, "Incorrect type. Cannot be bool");
                        return('e');
                    }
                    if (type == 'i')
                    {
                        assignExpression = new IntToDouble(assignExpression);
                        type             = assignExpression.CheckType();
                        if (type != 'd')
                        {
                            CallAnError(line_number, "Incorrect type. Must be double");
                            return('e');
                        }
                    }
                    break;

                case Var_type.integer:
                    if (type != 'i')
                    {
                        CallAnError(line_number, "Incorrect type. Must be int");
                        return('e');
                    }
                    break;

                case Var_type.boolean:
                    if (type != 'b')
                    {
                        CallAnError(line_number, "Incorrect type. Must be bool");
                        return('e');
                    }
                    break;
                }
                return(type);
            }
Пример #17
0
        public BinaryOpLogical(SyntaxTree l, SyntaxTree r, Tokens k, int ln)
        {
            left  = l;
            right = r;
            kind  = k;
            line  = ln;

            left.CheckType();
            right.CheckType();
            if (left.type != TypeT.BoolT || right.type != TypeT.BoolT)
            {
                SetError($"Wrong argument type in logical operation - expected bool, was {TypeToStr(left.type)} and {TypeToStr(right.type)}", line);
            }
        }
Пример #18
0
        public BinaryOpBitwise(SyntaxTree l, SyntaxTree r, Tokens k, int ln)
        {
            left  = l;
            right = r;
            kind  = k;
            line  = ln;

            left.CheckType();
            right.CheckType();
            if (left.type != TypeT.IntT || right.type != TypeT.IntT)
            {
                SetError($"Wrong argument type in bitwise operation - expected int, was {TypeToStr(left.type)} and {TypeToStr(right.type)}", line);
            }
        }
Пример #19
0
            public override char CheckType()
            {
                char childType = child.CheckType();

                if (childType == 'I')
                {
                    childType = FindIndentType(((Leaf)child).value);
                }
                if (childType != 'i')
                {
                    CallAnError(line_number, "Cannot cast to double");
                    return('e');
                }
                return(type);
            }
Пример #20
0
        public BinaryOpAddMul(SyntaxTree l, SyntaxTree r, Tokens k, int ln)
        {
            left  = l;
            right = r;
            kind  = k;
            line  = ln;

            left.CheckType();
            right.CheckType();

            if (left.type == TypeT.BoolT || left.type == TypeT.VoidT || right.type == TypeT.BoolT || left.type == TypeT.VoidT)
            {
                SetError($"Wrong argument type in {kind.ToString()} operation - expected int or double, was {TypeToStr(left.type)} and {TypeToStr(right.type)}", line);
            }
        }
Пример #21
0
            public override char CheckType()
            {
                type = expression.CheckType();
                if (type == 'e')
                {
                    return('e');
                }
                if (type == 'I')
                {
                    type = FindIndentType(((Leaf)expression).value);
                }
                switch (op)
                {
                case "-":
                    if (type == 'b')
                    {
                        CallAnError(line_number, "Incorrect type. Cannot be bool");
                        return('e');
                    }
                    break;

                case "~":
                    if (type != 'i')
                    {
                        CallAnError(line_number, "Incorrect type. Must be int");
                        return('e');
                    }
                    break;

                case "!":
                    if (type != 'b')
                    {
                        CallAnError(line_number, "Incorrect type. Must be bool");
                        return('e');
                    }
                    break;

                case "toI":
                    type = 'i';
                    break;

                case "toD":
                    type = 'd';
                    break;
                }
                return(type);
            }
Пример #22
0
            public override char CheckType()
            {
                char type1 = multiplicativeExpression.CheckType();

                if (type1 == 'I')
                {
                    type1 = FindIndentType(((Leaf)multiplicativeExpression).value);
                }
                char type2 = bitExpression.CheckType();

                if (type2 == 'I')
                {
                    type2 = FindIndentType(((Leaf)bitExpression).value);
                }
                if (type1 == 'b' || type2 == 'b')
                {
                    CallAnError(line_number, "Incorrect type. Cannot be bool");
                    return('e');
                }
                if (type1 == type2)
                {
                    type = type1;
                }
                else
                {
                    if (type1 == 'd')
                    {
                        bitExpression = new IntToDouble(bitExpression);
                        type2         = bitExpression.CheckType();
                    }
                    else
                    {
                        multiplicativeExpression = new IntToDouble(multiplicativeExpression);
                        type1 = multiplicativeExpression.CheckType();
                    }
                    if (type1 != type2)
                    {
                        CallAnError(line_number, "Incorrect type. Both arguments of operand must be of the same type.");
                        return('e');
                    }
                    type = 'd';
                }
                return(type);
            }
Пример #23
0
        public BinaryOpRelation(SyntaxTree l, SyntaxTree r, Tokens k, int ln)
        {
            left  = l;
            right = r;
            kind  = k;
            line  = ln;

            left.CheckType();
            right.CheckType();

            if (left.type == TypeT.BoolT && right.type == TypeT.BoolT)
            {
            }
            else if (left.type != TypeT.DoubleT && left.type != TypeT.IntT)
            {
                SetError($"Wrong argument type in binary relation operation - expected int or double, was {TypeToStr(left.type)}", line);
            }
            else if (right.type != TypeT.DoubleT && right.type != TypeT.IntT)
            {
                SetError($"Wrong argument type in binary relation operation - expected int or double, was {TypeToStr(right.type)}", line);
            }
        }
Пример #24
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);
        }
Пример #25
0
            public override char CheckType()
            {
                char type1 = unaryExpression.CheckType();

                if (type1 == 'I')
                {
                    type1 = FindIndentType(((Leaf)unaryExpression).value);
                }
                char type2 = bitExpression.CheckType();

                if (type2 == 'I')
                {
                    type2 = FindIndentType(((Leaf)bitExpression).value);
                }
                if (type1 != 'i' || type2 != 'i')
                {
                    CallAnError(line_number, "Incorrect type. Must be int");
                    return('e');
                }
                type = 'i';
                return(type);
            }
Пример #26
0
            public override char CheckType()
            {
                type = relationalExpression.CheckType();
                if (type == 'I')
                {
                    type = FindIndentType(((Leaf)relationalExpression).value);
                }
                char type2 = logicalExpression.CheckType();

                if (type2 == 'I')
                {
                    type2 = FindIndentType(((Leaf)logicalExpression).value);
                }
                if (type == 'e' || type2 == 'e')
                {
                    return('e');
                }
                if (type != 'b' || type2 != 'b')
                {
                    CallAnError(line_number, "Incorrect type. Must be bool");
                    return('e');
                }
                return(type);
            }
Пример #27
0
 public override char CheckType()
 {
     exp.CheckType(); return(' ');
 }
Пример #28
0
        public override void GenCode()
        {
            left.CheckType();
            right.CheckType();

            left.GenCode();
            right.GenCode();

            if (left.type == TypeT.BoolT && right.type == TypeT.BoolT)
            {
                switch (kind)
                {
                case Tokens.Equal:
                    Compiler.EmitCode("ceq");
                    break;

                case Tokens.NotEqual:
                    Compiler.EmitCode("ceq");
                    Compiler.EmitCode("ldc.i4 0");
                    Compiler.EmitCode("ceq");
                    break;

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

            // uzgodnienie typów
            SetTypesOnStack(left.type, right.type);

            switch (kind)
            {
            case Tokens.Equal:
                Compiler.EmitCode("ceq");
                break;

            case Tokens.NotEqual:
                Compiler.EmitCode("ceq");
                Compiler.EmitCode("ldc.i4 0");
                Compiler.EmitCode("ceq");
                break;

            case Tokens.More:
                Compiler.EmitCode("cgt");
                break;

            case Tokens.MoreEqual:
                Compiler.EmitCode("clt");
                Compiler.EmitCode("ldc.i4 0");
                Compiler.EmitCode("ceq");
                break;

            case Tokens.Less:
                Compiler.EmitCode("clt");
                break;

            case Tokens.LessEqual:
                Compiler.EmitCode("cgt");
                Compiler.EmitCode("ldc.i4 0");
                Compiler.EmitCode("ceq");
                break;

            default:
                SetError("internal gencode error", line);
                break;
            }
        }
Пример #29
0
 public override char CheckType()
 {
     type = assign.CheckType();
     return(type);
 }