|
@@ -29,14 +29,14 @@ class AnalisadorSintatico {
|
|
try {
|
|
try {
|
|
this.pos++;
|
|
this.pos++;
|
|
this.consumeNewLines();
|
|
this.consumeNewLines();
|
|
- parseOpenCurly();
|
|
|
|
|
|
+ checkOpenCurly();
|
|
this.pos++;
|
|
this.pos++;
|
|
this.consumeNewLines();
|
|
this.consumeNewLines();
|
|
const globalVars = parseGlobalVariables();
|
|
const globalVars = parseGlobalVariables();
|
|
this.consumeNewLines();
|
|
this.consumeNewLines();
|
|
const functions = parseFunctions();
|
|
const functions = parseFunctions();
|
|
this.consumeNewLines();
|
|
this.consumeNewLines();
|
|
- parseCloseCurly();
|
|
|
|
|
|
+ checkCloseCurly();
|
|
this.pos++;
|
|
this.pos++;
|
|
this.consumeNewLines();
|
|
this.consumeNewLines();
|
|
if(this.lexer.EOF !== (token = this.getToken()).type) {
|
|
if(this.lexer.EOF !== (token = this.getToken()).type) {
|
|
@@ -53,20 +53,44 @@ class AnalisadorSintatico {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- parseOpenCurly () {
|
|
|
|
|
|
+ checkOpenCurly () {
|
|
let token = null;
|
|
let token = null;
|
|
if(this.lexer.ABRE_CHA !== (token = this.getToken()).type){
|
|
if(this.lexer.ABRE_CHA !== (token = this.getToken()).type){
|
|
throw SintaxError.createError(this.getErrorString('{', token));
|
|
throw SintaxError.createError(this.getErrorString('{', token));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- parseCloseCurly () {
|
|
|
|
|
|
+ checkCloseCurly () {
|
|
let token = null;
|
|
let token = null;
|
|
if(this.lexer.FECHA_CHA !== (token = this.getToken()).type){
|
|
if(this.lexer.FECHA_CHA !== (token = this.getToken()).type){
|
|
throw SintaxError.createError(this.getErrorString('}', token));
|
|
throw SintaxError.createError(this.getErrorString('}', token));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ checkOpenBrace (attempt = false) {
|
|
|
|
+ const token = this.getToken();
|
|
|
|
+ if(this.lexer.ABRE_COL !== token.type){
|
|
|
|
+ if (!attempt) {
|
|
|
|
+ throw SintaxError.createError(this.getErrorString('[', token));
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ checkCloseBrace (attempt = false) {
|
|
|
|
+ const token = this.getToken();
|
|
|
|
+ if(this.lexer.FECHA_COL !== token.type){
|
|
|
|
+ if (!attempt) {
|
|
|
|
+ throw SintaxError.createError(this.getErrorString(']', token));
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
parseGlobalVariables () {
|
|
parseGlobalVariables () {
|
|
let vars = [];
|
|
let vars = [];
|
|
while(true) {
|
|
while(true) {
|
|
@@ -79,7 +103,7 @@ class AnalisadorSintatico {
|
|
if (decl === null)
|
|
if (decl === null)
|
|
break;
|
|
break;
|
|
else
|
|
else
|
|
- vars.push(decl);
|
|
|
|
|
|
+ vars.concat(decl);
|
|
}
|
|
}
|
|
return vars;
|
|
return vars;
|
|
}
|
|
}
|
|
@@ -100,6 +124,7 @@ class AnalisadorSintatico {
|
|
this.pos++;;
|
|
this.pos++;;
|
|
return parseDeclararion(typeToken, true);
|
|
return parseDeclararion(typeToken, true);
|
|
} else if(isVariableType(constToken)) {
|
|
} else if(isVariableType(constToken)) {
|
|
|
|
+ this.pos++;
|
|
return parseDeclararion(constToken);
|
|
return parseDeclararion(constToken);
|
|
} else {
|
|
} else {
|
|
return null;
|
|
return null;
|
|
@@ -111,22 +136,49 @@ class AnalisadorSintatico {
|
|
* Parses a declarion of the form: type --- id --- (= --- EAnd)?
|
|
* Parses a declarion of the form: type --- id --- (= --- EAnd)?
|
|
* @returns Declararion(const, type, id, initVal?)
|
|
* @returns Declararion(const, type, id, initVal?)
|
|
**/
|
|
**/
|
|
- parseDeclararion (isConst = false) {
|
|
|
|
- const typeToken = this.getToken();
|
|
|
|
- if(!this.isVariableType(typeToken)) {
|
|
|
|
- throw SintaxError.createError(this.getCommaTypeString(), typeToken);
|
|
|
|
- }
|
|
|
|
- this.pos++;
|
|
|
|
|
|
+ parseDeclararion (typeToken, isConst = false) {
|
|
const idToken = this.getToken();
|
|
const idToken = this.getToken();
|
|
|
|
+ let initial = null;
|
|
|
|
+ let dim1 = null;
|
|
|
|
+ let dim2 = null;
|
|
if(idToken.type !== this.lexer.ID) {
|
|
if(idToken.type !== this.lexer.ID) {
|
|
throw SintaxError.createError('ID', idToken);
|
|
throw SintaxError.createError('ID', idToken);
|
|
}
|
|
}
|
|
this.pos++;
|
|
this.pos++;
|
|
|
|
+ // Check for array or vector
|
|
|
|
+ // ID[int/IDi][int/IDj]
|
|
|
|
+ if (this.checkOpenBrace(true)) {
|
|
|
|
+ this.pos++;
|
|
|
|
+ dim1 = this.getArrayDimension();
|
|
|
|
+ this.checkCloseBrace();
|
|
|
|
+ this.pos++;
|
|
|
|
+ if(this.checkOpenBrace(true)) {
|
|
|
|
+ this.pos++;
|
|
|
|
+ dim2 = this.getArrayDimension();
|
|
|
|
+ this.checkCloseBrace();
|
|
|
|
+ this.pos++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
const equalsToken = this.getToken();
|
|
const equalsToken = this.getToken();
|
|
if(equalsToken.type === this.lexer.ATRIBUICAO) {
|
|
if(equalsToken.type === this.lexer.ATRIBUICAO) {
|
|
- //process Expression
|
|
|
|
|
|
+ //process Expression(EAnd) => initial != null
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const commaToken = this.getToken();
|
|
|
|
+ if(commaToken.type === this.lexer.VIRGULA) {
|
|
|
|
+ this.pos++;
|
|
|
|
+ return [{
|
|
|
|
+ isConst: isConst,
|
|
|
|
+ tipo: typeToken.text,
|
|
|
|
+ id: idToken.text,
|
|
|
|
+ lines: dim1,
|
|
|
|
+ columns: dim2,
|
|
|
|
+ initial: initial
|
|
|
|
+ }]
|
|
|
|
+ .concat(this.parseDeclararion(typeToken, isConst));
|
|
} else {
|
|
} else {
|
|
- return {isConst: isConst, tipo: typeToken.text, id: idToken.text};
|
|
|
|
|
|
+ return [{isConst: isConst, tipo: typeToken.text, id: idToken.text. initial: initial}];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -142,6 +194,18 @@ class AnalisadorSintatico {
|
|
return this.variableTypes.find(v => v === token.type);
|
|
return this.variableTypes.find(v => v === token.type);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /*
|
|
|
|
+ * Reads the next token of the stream to check if it is a Integer or an ID.
|
|
|
|
+ * @returns Integer | ID
|
|
|
|
+ **/
|
|
|
|
+ getArrayDimension () {
|
|
|
|
+ const dimToken = this.getToken();
|
|
|
|
+ if(dimToken.type !== this.lexer.PR_INTEIRO || dimToken.type !== this.lexer.ID) {
|
|
|
|
+ throw SintaxError.createError('int or ID', dimToken);
|
|
|
|
+ }
|
|
|
|
+ return dimToken.text;
|
|
|
|
+ }
|
|
|
|
+
|
|
getCommaTypeString () {
|
|
getCommaTypeString () {
|
|
return this.variableTypes.map( x => this.lexer.literalNames[x])
|
|
return this.variableTypes.map( x => this.lexer.literalNames[x])
|
|
.reduce((o, n) => {
|
|
.reduce((o, n) => {
|