My compiler is COMPLETELY messed up, any help?

Emily Stafford 1 Reputation point
2022-02-13T19:03:02.417+00:00

So I have made a simple compiler in C#. It works by this. (website link) And here is my source code:
NOTICE: I get no errors it just does, well, it does not compile.
using System;
using System.IO;
using System.Text;

class Class
{
    static void Main()
    {
        string path = @"c:\temp\SCRIPT.vis";

        char previous;
        string all = "";
        string cmd = "";
        string scpy;
        int mode = 0;
        string aug2 = "";

        Stream s = new FileStream(@"data.vis", FileMode.Open);
        int val = 0;
        char ch;

        while (true)
        {
            val = s.ReadByte();

            if (val < 0)
                break;
            ch = (char)val;
            Console.Write(ch);
            if (mode == 0)
            {
                if (ch != '(')
                {
                    all = ch.ToString();


                }
                else
                {
                    mode = 1;
                    all = "";

                }
            }
            if (mode == 1)
            {
                cmd = all;
                all = "";
                all = ch.ToString();
            }
            if (ch == ',')
            {
                mode = 2;
                all = "";
            }
            if (mode == 2)
            {
                aug2 = all;
                all = ch.ToString();
            }
            if (ch == ')')
            {
                if (cmd == "embed")
                {
                    File.Copy("C:/Visix/Embeds/" + all, all);
                }
                if (cmd == "copy")
                {
                    File.AppendAllText(@"main.vis", "File.Copy(" + all + ", " + aug2 + ");" + Environment.NewLine);
                }
                if (cmd == "using")
                {
                    File.AppendAllText(@"main.vis", "using " + all + Environment.NewLine);
                }
                if (cmd == "fileAppend")
                {
                    File.AppendAllText(@"main.vis", "File.AppendAllText(@" + all + ',' + aug2 + " + Environment.NewLine);" + Environment.NewLine);
                }
                if (cmd == "print")
                {
                    Console.WriteLine(all);
                }

            }
        }
        Console.WriteLine();
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,818 questions
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,656 Reputation points
    2022-02-13T20:38:08.787+00:00

    Ah I see - when you have a print call in your data.vis it doesn't print anything in the compiler program, but when you call copy from your compiler program it does create main.vis but the file contains File.Copy("@"C:/test.txt", "C:/output.txt");?

    There's an extra leading double quote there before the @ - is that the issue you're trying to solve?

    I've not done much compiler work myself but from what I've seen the common pattern is to split the process into "lexing" to find meaningful tokens from the stream of characters, then "parsing" to produce language constructs based on the tokens.

    Here's how I'd be tempted to structure it: https://pastebin.com/xbtv2WYG

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.