Friday, July 19, 2019

ZeptoN Echo Compiler

ZeptoN Echo Compiler July 19, 2019

Eureka and shiver my timbers! I have implemented a working prototype compiler for ZeptoN. This compiler I call the "echo" class compiler. The compiler is a transcompiler, no lexer, parser. The transcompiler compiles the ZeptoN source code into Java source code.

Echo...Echo...Echo

The "echo" is that, except for the ZeptoN keywords, all source code is "echoed" into the Java source code that is compiled.

The source to bytecode binary uses the Java Compiler API. I originally developed a simpler Java compiler "WEJAC" that I revamped into an in-memory compiler "WEIMJAC" that uses a Java source code object.

I then used the "WEIMJAC" compiler to implement the ZeptoN compiler--"zep" for the file extension ".zep" used for ZeptoN source code. The only difficulty was to create a method "transcompile()" to transform the ZeptoN source code into Java code.

The Challenge of Error Integrity

The challenge was to maintain the line numbers so that an errors reported actually show the ZeptoN source line, and code fragment.

Originally I "chomped" (to use a PERL term) the comments and whitespaces...but it obfuscated the line numbers for error messages.

The Main Challenge

One difficulty is if a "static void main" method is used. Since the block of code from "begin" to the closing brace is transcompiled into a "main" method, a duplicate main method is problematic.

A friend suggested a colorful error message:

    static void main
    ^^^---------------- We don't allow this here!
   
But, that is not very helpful. A newbie must not use a specific method without knowing why it is an error. Also a "main" method might be useful when writing a ZeptoN program.

The solution is to "mangle" a "main" method into a valid identifier, and any references to the existing main method before the auto-generated main method is created.

Compiler Options

Since the ZeptoN echo compiler is built upon the WEJAC compiler, it has the same options (I'm considering adding some more...) which means few. WEJAC was designed for simplicity, so the ZeptoN compiler also has fewer options.

Example ZeptoN Program

This 20-line ZeptoN program grabs a web page, and then dumps it to the screen, console, or terminal.

/*
ZeptoN Compiler Options: [-g] Files: [FetchURL.zep] Encoding: UTF-8

Time: 614-ms for: FetchURL.zep

ZeptoN Compiler result for file: 'FetchURL.zep' is: Success.

ZepC]$ls -l FetchURL.*

-rw-r--r--  1 williamgilreath  staff  9875 Jul 17 06:12 FetchURL.class
-rwxr-xr-x@ 1 williamgilreath  staff   706 Jul 17 06:13 FetchURL.zep
*/
prog FetchURL{
begin
    StringBuilder content = new StringBuilder();
   
    URL url = new URL("https://wgilreath.github.io/WillHome.html");
    URLConnection urlConnection = url.openConnection();

    BufferedReader bufferedReader = new BufferedReader(

        new InputStreamReader(urlConnection.getInputStream()));
    String line = EMPTY_STRING;

    while ((line = bufferedReader.readLine()) != null){

        content.append(line + "\n");
    }//end while
    bufferedReader.close();
   
    println(content.toString());
    exit(0);
}//end prog FetchURL

The multi-line header comments show the output of the command given. The command is:

[ZepC]$zep -echo -time FetchURL.zep

The ZeptoN Echo compiler echoes the parameters given, and times the compilation for the ZeptoN program FetchURL.zep

Going Forward

I intend to open-source and release the source code for the ZeptoN transcompiler on GitHub.

If interested, e-mail if you want to contribute and/or participate. The more the merrier!











No comments:

Post a Comment

Note: Only a member of this blog may post a comment.