This is a compiler for the Egg language that is mentioned in Chapter 12 of Eloquent Javascript. The compiler compiles the Egg language into javascript as defined in the original specification. It also adds some new features to the Egg language that did not exist in the original specification.
The code features here is built upon the original code by Marijn Haverbeke.
The javascript code which is generated by the EggLang Compiler is correctly indented and has semicolons inserted in the correct places for readability sake.
The compiler includes the extensions that are given in the exercises. Such as:
- Array support
- Comment support
- Set statement support
The compiler supports for statements in the form of
for(arg1, arg2, arg3, body)
which corresponds to the typical for loop in other languages where
for (arg1; arg2; arg3) {
body;
}The compiler supports a native statement. The native statement is used for writing inline javascript. It passed the expressions inside of it directly into the compiled code.
native(body)
becomes
bodyThe compiler supports an extern statement. The extern statement is used to define a label to the compiler that is outside of it's scope.
Normally, the compiler would throw an error when accessing a label that it does not recognize. The extern statement adds the label to the compiler so that it can properly recognize it. This statement is usually used to access global variables from javascript or from native statements.
extern(label)
becomes
// External label: labelNo code is generated since this statement is for the compiler only.
The compiler supports a push statement. The push statement is the Array.prototype.push method implemented for Egg language.
push(array, val)
becomes
array.push(val);For the most part, the compiler is compatible with the original Egg language interpreter mentioned in Eloquent Javascript. However, due to implementation details, there are some incompatibilities between the compiler here and the interpreter.
In the interpreter, the expressions if and while return the value 0. However, in the compiler, the compiler compiles them into if and while statements. Because of this, they do not return any value when compiled.