예제 #1
0
파일: PrefixCheck.cs 프로젝트: Blecki/misp
        internal static bool CheckPrefix(ScriptObject node)
        {
            if (allowed == null)
            {
                var allTypes = new string[] { "node", "stringexpression", "memberaccess", "string", "number", "token" };
                allowed = new Dictionary<string, List<string>>();
                foreach (var type in allTypes) allowed.Add(type, new List<string>());

                allowed["node"].Add("$");
                allowed["node"].Add("^");
                allowed["node"].Add("*");
                allowed["node"].Add("#");
                allowed["node"].Add(":");

                allowed["token"].Add("$");
                allowed["token"].Add("#");
                allowed["token"].Add(":");

                allowed["string"].Add("*");
                allowed["string"].Add(":");

                allowed["stringexpression"].Add("*");
                allowed["stringexpression"].Add(":");
            }

            if (String.IsNullOrEmpty(node.gsp("@prefix"))) return true;
            if (allowed.ContainsKey(node.gsp("@type"))) return allowed[node.gsp("@type")].Contains(node.gsp("@prefix"));
            return false;
        }
예제 #2
0
파일: Console.cs 프로젝트: Blecki/misp
 private void EmitFunctionListItem(ScriptObject item)
 {
     Write(item.gsp("@name"));
     if (item.gsp("@name").Length < 16)
     {
         Write(new String(' ', 16 - item.gsp("@name").Length));
     }
     Write(item.gsp("@help") + "\n");
 }
예제 #3
0
 public void EmitFunction(ScriptObject func, string type, System.IO.TextWriter to, bool recall = false, int indent = -1)
 {
     to.Write((recall ?  "" : "(") + type + " " + func.gsp("@name") + " ^(");
     var arguments = func["@arguments"] as ScriptList;
     foreach (var arg in arguments)
     {
         if (indent >= 0) to.Write("\n   ");
         emitArgumentSpec(arg as ScriptObject, to);
     }
     if (indent >= 0) to.Write("\n");
     to.Write(") ");
     Engine.SerializeCode(to, func["@function-body"] as ScriptObject, indent);
     if (!recall)
         to.Write((indent >= 0 ? "\n" : "") + " \"" + func.gsp("@help") + "\")\n");
 }
예제 #4
0
파일: PrefixCheck.cs 프로젝트: Blecki/misp
        internal static bool CheckPrefix(ScriptObject node)
        {
            if (allowed == null)
            {
                var allTypes = new string[] { "node", "stringexpression", "memberaccess", "string", "number", "token" };
                allowed = new Dictionary <string, List <string> >();
                foreach (var type in allTypes)
                {
                    allowed.Add(type, new List <string>());
                }

                allowed["node"].Add("$");
                allowed["node"].Add("^");
                allowed["node"].Add("*");
                allowed["node"].Add("#");
                allowed["node"].Add(":");

                allowed["token"].Add("$");
                allowed["token"].Add("#");
                allowed["token"].Add(":");

                allowed["string"].Add("*");
                allowed["string"].Add(":");

                allowed["stringexpression"].Add("*");
                allowed["stringexpression"].Add(":");
            }

            if (String.IsNullOrEmpty(node.gsp("@prefix")))
            {
                return(true);
            }
            if (allowed.ContainsKey(node.gsp("@type")))
            {
                return(allowed[node.gsp("@type")].Contains(node.gsp("@prefix")));
            }
            return(false);
        }
예제 #5
0
        public void EmitFunction(ScriptObject func, string type, System.IO.TextWriter to, bool recall = false, int indent = -1)
        {
            to.Write((recall ?  "" : "(") + type + " " + func.gsp("@name") + " ^(");
            var arguments = func["@arguments"] as ScriptList;

            foreach (var arg in arguments)
            {
                if (indent >= 0)
                {
                    to.Write("\n   ");
                }
                emitArgumentSpec(arg as ScriptObject, to);
            }
            if (indent >= 0)
            {
                to.Write("\n");
            }
            to.Write(") ");
            Engine.SerializeCode(to, func["@function-body"] as ScriptObject, indent);
            if (!recall)
            {
                to.Write((indent >= 0 ? "\n" : "") + " \"" + func.gsp("@help") + "\")\n");
            }
        }
