Example #1
0
        public static void Execute(string inSRC, bool verbose, Dictionary <string, string> argsVariables)
        {
            string contenu = FileReader.ReadComments(inSRC, true);

            Console.WriteLine("\nSOURCE CODE");
            Console.WriteLine("-----------------------------------------------");
            Console.WriteLine(contenu);

            // COMPILE.
            SourceCodeReader reader  = new SourceCodeReader();
            Node             program = reader.Read(contenu);

            Console.WriteLine("\nTREE VIEW");
            Console.WriteLine("-----------------------------------------------");
            Console.WriteLine(program.Show());

            Console.WriteLine("\nCOMPILED SCRIPT (debug)");
            Console.WriteLine("-----------------------------------------------");
            string contenuBinDebug = CompilerWriter.Compile(reader.Read(contenu), true);

            Console.WriteLine(contenuBinDebug);

            string contenuBin = CompilerWriter.Compile(program, false);

            Console.WriteLine("\nCOMPILED SCRIPT (release)");
            Console.WriteLine("-----------------------------------------------");
            Console.WriteLine(contenuBin);
            File.WriteAllText("bin.txt", contenuBin);

            // PREPARE RUN.
            string contenuBinRead = FileReader.ReadComments(File.ReadAllText("bin.txt"), true);

            try
            {
                BasicRunner readerBin = new BasicRunner();

                // Init the interpreter.
                readerBin.Init(contenuBinRead);

                foreach (KeyValuePair <string, string> pair in argsVariables.ToArray())
                {
                    readerBin.Variables.Add(pair.Key, pair.Value);
                }

                // EXECUTE
                Console.WriteLine("\nSCRIPT EXECUTION");
                Console.WriteLine("-----------------------------------------------");
                readerBin.Execute(false, verbose);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine("Exception : " + e.Message);
                Console.ReadLine();
            }
        }
        public void GetReader_TwoIndependentReadersAccessingSameData()
        {
            string        testInput = _codeSnippets[CodeType.SevenLinesOfAssignemtStatements];
            StringBuilder strBuffer = new StringBuilder();

            //Get the aprox midpoint length of codinput.
            int codeMidPoint = testInput.Length / 2;

            ScriptSource source = _testEng.CreateScriptSourceFromString(testInput,
                                                                        SourceCodeKind.Statements);
            // Create the Readers
            SourceCodeReader srcFirstUnitReader  = source.GetReader();
            SourceCodeReader srcSecondUnitReader = source.GetReader();

            int chrnbr = 0;
            int cnt    = 0;

            // Read first half of stream with first stream reader
            while (((chrnbr = srcFirstUnitReader.Read()) > 0) && (cnt < codeMidPoint))
            {
                strBuffer.Append((char)chrnbr);
                cnt++; // inc cnt
                // Increment Second Reader
                srcSecondUnitReader.Read();
            }

            // Now get the second half of the input stream with second reader
            while ((chrnbr = srcSecondUnitReader.Read()) > 0)
            {
                strBuffer.Append((char)chrnbr);
                cnt++;
            }

            Assert.AreEqual(cnt, testInput.Length);
            Assert.AreEqual(testInput, strBuffer.ToString());
        }
Example #3
0
        public virtual string Eval(string nom, int nbParams, LinkedList <string> param)
        {
            nom = nom.ToLower();

            if (nom == "()" && nbParams != 0)
            {
                return(param.ElementAt(nbParams - 1));
            }

            if ((nom == "=" || nom == "equals") && nbParams == 2)
            {
                return((param.ElementAt(0) == (param.ElementAt(1))) + "");
            }

            if (nom == "eval")
            {
                SourceCodeReader reader      = new SourceCodeReader();
                Node             program     = reader.Read(param.ElementAt(0));
                string           contenuBin  = CompilerWriter.Compile(program, false);
                BasicRunner      basicReader = new BasicRunner();
                basicReader.Init(contenuBin);
                basicReader.Execute(false, false);
            }

            if (nom == "call")
            {
                nom = param.ElementAt(0);
                nbParams--;
                param.RemoveFirst();
                return(Eval(nom, nbParams, param));
            }

            if (nom == "not" && nbParams == 1)
            {
                string condition = param.ElementAt(0);

                condition = condition.ToLower();

                return("" + (condition == "false" || condition == "0"));
            }

            if (nom == "or")
            {
                bool   continuer = nbParams > 0;
                int    i         = 0;
                string result    = "false";
                string condition;
                while (continuer)
                {
                    condition = param.ElementAt(i);
                    condition = condition.ToLower();

                    if (condition != "false" && condition != "0")
                    {
                        result = "true";
                        break;
                    }
                    i++;
                    continuer = i < nbParams;
                }
                return(result);
            }

            if (nom == "and")
            {
                bool   continuer = nbParams > 0;
                int    i         = 0;
                string result    = continuer + "";
                string condition;
                while (continuer)
                {
                    condition = param.ElementAt(i);
                    condition = condition.ToLower();

                    if (condition == "false" || condition == "0")
                    {
                        result = "false";
                        break;
                    }
                    i++;
                    continuer = i < nbParams;
                }
                return(result);
            }

            if (nom.StartsWith("read"))
            {
                switch (nom.Substring(4))
                {
                case "key":
                    Console.ReadKey();
                    return(null);

                case "line":
                    return(Console.ReadLine());

                default:
                    break;
                }
            }
            else if (nom == "print")
            {
                foreach (string s in param)
                {
                    Console.Write(s);
                }
                Console.WriteLine();
            }
            else if (nom == "substring")
            {
                if (nbParams == 2)
                {
                    return(param.ElementAt(0).Substring(TryParse(param.ElementAt(1))));
                }
                else if (nbParams == 3)
                {
                    int start  = TryParse(param.ElementAt(1));
                    int length = TryParse(param.ElementAt(2));

                    try
                    {
                        string s = param.ElementAt(0).Substring(start, length);
                        return(s);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else if (nom == "length" && nbParams == 1)
            {
                return(param.ElementAt(0).Length + "");
            }
            else if (nom == "replace" && nbParams == 3)
            {
                string exp      = param.ElementAt(0);
                string oldValue = param.ElementAt(1);
                string newValue = param.ElementAt(2);
                exp = exp.Replace(oldValue, newValue);
                return(exp);
            }
            else if (nom == "concat")
            {
                StringBuilder builder = new StringBuilder();
                foreach (string s in param)
                {
                    builder.Append(s);
                }
                return(builder.ToString());
            }
            else if (Regex.IsMatch(nom, "^[\\+|\\-|\\*|\\/|\\%]$") && nbParams == 2)
            {
                try
                {
                    return(EvalMath(nom[0], TryParse(param.ElementAt(0)),
                                    TryParse(param.ElementAt(1))));
                }
                catch (Exception e)
                {
                    return("Math Error : " + e.Message);
                }
            }

            return(null);        // Void.
        }