Beispiel #1
0
        public IList <IokeObject> GetMimics()
        {
            var result = new SaneList <IokeObject>();

            switch (body.mimicCount)
            {
            case 0:
                break;

            case 1:
                result.Add(body.mimic);
                break;

            default:
                for (int i = 0; i < body.mimicCount; i++)
                {
                    result.Add(body.mimics[i]);
                }
                break;
            }
            return(result);
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            string current = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string iokeHome = new FileInfo(current).Directory.Parent.FullName;
            string iokeLib = Path.Combine(iokeHome, "lib");

            Runtime r = new Runtime(new FunctionalOperatorShufflerFactory());
            r.Init();

            IokeObject context = r.Ground;
            Message mx = new Message(r, ".", null, true);
            mx.Line = 0;
            mx.Position = 0;
            IokeObject message = r.CreateMessage(mx);

            string cwd = null;

            var scripts = new SaneList<string>();
            var loadDirs = new SaneList<string>();
            bool debug = false;

            try {
                int start = 0;
                bool done = false;
                bool readStdin = false;
                bool printedSomething = false;

                for(;!done && start<args.Length;start++) {
                    string arg = args[start];
                    if(arg.Length > 0) {
                        if(arg[0] != '-') {
                            done = true;
                            break;
                        } else {
                            if(arg.Equals("--")) {
                                done = true;
                            } else if(arg.Equals("-d")) {
                                debug = true;
                                r.Debug = true;
                            } else if(arg.StartsWith("-e")) {
                                if(arg.Length == 2) {
                                    scripts.Add(args[++start]);
                                } else {
                                    scripts.Add(arg.Substring(2));
                                }
                            } else if(arg.StartsWith("-I")) {
                                if(arg.Length == 2) {
                                    loadDirs.Add(args[++start]);
                                } else {
                                    loadDirs.Add(arg.Substring(2));
                                }
                            } else if(arg.Equals("-h") || arg.Equals("--help")) {
                                Console.Error.Write(HELP);
                                return;
                            } else if(arg.Equals("--version")) {
                                Console.Error.WriteLine(getVersion());
                                printedSomething = true;
                            } else if(arg.Equals("--copyright")) {
                                Console.Error.Write(COPYRIGHT);
                                printedSomething = true;
                            } else if(arg.Equals("-")) {
                                readStdin = true;
                            } else if(arg[1] == 'C') {
                                if(arg.Length == 2) {
                                    cwd = args[++start];
                                } else {
                                    cwd = arg.Substring(2);
                                }
                            } else {
                                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(r.Condition,
                                                                                             message,
                                                                                             context,
                                                                                             "Error",
                                                                                             "CommandLine",
                                                                                             "DontUnderstandOption"), null).Mimic(message, context);
                                condition.SetCell("message", message);
                                condition.SetCell("context", context);
                                condition.SetCell("receiver", context);
                                condition.SetCell("option", r.NewText(arg));
                                r.ErrorCondition(condition);
                            }
                        }
                    }
                }

                if(cwd != null) {
                    r.CurrentWorkingDirectory = cwd;
                }

                ((IokeSystem)IokeObject.dataOf(r.System)).CurrentProgram = "-e";

                string lib = Environment.GetEnvironmentVariable("ioke.lib");
                if(lib == null) {
                    lib = iokeLib;
                }
                ((IokeSystem)IokeObject.dataOf(r.System)).AddLoadPath(lib + "/ioke");
                ((IokeSystem)IokeObject.dataOf(r.System)).AddLoadPath("lib/ioke");

                foreach(string ss in loadDirs) {
                    ((IokeSystem)IokeObject.dataOf(r.System)).AddLoadPath(ss);
                }

                foreach(string script in scripts) {
                    r.EvaluateStream("-e", new StringReader(script), message, context);
                }

                if(readStdin) {
                    ((IokeSystem)IokeObject.dataOf(r.System)).CurrentProgram = "<stdin>";
                    r.EvaluateStream("<stdin>", Console.In, message, context);
                }

                if(args.Length > start) {
                    if(args.Length > (start+1)) {
                        for(int i=start+1,j=args.Length; i<j; i++) {
                            r.AddArgument(args[i]);
                        }
                    }
                    string file = args[start];
                    if(file.StartsWith("\"")) {
                        file = file.Substring(1, file.Length-1);
                    }

                    if(file.Length > 1 && file[file.Length-1] == '"') {
                        file = file.Substring(0, file.Length-1);
                    }

                    ((IokeSystem)IokeObject.dataOf(r.System)).CurrentProgram = file;
                    r.EvaluateFile(file, message, context);
                } else {
                    if(!readStdin && scripts.Count == 0 && !printedSomething) {
                        r.EvaluateString("use(\"builtin/iik\"). IIk mainLoop", message, context);
                    }
                }

                r.TearDown();

            } catch(ControlFlow.Exit e) {
                int exitVal = e.ExitValue;
                try {
                    r.TearDown();
                } catch(ControlFlow.Exit e2) {
                    exitVal = e2.ExitValue;
                }
                Environment.Exit(exitVal);
            } catch(ControlFlow e) {
                string name = e.GetType().FullName;
                System.Console.Error.WriteLine("unexpected control flow: " + name.Substring(name.LastIndexOf(".") + 1).ToLower());
                if(debug) {
                    System.Console.Error.WriteLine(e);
                }
                Environment.Exit(1);
            }
        }
        public static DefaultArgumentsDefinition CreateFrom(IList args, int start, int end, IokeObject message, object on, IokeObject context)
        {
            Runtime          runtime   = context.runtime;
            IList <Argument> arguments = new SaneList <Argument>();
            IList <string>   keywords  = new SaneList <string>();

            int    min         = 0;
            int    max         = 0;
            bool   hadOptional = false;
            string rest        = null;
            string krest       = null;

            foreach (object obj in ArrayList.Adapter(args).GetRange(start, end - start))
            {
                Message m     = (Message)IokeObject.dataOf(obj);
                string  mname = m.Name;
                if (!"+:".Equals(mname) && m.IsKeyword())
                {
                    string     name   = mname;
                    IokeObject dValue = context.runtime.nilMessage;
                    if (m.next != null)
                    {
                        dValue = m.next;
                    }
                    arguments.Add(new KeywordArgument(name.Substring(0, name.Length - 1), dValue));
                    keywords.Add(name);
                }
                else if (mname.Equals("+"))
                {
                    string name = Message.GetName(m.Arguments(null)[0]);
                    if (name.StartsWith(":"))
                    {
                        krest = name.Substring(1);
                    }
                    else
                    {
                        rest = name;
                        max  = -1;
                    }
                    hadOptional = true;
                }
                else if (mname.Equals("+:"))
                {
                    string name = m.next != null?Message.GetName(m.next) : Message.GetName(m.Arguments(null)[0]);

                    krest       = name;
                    hadOptional = true;
                }
                else if (m.next != null)
                {
                    string name = mname;
                    hadOptional = true;
                    if (max != -1)
                    {
                        max++;
                    }
                    arguments.Add(new OptionalArgument(name, m.next));
                }
                else
                {
                    if (hadOptional)
                    {
                        int index = IndexOf(args, obj) + start;

                        IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                                     message,
                                                                                     context,
                                                                                     "Error",
                                                                                     "Invocation",
                                                                                     "ArgumentWithoutDefaultValue"), context).Mimic(message, context);
                        condition.SetCell("message", message);
                        condition.SetCell("context", context);
                        condition.SetCell("receiver", on);
                        condition.SetCell("argumentName", runtime.GetSymbol(m.Name));
                        condition.SetCell("index", runtime.NewNumber(index));

                        IList newValue = IokeList.GetList(runtime.WithRestartReturningArguments(() => { runtime.ErrorCondition(condition); },
                                                                                                context,
                                                                                                new NewArgumentGivingRestart("provideDefaultValue", "defaultValue"),
                                                                                                new Restart.DefaultValuesGivingRestart("substituteNilDefault", runtime.nil, 1)));
                        if (max != -1)
                        {
                            max++;
                        }

                        arguments.Add(new OptionalArgument(m.Name, runtime.CreateMessage(Message.Wrap(IokeObject.As(newValue[0], context)))));
                    }
                    else
                    {
                        min++;
                        max++;
                        arguments.Add(new Argument(IokeObject.As(obj, context).Name));
                    }
                }
            }

            return(new DefaultArgumentsDefinition(arguments, keywords, rest, krest, min, max, false));
        }
