Esempio n. 1
0
 public override Runlet Weave(Runlet continuation, GlobalDictionary g)
 {
     for (int i = Body.Length - 1; i >= 0; i -= 1)
     {
         Codelet codelet = Body[i];
         continuation = codelet.Weave(continuation, g);
     }
     return(continuation);
 }
Esempio n. 2
0
        public void Initialise(string key, Codelet value)
        {
            var    halt               = new HaltRunlet();
            var    unlock             = new UnlockRunlet(halt);
            var    pop                = new PopGlobalRunlet(this._dictionary.Get(key), unlock);
            var    init               = value.Weave(pop, this._dictionary);
            Runlet currentInstruction = new LockRunlet(init);

            while (true)
            {
                currentInstruction = currentInstruction.ExecuteRunlet(this);
            }
        }
Esempio n. 3
0
 public void Bind(string idName, Codelet codelet)
 {
     this._dictionary.Add(idName, codelet.Weave(null, this._dictionary));
 }
Esempio n. 4
0
 public If3Codelet(Codelet testPart, Codelet thenPart, Codelet elsePart)
 {
     TestPart = testPart;
     ThenPart = thenPart;
     ElsePart = elsePart;
 }
Esempio n. 5
0
 public CallCodelet(Codelet f, Codelet a)
 {
     this.Funarg    = f;
     this.Arguments = a;
 }
Esempio n. 6
0
 public FunctionCodelet(int nargs, int nlocals, Codelet body)
 {
     this.Nargs   = nargs;
     this.Nlocals = nlocals;
     this.Body    = body;
 }
Esempio n. 7
0
        private void Run()
        {
            TextWriter stdErr = Console.Error;

            if (this._debug)
            {
                stdErr.WriteLine("Nutmeg kicks the ball ...");
            }
            if (this._debug)
            {
                stdErr.WriteLine($"Bundle file: {this._bundleFile}");
            }
            if (this._debug)
            {
                stdErr.WriteLine($"Entry point: {this._entryPoint}");
            }
            try {
                RuntimeEngine runtimeEngine = new RuntimeEngine(this._debug);
                using (SQLiteConnection connection = new SQLiteConnection($"Data Source={this._bundleFile}")) {
                    connection.Open();
                    var cmd = new SQLiteCommand("SELECT B.[IdName], B.[Value] FROM [Bindings] B JOIN [EntryPoints] E ON E.[Needs] = B.[IdName] WHERE E.[EntryPoint]=@EntryPoint", connection);
                    cmd.Parameters.AddWithValue("@EntryPoint", this._entryPoint);
                    cmd.Prepare();
                    var reader          = cmd.ExecuteReader();
                    var bindings        = new Dictionary <string, Codelet>();
                    var initialisations = new List <KeyValuePair <string, Codelet> >();
                    while (reader.Read())
                    {
                        string idName    = reader.GetString(0);
                        string jsonValue = reader.GetString(1);
                        if (this._debug)
                        {
                            stdErr.WriteLine($"Loading definition: {idName}");
                        }
                        try {
                            Codelet codelet = Codelet.DeserialiseCodelet(jsonValue);
                            runtimeEngine.PreBind(idName);
                            if (codelet is FunctionCodelet fc)
                            {
                                bindings.Add(idName, codelet);
                            }
                            else
                            {
                                initialisations.Add(new KeyValuePair <string, Codelet>(idName, codelet));
                            }
                        } catch (Newtonsoft.Json.JsonSerializationException e) {
                            Exception inner = e.InnerException;
                            throw (inner is NutmegException nme) ? (Exception)nme : (Exception)e;
                        }
                    }
                    foreach (var k in bindings)
                    {
                        runtimeEngine.Bind(k.Key, k.Value);
                    }
                    foreach (var kvp in initialisations)
                    {
                        if (this._debug)
                        {
                            Console.WriteLine($"Binding {kvp.Key}");
                        }
                        try {
                            runtimeEngine.Initialise(kvp.Key, kvp.Value);
                        } catch (NormalExitNutmegException) {
                            //  This is how initialisation is halted.
                        }
                    }
                }
                runtimeEngine.Start(this._entryPoint, useEvaluate: false);
            } catch (NutmegException nme) {
                Console.Error.WriteLine($"MISHAP: {nme.Message}");
                foreach (var culprit in nme.Culprits)
                {
                    Console.Error.WriteLine($" {culprit.Key}: {culprit.Value}");
                }
                throw nme;  // rethrow
            }
            //var jobj = JToken.ReadFrom(new JsonTextReader(new StreamReader(Console.OpenStandardInput())));
            //Console.WriteLine($"Output = {jobj.ToString()}");
        }