semanticAnalyser.js 20 KB

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