Beispiel #4
0
        public static DefaultArgumentsDefinition CreateFrom(IList args, int start, int end, IokeObject message, object on, IokeObject context)
        {
            Runtime runtime = context.runtime;
            IList<Argument> arguments = new SaneList<Argument>();
            IList<string> keywords = new SaneList<string>();

            int min = 0;
            int max = 0;
            bool hadOptional = false;
            string rest = null;
            string krest = null;

            foreach(object obj in ArrayList.Adapter(args).GetRange(start, end-start)) {
                Message m = (Message)IokeObject.dataOf(obj);
                string mname = m.Name;
                if(!"+:".Equals(mname) && m.IsKeyword()) {
                    string name = mname;
                    IokeObject dValue = context.runtime.nilMessage;
                    if(m.next != null) {
                        dValue = m.next;
                    }
                    arguments.Add(new KeywordArgument(name.Substring(0, name.Length-1), dValue));
                    keywords.Add(name);
                } else if(mname.Equals("+")) {
                    string name = Message.GetName(m.Arguments(null)[0]);
                    if(name.StartsWith(":")) {
                        krest = name.Substring(1);
                    } else {
                        rest = name;
                        max = -1;
                    }
                    hadOptional = true;
                } else if(mname.Equals("+:")) {
                    string name = m.next != null ? Message.GetName(m.next) : Message.GetName(m.Arguments(null)[0]);
                    krest = name;
                    hadOptional = true;
                } else if(m.next != null) {
                    string name = mname;
                    hadOptional = true;
                    if(max != -1) {
                        max++;
                    }
                    arguments.Add(new OptionalArgument(name, m.next));
                } else {
                    if(hadOptional) {
                        int index = IndexOf(args, obj) + start;

                        IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                                     message,
                                                                                     context,
                                                                                     "Error",
                                                                                     "Invocation",
                                                                                     "ArgumentWithoutDefaultValue"), context).Mimic(message, context);
                        condition.SetCell("message", message);
                        condition.SetCell("context", context);
                        condition.SetCell("receiver", on);
                        condition.SetCell("argumentName", runtime.GetSymbol(m.Name));
                        condition.SetCell("index", runtime.NewNumber(index));

                        IList newValue = IokeList.GetList(runtime.WithRestartReturningArguments(()=>{runtime.ErrorCondition(condition);},
                                                                                                context,
                                                                                                new NewArgumentGivingRestart("provideDefaultValue", "defaultValue"),
                                                                                                new Restart.DefaultValuesGivingRestart("substituteNilDefault", runtime.nil, 1)));
                        if(max != -1) {
                            max++;
                        }

                        arguments.Add(new OptionalArgument(m.Name, runtime.CreateMessage(Message.Wrap(IokeObject.As(newValue[0], context)))));
                    } else {
                        min++;
                        max++;
                        arguments.Add(new Argument(IokeObject.As(obj, context).Name));
                    }
                }
            }

            return new DefaultArgumentsDefinition(arguments, keywords, rest, krest, min, max, false);
        }
