Ejemplo n.º 1
0
        public static Type WhileToType(WhileType whileType)
        {
            Type type = typeof(object);

            switch (whileType)
            {
            case WhileType.INT:
            {
                type = typeof(int);
                break;
            }

            case WhileType.BOOL:
            {
                type = typeof(bool);
                break;
            }

            case WhileType.STRING:
            {
                type = typeof(string);
                break;
            }
            }
            return(type);
        }
Ejemplo n.º 2
0
Archivo: Scope.cs Proyecto: dotted/csly
        /// <summary>
        /// Set a variable type
        /// </summary>
        /// <param name="name">varaible name</param>
        /// <param name="variableType">variable type</param>
        /// <returns>true if variable is a new variable in scope</returns>
        public bool SetVariableType(string name, WhileType variableType)
        {
            bool creation = !variables.ContainsKey(name);

            variables[name] = variableType;
            return(creation);
        }
Ejemplo n.º 3
0
        public static string WhileToCSharp(WhileType whileType)
        {
            string cSharpType = "";

            switch (whileType)
            {
            case WhileType.INT:
            {
                cSharpType = "int";
                break;
            }

            case WhileType.BOOL:
            {
                cSharpType = "bool";
                break;
            }

            case WhileType.STRING:
            {
                cSharpType = "string";
                break;
            }
            }
            return(cSharpType);
        }
Ejemplo n.º 4
0
 public WhileNode(Node test, Node body, WhileType type)
     : base(NodeType.While)
 {
     Test = test;
     Body = body;
     Loop = type;
 }
Ejemplo n.º 5
0
        private void SemanticCheck(ReturnStatement ast, CompilerContext context)
        {
            WhileType valType = expressionTyper.TypeExpression(ast.Value, context);

            if (valType != WhileType.INT)
            {
                throw new TypingException($"bad return type : {valType} expecting INT");
            }
        }
Ejemplo n.º 6
0
        public WhileType TypeExpression(BinaryOperation operation, CompilerContext context)
        {
            operation.CompilerScope = context.CurrentScope;
            WhileType left = TypeExpression(operation.Left, context);

            operation.Left.Whiletype = left;
            WhileType right = TypeExpression(operation.Right, context);

            operation.Right.Whiletype = right;
            WhileType resultType = signatures.CheckBinaryOperationTyping(operation.Operator, left, right);

            return(resultType);
        }
Ejemplo n.º 7
0
        public WhileType TypeExpression(Neg neg, CompilerContext context)
        {
            WhileType positiveVal = TypeExpression(neg.Value, context);

            neg.CompilerScope = context.CurrentScope;
            if (positiveVal != WhileType.INT)
            {
                throw new SignatureException($"invalid operation type({positiveVal}) : {neg.Dump("")}");
            }
            else
            {
                return(WhileType.INT);
            }
        }
Ejemplo n.º 8
0
Archivo: Scope.cs Proyecto: dotted/csly
        public WhileType GetVariableType(string name)
        {
            WhileType varType = WhileType.NONE;

            if (variables.ContainsKey(name))
            {
                varType = variables[name];
            }
            else if (ParentScope != null)
            {
                varType = ParentScope.GetVariableType(name);
            }
            return(varType);
        }
Ejemplo n.º 9
0
        private void SemanticCheck(WhileStatement ast, CompilerContext context)
        {
            WhileType cond = expressionTyper.TypeExpression(ast.Condition, context);

            if (cond != WhileType.BOOL)
            {
                throw new SignatureException($"invalid condition type {ast.Condition.Dump("")} at {ast.Position.ToString()}");
            }
            ast.CompilerScope = context.CurrentScope;

            context.OpenNewScope();
            SemanticCheck(ast.BlockStmt, context);
            context.CloseScope();
        }
Ejemplo n.º 10
0
        private void SemanticCheck(AssignStatement ast, CompilerContext context)
        {
            WhileType valType   = expressionTyper.TypeExpression(ast.Value, context);
            bool      varExists = context.VariableExists(ast.VariableName);

            //ast.CompilerScope = context.CurrentScope;
            if (varExists)
            {
                WhileType varType = context.GetVariableType(ast.VariableName);
                if (varType != valType)
                {
                    throw new TypingException($"bad assignment : {valType} expecting {varType}");
                }
            }
            else
            {
                bool creation = context.SetVariableType(ast.VariableName, valType);
                ast.IsVariableCreation = creation;
                ast.CompilerScope      = context.CurrentScope;
            }
        }
Ejemplo n.º 11
0
 public WhileType TypeExpression(Expression expr, CompilerContext context)
 {
     if (expr is BoolConstant b)
     {
         return(TypeExpression(b, context));
     }
     if (expr is IntegerConstant i)
     {
         return(TypeExpression(i, context));
     }
     if (expr is StringConstant s)
     {
         return(TypeExpression(s, context));
     }
     if (expr is BinaryOperation binary)
     {
         return(TypeExpression(binary, context));
     }
     if (expr is Neg neg)
     {
         return(TypeExpression(neg, context));
     }
     if (expr is Not not)
     {
         return(TypeExpression(not, context));
     }
     if (expr is Variable variable)
     {
         WhileType varType = context.GetVariableType(variable.Name);
         if (varType == WhileType.NONE)
         {
             throw new TypingException($" variable {variable.Name} {variable.Position} is not intialized");
         }
         variable.CompilerScope = context.CurrentScope;
         return(varType);
     }
     throw new SignatureException($"unknow expression type ({expr.GetType().Name})");
 }
