semanticAnalyser.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { ProcessorErrorFactory } from './../error/processorErrorFactory';
  2. import { LanguageDefinedFunction } from './../definedFunctions';
  3. import { LanguageService } from './../../services/languageService';
  4. import { ArrayDeclaration, While, For, Switch, Case, Declaration, Assign, Break, IfThenElse, Return } 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. const mainFunc = functions.filter((f) => f.name === null);
  65. if (mainFunc.length <= 0) {
  66. throw new Error("no main func...");
  67. } else if (mainFunc.length > 1) {
  68. throw new Error("only one main func...");
  69. }
  70. for (let i = 0; i < functions.length; i++) {
  71. const fun = functions[i];
  72. this.assertFunction(fun);
  73. }
  74. return this.ast;
  75. }
  76. assertDeclarations (list) {
  77. for (let i = 0; i < list.length; i++) {
  78. this.assertDeclaration(list[i]);
  79. }
  80. }
  81. assertDeclaration (declaration) {
  82. if (declaration instanceof ArrayDeclaration) {
  83. if(declaration.initial === null) {
  84. const lineType = this.evaluateExpressionType(declaration.lines);
  85. if (lineType !== Types.INTEGER) {
  86. throw new Error("dim must be int");
  87. }
  88. if (declaration.columns !== null) {
  89. const columnType = this.evaluateExpressionType(declaration.columns);
  90. if (columnType !== Types.INTEGER) {
  91. throw new Error("dim must be int");
  92. }
  93. }
  94. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type, subtype: declaration.subtype});
  95. return;
  96. }
  97. this.evaluateArrayLiteral(declaration.lines, declaration.columns, declaration.subtype, declaration.initial);
  98. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type, subtype: declaration.subtype});
  99. } else {
  100. if(declaration.initial === null) {
  101. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type});
  102. return;
  103. }
  104. const resultType = this.evaluateExpressionType(declaration.initial);
  105. if(declaration.type !== resultType) {
  106. throw new Error('Invalid type');
  107. } else {
  108. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
  109. }
  110. }
  111. }
  112. evaluateExpressionType (expression) {
  113. if(expression instanceof UnaryApp) {
  114. const op = expression.op;
  115. const resultType = this.evaluateExpressionType(expression.left);
  116. return resultTypeAfterUnaryOp(op, resultType);
  117. } else if (expression instanceof InfixApp) {
  118. const op = expression.op;
  119. const resultTypeLeft = this.evaluateExpressionType(expression.left);
  120. const resultTypeRight = this.evaluateExpressionType(expression.right);
  121. return resultTypeAfterInfixOp(op, resultTypeLeft, resultTypeRight);
  122. } else if (expression instanceof Literal) {
  123. return this.evaluateLiteralType(expression);
  124. } else if (expression instanceof FunctionCall) {
  125. const fun = this.findFunction(expression.id);
  126. if (fun.returnType === Types.VOID) {
  127. throw new Error("void return");
  128. }
  129. this.assertParameters(fun, expression.actualParameters);
  130. return fun.returnType;
  131. }
  132. }
  133. evaluateLiteralType (literal) {
  134. if(literal instanceof IntLiteral) {
  135. return literal.type;
  136. } else if (literal instanceof RealLiteral) {
  137. return literal.type;
  138. } else if (literal instanceof StringLiteral) {
  139. return literal.type;
  140. } else if (literal instanceof BoolLiteral) {
  141. return literal.type;
  142. } else if (literal instanceof VariableLiteral) {
  143. const typeInfo = this.findSymbol(literal.id, this.symbolMap);
  144. if (typeInfo.type === Types.ARRAY) {
  145. return typeInfo;
  146. }
  147. return typeInfo.type;
  148. } else {
  149. console.warn("Evaluating type only for an array literal...");
  150. return Types.UNDEFINED;
  151. }
  152. }
  153. evaluateArrayLiteral (lines, columns, subtype, literal) {
  154. if (literal instanceof ArrayLiteral) {
  155. if (columns === null) {
  156. // it's a vector...
  157. const dimType = this.evaluateExpressionType(lines);
  158. if (dimType !== Types.INTEGER) {
  159. throw new Error("dim must be int");
  160. }
  161. if ((lines instanceof IntLiteral) && lines.value !== literal.value.length) {
  162. throw new Error("invalid array size");
  163. }
  164. literal.value.reduce((last, next) => {
  165. const eType = this.evaluateExpressionType(next);
  166. if (subtype !== eType || eType !== last) {
  167. throw new Error("invalid array type");
  168. }
  169. return eType;
  170. });
  171. return true;
  172. } else {
  173. const dimType = this.evaluateExpressionType(columns);
  174. if (dimType !== Types.INTEGER) {
  175. throw new Error("dim must be int");
  176. }
  177. if ((columns instanceof IntLiteral) && columns.value !== literal.value.length) {
  178. throw new Error("invalid array size");
  179. }
  180. for (let i = 0; i < columns; i++) {
  181. const anotherArray = literal.value[i];
  182. this.evaluateArrayLiteral(lines, null, subtype, anotherArray)
  183. }
  184. }
  185. } else {
  186. const resultType = this.evaluateExpressionType(literal);
  187. if (!resultType.subtype) {
  188. throw new Error("initial must be of type array");
  189. }
  190. if (resultType.subtype !== subtype) {
  191. throw new Error("invalid array type");
  192. } else if (resultType.lines !== lines) {
  193. throw new Error("invalid array size");
  194. } else if (resultType.columns !== columns) {
  195. throw new Error("invalid array size");
  196. }
  197. return true;
  198. }
  199. }
  200. assertFunction (fun) {
  201. this.pushMap();
  202. this.assertDeclarations(fun.variablesDeclarations);
  203. const optional = fun.returnType === Types.VOID;
  204. const valid = this.assertReturn(fun, optional);
  205. if (!valid) {
  206. throw new Error("function has no accessible return");
  207. }
  208. this.popMap();
  209. }
  210. assertReturn (fun, optional) {
  211. return fun.commands.reduce(
  212. (last, next) => this.checkCommand(fun.returnType, next, optional) || last, optional
  213. );
  214. }
  215. checkCommand (type, cmd, optional) {
  216. if (cmd instanceof While) {
  217. const resultType = this.evaluateExpressionType(cmd.expression);
  218. if (resultType !== Types.BOOLEAN) {
  219. throw new Error("condition not boolean");
  220. }
  221. this.checkCommands(type, cmd.commands, optional);
  222. return false;
  223. } else if (cmd instanceof For) {
  224. this.checkCommand(type, cmd.assignment, optional);
  225. const resultType = this.evaluateExpressionType(cmd.condition);
  226. if (resultType !== Types.BOOLEAN) {
  227. throw new Error("condition not boolean");
  228. }
  229. this.checkCommand(type, cmd.increment, optional);
  230. this.checkCommands(type, cmd.commands, optional);
  231. return false;
  232. } else if (cmd instanceof Switch) {
  233. const sType = this.evaluateExpressionType(cmd.expression);
  234. let result = optional;
  235. let hasDefault = false;
  236. for (let i = 0; i < cmd.cases.length; i++) {
  237. const aCase = cmd.cases[i];
  238. if (aCase.expression !== null) {
  239. const caseType = this.evaluateExpressionType(aCase.expression);
  240. if (sType !== caseType) {
  241. throw new Error("invalid type in case");
  242. }
  243. } else {
  244. hasDefault = true;
  245. }
  246. result = result && this.checkCommands(type, aCase.commands, result);
  247. }
  248. return result && hasDefault;
  249. } else if (cmd instanceof Assign) {
  250. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  251. const exp = cmd.expression;
  252. if(exp instanceof ArrayLiteral) {
  253. if(!typeInfo.subtype) {
  254. throw new Error("type not compatible");
  255. }
  256. this.evaluateArrayLiteral(typeInfo.lines, typeInfo.columns, typeInfo.subtype, exp);
  257. } else {
  258. if(typeInfo.subtype) {
  259. throw new Error("type not compatible");
  260. }
  261. const resultType = this.evaluateExpressionType(exp);
  262. if(resultType !== typeInfo.type) {
  263. throw new Error("type not compatible");
  264. }
  265. }
  266. return optional;
  267. } else if (cmd instanceof Break) {
  268. return optional;
  269. } else if (cmd instanceof IfThenElse) {
  270. const resultType = this.evaluateExpressionType(cmd.condition);
  271. if (resultType !== Types.BOOLEAN) {
  272. throw new Error("condition not boolean");
  273. }
  274. console.log(cmd);
  275. if(cmd.ifFalse instanceof IfThenElse) {
  276. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommand(type, cmd.ifFalse, optional);
  277. } else {
  278. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommands(type, cmd.ifFalse.commands,optional);
  279. }
  280. } else if (cmd instanceof FunctionCall) {
  281. const fun = this.findFunction(cmd.id);
  282. this.assertParameters(fun, cmd.actualParameters);
  283. return optional;
  284. } else if (cmd instanceof Return) {
  285. if (cmd.expression === null && type !== Types.VOID) {
  286. throw new Error('invalid return type');
  287. } else if (cmd.expression !== null) {
  288. const resultType = this.evaluateExpressionType(cmd.expression);
  289. if (resultType !== type) {
  290. throw new Error('invalid return type');
  291. } else {
  292. return true;
  293. }
  294. } else {
  295. return true;
  296. }
  297. }
  298. }
  299. checkCommands (type, cmds, optional) {
  300. return cmds.reduce(
  301. (last, next) => this.checkCommand(type, next, optional) || last, optional
  302. );
  303. }
  304. assertParameters (fun, actualParametersList) {
  305. if (fun.formalParameters.length !== actualParametersList.length) {
  306. throw new Error("wrong number of parameters...");
  307. }
  308. for (let i = 0; i < actualParametersList.length; i++) {
  309. const param = actualParametersList[i];
  310. const formalParam = fun.formalParameters[i];
  311. if(formalParam.byRef && !(param instanceof VariableLiteral)) {
  312. throw new Error("Invalid param type");
  313. }
  314. const resultType = this.evaluateExpressionType(param);
  315. switch (formalParam.dimensions) {
  316. case 1: {
  317. if (!resultType.subtype) {
  318. throw new Error("invalid param type");
  319. } else if (resultType.subtype !== formalParam.type) {
  320. throw new Error("invalid param type");
  321. } else if (resultType.lines === null || resultType.columns !== null) {
  322. throw new Error("invalid param type");
  323. }
  324. break;
  325. }
  326. case 2: {
  327. if (!resultType.subtype) {
  328. throw new Error("invalid param type");
  329. } else if (resultType.subtype !== formalParam.type) {
  330. throw new Error("invalid param type");
  331. } else if (resultType.lines === null || resultType.columns === null) {
  332. throw new Error("invalid param type");
  333. }
  334. break;
  335. }
  336. default: {
  337. if (resultType.subtype) {
  338. throw new Error("invalid param type");
  339. }
  340. if (formalParam.type !== Types.ALL && resultType !== formalParam.type) {
  341. throw new Error("invalid param type");
  342. }
  343. break;
  344. }
  345. }
  346. }
  347. }
  348. }