semanticAnalyser.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { ProcessorErrorFactory } from './../error/processorErrorFactory';
  2. import { LanguageDefinedFunction } from './../definedFunctions';
  3. import { LanguageService } from './../../services/languageService';
  4. import { ArrayDeclaration } from '../../ast/commands';
  5. import { InfixApp, UnaryApp, FunctionCall, IntLiteral, RealLiteral, StringLiteral, BoolLiteral, VariableLiteral, ArrayLiteral } from '../../ast/expressions';
  6. import { Literal } from '../../ast/expressions/literal';
  7. import { resultTypeAfterInfixOp, resultTypeAfterUnaryOp } from '../compatibilityTable';
  8. import { Types } from '../../ast/types';
  9. export class SemanticAnalyser {
  10. constructor(ast) {
  11. this.ast = ast;
  12. this.lexerClass = LanguageService.getCurrentLexer();
  13. const lexer = new this.lexerClass(null);
  14. this.literalNames = lexer.literalNames;
  15. this.symbolMap = null;
  16. }
  17. pushMap () {
  18. if(this.symbolMap === null) {
  19. this.symbolMap = {map:{}, next: null};
  20. } else {
  21. const n = {map:{}, next: this.symbolMap};
  22. this.symbolMap = n;
  23. }
  24. }
  25. popMap () {
  26. if(this.symbolMap !== null) {
  27. this.symbolMap = this.symbolMap.next;
  28. }
  29. }
  30. insertSymbol (id, typeInfo) {
  31. this.symbolMap.map[id] = typeInfo;
  32. }
  33. findSymbol (id, symMap) {
  34. if(!symMap.map[id]) {
  35. if(symMap.next) {
  36. return this.findSymbol(id, symMap.next);
  37. }
  38. throw new Error("variable not defined");
  39. } else {
  40. return symMap.map[id];
  41. }
  42. }
  43. findFunction (name) {
  44. if(name.match(/^\$.+$/)) {
  45. const fun = LanguageDefinedFunction[name];
  46. if(!!!fun) {
  47. throw new Error("!!!Internal Error. Language defined function not implemented -> " + name + "!!!");
  48. }
  49. return fun;
  50. } else {
  51. const val = this.ast.functions.find( v => v.name === name);
  52. if (!!!val) {
  53. // TODO: better error message;
  54. throw new Error(`Function ${name} is not defined.`);
  55. }
  56. return val;
  57. }
  58. }
  59. analyseTree () {
  60. const globalVars = this.ast.global;
  61. this.pushMap();
  62. this.assertDeclarations(globalVars);
  63. const functions = this.ast.functions;
  64. for (let i = 0; i < functions.length; i++) {
  65. const fun = functions[i];
  66. this.assertFunction(fun);
  67. }
  68. return this.ast;
  69. }
  70. assertDeclarations (list) {
  71. for (let i = 0; i < list.length; i++) {
  72. this.assertDeclaration(list[i]);
  73. }
  74. }
  75. assertDeclaration (declaration) {
  76. if (declaration instanceof ArrayDeclaration) {
  77. if(declaration.initial === null) {
  78. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type, subtype: declaration.subtype});
  79. return;
  80. }
  81. this.evaluateArrayLiteral(declaration.lines, declaration.columns, declaration.subtype, declaration.initial);
  82. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type, subtype: declaration.subtype});
  83. } else {
  84. if(declaration.initial === null) {
  85. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type});
  86. return;
  87. }
  88. const resultType = this.evaluateExpressionType(declaration.initial);
  89. if(declaration.type !== resultType) {
  90. throw new Error('Invalid type');
  91. } else {
  92. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
  93. }
  94. }
  95. }
  96. evaluateExpressionType (expression) {
  97. if(expression instanceof UnaryApp) {
  98. const op = expression.op;
  99. const resultType = this.evaluateExpressionType(expression.left);
  100. return resultTypeAfterUnaryOp(op, resultType);
  101. } else if (expression instanceof InfixApp) {
  102. const op = expression.op;
  103. const resultTypeLeft = this.evaluateExpressionType(expression.left);
  104. const resultTypeRight = this.evaluateExpressionType(expression.right);
  105. return resultTypeAfterInfixOp(op, resultTypeLeft, resultTypeRight);
  106. } else if (expression instanceof Literal) {
  107. return this.evaluateLiteralType(expression);
  108. } else if (expression instanceof FunctionCall) {
  109. const fun = this.findFunction(expression.id);
  110. if (fun.returnType === Types.VOID) {
  111. throw new Error("void return");
  112. }
  113. this.assertParameters(fun, expression.actualParameters);
  114. return fun.returnType;
  115. }
  116. }
  117. evaluateLiteralType (literal) {
  118. if(literal instanceof IntLiteral) {
  119. return literal.type;
  120. } else if (literal instanceof RealLiteral) {
  121. return literal.type;
  122. } else if (literal instanceof StringLiteral) {
  123. return literal.type;
  124. } else if (literal instanceof BoolLiteral) {
  125. return literal.type;
  126. } else if (literal instanceof VariableLiteral) {
  127. const typeInfo = this.findSymbol(literal.id, this.symbolMap);
  128. if (typeInfo.type === Types.ARRAY) {
  129. return typeInfo;
  130. }
  131. return typeInfo.type;
  132. } else {
  133. console.warn("Evaluating type only for an array literal...");
  134. return Types.UNDEFINED;
  135. }
  136. }
  137. evaluateArrayLiteral (lines, columns, subtype, literal) {
  138. if (literal instanceof ArrayLiteral) {
  139. if (columns === null) {
  140. // it's a vector...
  141. if (lines !== literal.value.length) {
  142. throw new Error("invalid array size");
  143. }
  144. literal.value.reduce((last, next) => {
  145. const eType = this.evaluateExpressionType(next);
  146. if (subtype !== eType || eType !== last) {
  147. throw new Error("invalid array type");
  148. }
  149. return eType;
  150. });
  151. return true;
  152. } else {
  153. if (columns !== literal.value.length) {
  154. throw new Error("invalid array size");
  155. }
  156. for (let i = 0; i < columns; i++) {
  157. const anotherArray = literal.value[i];
  158. this.evaluateArrayLiteral(lines, null, subtype, anotherArray)
  159. }
  160. }
  161. } else {
  162. const resultType = this.evaluateExpressionType(literal);
  163. if (!resultType.subtype) {
  164. throw new Error("initial must be of type array");
  165. }
  166. if (resultType.subtype !== subtype) {
  167. throw new Error("invalid array type");
  168. } else if (resultType.lines !== lines) {
  169. throw new Error("invalid array size");
  170. } else if (resultType.columns !== columns) {
  171. throw new Error("invalid array size");
  172. }
  173. return true;
  174. }
  175. }
  176. assertFunction (fun) {
  177. this.pushMap();
  178. this.assertDeclarations(fun.variablesDeclarations);
  179. if(fun.returnType === Types.VOID) {
  180. this.assertOptionalReturn(fun);
  181. } else {
  182. this.assertReturn(fun);
  183. }
  184. this.popMap();
  185. }
  186. assertOptionalReturn (fun) {
  187. }
  188. assertParameters (fun, actualParametersList) {
  189. if (fun.formalParameters.length !== actualParametersList.length) {
  190. throw new Error("wrong number of parameters...");
  191. }
  192. for (let i = 0; i < actualParametersList.length; i++) {
  193. const param = actualParametersList[i];
  194. const formalParam = fun.formalParameters[i];
  195. if(formalParam.byRef && !(param instanceof VariableLiteral)) {
  196. throw new Error("Invalid param type");
  197. }
  198. const resultType = this.evaluateExpressionType(param);
  199. switch (formalParam.dimensions) {
  200. case 1: {
  201. if (!resultType.subtype) {
  202. throw new Error("invalid param type");
  203. } else if (resultType.subtype !== formalParam.type) {
  204. throw new Error("invalid param type");
  205. } else if (resultType.lines === null || resultType.columns !== null) {
  206. throw new Error("invalid param type");
  207. }
  208. break;
  209. }
  210. case 2: {
  211. if (!resultType.subtype) {
  212. throw new Error("invalid param type");
  213. } else if (resultType.subtype !== formalParam.type) {
  214. throw new Error("invalid param type");
  215. } else if (resultType.lines === null || resultType.columns === null) {
  216. throw new Error("invalid param type");
  217. }
  218. break;
  219. }
  220. default: {
  221. if (resultType.subtype || resultType !== formalParam.type) {
  222. throw new Error("invalid param type");
  223. }
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. }