- La gramática se encuentra en
grammar/grammar.pegjs
- Para compilar la gramática, existe una tarea en el Rakefile o directamente
pegjs -o ./public/grammar.js ./grammar/grammar.pegjs
(El archivo ya se encuentra compilado en./public/grammar.js
) - Se puede desplegar un servidor local mediante
node app.js
o utilizar el despliegue de Heroku. - Las pruebas son ejecutas al desplegar la aplicación web. En la pestaña Tests.
Airam Manuel Navas Simón | GitHub | Página personal | Heroku |
Kevin Días Marrero | GitHub | Página personal | Heroku |
Jorge Sierra Acosta | GitHub | Página personal | Heroku |
- La descripción del lenguaje se encuentra en `./Rules.md`.
-
Las sentencias pueden ser asignaciones, funciones o declaraciones.
-
Las funciones se declaran de la siguiente forma. Pueden ser declaradas en cualquier momento y accedidas globalmente:
<type> ID(ID, ID, ...) { ... return ...; }
Por ejemplo:
void test(x){ x = 3; } numeric foo() { return 3; }
-
Condicionales:
if condicion { ... } if condition { ... } else if condition { ... } else if condition { ... } else { ... }
-
Bucles:
for (#1; #2; #3) { ... }[else {...}] while (parcondition) { ... }[else {...}] // #1 => Operaciones que se ejecutan antes de entrar al bucle. // #2 => Condición que se debe cumplir para que continue el bucle. // #3 => Operaciones que se ejecutan cada vez que se itera sobre el bucle.
Por ejemplo:
for (i = 0; i < 3; i = i + 1) { ... } else { ... } while (i < 50) { ... }
-
La asignación puede se puede realizar a cualquier tipo de expresión Dichas asignaciones se declaran de la siguiente forma:
y = 5; x = 3 * 2; z = foo( 3 * 4) * 4; h = 1 > 2;
No es necesario declarar las variables previamente para que la asignación se produzca.
-
Las condiciones toman valor true o false. Por ejemplo:
condition1 = TRUE condition2 = i < 5
-
Podemos declarar atributos de la clase de la siguiente forma:
<visibility> <returnType> <name> = [<value> | <constructor>];
Por ejemplo:
public numeric x = 7; private otraClase y = otraClase.init();
-
Para declarar métodos de una clase hacemos lo siguiente:
<visibility> <functionDeclare>;
public void x() {} private numeric y(numeric a) { return a*7; }
-
Para declarar una clase seguiremos la siguiente sintáxis:
class <name> { [<atributos> | <metodos> ]* }
Por ejemplo:
class A { public numeric n = 0; } class B { public A a = A.init(); } class C { public B b = B.init(); }
El árbol sintáctico generado contendrá los siguientes atributos.
Atributo | Descripción |
---|---|
result | Contiene el código |
symbolTable | Contiene información sobre los símbolos globales de variables del programa |
functionTable | Contiene información sobre los símbolos que representan funciones y su propia tabla de símbolos locales |
initialConstantTable | Contiene información sobre las constantes predefinidas y sus valores (PI, TRUE, FALSE, ...) |
reservedWords | Conjunto de palabras reservadas |
-
Código simple donde declaramos un array y un numeric:
string[][] c = {"a", "b"}; numeric k = 0;
Árbol resultado:
{ "builtInTypes": [ "numeric", "bool", "string", "void" ], "reservedWords": [ "else", "if", "while", "for", "const", "numeric", "bool", "string", "void", "public", "private", "class", "true", "false", "return" ], "symbolTable": { "PI": 3.14159265359, "TRUE": true, "FALSE": false }, "result": [ { "type": "declaration", "constant": false, "varType": { "array": "true", "arrayCount": 2, "type": "string" }, "assignations": [ { "id": "c", "to": [ { "type": "string", "value": "a" }, { "type": "string", "value": "b" } ] } ] }, { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "k", "to": { "type": "numeric", "value": 0 } } ] } ] }
-
Ejemplo variado donde aparece una funcion en la cual creamos variables, dos bucles de ambos tipos (while y for), y dos condiciones if diferentes:
numeric function(string a, numeric d) { numeric l = 0; for (numeric i = 0; i < 10; i = i + 1) { } else { numeric x = 0; } while (TRUE) { numeric xx = 1; } else { numeric ll = 11; } if (TRUE) { numeric a = 1; } else { numeric d = 1; } if (TRUE) { numeric a = 1; } else if (TRUE) { numeric x = 0; a = "9"; } }
Árbol resultado:
{ "builtInTypes": [ "numeric", "bool", "string", "void" ], "reservedWords": [ "else", "if", "while", "for", "const", "numeric", "bool", "string", "void", "public", "private", "class", "true", "false", "return" ], "symbolTable": { "PI": 3.14159265359, "TRUE": true, "FALSE": false }, "result": [ { "type": "function", "returnType": "numeric", "functionName": "function", "params": [ { "type": "parameter", "vartype": "string", "id": "a" }, { "type": "parameter", "vartype": "numeric", "id": "d" } ], "contents": { "type": "block", "contents": [ { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "l", "to": { "type": "numeric", "value": 0 } } ] }, { "id": 0, "type": "for", "start": { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "i", "to": { "type": "numeric", "value": 0 } } ] }, "check": { "type": "condition", "op": "<", "left": { "type": "id", "id": "i" }, "right": { "type": "numeric", "value": 10 } }, "iterate": { "type": "assign", "assignations": [ { "element": "i", "to": { "type": "expression", "op": "+", "left": { "type": "id", "id": "i" }, "right": { "type": "numeric", "value": 1 } } } ] }, "contents": { "type": "block", "contents": [] }, "else": { "type": "block", "contents": [ { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "x", "to": { "type": "numeric", "value": 0 } } ] } ] } }, { "id": 1, "type": "while", "check": { "type": "bool", "value": "true" }, "contents": { "type": "block", "contents": [ { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "xx", "to": { "type": "numeric", "value": 1 } } ] } ] }, "else": { "type": "block", "contents": [ { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "ll", "to": { "type": "numeric", "value": 11 } } ] } ] } }, { "id": 2, "type": "if", "ifCode": { "check": { "type": "bool", "value": "true" }, "contents": { "type": "block", "contents": [ { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "a", "to": { "type": "numeric", "value": 1 } } ] } ] } }, "elseIfCode": [], "elseCode": { "type": "block", "contents": [ { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "d", "to": { "type": "numeric", "value": 1 } } ] } ] } }, { "id": 3, "type": "if", "ifCode": { "check": { "type": "bool", "value": "true" }, "contents": { "type": "block", "contents": [ { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "a", "to": { "type": "numeric", "value": 1 } } ] } ] } }, "elseIfCode": [ { "check": { "type": "bool", "value": "true" }, "contents": { "type": "block", "contents": [ { "type": "declaration", "constant": false, "varType": "numeric", "assignations": [ { "id": "x", "to": { "type": "numeric", "value": 0 } } ] }, { "type": "assign", "assignations": [ { "element": "a", "to": { "type": "string", "value": "9" } } ] } ] } } ], "elseCode": null } ] } } ] }
-
Ejemplo de la declaración de una clase:
class A {}
Árbol resultado:
{ "builtInTypes": [ "numeric", "bool", "string", "void" ], "reservedWords": [ "else", "if", "while", "for", "const", "numeric", "bool", "string", "void", "public", "private", "class", "true", "false", "return" ], "symbolTable": { "PI": 3.14159265359, "TRUE": true, "FALSE": false }, "result": [ { "type": "class", "id": "A", "content": { "type": "classBlock", "classStatement": [] } } ] }