Beispiel #5
0
        public IList<HandlerInfo> FindActiveHandlersFor(IokeObject condition, BindIndex stopIndex)
        {
            var result = new SaneList<HandlerInfo>();

            foreach(IList<HandlerInfo> lrp in Handlers) {
                foreach(HandlerInfo rp in lrp) {
                    if(rp.index.LessThan(stopIndex)) {
                        return result;
                    }

                    foreach(object possibleKind in rp.applicableConditions) {
                        if(IokeObject.IsMimic(condition, IokeObject.As(possibleKind, condition))) {
                            result.Add(rp);
                        }
                    }
                }
            }

            return result;
        }
Beispiel #6
0
 public object WithReturningRescue(IokeObject context, object toReturn, RunnableWithReturnAndControlFlow nativeRescue)
 {
     IList<RescueInfo> rescues = new SaneList<RescueInfo>();
     IokeObject rr = IokeObject.As(Interpreter.Send(this.mimicMessage, context, this.Rescue), context);
     IList conds = new SaneArrayList();
     conds.Add(this.Condition);
     rescues.Add(new RescueInfo(rr, conds, rescues, GetBindIndex()));
     RegisterRescues(rescues);
     try {
         return nativeRescue();
     } catch(ControlFlow.Rescue e) {
         if(e.GetRescue.token == rescues) {
             return toReturn;
         } else {
             throw e;
         }
     } finally {
         UnregisterRescues(rescues);
     }
 }
