semanticAnalyser.js 21 KB

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