예제 #6
0
파일: Console.cs 프로젝트: Blecki/misp
 private void EmitFunctionListItem(ScriptObject item)
 {
     Write(item.gsp("@name"));
     if (item.gsp("@name").Length < 16)
         Write(new String(' ', 16 - item.gsp("@name").Length));
     Write(item.gsp("@help") + "\n");
 }
예제 #7
0
파일: Parser.cs 프로젝트: hvp/Gemgine
 private static bool isType(ScriptObject obj, String type)
 {
     return obj.gsp("@type") == type;
 }
예제 #8
0
 private static bool isType(ScriptObject obj, String type)
 {
     return(obj.gsp("@type") == type);
 }
예제 #9
0
파일: Function.cs 프로젝트: hvp/Gemgine
        public static Object Invoke(ScriptObject func, Engine engine, Context context, ScriptList arguments)
        {
            var name = func.gsp("@name");
            var argumentInfo = func["@arguments"] as ScriptList;

            if (context.trace != null)
            {
                context.trace(new String('.', context.traceDepth) + "Entering " + name +"\n");
                context.traceDepth += 1;
            }

            var newArguments = new ScriptList();
            //Check argument types
            if (argumentInfo.Count == 0 && arguments.Count != 0)
            {
                context.RaiseNewError("Function expects no arguments.", context.currentNode);
                return null;
            }

                int argumentIndex = 0;
                for (int i = 0; i < argumentInfo.Count; ++i)
                {
                    var info = argumentInfo[i] as ScriptObject;
                    if (info == null)
                        throw new ScriptError("Invalid argument descriptor on function object", context.currentNode);
                    if (info["@repeat"] != null)
                    {
                        var list = new ScriptList();
                        while (argumentIndex < arguments.Count) //Handy side effect: If no argument is passed for an optional repeat
                        {                                       //argument, it will get an empty list.
                            list.Add(MutateArgument(arguments[argumentIndex], info, engine, context));
                            //list.Add((info["@type"] as Type).ProcessArgument(context, arguments[argumentIndex]));
                            if (context.evaluationState != EvaluationState.Normal) return null;
                            ++argumentIndex;
                        }
                        newArguments.Add(list);
                    }
                    else
                    {
                        if (argumentIndex < arguments.Count)
                        {
                            newArguments.Add(MutateArgument(arguments[argumentIndex], info, engine, context));
                            //newArguments.Add((info["@type"] as Type).ProcessArgument(context, arguments[argumentIndex]));
                            if (context.evaluationState == EvaluationState.UnwindingError) return null;
                        }
                        else if (info["@optional"] != null)
                            newArguments.Add(MutateArgument(null, info, engine, context));
                        else
                        {
                            context.RaiseNewError("Not enough arguments to " + name, context.currentNode);
                            return null;
                        }
                        ++argumentIndex;
                    }
                }
                if (argumentIndex < arguments.Count)
                {
                    context.RaiseNewError("Too many arguments to " + name, context.currentNode);
                    return null;
                }

            Object r = null;

            if (func["@function-body"] is ScriptObject)
            {
                var declarationScope = func["@declaration-scope"];
                if (declarationScope is GenericScriptObject)
                {
                    var newScope = new Scope();
                    foreach (var valueName in (declarationScope as GenericScriptObject).properties)
                        newScope.PushVariable(valueName.Key, valueName.Value);
                    func["@declaration-scope"] = newScope;
                }

                context.PushScope(func["@declaration-scope"] as Scope);

                for (int i = 0; i < argumentInfo.Count; ++i)
                    context.Scope.PushVariable((argumentInfo[i] as ScriptObject).gsp("@name"), newArguments[i]);

                r = engine.Evaluate(context, func["@function-body"], true);

                for (int i = 0; i < argumentInfo.Count; ++i)
                    context.Scope.PopVariable((argumentInfo[i] as ScriptObject).gsp("@name"));

                context.PopScope();
            }
            else
            {
                try
                {
                    r = (func["@function-body"] as Func<Context, ScriptList, Object>)(context, newArguments);
                }
                catch (Exception e)
                {
                    context.RaiseNewError("System Exception: " + e.Message, context.currentNode);
                    return null;
                }
            }

            if (context.trace != null)
            {
                context.traceDepth -= 1;
                context.trace(new String('.', context.traceDepth) + "Leaving " + name +
                    (context.evaluationState == EvaluationState.UnwindingError ?
                    (" -Error: " + context.errorObject.GetLocalProperty("message").ToString()) :
                    "") +
                    (context.evaluationState == EvaluationState.UnwindingBreak ? " -Breaking" : "") +
                    "\n");
            }

            return r;
        }
