2

i am programming my own programming language in c# and i am using CompileAssemblyFromSource to achieve this goal.

I am creating a string that contains a function that will be called after i add all the interpreted code.

The main class in my project turns the syntax of the new language into c# code and puts it into that string line by line.

so far so good, it works excellently.

now because the language is supposed to be extremely "high level" and is just a fancy way of writing more complex code, im writing custom functions in c# that my language uses when interpreting it.

for example in my language a line being "DO FANCY" (not really a thing) would print all prime numbers from 0-1000, so i create a function called "prime()" which Outputs all these numbers when called,and interpret "DO FANCY" into "prime()".

now here my problem lays, to add these custom functions to the string i mentioned above (so the interpreted code can access them) i need to add them line by line to the string, this has proven to be very awkward and cringeworthy to me so i thought "there must be a way to simply code all these functions inside a class, and add that class to the "resources string[]" CompileAssemblyFromSource uses.

appreantly i was wrong, and therefor after getting a headache from this problem i thought of maybe asking it here.

i tried:

1) finding a way to turn a class into a string this proven to be impossible because that would require me to decompile the class while executing the main program binary.

2) reading all these functions with I/O from a local directory, i highly dislike this because that means i will need to carry files around and cant keep it all in 1 executeable

here is what adding functions to my language looks like if you wondered why this is so horrible:

        code += "public string[] Combine(string[][] srar,string bywhat)";
        code += Environment.NewLine;
        code += "{";
        code += Environment.NewLine;
        code += "int max = 0;";
        code += Environment.NewLine;
        code += "for (int i = 0; i<srar.Length; i++)";
        code += Environment.NewLine;
        code += "{";
        code += Environment.NewLine;
        code += "if (srar[i].Length > max)";
        code += Environment.NewLine;
        code += "max = srar[i].Length;";
        code += Environment.NewLine;
        code += "}";
        code += Environment.NewLine;
        code += "string[] tempo = new string[max];";
        code += Environment.NewLine;
        code += "string sinline = \"\";";
        code += Environment.NewLine;
        code += "for (int i = 0; i < max; i++)";
        code += Environment.NewLine;
        code += "{";
        code += Environment.NewLine;
        code += "for (int j = 0; j < srar.Length; j++)";
        code += Environment.NewLine;
        code += "{";
        code += Environment.NewLine;
        code += "if (i < srar[j].Length)";
        code += Environment.NewLine;
        code += "sinline += srar[j][i];";
        code += Environment.NewLine;
        code += "if (j != (srar.Length - 1))";
        code += Environment.NewLine;
        code += "sinline += bywhat;";
        code += Environment.NewLine;
        code += "}";
        code += Environment.NewLine;
        code += "tempo[i] = sinline;";
        code += Environment.NewLine;
        code += "sinline = \"\";";
        code += Environment.NewLine;
        code += "}";
        code += Environment.NewLine;
        code += "return tempo;";
        code += Environment.NewLine;
        code += "}";
        code += Environment.NewLine;

here is the code i use to compile and run my code

using (Microsoft.CSharp.CSharpCodeProvider CodeProv =
        new Microsoft.CSharp.CSharpCodeProvider())
        {
            CompilerResults results = CodeProv.CompileAssemblyFromSource(
                 new System.CodeDom.Compiler.CompilerParameters()
                 {
                     GenerateInMemory = true
                 },
                 code);

             var type = results.CompiledAssembly.GetType("MainClass");

             var obj = Activator.CreateInstance(type);

             var output = type.GetMethod("Execute").Invoke(obj, new object[] { });

             Console.WriteLine(output);
        }

thank you

ThatOneLad
  • 51
  • 2

0 Answers0