loading...

Inspired to write compiler

amir_rezai profile image Amir Mohammad Rezai Updated on ・3 min read

It's just an Idea:

Haxe

Haxe is simply a transpiler or source-to-source compiler written in OCaml, and transforms into multiple different codes of C++, Java, PHP, Javascript and...

When you have to use a library in different technologies like a server written in Java and a client written in C++ you can either use message passing or you can use a library which communicates with both. Haxe is the best option to write this cross communication library.

Compiler Idea

Haxe is written in OCaml it has many disadvantages yet it's not that bad. First it's recommended to read the disadvantages here.
Comparing OCaml with C, C is much faster.

hxcpp, hxjava, ...

Yet another haxe library is required to generate C++ code this goes the same for Java but the hxml file is not compatible and I don't like it personally.

Until this point I thought writing in haxe is bearable but the using foreign functions in haxe is not so nice, the point is that assembly is not even on the list. it's not recommended to generate asm code as long as the overhead of generating it is the same as C but at least we need a foreign functioning of assembly.

Write once then add platform specific extensions

I want to design the same thing as Adobe did with AIR but those who have been through writing native extensions have experienced the difficulties of writing native extensions for it.

It's so nice to write the same thing to write a code once and then add it like extension to the compiler.

This is a small part of my thought:

#if java
@:extends("java"){
  import java.lang.System;
  public function say_hello_in_java(){
    System.out.println("Hello Java");
  }
}
#end

Also extending binaries:

class Point() {
  private int x;
  private int y;

  public norm function new(int x, int y){
    this.x = x;
    this.y = y;
  }
  #if asm("x86")
    @:section("text"){
      mov [this.x], eax
    }
  #end
}

Before I explain another fine idea I would like to show the design of build system.

build.json

{
 "bin": {
    "x86_win": {
       "out": "./bin/windows.exe",
       "libs": ["/C/hello.dll"],
       "includes": ["/C/hello.h"],
       "signing": { ??? }
     },
     "x86": {
       "..."
     },
     "machO": {
       "..."
     },
     "arm": {
       "..."
     },
     "arm64": {
       "..."
     },
     "alpha": {
       "..."
     }
  }
}

Although this design seems so idealistic but I think it is the best just requires to generate C code and use Cross-Compilers with defined flags.

Also these are for advanced users so they will know how to create that library they are willing to cross compile.

Now let's see how does the idea of foreign function has to work here:
A library of hello with hello.h and hello.c is written as bellow:

#include <stdio.h>
#include <hello.h>

void say_hello() {
  printf("Hello from C!");
}

Also java class:

class Hello {
  public void say_hello() {
    System.out.println("Hello from Java!");
  }
}

If you link it right in the build file if we build it as 'hello.so' and include java class as 'Hello.class' then you can use it like this:

public static class SayHello(){
  #if c
  public function extC():void = @externC("dynamic", "hello", "say_hello");
  #end
  #if java
  public function extJava():void = @externJava("class", "Hello", "say_hello"); 
  #end
}

@externC args are:
$1 -> library type.
$2 -> library name.
$3 -> function name.

@externJava args are:
$1 -> library type. which can be jar or class.
$2 -> class name.
$3 -> function name.

Isn't that nice? It just requires some patience yet haxe proved that this kind of building is possible.

Posted on Dec 12 '19 by:

amir_rezai profile

Amir Mohammad Rezai

@amir_rezai

I'm a game developer, with performance issues in mind. Live in Nerverland, Iran.

Discussion

markdown guide