An open API service indexing awesome lists of open source software.

https://github.com/syildizz/egglang-compiler-extended

This is a compiler for the Egg language that is mentioned in Chapter 12 of Eloquent Javascript
https://github.com/syildizz/egglang-compiler-extended

compiler eloquent-javascript javascript recursive-descent-parser

Last synced: 11 months ago
JSON representation

This is a compiler for the Egg language that is mentioned in Chapter 12 of Eloquent Javascript

Awesome Lists containing this project

README

          

# EggLang Compiler Extended

This is a compiler for the Egg language that is mentioned in Chapter 12 of [Eloquent Javascript](https://eloquentjavascript.net/12_language.html).
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.

## Extensions

The compiler includes the extensions that are given in the exercises.
Such as:

* Array support
* Comment support
* Set statement support

### For statement

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

```js
for (arg1; arg2; arg3) {
body;
}
```

### Native statement

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

```js
body
```

### Extern statement

The 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

```js
// External label: label
```

No code is generated since this statement is for the compiler only.

### Push statement

The compiler supports a push statement. The push statement is the [_Array.prototype.push_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) method implemented for Egg language.

```
push(array, val)
```

becomes

```js
array.push(val);
```

# Incompatibility

For the most part, the compiler is compatible with the original Egg language interpreter mentioned in [Eloquent Javascript](https://eloquentjavascript.net/12_language.html).
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.