Beispiel #1
0
        static int StreamInput(TextReader input, Encoding encoding, bool errorsWithStackTrace)
        {
            var builtinTypes = LoadBuiltinTypes();
            var charReader   = new TextReaderCharReader(input);
            var tokenizer    = new Tokenizer(charReader);
            var interpreter  = new DocumentProcessor(tokenizer, builtinTypes, new InstructionInterpreterFactory());

            try
            {
                var namespaces = interpreter.Read();
                var compiler   = new JavascriptCompiler("example", namespaces, builtinTypes);
                foreach (var f in LoadBuiltinFunctions())
                {
                    compiler.AddBuiltinFunction(f);
                }
                using (MemoryStream mem = new MemoryStream())
                    using (StreamWriter memW = new StreamWriter(mem, encoding))
                    {
                        compiler.Compile(memW);
                        memW.Flush();
                        Console.Out.Write(encoding.GetString(mem.ToArray()));
                    }
                return(0);
            }
            catch (SyntaxException ex)
            {
                Console.Error.WriteLine("Syntax error at " + ex.Position);
                Console.Error.WriteLine(ex.Message);
                if (errorsWithStackTrace)
                {
                    Console.Error.WriteLine(ex.StackTrace);
                }
                return(0x101);
            }
            catch (CompileException ex)
            {
                Console.Error.WriteLine("Compile error at " + ex.Position);
                Console.Error.WriteLine(ex.Message);
                if (errorsWithStackTrace)
                {
                    Console.Error.WriteLine(ex.StackTrace);
                }
                return(0x102);
            }
        }
Beispiel #2
0
        static void InteractiveConsoleInput()
        {
            Console.WriteLine("Type 'exit' to exit.");
            Console.WriteLine("Type any code and press ENTER to parse it.");
            Console.WriteLine();

            var charReader  = new VarStringCharReader();
            var tokenizer   = new Tokenizer(charReader);
            var interpreter = new DocumentProcessor(tokenizer, LoadBuiltinTypes(), new InstructionInterpreterFactory());

            while (true)
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.White;

                string line = Console.ReadLine();
                if (line.ToLower() == "exit")
                {
                    return;
                }
                charReader.SetString(line);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Gray;

                try
                {
                    var namespaces = interpreter.Read();
                    OutputNamespaces(namespaces);
                    Console.WriteLine();
                }
                catch (SyntaxException ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("^".PadLeft((int)ex.Position + 1, '-'));
                    Console.WriteLine(ex);
                }
            }
        }