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
- Host: GitHub
- URL: https://github.com/syildizz/egglang-compiler-extended
- Owner: syildizz
- License: mit
- Created: 2024-08-06T09:03:30.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-08-13T08:05:15.000Z (almost 2 years ago)
- Last Synced: 2025-06-02T04:40:02.959Z (12 months ago)
- Topics: compiler, eloquent-javascript, javascript, recursive-descent-parser
- Language: JavaScript
- Homepage: https://eloquentjavascript.net/12_language.html
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.