Ejemplo n.º 1
0
 public Statement Visit(IdentifierStatement identifierStatement)
 {
     if (identifierStatement.list == null)
     {
         identifierStatement.id = State.UnShadow(identifierStatement.id);
         //Console.WriteLine($"**********IDENTIFIER STATMENT:{identifierStatement.id}**********");
     }
     else
     {
         foreach (var e in ((ListStatement)identifierStatement.list).exprs)
         {
             e.Accept(this);
         }
     }
     return(null);
 }
Ejemplo n.º 2
0
 public Type Visit(IdentifierStatement identifierStatement, FunctionGeneratorEnvironment arg)
 {
     if (identifierStatement.list == null)
     {
         var type   = arg.GetIdentifierType(identifierStatement.id);
         var offset = arg.GetIdentifierOffset(identifierStatement.id);
         if (type.GetType() == typeof(IntType))
         {
             Program.Emit(T42Instruction.RVALINT(offset));
             return(new IntType());
         }
         else
         {
             Program.Emit(T42Instruction.RVALBOOL(offset));
             return(new BoolType());
         }
     }
     else
     {
         // CALL STATEMENT
         return(new BoolType());
     }
 }
Ejemplo n.º 3
0
        public IValue Visit(IdentifierStatement identifierStatement)
        {
            //Console.WriteLine("IDENTIFIER STATEMENT");
            if (identifierStatement.list == null)
            {
                if (!Environment.ContainsVarInCurrCall(identifierStatement.id))
                {
                    var err = new ErrorMessage();
                    throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.ID, identifierStatement));
                }
                //identifierStatement.id.Accept(this);
                return(Environment.GetVarValue(identifierStatement.id));
            }
            else // if it is a function call
            {
                if (identifierStatement.id == "print")
                {
                    //Print
                    foreach (var e in ((ListStatement)identifierStatement.list).exprs)
                    {
                        Console.Write(((IValue)e.Accept(this)).ToString() + " ");
                    }
                    Console.Write("\n");
                    return(null);
                }
                else
                {
                    if (!Environment.FunctionDict.ContainsKey(identifierStatement.id))
                    {
                        var err = new ErrorMessage();
                        throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.CALL_1, identifierStatement));
                    }
                    else
                    {
                        var info = Environment.FunctionDict[identifierStatement.id];
                        if (info.Item2 == true) // If void function
                        {
                            var function = (VoidDeclaration)Environment.FunctionDict[identifierStatement.id].Item1;
                            var numOfArg = (function.flist == null) ? 0 : ((FormalList)function.flist).list.Count;
                            if (((ListStatement)identifierStatement.list).exprs.Count != numOfArg)
                            {
                                var err = new ErrorMessage();
                                throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.CALL_2, identifierStatement));
                            }
                            else
                            {
                                var parameters = new Dictionary <string, IValue>();
                                for (int i = 0; i < ((ListStatement)identifierStatement.list).exprs.Count; i++)
                                {
                                    parameters.Add(((FormalList)function.flist).list[i].id, ((ListStatement)identifierStatement.list).exprs[i].Accept(this));
                                }
                                Environment.CallFunction(parameters);
                                function.Accept(this);
                                //ovde treba pop value
                                Environment.PopFunction();
                                var result = Environment.ReturnValue;
                                Environment.ReturnValue = null;
                                return(result);
                            }
                        }
                        else // if type function
                        {
                            var function = (TypeDeclaration)Environment.FunctionDict[identifierStatement.id].Item1;
                            var numOfArg = (function.flist == null) ? 0 : ((FormalList)function.flist).list.Count;
                            if (((ListStatement)identifierStatement.list).exprs.Count != numOfArg)
                            {
                                var err = new ErrorMessage();
                                throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.CALL_2, identifierStatement));
                            }
                            else
                            {
                                var parameters = new Dictionary <string, IValue>();
                                for (int i = 0; i < ((ListStatement)identifierStatement.list).exprs.Count; i++)
                                {
                                    parameters.Add(((FormalList)function.flist).list[i].id, ((ListStatement)identifierStatement.list).exprs[i].Accept(this));
                                }

                                Environment.CallFunction(parameters);

                                function.Accept(this);
                                //ovde treba pop value
                                Environment.PopFunction();
                                var result = Environment.ReturnValue;
                                Environment.ReturnValue = null;
                                var funcType = (function.type.GetType().Name == "IntType") ? ValueType.Number : ValueType.Bool;
                                if (result == null || result.Type != funcType)
                                {
                                    var err = new ErrorMessage();
                                    throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.CALL_3, identifierStatement));
                                }
                                return(result);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public Void Visit(IdentifierStatement identifierStatement, SortedSet <string> free)
 {
     free.Add(identifierStatement.id);
     return(null);
 }
Ejemplo n.º 5
0
        public Type Visit(IdentifierStatement identifierStatement)
        {
            if (identifierStatement.list == null)
            {
                if (!Environment.VariableType.ContainsKey(identifierStatement.id))
                {
                    var err = new TypeErrorMessage();
                    throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.ID, identifierStatement));
                }
                return(Environment.VariableType[identifierStatement.id]);
            }
            else
            {
                if (identifierStatement.id == "print")
                {
                    foreach (var e in ((ListStatement)identifierStatement.list).exprs)
                    {
                        var type      = e.Accept(this);
                        var type_text = type.GetType().Name;
                        if (type_text != "BoolType" && type_text != "IntType")
                        {
                            var err = new TypeErrorMessage();
                            throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.CALL_3, identifierStatement));
                        }
                    }
                    return(null);
                }
                else
                {
                    if (!Environment.FuncDeclaration.ContainsKey(identifierStatement.id))
                    {
                        var err = new TypeErrorMessage();
                        throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.CALL_1, identifierStatement));
                    }

                    var function = Environment.FuncDeclaration[identifierStatement.id];

                    if (function.GetType().Name == "VoidDeclaration")
                    {
                        if (((FormalList)((VoidDeclaration)function).flist).list.Count != ((ListStatement)identifierStatement.list).exprs.Count)
                        {
                            var err = new TypeErrorMessage();
                            throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.CALL_2, identifierStatement));
                        }

                        var numOfArgs = ((ListStatement)identifierStatement.list).exprs.Count;

                        for (int i = 0; i < numOfArgs; i++)
                        {
                            var arg = ((FormalList)((VoidDeclaration)function).flist).list[i];
                            var e   = ((ListStatement)identifierStatement.list).exprs[i];
                            if (e.Accept(this).GetType() != arg.type.GetType())
                            {
                                var err = new TypeErrorMessage();
                                throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.CALL_3, identifierStatement));
                            }
                        }

                        return(null);
                    }
                    else
                    {
                        if (((FormalList)((TypeDeclaration)function).flist).list.Count != ((ListStatement)identifierStatement.list).exprs.Count)
                        {
                            var err = new TypeErrorMessage();
                            throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.CALL_2, identifierStatement));
                        }

                        var numOfArgs = ((ListStatement)identifierStatement.list).exprs.Count;

                        for (int i = 0; i < numOfArgs; i++)
                        {
                            var arg = ((FormalList)((TypeDeclaration)function).flist).list[i];
                            var e   = ((ListStatement)identifierStatement.list).exprs[i];
                            if (e.Accept(this).GetType() != arg.type.GetType())
                            {
                                var err = new TypeErrorMessage();
                                throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.CALL_3, identifierStatement));
                            }
                        }

                        return((Type)((TypeDeclaration)function).type);
                    }
                }
            }
        }