OneCompiler

basic compiler thing

1667

documentation is non existant

here's an example of an input

void thing args endarg
print " space_exists " endprint .
endvoid
endfunc
endfuncs

any i = 0 .

if i > 0 then {
print " yes " endprint .
} else {
print " no " endprint .
}

thing ( ) .

endfile

output

#include <iostream>
#include <stdio.h>
#include <cmath>

using namespace std;

int thing() {
	cout << "space exists" << endl;
	return 0;

}
int main()
{
	
	auto i = 0;
	
	if (i > 0) 
	{
		cout << "yes" << endl;
	}
	else
	{
		cout << "no" << endl;
	}
	
	thing (  ) ;
	
}

actual code

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        //pretty
        bool[] pbools = new bool[] {};
        pbools = pbools.Append<bool>(false).ToArray(); //tabs
        pbools = pbools.Append<bool>(false).ToArray(); //function tabs
        
        
        string[] vars = new string[] {};
        string[] functions = new string[] {};
        
        List<string> lines = new List<string>();
        List<string> funcs = new List<string>();
        
        string input;

        while (true)
        {
            input = Console.ReadLine();

            // Check if the input is empty to stop reading
            if (input == "endfuncs")
            {
                break;
            }

            funcs.Add(input);
        }
        
        List<string> output = new List<string>();

        output.Add("#include <iostream>");
        output.Add("#include <stdio.h>");
        output.Add("#include <cmath>\r\n");
        output.Add("using namespace std;\r\n");
        
        
        foreach (var line in funcs)
        {
            string[] tokens = line.Split(" ");
            string[] nums = new string[10];
            
            for (int i = 0; i < 10; i++)
              nums[i] = Convert.ToString(i);
            
            string l = "";
            bool is_string = false;
            bool var_dec = false;
            bool is_comment = false;
            bool is_if = false;
            bool is_dec = false;
            
            
            
            //Console.WriteLine("[{0}]", string.Join(", ", tokens));
            foreach (var i in tokens)
            {
              
              switch (i)
              {
                case "print":
                  l += "cout << ";
                  break;
                case "endprint":
                  l += " << endl";
                  break;
                case ".":
                  l += ";";
                  break;
                case ",":
                  l += ",";
                  break;
                case ";":
                  l += ";";
                  break;
                case "any":
                  l += "auto ";
                  var_dec = true;
                  break;
                case "if":
                  l += "if (";
                  is_if = true;
                  break;
                case "then":
                  l += ") ";
                  is_if = false;
                  l += "\r\n\t";
                  pbools[0] = true;
                  break;
                case "=":
                  l += " = ";
                  break;
                case "{":
                  l += "{";
                  break;
                case "}":
                  l += "}";
                  pbools[0] = false;
                  break;
                case "else":
                  l += "\r\n\telse";
                  l += "\r\n\t";
                  pbools[0] = true;
                  break;
                case "while":
                  l += "while (";
                  break;
                case "\"":
                  l += "\"";
                  is_string = !is_string;
                  break;
                case "":
                  l += "";
                  break;
                case "//":
                  l += "//";
                  is_comment = true;
                  break;
                case "+=":
                  l += "+= ";
                  break;
                case "-=":
                  l += "-= ";
                  break;
                case "++":
                  l += " ++";
                  break;
                case "--":
                  l += " --";
                  break;
                
                case "or":
                  l += " || ";
                  break;
                case "and":
                  l += " && ";
                  break;
                
                case "<":
                  l += " < ";
                  break;
                case ">":
                  l += " > ";
                  break;
                case "is":
                  l += " == ";
                  break;
                
                case "void":
                  l += "int ";
                  is_dec = true;
                  pbools[1] = true;
                  break;
                case "return":
                  l += "return ";
                  break;
                case "args":
                  l += "(";
                  is_dec = false;
                  break;
                case "endarg":
                  l += ") {";
                  break;
                case "endvoid":
                  l += "return 0;";
                  break;
                case "endfunc":
                  l += "\r\n}";
                  pbools[1] = false;
                  break;
                  
                case "sqrt":
                  l += "sqrt(";
                  break;
                case "abs":
                  l += "abs(";
                  break;
                case "floor":
                  l += "floor(";
                  break;
                case "ceil":
                  l += "ceil(";
                  break;
                case "endmath":
                  l += ")";
                  break;
                
                default:
                  if (is_string) {
                    l += i.Replace('_', ' ');
                  } else if (var_dec) {
                    l += i;
                    vars = vars.Append<string>(i).ToArray();
                    var_dec = false;
                  } else if (is_comment) {
                    l += i;
                    l += " ";
                  } else if (is_if) {
                    l += i;
                  } else if (vars.Contains(i)) {
                    l += i;
                  } else if (is_dec) {
                    functions = functions.Append<string>(i).ToArray();
                    l += i;
                  } else {
                    try {
                      int digit = Convert.ToInt32(i);
                      l += i;
                    } catch (Exception err) {
                      Console.WriteLine("[{0}]", string.Join(", ", vars));
                      Console.WriteLine("INVALID TOKEN: {0}", i);
                      System.Environment.Exit(1);
                    }
                  }
                  break;
              }
            }
            if (pbools[0]) {
              if (!l.Contains("if") 
              && !l.Contains("else") 
              && !l.Contains("while"))
                l = "\t" + l;
            }
            if (pbools[1]) {
              if (!l.Contains("() {"))
                l = "\t" + l;
            }
            
            output.Add(l);
            
        }
        
        
        
        
        
        
        output.Add("int main()");
        output.Add("{");

        while (true)
        {
            input = Console.ReadLine();

            // Check if the input is empty to stop reading
            if (input == "endfile")
            {
                break;
            }

            lines.Add(input);
        }
        
        foreach (var line in lines)
        {
            string[] tokens = line.Split(" ");
            string[] nums = new string[10];
            
            for (int i = 0; i < 10; i++)
              nums[i] = Convert.ToString(i);
            string l = "";
            bool is_string = false;
            bool var_dec = false;
            bool is_comment = false;
            bool is_if = false;
            
            
            
            //Console.WriteLine("[{0}]", string.Join(", ", tokens));
            foreach (var i in tokens)
            {
              
              switch (i)
              {
                case "print":
                  l += "cout << ";
                  break;
                case "endprint":
                  l += " << endl";
                  break;
                case ".":
                  l += ";";
                  break;
                  break;
                case ",":
                  l += ",";
                  break;
                case ";":
                  l += ";";
                  break;
                case "any":
                  l += "auto ";
                  var_dec = true;
                  break;
                case "if":
                  l += "if (";
                  is_if = true;
                  break;
                case "then":
                  l += ") ";
                  is_if = false;
                  l += "\r\n\t";
                  pbools[0] = true;
                  break;
                case "=":
                  l += " = ";
                  break;
                case "{":
                  l += "{";
                  break;
                case "}":
                  l += "}";
                    pbools[0] = false;
                  break;
                case "else":
                  l += "\r\n\telse";
                  l += "\r\n\t";
                  pbools[0] = true;
                  break;
                case "while":
                  l += "while (";
                  break;
                case "\"":
                  l += "\"";
                  is_string = !is_string;
                  break;
                case "":
                  l += "";
                  break;
                case "//":
                  l += "//";
                  is_comment = true;
                  break;
                case "+=":
                  l += " += ";
                  break;
                case "-=":
                  l += " -= ";
                  break;
                case "++":
                  l += "++";
                  break;
                case "--":
                  l += "--";
                  break;
                
                case "or":
                  l += " || ";
                  break;
                case "and":
                  l += " && ";
                  break;
                
                case "<":
                  l += " < ";
                  break;
                case ">":
                  l += " > ";
                  break;
                case "is":
                  l += " == ";
                  break;
                  
                case "(":
                  l += " ( ";
                  break;
                case ")":
                  l += " ) ";
                  break;
                  
                case "sqrt":
                  l += "sqrt(";
                  break;
                case "abs":
                  l += "abs(";
                  break;
                case "floor":
                  l += "floor(";
                  break;
                case "ceil":
                  l += "ceil(";
                  break;
                case "endmath":
                  l += ")";
                  break;
                
                default:
                  if (is_string) {
                    l += i.Replace('_', ' ');
                  } else if (var_dec) {
                    l += i;
                    vars = vars.Append<string>(i).ToArray();
                    var_dec = false;
                  } else if (is_comment) {
                    l += i;
                    l += " ";
                  } else if (is_if) {
                    l += i;
                  } else if (vars.Contains(i)) {
                    l += i;
                  } else if (functions.Contains(i)) {
                    l += i;
                  } else {
                    try {
                      int digit = Convert.ToInt32(i);
                      l += i;
                    } catch (Exception err) {
                      Console.WriteLine("[{0}]", string.Join(", ", vars));
                      Console.WriteLine("INVALID TOKEN: {0}", i);
                      System.Environment.Exit(1);
                    }
                  }
                  break;
              }
            }
            if (pbools[0]) {
              if (!l.Contains("if") && !l.Contains("else") && !l.Contains("while"))
                l = "\t" + l;
            }
            
            output.Add("\t" + l);
        }
        
        output.Add("}");
        
        Console.WriteLine("   ");
        
        Console.WriteLine("{0}", string.Join("\r\n", output));
        
        for (int i = 0; i < 20; i++)
          Console.WriteLine("   ");
    }
}