예제 #10
0
 public static void SerializeCode(System.IO.TextWriter to, ScriptObject root, int indent = -1)
 {
     if (root.gsp("@type") == "string")
     {
         to.Write(root.gsp("@prefix") + "\"" + root.gsp("@token") + "\"");
     }
     else if (root.gsp("@type") == "stringexpression")
     {
         to.Write(root.gsp("@prefix"));
         to.Write("\"");
         foreach (var item in root._children)
         {
             if ((item as ScriptObject).gsp("@type") == "string")
             {
                 to.Write((item as ScriptObject).gsp("@token"));
             }
             else
             {
                 SerializeCode(to, item as ScriptObject);
             }
         }
         to.Write("\"");
     }
     else if (root.gsp("@type") == "node")
     {
         to.Write(root.gsp("@prefix") + "(");
         foreach (var item in root._children)
         {
             //if (indent >= 0)
             //{
             //    to.Write("\n" + new String(' ', indent * 3));
             //    SerializeCode(to, item as ScriptObject, indent + 1);
             //}
             //else
             SerializeCode(to, item as ScriptObject);
             to.Write(" ");
         }
         to.Write(")");
     }
     else if (root.gsp("@type") == "memberaccess")
     {
         SerializeCode(to, root._child(0) as ScriptObject);
         to.Write(root.gsp("@token"));
         SerializeCode(to, root._child(1) as ScriptObject);
     }
     else if (root.gsp("@type") == "dictionaryentry")
     {
         to.Write(root.gsp("@prefix") + "[");
         foreach (var item in root._children)
         {
             SerializeCode(to, item as ScriptObject);
             to.Write(" ");
         }
         to.Write("]");
     }
     else
     {
         to.Write(root.gsp("@token"));
     }
 }
예제 #11
0
 public static void SerializeCode(System.IO.TextWriter to, ScriptObject root, int indent = -1)
 {
     if (root.gsp("@type") == "string")
         to.Write(root.gsp("@prefix") + "\"" + root.gsp("@token") + "\"");
     else if (root.gsp("@type") == "stringexpression")
     {
         to.Write(root.gsp("@prefix"));
         to.Write("\"");
         foreach (var item in root._children)
         {
             if ((item as ScriptObject).gsp("@type") == "string")
                 to.Write((item as ScriptObject).gsp("@token"));
             else
                 SerializeCode(to, item as ScriptObject);
         }
         to.Write("\"");
     }
     else if (root.gsp("@type") == "node")
     {
         to.Write(root.gsp("@prefix") + "(");
         foreach (var item in root._children)
         {
             //if (indent >= 0)
             //{
             //    to.Write("\n" + new String(' ', indent * 3));
             //    SerializeCode(to, item as ScriptObject, indent + 1);
             //}
             //else
                 SerializeCode(to, item as ScriptObject);
             to.Write(" ");
         }
         to.Write(")");
     }
     else if (root.gsp("@type") == "memberaccess")
     {
         SerializeCode(to, root._child(0) as ScriptObject);
         to.Write(root.gsp("@token"));
         SerializeCode(to, root._child(1) as ScriptObject);
     }
     else if (root.gsp("@type") == "dictionaryentry")
     {
         to.Write(root.gsp("@prefix") + "[");
         foreach (var item in root._children)
         {
             SerializeCode(to, item as ScriptObject);
             to.Write(" ");
         }
         to.Write("]");
     }
     else
     {
         to.Write(root.gsp("@token"));
     }
 }
