EGG Functional Language Module
Author: Aaron Jose Cabrera Martin
This module contains a compiler and an interpreter of the EGG lenguage decribed on the Eloquent JavaScript Book with a few modifications for expand the lenguage.
Besides, it contains three executables, egg.js, eggc.js and evm.js on the bin folder.
- eggc.js: compiles an egg program and produces the .evm equivalent file. This file can be executed by the evm program.
- evm.js: executes an egg compiled program, the file must be on evm format (JSON).
- egg.js: executes and compiles an egg program automatically. The file must be on plane text format.
This module is open for expansions, anyone can program a plugin and extend the program, but the main lenguague will be the same.
Installation
npm intall @ULL-ESIT-PL-2021/egg-aaronjosecabreramartin
Example of use module
const { runFromFile } = require('@ULL-ESIT-PL-2021/egg-aaronjosecabreramartin');
runFromFile('./eggProgram.egg');
And the eggProgram.egg file contains:
do(
define(x,y,5); # x = 5, y = 5
set(y,+(y,1)); # y = y+1
print(*(x,y)); # 5*6 --> 30
);
This simple program runs an egg code from a file, we also can use the run function wich runs an egg program from a given string.
Example of use executables
On the command line:
egg file.egg
This command will executes the file 'file.egg'.
eggc file.egg
This command will compile the file 'file.egg' and produces the 'file.egg.evm' wich is the compiled version of the program. The produced file is stored on the same directory of the given file.
evm file.egg.evm
This command will execute a previous egg compiled program.
Example of a lenguage plugin
This module is open to extensions, you can extend the specialForms map of the topScope map for add new functionalities on the module.
For example:
const { run, topScope } = require('@ULL-ESIT-PL-2021/egg-aaronjosecabreramartin');
topScope['^'] = function (base, power){
return Math.pow(base,power);
}
run(`print(^(2,3))`); // will print 8
It's important distinguish topScope and specialForms. On the specialForms hash must be stored instructions that change the way that the egg virtual machine execute the AST of an egg program. In the other hand, the topScope hash must be stored instructions that works with values of the internal machine (JavaScript).
Documentation
Please consider visit de official documentation page
Lenguage Grammar
My implementation of EGG follows this grammar:
expression -> SPACE | NEWLINE | STRING | NUMBER | WORD apply
apply -> LP RP | LP (expression COMMA)* expression RP
SPACE -> /(?<SPACE>(\s|#.*)+)/;
NEWLINE -> /(?<NEWLINE>\r\n|\n|\r)/;
STRING -> /"(?<STRING>(?:[^"\\]|\\.)*)"/;
NUMBER -> /(?<NUMBER>[+-]?((\d+\.\d+)|(\d+))\b)/;
LP -> /(?<LP>\()/;
RP -> /(?<RP>\))/;
COMMA -> /(?<COMMA>,)/;
WORD -> /(?<WORD>[^\s(),#"]+)/;
Bugs and Reports
Use the main repository of the proyect for report bugs.
Version History
- 0.0.1 - First release, module and executables creations.