public FUNCTION_INFO(string name, TYPE_INFO ret_value,
                      ArrayList formals)
 {
     _ret_value = ret_value;
     _typeinfo  = formals;
     _name      = name;
 }
        /// <summary>
        ///     Recursively check the type and bubble up the type
        ///     information to the top...
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            TYPE_INFO eval_left  = ex1.TypeCheck(cont);
            TYPE_INFO eval_right = ex2.TypeCheck(cont);

            if (eval_left != eval_right)
            {
                throw new Exception("Wrong Type in expression");
            }

            if (eval_left == TYPE_INFO.TYPE_STRING &&
                (!(m_op == RELATION_OPERATOR.TOK_EQ ||
                   m_op == RELATION_OPERATOR.TOK_NEQ)))
            {
                throw new Exception("Only == amd != supported for string type ");
            }

            if (eval_left == TYPE_INFO.TYPE_BOOL &&
                (!(m_op == RELATION_OPERATOR.TOK_EQ ||
                   m_op == RELATION_OPERATOR.TOK_NEQ)))
            {
                throw new Exception("Only == amd != supported for boolean type ");
            }
            // store the operand type as well
            _optype = eval_left;
            _type   = TYPE_INFO.TYPE_BOOL;
            return(_type);
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ret_type"></param>
        /// <param name="type_infos"></param>
        /// <returns></returns>
        public bool CheckFunctionProtoType(string name, TYPE_INFO ret_type,
                                           ArrayList type_infos)
        {
            foreach (FUNCTION_INFO fpinfo in protos)
            {
                if (fpinfo._name.Equals(name))
                {
                    if (fpinfo._ret_value == ret_type)
                    {
                        if (type_infos.Count == fpinfo._typeinfo.Count)
                        {
                            int i = 0;
                            for (i = 0; i < type_infos.Count; ++i)
                            {
                                TYPE_INFO a = (TYPE_INFO)type_infos[i];
                                TYPE_INFO b = (TYPE_INFO)type_infos[i];

                                if (a != b)
                                {
                                    return(false);
                                }
                            }

                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Beispiel #4
0
        public override bool Compile(DNET_EXECUTABLE_GENERATION_CONTEXT cont)
        {
            //
            //  Compile the Expression
            //  The Output will be on the top of stack
            exp1.Compile(cont);
            //
            // Generate Code to Call Console.Write
            //
            System.Type typ        = Type.GetType("System.Console");
            Type[]      Parameters = new Type[1];

            TYPE_INFO tdata = exp1.get_type();

            if (tdata == TYPE_INFO.TYPE_STRING)
            {
                Parameters[0] = typeof(string);
            }
            else if (tdata == TYPE_INFO.TYPE_NUMERIC)
            {
                Parameters[0] = typeof(double);
            }
            else
            {
                Parameters[0] = typeof(bool);
            }
            cont.CodeOutput.Emit(OpCodes.Call, typ.GetMethod("Write", Parameters));
            return(true);
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ret_type"></param>
        /// <param name="type_infos"></param>
        public void AddFunctionProtoType(string name, TYPE_INFO ret_type,
                                         ArrayList type_infos)
        {
            FUNCTION_INFO info = new FUNCTION_INFO(name, ret_type, type_infos);

            protos.Add(info);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            if (m_proc != null)
            {
                _type = m_proc.TypeCheck(cont);
            }

            return(_type);
        }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="formals"></param>
        /// <param name="stats"></param>
        /// <param name="locals"></param>
        /// <param name="type"></param>

        public Procedure(string name,
                         ArrayList stats,
                         SymbolTable locals,
                         TYPE_INFO type)
        {
            m_name       = name;
            m_formals    = null;
            m_statements = stats;
            m_locals     = locals;
            _type        = type;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="formals"></param>
        /// <param name="stats"></param>
        /// <param name="locals"></param>
        /// <param name="type"></param>

        public Procedure(string name,
                         ArrayList formals,
                         ArrayList stats,
                         SymbolTable locals,
                         TYPE_INFO type)
        {
            m_name = name;
            //
            // The value is only supplied for STEP 7
            m_formals    = formals;
            m_statements = stats;
            m_locals     = locals;
            _type        = type;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="local"></param>
        /// <param name="global"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            TYPE_INFO eval_left = exp1.TypeCheck(cont);


            if (eval_left == TYPE_INFO.TYPE_NUMERIC)
            {
                _type = eval_left;
                return(_type);
            }
            else
            {
                throw new Exception("Type mismatch failure");
            }
        }
Beispiel #10
0
 public AssignmentStatement(SYMBOL_INFO var, Exp ind, Exp e)
 {
     if (var.Type == TYPE_INFO.TYPE_ARRAY)
     {
         variable = new IndexedVariable(var, ind, e);
         exp1     = e;
         tp       = TYPE_INFO.TYPE_ARRAY;
     }
     else if (var.Type == TYPE_INFO.TYPE_MAP)
     {
         variable = new HashedVariable(var, ind, e);
         exp1     = e;
         tp       = TYPE_INFO.TYPE_MAP;
     }
 }
Beispiel #11
0
        public Procedure(string name,
                         ArrayList formals,
                         ArrayList stats,
                         SymbolTable locals,
                         TYPE_INFO type,
                         Procedure clsr)
        {
            m_name = name;
            //

            m_formals    = formals;
            m_statements = stats;
            m_locals     = locals;
            _type        = type;
            clossure     = clsr;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            TYPE_INFO eval_left = ex1.TypeCheck(cont);


            if (
                eval_left == TYPE_INFO.TYPE_BOOL)
            {
                _type = TYPE_INFO.TYPE_BOOL;
                return(_type);
            }
            else
            {
                throw new Exception("Wrong Type in expression");
            }
        }
        /// <summary>
        ///     Look it up in the Symbol Table and
        ///     return the type
        /// </summary>
        /// <param name="local"></param>
        /// <param name="global"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            if (cont.TABLE == null)
            {
                return(TYPE_INFO.TYPE_ILLEGAL);
            }
            else
            {
                SYMBOL_INFO a = cont.TABLE.Get(m_name);
                if (a != null)
                {
                    _type = a.Type;
                    return(_type);
                }


                return(TYPE_INFO.TYPE_ILLEGAL);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            TYPE_INFO eval_left  = ex1.TypeCheck(cont);
            TYPE_INFO eval_right = ex2.TypeCheck(cont);

            // The Types should be Boolean...
            // Logical Operators only make sense
            // with Boolean Types

            if (eval_left == eval_right &&
                eval_left == TYPE_INFO.TYPE_BOOL)
            {
                _type = TYPE_INFO.TYPE_BOOL;
                return(_type);
            }
            else
            {
                throw new Exception("Wrong Type in expression");
            }
        }
Beispiel #15
0
        public override bool Compile(DNET_EXECUTABLE_GENERATION_CONTEXT cont)
        {
            exp1.Compile(cont);
            System.Type typ        = Type.GetType("System.Console");
            Type[]      Parameters = new Type[1];

            TYPE_INFO tdata = exp1.get_type();

            if (tdata == TYPE_INFO.TYPE_STRING)
            {
                Parameters[0] = typeof(string);
            }
            else if (tdata == TYPE_INFO.TYPE_NUMERIC)
            {
                Parameters[0] = typeof(double);
            }
            else
            {
                Parameters[0] = typeof(bool);
            }
            cont.CodeOutput.Emit(OpCodes.Call, typ.GetMethod("WriteLine", Parameters));
            return(true);
        }
        /// <summary>
        ///    Parse the Assignment Statement
        ///    <variable> = <expr>
        /// </summary>
        /// <param name="pb"></param>
        /// <returns></returns>
        public Stmt ParseAssignmentStatement(ProcedureBuilder ctx)
        {
            //
            // Retrieve the variable and look it up in
            // the symbol table ..if not found throw exception
            //
            string      variable         = base.last_str;
            SYMBOL_INFO s                = ctx.TABLE.Get(variable);
            Exp         index_expression = null;

            if (s == null)
            {
                CSyntaxErrorLog.AddLine("Variable not found  " + last_str);
                CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                throw new CParserException(-100, "Variable not found", SaveIndex());
            }


            if (s.Type == TYPE_INFO.TYPE_ARRAY || s.Type == TYPE_INFO.TYPE_MAP)
            {
                GetNext();

                if (Current_Token != TOKEN.TOK_OSUBSCRIPT)
                {
                    CSyntaxErrorLog.AddLine("[ expected ");
                    CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                    throw new CParserException(-100, "[ expected ", SaveIndex());
                }

                GetNext();
                Exp a = BExpr(ctx);
                //
                // Do the type analysis ...
                //
                TYPE_INFO tp = a.TypeCheck(ctx.Context);

                if (s.Type == TYPE_INFO.TYPE_MAP)
                {
                    if (tp != s.m_info.key)
                    {
                        CSyntaxErrorLog.AddLine("Type mismatch in key ");
                        CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                        throw new CParserException(-100, "Type mismatch in key ", SaveIndex());
                    }
                }
                else
                {
                    if (tp != s.a_info.tf)
                    {
                        CSyntaxErrorLog.AddLine("Type mismatch in index ");
                        CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                        throw new CParserException(-100, "Type mismatch in index ", SaveIndex());
                    }
                }

                index_expression = a;

                if (Current_Token != TOKEN.TOK_CSUBSCRIPT)
                {
                    throw new CParserException(-100, "] is expected", SaveIndex());
                }
            }

            //------------ The next token ought to be an assignment
            // expression....



            GetNext();

            if (Current_Token != TOKEN.TOK_ASSIGN)
            {
                CSyntaxErrorLog.AddLine("= expected");
                CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                throw new CParserException(-100, "= expected", SaveIndex());
            }

            //-------- Skip the token to start the expression
            // parsing on the RHS
            GetNext();
            Exp exp = BExpr(ctx);

            //------------ Do the type analysis ...
            if (s.Type == TYPE_INFO.TYPE_BOOL || s.Type == TYPE_INFO.TYPE_NUMERIC ||
                s.Type == TYPE_INFO.TYPE_STRING)
            {
                if (exp.TypeCheck(ctx.Context) != s.Type)
                {
                    throw new CParserException(-1, "Type mismatch in assignment", SaveIndex());
                }
            }
            else
            {
                exp.TypeCheck(ctx.Context);
            }

            // -------------- End of statement ( ; ) is expected
            if (Current_Token != TOKEN.TOK_SEMI)
            {
                CSyntaxErrorLog.AddLine("; expected");
                CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                throw new CParserException(-100, " ; expected", SaveIndex());
            }
            // return an instance of AssignmentStatement node..
            //   s => Symbol info associated with variable
            //   exp => to evaluated and assigned to symbol_info
            if (index_expression == null)
            {
                return(new AssignmentStatement(s, exp));
            }
            else
            {
                return(new AssignmentStatement(s, index_expression, exp));
            }
        }
Beispiel #17
0
 public AssignmentStatement(Variable var, Exp e)
 {
     variable = var;
     exp1     = e;
     tp       = TYPE_INFO.TYPE_ILLEGAL;
 }