Esempio n. 1
0
        public void TestFor()
        {
            for (int i = 0; i < testForStatements.Length; ++i)
            {
                var main  = new Interpreter.Sequence();
                var queue = new List <string>();

                queue.AddRange(testForStatements[i]);

                int line = 0;

                while (line < queue.Count)
                {
                    line = main.Execute(queue, line);
                }

                switch (resultForRuns[i].GetType().Name)
                {
                case "Double": Assert.AreEqual((Double)resultForRuns[i], (Double)main.GetNamedValue("sum"), 0.05);
                    break;

                default: Assert.AreEqual(resultForRuns[i], main.GetNamedValue("sum"));
                    break;
                }
            }
        }
Esempio n. 2
0
        public void TestWhile()
        {
            var queue = new List <string>();

            queue.AddRange(testWhileProcess.Split('\n'));

            for (int i = 0; i < testWhileDeclarations.Length; ++i)
            {
                var main      = new Interpreter.Sequence();
                var testQueue = new List <string>();

                testQueue.Add(testWhileDeclarations[i]);
                testQueue.AddRange(queue);

                int line = 0;

                while (line < testQueue.Count)
                {
                    line = main.Execute(testQueue, line);
                }

                Assert.AreEqual(resultWhileRuns[i], main.GetNamedValue("counter"));
            }
        }
Esempio n. 3
0
        public static object Call(Interpreter.Sequence sender, string name, List <object> arguments)
        {
            Definition newProcess = Get(name, arguments);

            int lineNumber = newProcess.LineNumber;

            List <string> script = Transcript.Table[newProcess.Path];

            int startingLvl = Interpreter.Sequence.GetLevel(script[lineNumber]);

            List <Parameter> parameters = ParseHeading(script, ref lineNumber);

            var receiver = new Interpreter.Sequence();

            Dictionary <string, Named> table;

            for (int i = 0; i < parameters.Count; ++i)
            {
                if (parameters[i].referential)
                {
                    try
                    {
                        table = sender.Names.GetTable((string)arguments[i]);
                    }
                    catch (Exception)
                    {
                        throw new Exception("Failed to get a reference for a bidirectional parameter. Are you missing a 'ref' keyword?");
                    }
                }
                else
                {
                    table = null;
                }

                Verbs.Declare(receiver,
                              parameters[i].key,
                              parameters[i].key,
                              parameters[i].readable,
                              parameters[i].writeable,
                              arguments[i],
                              table,
                              parameters[i].typename);
            }

            try
            {
                while (lineNumber < script.Count)
                {
                    if (String.IsNullOrWhiteSpace(script[lineNumber]))
                    {
                        lineNumber = lineNumber + 1;
                    }
                    else if (Interpreter.Sequence.GetLevel(script[lineNumber]) > startingLvl)
                    {
                        lineNumber = receiver.Execute(script, lineNumber);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Return r)
            {
                if (Operators.returnvoid.Contains(newProcess.ReturnType))
                {
                    if (r.Payload == null)
                    {
                        throw new Exception("Line " + (lineNumber + 1) + ": Syntax Error: Returning 'null' from a process that returns void.");
                    }

                    throw new Exception("Line " + (lineNumber + 1) + ": Syntax Error: Returning a '" + r.Payload.GetType().Name + "' from a process that returns void.");
                }

                if (r.Payload != null && !r.Payload.GetType().Name.Equals(newProcess.ReturnType))
                {
                    throw new Exception("Line " + (lineNumber + 1) + ": Type Mismatch: Returning a '" + r.Payload.GetType().Name + "' from a process that returns '" + newProcess.ReturnType + "'.");
                }

                return(r.Payload);
            }

            if (!Operators.returnvoid.Contains(newProcess.ReturnType))
            {
                throw new Exception("Line " + (newProcess.LineNumber + 1) + ": Syntax Error: No return at the end of non-void returning process.");
            }

            return(null);
        }