Beispiel #7
0
        public static void Main(string[] args)
        {
            string current  = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string iokeHome = new FileInfo(current).Directory.Parent.FullName;
            string iokeLib  = Path.Combine(iokeHome, "lib");

            Runtime r = new Runtime();

            r.Init();

            IokeObject context = r.Ground;
            Message    mx      = new Message(r, ".", null, true);

            mx.Line     = 0;
            mx.Position = 0;
            IokeObject message = r.CreateMessage(mx);

            string cwd = null;

            var  scripts  = new SaneList <string>();
            var  loadDirs = new SaneList <string>();
            bool debug    = false;

            try {
                int  start            = 0;
                bool done             = false;
                bool readStdin        = false;
                bool printedSomething = false;

                for (; !done && start < args.Length; start++)
                {
                    string arg = args[start];
                    if (arg.Length > 0)
                    {
                        if (arg[0] != '-')
                        {
                            done = true;
                            break;
                        }
                        else
                        {
                            if (arg.Equals("--"))
                            {
                                done = true;
                            }
                            else if (arg.Equals("-d"))
                            {
                                debug   = true;
                                r.Debug = true;
                            }
                            else if (arg.StartsWith("-e"))
                            {
                                if (arg.Length == 2)
                                {
                                    scripts.Add(args[++start]);
                                }
                                else
                                {
                                    scripts.Add(arg.Substring(2));
                                }
                            }
                            else if (arg.StartsWith("-I"))
                            {
                                if (arg.Length == 2)
                                {
                                    loadDirs.Add(args[++start]);
                                }
                                else
                                {
                                    loadDirs.Add(arg.Substring(2));
                                }
                            }
                            else if (arg.Equals("-h") || arg.Equals("--help"))
                            {
                                Console.Error.Write(HELP);
                                return;
                            }
                            else if (arg.Equals("--version"))
                            {
                                Console.Error.WriteLine(getVersion());
                                printedSomething = true;
                            }
                            else if (arg.Equals("--copyright"))
                            {
                                Console.Error.Write(COPYRIGHT);
                                printedSomething = true;
                            }
                            else if (arg.Equals("-"))
                            {
                                readStdin = true;
                            }
                            else if (arg[1] == 'C')
                            {
                                if (arg.Length == 2)
                                {
                                    cwd = args[++start];
                                }
                                else
                                {
                                    cwd = arg.Substring(2);
                                }
                            }
                            else
                            {
                                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(r.Condition,
                                                                                             message,
                                                                                             context,
                                                                                             "Error",
                                                                                             "CommandLine",
                                                                                             "DontUnderstandOption"), null).Mimic(message, context);
                                condition.SetCell("message", message);
                                condition.SetCell("context", context);
                                condition.SetCell("receiver", context);
                                condition.SetCell("option", r.NewText(arg));
                                r.ErrorCondition(condition);
                            }
                        }
                    }
                }

                if (cwd != null)
                {
                    r.CurrentWorkingDirectory = cwd;
                }

                ((IokeSystem)IokeObject.dataOf(r.System)).CurrentProgram = "-e";

                string lib = Environment.GetEnvironmentVariable("ioke.lib");
                if (lib == null)
                {
                    lib = iokeLib;
                }
                ((IokeSystem)IokeObject.dataOf(r.System)).AddLoadPath(lib + "/ioke");
                ((IokeSystem)IokeObject.dataOf(r.System)).AddLoadPath("lib/ioke");

                foreach (string ss in loadDirs)
                {
                    ((IokeSystem)IokeObject.dataOf(r.System)).AddLoadPath(ss);
                }

                foreach (string script in scripts)
                {
                    r.EvaluateStream("-e", new StringReader(script), message, context);
                }

                if (readStdin)
                {
                    ((IokeSystem)IokeObject.dataOf(r.System)).CurrentProgram = "<stdin>";
                    r.EvaluateStream("<stdin>", Console.In, message, context);
                }

                if (args.Length > start)
                {
                    if (args.Length > (start + 1))
                    {
                        for (int i = start + 1, j = args.Length; i < j; i++)
                        {
                            r.AddArgument(args[i]);
                        }
                    }
                    string file = args[start];
                    if (file.StartsWith("\""))
                    {
                        file = file.Substring(1, file.Length - 1);
                    }

                    if (file.Length > 1 && file[file.Length - 1] == '"')
                    {
                        file = file.Substring(0, file.Length - 1);
                    }

                    ((IokeSystem)IokeObject.dataOf(r.System)).CurrentProgram = file;
                    r.EvaluateFile(file, message, context);
                }
                else
                {
                    if (!readStdin && scripts.Count == 0 && !printedSomething)
                    {
                        r.EvaluateString("use(\"builtin/iik\"). IIk mainLoop", message, context);
                    }
                }

                r.TearDown();
            } catch (ControlFlow.Exit e) {
                int exitVal = e.ExitValue;
                try {
                    r.TearDown();
                } catch (ControlFlow.Exit e2) {
                    exitVal = e2.ExitValue;
                }
                Environment.Exit(exitVal);
            } catch (ControlFlow e) {
                string name = e.GetType().FullName;
                System.Console.Error.WriteLine("unexpected control flow: " + name.Substring(name.LastIndexOf(".") + 1).ToLower());
                if (debug)
                {
                    System.Console.Error.WriteLine(e);
                }
                Environment.Exit(1);
            }
        }
Beispiel #8
0
 public IList<IokeObject> GetMimics()
 {
     var result = new SaneList<IokeObject>();
     switch(body.mimicCount) {
     case 0:
         break;
     case 1:
         result.Add(body.mimic);
         break;
     default:
         for(int i=0; i<body.mimicCount; i++) {
             result.Add(body.mimics[i]);
         }
         break;
     }
     return result;
 }