Example #1
0
        public void Loop(bool debug = false)
        {
            var interpreter = new Interpreter.Interpreter();
            var resolver    = new Resolver.Resolver(interpreter);

            int    openBrackets = 0;
            string buffer       = "";

            while (true)
            {
                Console.Write("$> ");

                buffer += Console.ReadLine();

                char lastCharacter = buffer[buffer.Length - 1];
                if (lastCharacter == '{')
                {
                    ++openBrackets;
                }
                else if (lastCharacter == '}' && openBrackets > 0)
                {
                    --openBrackets;
                }

                if (openBrackets == 0)
                {
                    var tokens = new Tokenizer(buffer).Tokenize();

                    if (debug)
                    {
                        DebugTokens(tokens);
                    }

                    var parser = new Parser.Parser(tokens);

                    var parserResult = parser.Run();
                    if (parserResult.Ok)
                    {
                        resolver.Resolve(parserResult.Statements);
                        interpreter.Run(parserResult.Statements);
                    }

                    buffer = "";
                }
            }
        }
Example #2
0
        public void FromFile(string filePath)
        {
            var sourceCode = File.ReadAllText(filePath);

            var tokens = new Tokenizer(sourceCode).Tokenize();

            var parser      = new Parser.Parser(tokens);
            var interpreter = new Interpreter.Interpreter();

            var parserResult = parser.Run();

            if (parserResult.Ok)
            {
                new Resolver.Resolver(interpreter).Resolve(parserResult.Statements);
                interpreter.Run(parserResult.Statements);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var examplesDir = new DirectoryInfo("Examples");
            var interpreter = new Interpreter.Interpreter();

            foreach (var exmapleFile in examplesDir.GetFiles("*.vec"))
            {
                Console.Write($"Parsing file '{exmapleFile.Name}'...");
                var result = interpreter.Run(exmapleFile);

                if (result.Errors.Count == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(" DONE!");
                    Console.ResetColor();

                    var svgFile = Path.Join(
                        exmapleFile.DirectoryName,
                        Path.GetFileNameWithoutExtension(exmapleFile.Name) + ".svg"
                        );

                    File.WriteAllText(svgFile, result.Output);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(" FAIL!");
                    Console.ResetColor();

                    foreach (var error in result.Errors)
                    {
                        Console.WriteLine(error);
                    }
                }
            }
        }