Ejemplo n.º 12
0
        public WhileType CheckBinaryOperationTyping(BinaryOperator oper, WhileType left, WhileType right)
        {
            WhileType result;

            if (binaryOperationSignatures.ContainsKey(oper))
            {
                List <Signature> signatures = binaryOperationSignatures[oper];
                Signature        res        = signatures.Find((Signature sig) => sig.Match(left, right));
                if (res != null)
                {
                    result = res.Result;
                }
                else
                {
                    throw new SignatureException($"invalid operation {left} {oper} {right}");
                }
            }
            else
            {
                result = left;
            }
            return(result);
        }
Ejemplo n.º 13
0
 public bool SetVariableType(string name, WhileType variableType)
 {
     return(CurrentScope.SetVariableType(name, variableType));
 }
Ejemplo n.º 14
0
 public Signature(WhileType left, WhileType right, WhileType result)
 {
     Left   = left;
     Right  = right;
     Result = result;
 }
Ejemplo n.º 15
0
 public bool Match(WhileType l, WhileType r)
 {
     return((Left == WhileType.ANY || l == Left) &&
            (Right == WhileType.ANY || r == Right));
 }
Ejemplo n.º 16
0
 private void SemanticCheck(PrintStatement ast, CompilerContext context)
 {
     WhileType val = expressionTyper.TypeExpression(ast.Value, context);
 }
Ejemplo n.º 17
0
 public TypedValue(string val)
 {
     ValueType = WhileType.STRING;
     value     = val;
 }
Ejemplo n.º 18
0
        public TypedValue Evaluate(BinaryOperation operation, InterpreterContext context)
        {
            TypedValue left       = Evaluate(operation.Left, context);
            TypedValue right      = Evaluate(operation.Right, context);
            WhileType  resultType = CheckBinaryOperationTyping(operation.Operator, left.ValueType, right.ValueType);
            object     value      = null;

            switch (operation.Operator)
            {
            case BinaryOperator.ADD:
            {
                value = left.IntValue + right.IntValue;
                break;
            }

            case BinaryOperator.SUB:
            {
                value = left.IntValue - right.IntValue;
                break;
            }

            case BinaryOperator.MULTIPLY:
            {
                value = left.IntValue * right.IntValue;
                break;
            }

            case BinaryOperator.DIVIDE:
            {
                value = left.IntValue / right.IntValue;
                break;
            }

            case BinaryOperator.AND:
            {
                value = left.BoolValue && right.BoolValue;
                break;
            }

            case BinaryOperator.OR:
            {
                value = left.BoolValue || right.BoolValue;
                break;
            }

            case BinaryOperator.GREATER:
            {
                switch (left.ValueType)
                {
                case WhileType.BOOL:
                {
                    value = left.BoolValue == true && right.BoolValue == false;
                    break;
                }

                case WhileType.INT:
                {
                    value = left.IntValue > right.IntValue;
                    break;
                }

                case WhileType.STRING:
                {
                    value = left.StringValue.CompareTo(right.StringValue) == 1;
                    break;
                }

                default:
                {
                    value = false;
                    break;
                }
                }
                break;
            }

            case BinaryOperator.LESSER:
            {
                switch (left.ValueType)
                {
                case WhileType.BOOL:
                {
                    value = left.BoolValue == false && right.BoolValue == true;
                    break;
                }

                case WhileType.INT:
                {
                    value = left.IntValue < right.IntValue;
                    break;
                }

                case WhileType.STRING:
                {
                    value = left.StringValue.CompareTo(right.StringValue) == -1;
                    break;
                }

                default:
                {
                    value = false;
                    break;
                }
                }
                break;
            }

            case BinaryOperator.EQUALS:
            {
                switch (left.ValueType)
                {
                case WhileType.BOOL:
                {
                    value = left.BoolValue == right.BoolValue;
                    break;
                }

                case WhileType.INT:
                {
                    value = left.IntValue == right.IntValue;
                    break;
                }

                case WhileType.STRING:
                {
                    value = left.StringValue == right.StringValue;
                    break;
                }

                default:
                {
                    value = false;
                    break;
                }
                }
                break;
            }

            case BinaryOperator.DIFFERENT:
            {
                switch (left.ValueType)
                {
                case WhileType.BOOL:
                {
                    value = left.BoolValue != right.BoolValue;
                    break;
                }

                case WhileType.INT:
                {
                    value = left.IntValue != right.IntValue;
                    break;
                }

                case WhileType.STRING:
                {
                    value = left.StringValue != right.StringValue;
                    break;
                }

                default:
                {
                    value = false;
                    break;
                }
                }
                break;
            }

            case BinaryOperator.CONCAT:
            {
                value = left.StringValue + right.StringValue;
                break;
            }

            default:
            {
                value = null;
                break;
            }
            }
            return(new TypedValue(resultType, value));
        }
Ejemplo n.º 19
0
 public TypedValue(int val)
 {
     ValueType = WhileType.INT;
     value     = val;
 }
Ejemplo n.º 20
0
 public TypedValue(bool val)
 {
     ValueType = WhileType.BOOL;
     value     = val;
 }
Ejemplo n.º 21
0
 public TypedValue(WhileType valType, object val)
 {
     ValueType = valType;
     Value     = val;
 }
Ejemplo n.º 22
0
 public WhileStat(ExpNode exp, Node stat, WhileType whileType)
 {
     Exp       = exp;
     Stat      = stat;
     WhileType = whileType;
 }