semanticAnalyser.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. import { ProcessorErrorFactory } from './../error/processorErrorFactory';
  2. import { LanguageDefinedFunction } from './../definedFunctions';
  3. import { LanguageService } from './../../services/languageService';
  4. import { ArrayDeclaration, While, For, Switch, Assign, Break, IfThenElse, Return, ArrayIndexAssign } from '../../ast/commands';
  5. import { InfixApp, UnaryApp, FunctionCall, IntLiteral, RealLiteral, StringLiteral, BoolLiteral, VariableLiteral, ArrayLiteral, ArrayAccess } from '../../ast/expressions';
  6. import { Literal } from '../../ast/expressions/literal';
  7. import { resultTypeAfterInfixOp, resultTypeAfterUnaryOp } from '../compatibilityTable';
  8. import { Types } from '../../typeSystem/types';
  9. import { CompoundType } from '../../typeSystem/compoundType';
  10. import { MultiType } from '../../typeSystem/multiType';
  11. export class SemanticAnalyser {
  12. constructor(ast) {
  13. this.ast = ast;
  14. this.lexerClass = LanguageService.getCurrentLexer();
  15. const lexer = new this.lexerClass(null);
  16. this.literalNames = lexer.literalNames;
  17. this.symbolMap = null;
  18. this.currentFunction = null;
  19. }
  20. pushMap () {
  21. if(this.symbolMap === null) {
  22. this.symbolMap = {map:{}, next: null};
  23. } else {
  24. const n = {map:{}, next: this.symbolMap};
  25. this.symbolMap = n;
  26. }
  27. }
  28. popMap () {
  29. if(this.symbolMap !== null) {
  30. this.symbolMap = this.symbolMap.next;
  31. }
  32. }
  33. insertSymbol (id, typeInfo) {
  34. this.symbolMap.map[id] = typeInfo;
  35. }
  36. findSymbol (id, symMap) {
  37. if(!symMap.map[id]) {
  38. if(symMap.next) {
  39. return this.findSymbol(id, symMap.next);
  40. }
  41. return null;
  42. } else {
  43. return symMap.map[id];
  44. }
  45. }
  46. getMainFunction () {
  47. return this.ast.functions.find(v => v.isMain);
  48. }
  49. findFunction (name) {
  50. console.log(name);
  51. if(name.match(/^\$.+$/)) {
  52. const fun = LanguageDefinedFunction.getFunction(name);
  53. if(!!!fun) {
  54. throw ProcessorErrorFactory.not_implemented(name);
  55. }
  56. return fun;
  57. } else {
  58. const val = this.ast.functions.find( v => v.name === name);
  59. if (!!!val) {
  60. return null;
  61. }
  62. return val;
  63. }
  64. }
  65. analyseTree () {
  66. const globalVars = this.ast.global;
  67. this.pushMap();
  68. this.assertDeclarations(globalVars);
  69. const functions = this.ast.functions;
  70. const mainFunc = functions.filter((f) => f.name === null);
  71. if (mainFunc.length <= 0) {
  72. throw ProcessorErrorFactory.main_missing();
  73. }
  74. for (let i = 0; i < functions.length; i++) {
  75. const fun = functions[i];
  76. this.assertFunction(fun);
  77. }
  78. return this.ast;
  79. }
  80. assertDeclarations (list) {
  81. for (let i = 0; i < list.length; i++) {
  82. this.assertDeclaration(list[i]);
  83. }
  84. }
  85. assertDeclaration (declaration) {
  86. if (declaration instanceof ArrayDeclaration) {
  87. if(declaration.initial === null) {
  88. const lineType = this.evaluateExpressionType(declaration.lines);
  89. if (!lineType.isCompatible(Types.INTEGER)) {
  90. throw ProcessorErrorFactory.array_dimension_not_int_full(declaration.sourceInfo);
  91. }
  92. if (declaration.columns !== null) {
  93. const columnType = this.evaluateExpressionType(declaration.columns);
  94. if (!columnType.isCompatible(Types.INTEGER)) {
  95. throw ProcessorErrorFactory.array_dimension_not_int_full(declaration.sourceInfo);
  96. }
  97. }
  98. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type});
  99. return;
  100. }
  101. this.evaluateArrayLiteral(declaration.id, declaration.lines, declaration.columns, declaration.type, declaration.initial);
  102. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type});
  103. } else {
  104. if(declaration.initial === null) {
  105. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type});
  106. return;
  107. }
  108. const resultType = this.evaluateExpressionType(declaration.initial);
  109. if(resultType instanceof MultiType) {
  110. if(!resultType.isCompatible(declaration.type)) {
  111. const stringInfo = declaration.type.stringInfo();
  112. const info = stringInfo[0];
  113. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, declaration.sourceInfo);
  114. }
  115. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
  116. } else if(!declaration.type.isCompatible(resultType)) {
  117. const stringInfo = declaration.type.stringInfo();
  118. const info = stringInfo[0];
  119. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, declaration.sourceInfo);
  120. } else {
  121. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
  122. }
  123. }
  124. }
  125. evaluateExpressionType (expression) {
  126. if(expression instanceof UnaryApp) {
  127. const op = expression.op;
  128. const resultType = this.evaluateExpressionType(expression.left);
  129. return resultTypeAfterUnaryOp(op, resultType);
  130. } else if (expression instanceof InfixApp) {
  131. const op = expression.op;
  132. const resultTypeLeft = this.evaluateExpressionType(expression.left);
  133. const resultTypeRight = this.evaluateExpressionType(expression.right);
  134. return resultTypeAfterInfixOp(op, resultTypeLeft, resultTypeRight);
  135. } else if (expression instanceof Literal) {
  136. return this.evaluateLiteralType(expression);
  137. } else if (expression instanceof FunctionCall) {
  138. if (expression.isMainCall) {
  139. throw ProcessorErrorFactory.void_in_expression_full(LanguageDefinedFunction.getMainFunctionName(), expression.sourceInfo);
  140. }
  141. const fun = this.findFunction(expression.id);
  142. if(fun === null) {
  143. throw ProcessorErrorFactory.function_missing_full(expression.id, expression.sourceInfo);
  144. }
  145. if (fun.returnType.isCompatible(Types.VOID)) {
  146. throw ProcessorErrorFactory.void_in_expression_full(expression.id, expression.sourceInfo);
  147. }
  148. this.assertParameters(fun, expression.actualParameters);
  149. return fun.returnType;
  150. } else if (expression instanceof ArrayAccess) {
  151. const arrayTypeInfo = this.findSymbol(expression.id, this.symbolMap);
  152. if(arrayTypeInfo === null) {
  153. throw ProcessorErrorFactory.symbol_not_found_full(expression.id, expression.sourceInfo);
  154. }
  155. if (!(arrayTypeInfo.type instanceof CompoundType)) {
  156. throw ProcessorErrorFactory.invalid_array_access_full(expression.id, expression.sourceInfo);
  157. }
  158. const lineType = this.evaluateExpressionType(expression.line);
  159. if (!lineType.isCompatible(Types.INTEGER)) {
  160. throw ProcessorErrorFactory.array_dimension_not_int_full(expression.sourceInfo);
  161. }
  162. if (expression.column !== null) {
  163. if (arrayTypeInfo.columns === null) {
  164. throw ProcessorErrorFactory.invalid_matrix_access_full(expression.id, expression.sourceInfo);
  165. }
  166. const columnType = this.evaluateExpressionType(expression.column);
  167. if(!columnType.isCompatible(Types.INTEGER)) {
  168. throw ProcessorErrorFactory.array_dimension_not_int_full(expression.sourceInfo);
  169. }
  170. }
  171. const arrType = arrayTypeInfo.type;
  172. if(expression.column !== null) {
  173. // indexing matrix
  174. return arrType.innerType;
  175. } else {
  176. if(arrayTypeInfo.columns === null) {
  177. return arrType.innerType;
  178. }
  179. return new CompoundType(arrType.innerType, 1);
  180. }
  181. }
  182. }
  183. evaluateLiteralType (literal) {
  184. if(literal instanceof IntLiteral) {
  185. return literal.type;
  186. } else if (literal instanceof RealLiteral) {
  187. return literal.type;
  188. } else if (literal instanceof StringLiteral) {
  189. return literal.type;
  190. } else if (literal instanceof BoolLiteral) {
  191. return literal.type;
  192. } else if (literal instanceof VariableLiteral) {
  193. const typeInfo = this.findSymbol(literal.id, this.symbolMap);
  194. if(typeInfo === null) {
  195. throw ProcessorErrorFactory.symbol_not_found_full(literal.id, literal.sourceInfo);
  196. }
  197. if (typeInfo.type instanceof CompoundType) {
  198. return typeInfo.type;
  199. }
  200. return typeInfo.type;
  201. } else {
  202. console.warn("Evaluating type only for an array literal...");
  203. let last = null;
  204. if(literal.value.length === 1) {
  205. last = this.evaluateExpressionType(literal.value[0]);
  206. } else {
  207. for (let i = 0; i < literal.value.length; i++) {
  208. const e = this.evaluateExpressionType(literal.value[i]);
  209. if(last === null) {
  210. last = e;
  211. } else if(!last.isCompatible(e)) {
  212. const strInfo = last.stringInfo();
  213. const info = strInfo[0];
  214. const strExp = literal.toString();
  215. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  216. }
  217. }
  218. }
  219. if(last instanceof CompoundType) {
  220. return new CompoundType(last.innerType, last.dimensions + 1);
  221. }
  222. return new CompoundType(last, 1);
  223. }
  224. }
  225. evaluateArrayLiteral (id, lines, columns, type, literal) {
  226. /* if (literal instanceof ArrayLiteral) {
  227. const dimType = this.evaluateExpressionType(lines);
  228. if (!dimType.isCompatible(Types.INTEGER)) {
  229. throw ProcessorErrorFactory.array_dimension_not_int_full(literal.sourceInfo);
  230. }
  231. if ((lines instanceof IntLiteral)) {
  232. if (!lines.value.eq(literal.value.length)) {
  233. if(type.dimensions > 1) {
  234. throw ProcessorErrorFactory.matrix_line_outbounds_full(id, literal.value.length, lines.value.toNumber(), literal.sourceInfo)
  235. } else {
  236. throw ProcessorErrorFactory.vector_line_outbounds_full(id, literal.value.length, lines.value.toNumber(), literal.sourceInfo)
  237. }
  238. } else if (lines.value.isNeg()) {
  239. throw ProcessorErrorFactory.array_dimension_not_positive_full(literal.sourceInfo);
  240. }
  241. }
  242. if (columns === null) {
  243. // it's a vector...
  244. literal.value.reduce((last, next) => {
  245. const eType = this.evaluateExpressionType(next);
  246. if (!last.canAccept(eType)) {
  247. const strInfo = last.stringInfo();
  248. const info = strInfo[0];
  249. const strExp = literal.toString();
  250. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  251. }
  252. return last;
  253. }, type);
  254. return true;
  255. } else {
  256. const dimType = this.evaluateExpressionType(columns);
  257. if (!dimType.isCompatible(Types.INTEGER)) {
  258. throw ProcessorErrorFactory.array_dimension_not_int_full(literal.sourceInfo);
  259. }
  260. if ((columns instanceof IntLiteral)) {
  261. const columnValue = literal.value[0].value.length;
  262. if (!columns.value.eq(columnValue)) {
  263. if(type.dimensions > 1) {
  264. throw ProcessorErrorFactory.matrix_column_outbounds_full(id, literal.value.length, columns.value.toNumber(), literal.sourceInfo)
  265. } else {
  266. throw ProcessorErrorFactory.invalid_matrix_access_full(id, literal.sourceInfo);
  267. }
  268. } else if (columns.value.isNeg()) {
  269. throw ProcessorErrorFactory.array_dimension_not_positive_full(literal.sourceInfo);
  270. }
  271. for (let i = 0; i < columns; i++) {
  272. const anotherArray = literal.value[i];
  273. this.evaluateArrayLiteral(id, columns, null, type, anotherArray)
  274. }
  275. }
  276. }
  277. } else {
  278. const resultType = this.evaluateExpressionType(literal);
  279. if (!(resultType instanceof CompoundType)) {
  280. const strInfo = type.stringInfo();
  281. const info = strInfo[0];
  282. const strExp = literal.toString();
  283. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  284. }
  285. if (!type.isCompatible(resultType)) {
  286. const strInfo = type.stringInfo();
  287. const info = strInfo[0];
  288. const strExp = literal.toString();
  289. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  290. }
  291. return true;
  292. } */
  293. return true;
  294. }
  295. assertFunction (fun) {
  296. this.pushMap();
  297. this.currentFunction = fun;
  298. fun.formalParameters.forEach(formalParam => {
  299. if(formalParam.type instanceof CompoundType) {
  300. if(formalParam.type.dimensions > 1) {
  301. this.insertSymbol(formalParam.id, {id: formalParam.id, lines: -1, columns: -1, type: formalParam.type});
  302. } else {
  303. this.insertSymbol(formalParam.id, {id: formalParam.id, lines: -1, columns: null, type: formalParam.type});
  304. }
  305. } else {
  306. this.insertSymbol(formalParam.id, {id: formalParam.id, type: formalParam.type});
  307. }
  308. })
  309. this.assertDeclarations(fun.variablesDeclarations);
  310. const optional = fun.returnType.isCompatible(Types.VOID);
  311. const valid = this.assertReturn(fun, optional);
  312. if (!valid) {
  313. throw ProcessorErrorFactory.function_no_return(fun.name);
  314. }
  315. this.popMap();
  316. }
  317. assertReturn (fun, optional) {
  318. return fun.commands.reduce(
  319. (last, next) => this.checkCommand(fun.returnType, next, optional) || last, optional
  320. );
  321. }
  322. checkCommand (type, cmd, optional) {
  323. if (cmd instanceof While) {
  324. const resultType = this.evaluateExpressionType(cmd.expression);
  325. if (!resultType.isCompatible(Types.BOOLEAN)) {
  326. throw ProcessorErrorFactory.loop_condition_type_full(cmd.sourceInfo);
  327. }
  328. this.checkCommands(type, cmd.commands, optional);
  329. return false;
  330. } else if (cmd instanceof For) {
  331. this.checkCommand(type, cmd.assignment, optional);
  332. const resultType = this.evaluateExpressionType(cmd.condition);
  333. if (!resultType.isCompatible(Types.BOOLEAN)) {
  334. throw ProcessorErrorFactory.for_condition_type_full(cmd.sourceInfo);
  335. }
  336. this.checkCommand(type, cmd.increment, optional);
  337. this.checkCommands(type, cmd.commands, optional);
  338. return false;
  339. } else if (cmd instanceof Switch) {
  340. const sType = this.evaluateExpressionType(cmd.expression);
  341. let result = optional;
  342. let hasDefault = false;
  343. for (let i = 0; i < cmd.cases.length; i++) {
  344. const aCase = cmd.cases[i];
  345. if (aCase.expression !== null) {
  346. const caseType = this.evaluateExpressionType(aCase.expression);
  347. if (!sType.isCompatible(caseType)) {
  348. const strInfo = sType.stringInfo();
  349. const info = strInfo[0];
  350. const strExp = aCase.expression.toString();
  351. throw ProcessorErrorFactory.invalid_case_type_full(strExp, info.type, info.dim, aCase.sourceInfo);
  352. }
  353. } else {
  354. hasDefault = true;
  355. }
  356. result = result && this.checkCommands(type, aCase.commands, result);
  357. }
  358. return result && hasDefault;
  359. } else if (cmd instanceof ArrayIndexAssign) {
  360. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  361. if(typeInfo === null) {
  362. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  363. }
  364. if(!(typeInfo.type instanceof CompoundType)) {
  365. throw ProcessorErrorFactory.invalid_array_access_full(cmd.id, cmd.sourceInfo);
  366. }
  367. const exp = cmd.expression;
  368. const lineExp = cmd.line;
  369. const lineType = this.evaluateExpressionType(lineExp);
  370. if (!lineType.isCompatible(Types.INTEGER)) {
  371. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  372. }
  373. const columnExp = cmd.column;
  374. if (typeInfo.columns === null && columnExp !== null) {
  375. throw ProcessorErrorFactory.invalid_matrix_access_full(cmd.id, cmd.sourceInfo);
  376. } else if (columnExp !== null) {
  377. const columnType = this.evaluateExpressionType(columnExp);
  378. if (!columnType.isCompatible(Types.INTEGER)) {
  379. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  380. }
  381. }
  382. // exp can be a arrayLiteral, a single value exp or an array access
  383. if(exp instanceof ArrayLiteral) {
  384. this.evaluateArrayLiteral(cmd.id, typeInfo.lines, (columnExp ? typeInfo.columns : null), typeInfo.type, exp);
  385. } else {
  386. // cannot properly evaluate since type system is poorly constructed
  387. }
  388. return optional;
  389. } else if (cmd instanceof Assign) {
  390. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  391. if(typeInfo === null) {
  392. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  393. }
  394. const exp = cmd.expression;
  395. if(exp instanceof ArrayLiteral) {
  396. if(!(typeInfo.type instanceof CompoundType)) {
  397. const stringInfo = typeInfo.type.stringInfo();
  398. const info = stringInfo[0];
  399. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo);
  400. }
  401. this.evaluateArrayLiteral(cmd.id, typeInfo.lines, typeInfo.columns, typeInfo.type, exp);
  402. } else {
  403. const resultType = this.evaluateExpressionType(exp);
  404. if(!resultType.isCompatible(typeInfo.type)) {
  405. const stringInfo = typeInfo.type.stringInfo();
  406. const info = stringInfo[0];
  407. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo);
  408. }
  409. }
  410. return optional;
  411. } else if (cmd instanceof Break) {
  412. return optional;
  413. } else if (cmd instanceof IfThenElse) {
  414. const resultType = this.evaluateExpressionType(cmd.condition);
  415. if (!resultType.isCompatible(Types.BOOLEAN)) {
  416. throw ProcessorErrorFactory.if_condition_type_full(cmd.sourceInfo);
  417. }
  418. if(cmd.ifFalse instanceof IfThenElse) {
  419. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommand(type, cmd.ifFalse, optional);
  420. } else {
  421. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommands(type, cmd.ifFalse.commands,optional);
  422. }
  423. } else if (cmd instanceof FunctionCall) {
  424. let fun = null;
  425. if (cmd.isMainCall) {
  426. fun = this.getMainFunction();
  427. } else {
  428. fun = this.findFunction(cmd.id);
  429. }
  430. if(fun === null) {
  431. throw ProcessorErrorFactory.function_missing_full(cmd.id, cmd.sourceInfo);
  432. }
  433. this.assertParameters(fun, cmd.actualParameters);
  434. return optional;
  435. } else if (cmd instanceof Return) {
  436. const funcName = this.currentFunction.isMain ? LanguageDefinedFunction.getMainFunctionName() : this.currentFunction.name
  437. if (cmd.expression === null && !type.isCompatible(Types.VOID)) {
  438. const stringInfo = type.stringInfo();
  439. const info = stringInfo[0];
  440. throw ProcessorErrorFactory.invalid_void_return_full(funcName, info.type, info.dim, cmd.sourceInfo);
  441. } else if (cmd.expression !== null) {
  442. const resultType = this.evaluateExpressionType(cmd.expression);
  443. if (!type.isCompatible(resultType)) {
  444. const stringInfo = type.stringInfo();
  445. const info = stringInfo[0];
  446. throw ProcessorErrorFactory.invalid_return_type_full(funcName, info.type, info.dim, cmd.sourceInfo);
  447. } else {
  448. return true;
  449. }
  450. } else {
  451. return true;
  452. }
  453. }
  454. }
  455. checkCommands (type, cmds, optional) {
  456. return cmds.reduce(
  457. (last, next) => this.checkCommand(type, next, optional) || last, optional
  458. );
  459. }
  460. assertParameters (fun, actualParametersList) {
  461. if (fun.formalParameters.length !== actualParametersList.length) {
  462. throw ProcessorErrorFactory.invalid_parameters_size_full(fun.name, actualParametersList.length, fun.formalParameters.length, null);
  463. }
  464. for (let i = 0; i < actualParametersList.length; i++) {
  465. const param = actualParametersList[i];
  466. const formalParam = fun.formalParameters[i];
  467. const id = formalParam.id;
  468. if(formalParam.byRef) {
  469. if (!(param instanceof VariableLiteral || param instanceof ArrayAccess)) {
  470. throw ProcessorErrorFactory.invalid_parameter_type_full(id, param.toString(), param.sourceInfo);
  471. }
  472. }
  473. const resultType = this.evaluateExpressionType(param);
  474. if(resultType instanceof MultiType && formalParam.type instanceof MultiType) {
  475. let shared = 0
  476. for (let j = 0; j < resultType.types.length; j++) {
  477. const element = resultType.types[j];
  478. if(formalParam.type.types.indexOf(element) !== -1) {
  479. shared++;
  480. }
  481. }
  482. if(shared <= 0) {
  483. throw ProcessorErrorFactory.invalid_parameter_type_full(id, param.toString(), param.sourceInfo);
  484. }
  485. } else if (resultType instanceof MultiType) {
  486. if(!resultType.isCompatible(formalParam.type)) {
  487. throw ProcessorErrorFactory.invalid_parameter_type_full(id, param.toString(), param.sourceInfo);
  488. }
  489. } else if(!formalParam.type.isCompatible(resultType)) {
  490. throw ProcessorErrorFactory.invalid_parameter_type_full(id, param.toString(), param.sourceInfo);
  491. }
  492. }
  493. }
  494. }