Beispiel #1
0
        private void LoadFile(String FileName)
        {
            if (!System.IO.File.Exists(FileName))
            {
                throw new MISPLIB.EvaluationError("File not found");
            }

            var text   = System.IO.File.ReadAllText(FileName);
            var parsed = MISPLIB.Core.Parse(new MISPLIB.StringIterator(text));
            var result = MISPLIB.Core.Evaluate(parsed, GlobalScope);

            if (result.Type != MISPLIB.AtomType.Record)
            {
                throw new MISPLIB.EvaluationError("Loading of file did not produce record.");
            }
            OpenFilePath = FileName;
            this.Title   = OpenFilePath;
            GlobalScope  = result as MISPLIB.RecordAtom;
            GlobalScope.Variables.Upsert("@", GlobalScope);
        }
Beispiel #2
0
        static void Main(string[] clargs)
        {
            try
            {
                MISPLIB.RecordAtom GlobalScope = new MISPLIB.RecordAtom();
                GlobalScope.Variables.Upsert("@", GlobalScope);
                MISPLIB.Core.InitiateCore(Console.Write);

                MISPLIB.Core.AddCoreFunction("print +arg", (args, c) =>
                {
                    var builder = new StringBuilder();
                    foreach (var v in (args[0] as MISPLIB.ListAtom).Value)
                    {
                        if (v.Type == MISPLIB.AtomType.String)
                        {
                            builder.Append((v as MISPLIB.StringAtom).Value);
                        }
                        else
                        {
                            v.Emit(builder);
                        }
                    }
                    Console.WriteLine(builder.ToString());
                    return(new MISPLIB.NilAtom());
                });

                MISPLIB.Core.AddCoreFunction("core", (args, c) =>
                {
                    var builder = new StringBuilder();
                    foreach (var func in MISPLIB.Core.CoreFunctions)
                    {
                        builder.Append(func.Name);
                        foreach (var name in func.ArgumentNames)
                        {
                            builder.Append(" ");
                            name.Emit(builder);
                        }
                        builder.Append("\n");
                    }

                    Console.WriteLine(builder.ToString());
                    return(new MISPLIB.NilAtom());
                });

                //MISPLIB.Core.AddCoreFunction("@", (args, c) =>
                //{
                //    return GlobalScope;
                //});

                var place = 0;
                while (place < clargs.Length)
                {
                    if (clargs[place] == "-f" || clargs[place] == "-e")
                    {
                        ++place;
                        if (place == clargs.Length)
                        {
                            Console.WriteLine("Expected an argument to -f");
                            return;
                        }

                        var text   = System.IO.File.ReadAllText(clargs[place]);
                        var parsed = MISPLIB.Core.Parse(new MISPLIB.StringIterator(text));
                        var result = MISPLIB.Core.Evaluate(parsed, GlobalScope);
                        if (result.Type != MISPLIB.AtomType.Record)
                        {
                            throw new MISPLIB.EvaluationError("Loading of file did not produce record.");
                        }
                        GlobalScope = result as MISPLIB.RecordAtom;
                        GlobalScope.Variables.Upsert("@", GlobalScope);
                    }
                    else
                    {
                        var parsed        = MISPLIB.Core.Parse(new MISPLIB.StringIterator(clargs[place]));
                        var result        = MISPLIB.Core.Evaluate(parsed, GlobalScope);
                        var outputBuilder = new StringBuilder();
                        MISPLIB.Core.EmissionID = Guid.NewGuid();
                        result.Emit(outputBuilder);
                        Console.WriteLine(outputBuilder.ToString());
                    }

                    ++place;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
Beispiel #3
0
        public MainWindow()
        {
            InitializeComponent();
            TextBox_TextChanged(null, null);
            String OpenFilePath = null;

            InputBox.Focus();

            this.Title = "New Environment";

            MISPLIB.Core.InitiateCore(s =>
            {
                OutputBox.AppendText(s);
                OutputBox.ScrollToEnd();
            });

            MISPLIB.Core.AddCoreFunction("recall function", (args, c) =>
            {
                MISPLIB.Core.EmissionID = Guid.NewGuid();
                var builder             = new StringBuilder();
                args[0].Emit(builder);
                InputBox.Text = builder.ToString();
                return(args[0]);
            });

            MISPLIB.Core.AddCoreFunction("@", (args, c) =>
            {
                return(GlobalScope);
            });

            MISPLIB.Core.AddCoreFunction("save file", (args, c) =>
            {
                if (args[0].Type != MISPLIB.AtomType.String)
                {
                    throw new MISPLIB.EvaluationError("Expected string as first argument to save.");
                }
                var saveFileName = (args[0] as MISPLIB.StringAtom).Value;

                var serializer = new MISPLIB.SerializationContext();
                var builder    = new StringBuilder();
                serializer.Serialize(GlobalScope, builder);

                var dirName = System.IO.Path.GetDirectoryName(saveFileName);
                if (!String.IsNullOrEmpty(dirName))
                {
                    System.IO.Directory.CreateDirectory(dirName);
                }

                System.IO.File.WriteAllText(saveFileName, builder.ToString());
                OpenFilePath = saveFileName;
                this.Title   = OpenFilePath;
                return(new MISPLIB.StringAtom {
                    Value = OpenFilePath
                });
            });

            MISPLIB.Core.AddCoreFunction("load file", (args, c) =>
            {
                if (args[0].Type != MISPLIB.AtomType.String)
                {
                    throw new MISPLIB.EvaluationError("Expected path as first argument to load.");
                }

                var text   = System.IO.File.ReadAllText((args[0] as MISPLIB.StringAtom).Value);
                var parsed = MISPLIB.Core.Parse(new MISPLIB.StringIterator(text));
                var result = MISPLIB.Core.Evaluate(parsed, GlobalScope);
                if (result.Type != MISPLIB.AtomType.Record)
                {
                    throw new MISPLIB.EvaluationError("Loading of file did not produce record.");
                }
                OpenFilePath = (args[0] as MISPLIB.StringAtom).Value;
                this.Title   = OpenFilePath;
                GlobalScope  = result as MISPLIB.RecordAtom;
                return(GlobalScope);
            });

            OutputBox.Document.Blocks.Add(OutputRoot);

            var buildVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            var run          = new Run("MISP 3.0 Build " + buildVersion + "\n")
            {
                Foreground = Brushes.Red
            };

            OutputRoot.Inlines.Add(run);
        }