예제 #12
0
        public static Object Invoke(ScriptObject func, Engine engine, Context context, ScriptList arguments)
        {
            var name         = func.gsp("@name");
            var argumentInfo = func["@arguments"] as ScriptList;

            if (context.trace != null)
            {
                context.trace(new String('.', context.traceDepth) + "Entering " + name + "\n");
                context.traceDepth += 1;
            }

            var newArguments = new ScriptList();

            //Check argument types
            if (argumentInfo.Count == 0 && arguments.Count != 0)
            {
                context.RaiseNewError("Function expects no arguments.", context.currentNode);
                return(null);
            }

            int argumentIndex = 0;

            for (int i = 0; i < argumentInfo.Count; ++i)
            {
                var info = argumentInfo[i] as ScriptObject;
                if (info == null)
                {
                    throw new ScriptError("Invalid argument descriptor on function object", context.currentNode);
                }
                if (info["@repeat"] != null)
                {
                    var list = new ScriptList();
                    while (argumentIndex < arguments.Count)     //Handy side effect: If no argument is passed for an optional repeat
                    {                                           //argument, it will get an empty list.
                        list.Add(MutateArgument(arguments[argumentIndex], info, engine, context));
                        //list.Add((info["@type"] as Type).ProcessArgument(context, arguments[argumentIndex]));
                        if (context.evaluationState != EvaluationState.Normal)
                        {
                            return(null);
                        }
                        ++argumentIndex;
                    }
                    newArguments.Add(list);
                }
                else
                {
                    if (argumentIndex < arguments.Count)
                    {
                        newArguments.Add(MutateArgument(arguments[argumentIndex], info, engine, context));
                        //newArguments.Add((info["@type"] as Type).ProcessArgument(context, arguments[argumentIndex]));
                        if (context.evaluationState == EvaluationState.UnwindingError)
                        {
                            return(null);
                        }
                    }
                    else if (info["@optional"] != null)
                    {
                        newArguments.Add(MutateArgument(null, info, engine, context));
                    }
                    else
                    {
                        context.RaiseNewError("Not enough arguments to " + name, context.currentNode);
                        return(null);
                    }
                    ++argumentIndex;
                }
            }
            if (argumentIndex < arguments.Count)
            {
                context.RaiseNewError("Too many arguments to " + name, context.currentNode);
                return(null);
            }


            Object r = null;

            if (func["@function-body"] is ScriptObject)
            {
                var declarationScope = func["@declaration-scope"];
                if (declarationScope is GenericScriptObject)
                {
                    var newScope = new Scope();
                    foreach (var valueName in (declarationScope as GenericScriptObject).properties)
                    {
                        newScope.PushVariable(valueName.Key, valueName.Value);
                    }
                    func["@declaration-scope"] = newScope;
                }

                context.PushScope(func["@declaration-scope"] as Scope);

                for (int i = 0; i < argumentInfo.Count; ++i)
                {
                    context.Scope.PushVariable((argumentInfo[i] as ScriptObject).gsp("@name"), newArguments[i]);
                }

                r = engine.Evaluate(context, func["@function-body"], true);

                for (int i = 0; i < argumentInfo.Count; ++i)
                {
                    context.Scope.PopVariable((argumentInfo[i] as ScriptObject).gsp("@name"));
                }

                context.PopScope();
            }
            else
            {
                try
                {
                    r = (func["@function-body"] as Func <Context, ScriptList, Object>)(context, newArguments);
                }
                catch (Exception e)
                {
                    context.RaiseNewError("System Exception: " + e.Message, context.currentNode);
                    return(null);
                }
            }


            if (context.trace != null)
            {
                context.traceDepth -= 1;
                context.trace(new String('.', context.traceDepth) + "Leaving " + name +
                              (context.evaluationState == EvaluationState.UnwindingError ?
                               (" -Error: " + context.errorObject.GetLocalProperty("message").ToString()) :
                               "") +
                              (context.evaluationState == EvaluationState.UnwindingBreak ? " -Breaking" : "") +
                              "\n");
            }

            return(r);
        }