Custom Compilers


Currently code generation in Korduene is string based, which means code is generated in plain text and you can write your code in plain text, so you can create custom compilers for any language Using Korduene SDK that is in contrast with your custom projects. For creating custom compilers, CompilerBase class is provided that gives you all the needed interface to create your own compiler.

Below is an example of a custom C++ compiler:

Custom C++ Compiler (using MinGW compiler)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CppProject
{
    class CppCompiler : Korduene.Compilers.CompilerBase
    {
        public override void Compile(Korduene.Project.ProjectBase project, bool modify = false)
        {
            base.Compile(project, modify);
            CompileMessage("Compiling project...", 30);
            string code = GenerateCode();
            CodeGenerated(code);
            
            string file = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "test.cpp");
            System.IO.File.WriteAllText(file, code);
            CompileMessage("Running MinGW compiler...", 30);
            RunMinGW(file);
            
            CompileEnded();
        }
        public override string GenerateCode()
        {
            CompileMessage("Generating code...");
            string code = "#include <windows.h>\r\n#include <stdio.h>\r\n";
            foreach (var file in System.IO.Directory.GetFiles(Project.Directories.Graphs, "*" + Korduene.KordueneCore.FileExtensions.GRAPH))
            {
                Korduene.KGraph.Graph graph = new Korduene.KGraph.Graph(Korduene.KGraph.GraphMode.Compile);
                graph.LoadGraph(file);
                code += GetGraphCode(graph);
            }
            return code;
        }
        public override string GetGraphCode(Korduene.KGraph.Graph graph)
        {
            string code = string.Empty;
            foreach (var node in graph.Nodes)
            {
                if (node.NodeType == Korduene.KGraph.NodeType.Function)
                {
                    code += node.ReturnType.Name.ToUpper() + " " + node.ID + "(" + node.Parameters.ToString(false, true) + ")\r\n{\r\n" + node.Code() + "\r\n}\r\n";
                }
            }
            return code;
        }
        private void RunMinGW(string file)
        {
            string args = @"cd C:\MinGW\bin && c++.exe -static-libgcc " + file + " -o " + Project.ExecutablePath;
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C" + args;
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
            if (System.IO.File.Exists(Project.ExecutablePath))
            {
                System.Diagnostics.Process.Start(Project.ExecutablePath);
            }
        }
    }
}</stdio.h></windows.h>