Exemplo n.º 1
0
        private object CodeReadSymbol(Variable variable)
        {
            object      resGetValue;
            IdentObject ident = new IdentObject(variable.Ident);

            if (symbolTable.ContainsKey(ident.RootObj))
            {
                if (string.IsNullOrEmpty(ident.Member))
                {
                    return(symbolTable[ident.RootObj]);
                }
                else
                {
                    try
                    {
                        GetValue(symbolTable[ident.RootObj], ident, out resGetValue);
                        return(resGetValue);
                    }
                    catch (Exception ex)
                    {
                        throw new System.Exception(" error in read code  '" + ident.Member + " :" + ex.Message);
                    }
                }
            }
            else
            if (GetStaticValue(ident, out resGetValue))
            {
                return(resGetValue);
            }
            else
            {
                throw new System.Exception(" undeclared variable '" + ident.RootObj);
            }
        }
Exemplo n.º 2
0
        private void GetObjectMethod(object objRoot, IdentObject ident, out object objRet, out MethodInfo methodInfo, Type[] types, int i = 0)
        {
            Type         t;
            PropertyInfo pi;
            string       literalObjChild;
            object       objChild;

            t = objRoot.GetType();

            if (i < ident.ChainObjs.Count)
            {
                literalObjChild = ident.ChainObjs[i];
                pi       = t.GetProperty(literalObjChild);
                objChild = pi.GetValue(objRoot, null);
                i++;
                GetObjectMethod(objChild, ident, out objRet, out methodInfo, types, i);
            }
            else
            {
                objRet = objRoot;
                if (types == null)
                {
                    methodInfo = t.GetMethod(ident.Member, Type.EmptyTypes);
                }
                else
                {
                    methodInfo = t.GetMethod(ident.Member, types);
                }
            }

            return;
        }
Exemplo n.º 3
0
        private void CodeStoreSymbol(string identString, object varValue)
        {
            IdentObject ident = new IdentObject(identString);

            if (this.symbolTable.ContainsKey(ident.RootObj))
            {
                if (string.IsNullOrEmpty(ident.Member))
                {
                    symbolTable[ident.RootObj] = varValue;
                }
                else
                {
                    try
                    {
                        SetValue(symbolTable[ident.RootObj], ident, varValue);
                    }
                    catch (Exception ex)
                    {
                        throw new System.Exception(" error in assign code  '" + ident.Member + " :" + ex.Message);
                    }
                }
            }
            else
            if (!SetStaticValue(ident, varValue))
            {
                throw new System.Exception(" undeclared variable '" + ident.RootObj);
            }
        }
Exemplo n.º 4
0
        private void GetValue(object varObj, IdentObject ident, out object retValue, int i = 0)
        {
            Type         t;
            PropertyInfo pi;
            string       literalObjChild;
            object       objChild;

            t = varObj.GetType();

            if (i < ident.ChainObjs.Count)
            {
                literalObjChild = ident.ChainObjs[i];
                pi       = t.GetProperty(literalObjChild);
                objChild = pi.GetValue(varObj, null);
                i++;
                GetValue(objChild, ident, out retValue, i);
            }
            else
            {
                pi       = t.GetProperty(ident.Member);
                retValue = pi.GetValue(varObj, null);
            }

            return;
        }
Exemplo n.º 5
0
        private bool GetStaticValue(IdentObject ident, out object retValue)
        {
            Type t;

            retValue = null;
            bool         found = false;
            PropertyInfo pi;
            FieldInfo    fi;

            string idObj;
            string idMember;

            if (ident.PathObj == ident.RootObj)
            {
                idObj    = "KntScript.Library";
                idMember = ident.RootObj;
            }
            else
            {
                idObj    = ident.PathObj;
                idMember = ident.Member;
            }

            if (TryFindType(idObj, out t))
            {
                fi = t.GetField(idMember, BindingFlags.Static | BindingFlags.Public);
                pi = t.GetProperty(idMember);

                if (fi != null)
                {
                    retValue = fi.GetValue(null);
                }
                else if (pi != null)
                {
                    retValue = pi.GetValue(null, null);
                }

                if (retValue != null)
                {
                    found = true;
                }
            }

            return(found);
        }
Exemplo n.º 6
0
        private void CodeDeclareSymbol(DeclareVar declare)
        {
            IdentObject ident = new IdentObject(declare.Ident);

            if (!string.IsNullOrEmpty(ident.Member))
            {
                throw new System.Exception(" variable declaration '" + declare.Ident + "' incorrect ");
            }

            if (!symbolTable.ContainsKey(ident.RootObj))
            {
                symbolTable.Add(declare.Ident, GenExpr(declare.Expr));
            }
            else
            {
                throw new System.Exception(" variable '" + ident.RootObj + "' already declared");
            }
        }
Exemplo n.º 7
0
        private void GetMethodStaticClass(IdentObject ident, out MethodInfo methodInfo, Type[] types)
        {
            Type t;

            methodInfo = null;

            if (TryFindType(ident.PathObj, out t))
            {
                if (types == null)
                {
                    methodInfo = t.GetMethod(ident.Member, Type.EmptyTypes);
                }
                else
                {
                    methodInfo = t.GetMethod(ident.Member, types);
                }
            }
        }
Exemplo n.º 8
0
        private object CodeExecuteFunction(FunctionExpr function)
        {
            CodeSpecialStoreObject("_KNTERRORCODE", 0);
            CodeSpecialStoreObject("_KNTERRORDESCRIPTION", "");

            try
            {
                Type       t;
                object     obj;
                object     objRoot;
                string     funName;
                MethodInfo mi;

                IdentObject ident = new IdentObject(function.FunctionName);

                // Params
                object[] param;
                Type[]   types;
                if (function.Args.Count > 0)
                {
                    param = new object[function.Args.Count];
                    types = new Type[function.Args.Count];
                    for (int i = 0; i < function.Args.Count; i++)
                    {
                        param[i] = GenExpr(function.Args[i]);
                        types[i] = param[i].GetType();
                    }
                }
                else
                {
                    param = null;
                    types = null;
                }

                // Get method
                if (string.IsNullOrEmpty(ident.Member))
                {
                    t       = DefaultFunctionLibraryType;
                    obj     = DefaultFunctionLibrary;
                    funName = ident.RootObj;
                    if (types == null)
                    {
                        mi = t.GetMethod(funName, Type.EmptyTypes);
                    }
                    else
                    {
                        mi = t.GetMethod(funName, types);
                    }
                }
                else
                {
                    if (symbolTable.ContainsKey(ident.RootObj))
                    {
                        objRoot = symbolTable[ident.RootObj];
                        GetObjectMethod(objRoot, ident, out obj, out mi, types);
                    }
                    // method in static class
                    else
                    {
                        obj = null;
                        GetMethodStaticClass(ident, out mi, types);
                    }
                }

                // Execute
                return(mi.Invoke(obj, param));
            }
            catch (Exception ex)
            {
                if (!((bool)CodeReadSymbol(new Variable {
                    Ident = "_KNTERRORTRAP"
                })))
                {
                    throw;
                }
                else
                {
                    CodeSpecialStoreObject("_KNTERRORCODE", 10);
                    CodeSpecialStoreObject("_KNTERRORDESCRIPTION", ex.Message);
                    return(null);
                }
            }
        }
Exemplo n.º 9
0
 private Type TypeOfExpr(Expr expr)
 {
     if (expr is StringVal)
     {
         return(typeof(string));
     }
     else if (expr is IntVal)
     {
         return(typeof(int));
     }
     else if (expr is FloatVal)
     {
         return(typeof(float));
     }
     else if (expr is DoubleVal)
     {
         return(typeof(double));
     }
     else if (expr is DecimalVal)
     {
         return(typeof(decimal));
     }
     else if (expr is DateTimeVal)
     {
         return(typeof(DateTime));
     }
     else if (expr is BoolVal)
     {
         return(typeof(bool));
     }
     else if (expr is Variable)
     {
         Variable    var = (Variable)expr;
         IdentObject io  = new IdentObject(var.Ident);
         if (this.symbolTable.ContainsKey(io.RootObj))
         {
             return(symbolTable[io.RootObj].GetType());
         }
         else
         {
             throw new System.Exception("undeclared variable '" + var.Ident + "'");
         }
     }
     else if (expr is FunctionExpr)
     {
         return(typeof(FunctionExpr));
     }
     else if (expr is NewObjectExpr)
     {
         return(typeof(NewObjectExpr));
     }
     else if (expr is BinaryExpr)
     {
         return(typeof(BinaryExpr));
     }
     else if (expr is UnaryExpr)
     {
         return(typeof(UnaryExpr));
     }
     else
     {
         throw new System.Exception("don't know how to calculate the type of " + expr.GetType().Name);
     }
 }