semanticAnalyser.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  6. import { Expression } from './../../ast/expressions/expression'
  7. import { InfixApp, UnaryApp, FunctionCall, IntLiteral, RealLiteral, StringLiteral, BoolLiteral, VariableLiteral, ArrayAccess } from '../../ast/expressions';
  8. import { Literal } from '../../ast/expressions/literal';
  9. import { resultTypeAfterInfixOp, resultTypeAfterUnaryOp } from '../compatibilityTable';
  10. import { Types } from '../../typeSystem/types';
  11. import { ArrayType } from '../../typeSystem/array_type';
  12. import { MultiType } from '../../typeSystem/multiType';
  13. import { Config } from '../../util/config';
  14. import { Store } from '../store/store';
  15. import { IVProgParser } from '../../ast/ivprogParser';
  16. export class SemanticAnalyser {
  17. static analyseFromSource (stringCode) {
  18. const parser = IVProgParser.createParser(stringCode);
  19. const semantic = new SemanticAnalyser(parser.parseTree());
  20. return semantic.analyseTree();
  21. }
  22. constructor(ast) {
  23. this.ast = ast;
  24. this.lexerClass = LanguageService.getCurrentLexer();
  25. const lexer = new this.lexerClass(null);
  26. this.literalNames = lexer.literalNames;
  27. this.symbolMap = null;
  28. this.currentFunction = null;
  29. }
  30. pushMap () {
  31. if(this.symbolMap === null) {
  32. this.symbolMap = {map:{}, next: null};
  33. } else {
  34. const n = {map:{}, next: this.symbolMap};
  35. this.symbolMap = n;
  36. }
  37. }
  38. popMap () {
  39. if(this.symbolMap !== null) {
  40. this.symbolMap = this.symbolMap.next;
  41. }
  42. }
  43. insertSymbol (id, typeInfo) {
  44. this.symbolMap.map[id] = typeInfo;
  45. }
  46. findSymbol (id, symbol_map) {
  47. if(!symbol_map.map[id]) {
  48. if(symbol_map.next) {
  49. return this.findSymbol(id, symbol_map.next);
  50. }
  51. return null;
  52. } else {
  53. return symbol_map.map[id];
  54. }
  55. }
  56. getMainFunction () {
  57. return this.ast.functions.find(v => v.isMain);
  58. }
  59. findFunction (name) {
  60. if(name.match(/^\$.+$/)) {
  61. const fun = LanguageDefinedFunction.getFunction(name);
  62. if(!fun) {
  63. throw ProcessorErrorFactory.not_implemented(name);
  64. }
  65. return fun;
  66. } else {
  67. const val = this.ast.functions.find( v => v.name === name);
  68. if (!val) {
  69. return null;
  70. }
  71. return val;
  72. }
  73. }
  74. analyseTree () {
  75. const globalVars = this.ast.global;
  76. this.pushMap();
  77. this.assertDeclarations(globalVars);
  78. const functions = this.ast.functions;
  79. const mainFunc = functions.filter((f) => f.name === null);
  80. if (mainFunc.length <= 0) {
  81. throw ProcessorErrorFactory.main_missing();
  82. }
  83. for (let i = 0; i < functions.length; i++) {
  84. const fun = functions[i];
  85. this.assertFunction(fun);
  86. }
  87. return this.ast;
  88. }
  89. assertDeclarations (list) {
  90. for (let i = 0; i < list.length; i++) {
  91. this.assertDeclaration(list[i]);
  92. }
  93. }
  94. assertDeclaration (declaration) {
  95. if (declaration instanceof ArrayDeclaration) {
  96. this.assertArrayDeclaration(declaration);
  97. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines,
  98. columns: declaration.columns, type: declaration.type, isConst: declaration.isConst});
  99. } else {
  100. if(declaration.initial === null) {
  101. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type, isConst: declaration.isConst});
  102. return;
  103. }
  104. const resultType = this.evaluateExpressionType(declaration.initial);
  105. if(resultType instanceof MultiType) {
  106. if(!resultType.isCompatible(declaration.type)) {
  107. const stringInfo = declaration.type.stringInfo();
  108. const info = stringInfo[0];
  109. const result_string_info = resultType.stringInfo();
  110. const result_info = result_string_info[0];
  111. const exp = declaration.initial;
  112. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, result_info.type, result_info.dim, exp.toString(), declaration.sourceInfo);
  113. }
  114. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type, isConst: declaration.isConst})
  115. } else if((!declaration.type.isCompatible(resultType) && !Config.enable_type_casting)
  116. || (!declaration.type.isCompatible(resultType) && Config.enable_type_casting
  117. && !Store.canImplicitTypeCast(declaration.type, resultType))) {
  118. const stringInfo = declaration.type.stringInfo();
  119. const info = stringInfo[0];
  120. const result_string_info = resultType.stringInfo();
  121. const result_info = result_string_info[0];
  122. const exp = declaration.initial;
  123. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, result_info.type, result_info.dim, exp.toString(), declaration.sourceInfo);
  124. } else {
  125. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type, isConst: declaration.isConst});
  126. }
  127. }
  128. }
  129. assertArrayDeclaration (declaration) {
  130. if(declaration.initial === null) {
  131. const lineType = this.evaluateExpressionType(declaration.lines);
  132. if (!lineType.isCompatible(Types.INTEGER)) {
  133. throw ProcessorErrorFactory.array_dimension_not_int_full(declaration.sourceInfo);
  134. }
  135. if (declaration.columns !== null) {
  136. const columnType = this.evaluateExpressionType(declaration.columns);
  137. if (!columnType.isCompatible(Types.INTEGER)) {
  138. throw ProcessorErrorFactory.array_dimension_not_int_full(declaration.sourceInfo);
  139. }
  140. }
  141. } else {
  142. this.evaluateArrayLiteral(declaration);
  143. }
  144. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type});
  145. return;
  146. }
  147. evaluateExpressionType (expression) {
  148. // TODO: Throw operator error in case type == UNDEFINED
  149. if(expression instanceof UnaryApp) {
  150. const op = expression.op;
  151. const resultType = this.evaluateExpressionType(expression.left);
  152. const finalResult = resultTypeAfterUnaryOp(op, resultType);
  153. if (Types.UNDEFINED.isCompatible(finalResult)) {
  154. const stringInfo = resultType.stringInfo();
  155. const info = stringInfo[0];
  156. const expString = expression.toString();
  157. throw ProcessorErrorFactory.invalid_unary_op_full(expString, op, info.type, info.dim, expression.sourceInfo);
  158. }
  159. return finalResult;
  160. } else if (expression instanceof InfixApp) {
  161. const op = expression.op;
  162. const resultTypeLeft = this.evaluateExpressionType(expression.left);
  163. const resultTypeRight = this.evaluateExpressionType(expression.right);
  164. const finalResult = resultTypeAfterInfixOp(op, resultTypeLeft, resultTypeRight);
  165. if (Types.UNDEFINED.isCompatible(finalResult)) {
  166. const stringInfoLeft = resultTypeLeft.stringInfo();
  167. const infoLeft = stringInfoLeft[0];
  168. const stringInfoRight = resultTypeRight.stringInfo();
  169. const infoRight = stringInfoRight[0];
  170. const expString = expression.toString();
  171. throw ProcessorErrorFactory.invalid_infix_op_full(expString,op, infoLeft.type, infoLeft.dim, infoRight.type, infoRight.dim, expression.sourceInfo);
  172. }
  173. return finalResult;
  174. } else if (expression instanceof Literal) {
  175. return this.evaluateLiteralType(expression);
  176. } else if (expression instanceof FunctionCall) {
  177. if (expression.isMainCall) {
  178. throw ProcessorErrorFactory.void_in_expression_full(LanguageDefinedFunction.getMainFunctionName(), expression.sourceInfo);
  179. }
  180. const fun = this.findFunction(expression.id);
  181. if(fun === null) {
  182. throw ProcessorErrorFactory.function_missing_full(expression.id, expression.sourceInfo);
  183. }
  184. if (fun.returnType.isCompatible(Types.VOID)) {
  185. throw ProcessorErrorFactory.void_in_expression_full(expression.id, expression.sourceInfo);
  186. }
  187. this.assertParameters(fun, expression.actualParameters);
  188. return fun.returnType;
  189. } else if (expression instanceof ArrayAccess) {
  190. const arrayTypeInfo = this.findSymbol(expression.id, this.symbolMap);
  191. if(arrayTypeInfo === null) {
  192. throw ProcessorErrorFactory.symbol_not_found_full(expression.id, expression.sourceInfo);
  193. }
  194. if (!(arrayTypeInfo.type instanceof ArrayType)) {
  195. throw ProcessorErrorFactory.invalid_array_access_full(expression.id, expression.sourceInfo);
  196. }
  197. const lineType = this.evaluateExpressionType(expression.line);
  198. if (!lineType.isCompatible(Types.INTEGER)) {
  199. throw ProcessorErrorFactory.array_dimension_not_int_full(expression.sourceInfo);
  200. }
  201. if (expression.column !== null) {
  202. if (arrayTypeInfo.columns === null) {
  203. throw ProcessorErrorFactory.invalid_matrix_access_full(expression.id, expression.sourceInfo);
  204. }
  205. const columnType = this.evaluateExpressionType(expression.column);
  206. if(!columnType.isCompatible(Types.INTEGER)) {
  207. throw ProcessorErrorFactory.array_dimension_not_int_full(expression.sourceInfo);
  208. }
  209. }
  210. const arrType = arrayTypeInfo.type;
  211. if(expression.column !== null) {
  212. // indexing matrix
  213. return arrType.innerType;
  214. } else {
  215. if(arrayTypeInfo.columns === null) {
  216. return arrType.innerType;
  217. }
  218. return new ArrayType(arrType.innerType, 1);
  219. }
  220. }
  221. }
  222. evaluateLiteralType (literal) {
  223. if(literal instanceof IntLiteral) {
  224. return literal.type;
  225. } else if (literal instanceof RealLiteral) {
  226. return literal.type;
  227. } else if (literal instanceof StringLiteral) {
  228. return literal.type;
  229. } else if (literal instanceof BoolLiteral) {
  230. return literal.type;
  231. } else if (literal instanceof VariableLiteral) {
  232. const typeInfo = this.findSymbol(literal.id, this.symbolMap);
  233. if(typeInfo === null) {
  234. throw ProcessorErrorFactory.symbol_not_found_full(literal.id, literal.sourceInfo);
  235. }
  236. if (typeInfo.type instanceof ArrayType) {
  237. return typeInfo.type;
  238. }
  239. return typeInfo.type;
  240. } else {
  241. // console.warn("Evaluating type only for an array literal...");
  242. let last = null;
  243. if(literal.value.length === 1) {
  244. last = this.evaluateExpressionType(literal.value[0]);
  245. } else {
  246. for (let i = 0; i < literal.value.length; i++) {
  247. const e = this.evaluateExpressionType(literal.value[i]);
  248. if(last === null) {
  249. last = e;
  250. } else if(!last.isCompatible(e)) {
  251. const strInfo = last.stringInfo();
  252. const info = strInfo[0];
  253. const strExp = literal.toString();
  254. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  255. }
  256. }
  257. }
  258. if(last instanceof ArrayType) {
  259. return new ArrayType(last.innerType, last.dimensions + 1);
  260. }
  261. return new ArrayType(last, 1);
  262. }
  263. }
  264. evaluateArrayLiteral (arrayDeclaration) {
  265. const type = arrayDeclaration.type;
  266. const literal = arrayDeclaration.initial;
  267. // console.log(arrayDeclaration);
  268. if(arrayDeclaration.isVector) {
  269. this.evaluateVectorLiteralType(literal, type);
  270. } else {
  271. // TODO matrix type check
  272. for(let i = 0; i < literal.lines; ++i) {
  273. const line_literal = literal.value[i];
  274. this.evaluateVectorLiteralType(line_literal, new ArrayType(type.innerType, 1));
  275. }
  276. }
  277. return true;
  278. }
  279. assertFunction (fun) {
  280. this.pushMap();
  281. this.currentFunction = fun;
  282. fun.formalParameters.forEach(formalParam => {
  283. if(formalParam.type instanceof ArrayType) {
  284. if(formalParam.type.dimensions > 1) {
  285. this.insertSymbol(formalParam.id, {id: formalParam.id, lines: -1, columns: -1, type: formalParam.type});
  286. } else {
  287. this.insertSymbol(formalParam.id, {id: formalParam.id, lines: -1, columns: null, type: formalParam.type});
  288. }
  289. } else {
  290. this.insertSymbol(formalParam.id, {id: formalParam.id, type: formalParam.type});
  291. }
  292. })
  293. this.assertDeclarations(fun.variablesDeclarations);
  294. const optional = fun.returnType.isCompatible(Types.VOID);
  295. const valid = this.assertReturn(fun, optional);
  296. if (!valid) {
  297. throw ProcessorErrorFactory.function_no_return(fun.name);
  298. }
  299. this.popMap();
  300. }
  301. assertReturn (fun, optional) {
  302. return fun.commands.reduce(
  303. (last, next) => this.checkCommand(fun.returnType, next, optional) || last, optional
  304. );
  305. }
  306. checkCommand (type, cmd, optional) {
  307. if (cmd instanceof While) {
  308. const resultType = this.evaluateExpressionType(cmd.expression);
  309. if (!resultType.isCompatible(Types.BOOLEAN)) {
  310. throw ProcessorErrorFactory.loop_condition_type_full(cmd.expression.toString(), cmd.sourceInfo);
  311. }
  312. this.checkCommands(type, cmd.commands, optional);
  313. return false;
  314. } else if (cmd instanceof For) {
  315. const var_type = this.evaluateExpressionType(cmd.for_id);
  316. if (!var_type.isCompatible(Types.INTEGER)) {
  317. throw ProcessorErrorFactory.invalid_for_variable(cmd.for_id, cmd.sourceInfo);
  318. }
  319. const from_type = this.evaluateExpressionType(cmd.for_from);
  320. if (!from_type.isCompatible(Types.INTEGER)) {
  321. throw ProcessorErrorFactory.invalid_for_from(cmd.for_from, cmd.sourceInfo);
  322. }
  323. const to_type = this.evaluateExpressionType(cmd.for_to);
  324. if (!to_type.isCompatible(Types.INTEGER)) {
  325. throw ProcessorErrorFactory.invalid_for_to(cmd.for_to, cmd.sourceInfo);
  326. }
  327. if (cmd.for_pass != null) {
  328. const pass_type = this.evaluateExpressionType(cmd.for_pass);
  329. if (!pass_type.isCompatible(Types.INTEGER)) {
  330. throw ProcessorErrorFactory.invalid_for_pass(cmd.for_pass, cmd.sourceInfo);
  331. }
  332. }
  333. this.checkCommands(type, cmd.commands, optional);
  334. return false;
  335. } else if (cmd instanceof Switch) {
  336. const sType = this.evaluateExpressionType(cmd.expression);
  337. let result = optional;
  338. let hasDefault = false;
  339. for (let i = 0; i < cmd.cases.length; i++) {
  340. const aCase = cmd.cases[i];
  341. if (aCase.expression !== null) {
  342. const caseType = this.evaluateExpressionType(aCase.expression);
  343. if (!sType.isCompatible(caseType)) {
  344. const strInfo = sType.stringInfo();
  345. const info = strInfo[0];
  346. const strExp = aCase.expression.toString();
  347. throw ProcessorErrorFactory.invalid_case_type_full(strExp, info.type, info.dim, aCase.sourceInfo);
  348. }
  349. } else {
  350. hasDefault = true;
  351. }
  352. result = result && this.checkCommands(type, aCase.commands, result);
  353. }
  354. return result && hasDefault;
  355. } else if (cmd instanceof ArrayIndexAssign) {
  356. // TODO - rework!!!!!
  357. let used_dims = 0;
  358. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  359. if(typeInfo === null) {
  360. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  361. }
  362. if(typeInfo.isConst) {
  363. throw ProcessorErrorFactory.invalid_const_assignment_full(cmd.id, cmd.sourceInfo);
  364. }
  365. if(!(typeInfo.type instanceof ArrayType)) {
  366. throw ProcessorErrorFactory.invalid_array_access_full(cmd.id, cmd.sourceInfo);
  367. }
  368. const exp = cmd.expression;
  369. const lineExp = cmd.line;
  370. const lineType = this.evaluateExpressionType(lineExp);
  371. if (!lineType.isCompatible(Types.INTEGER)) {
  372. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  373. }
  374. used_dims += 1;
  375. const columnExp = cmd.column;
  376. if (typeInfo.columns === null && columnExp !== null) {
  377. throw ProcessorErrorFactory.invalid_matrix_access_full(cmd.id, cmd.sourceInfo);
  378. } else if (columnExp !== null) {
  379. const columnType = this.evaluateExpressionType(columnExp);
  380. if (!columnType.isCompatible(Types.INTEGER)) {
  381. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  382. }
  383. used_dims += 1;
  384. }
  385. // exp a single value exp or an array access
  386. const exp_type = this.evaluateExpressionType(exp);
  387. const access_type = typeInfo.type;
  388. let compatible = false;
  389. if(exp_type instanceof MultiType) {
  390. let type = access_type;
  391. if(access_type.dimensions - used_dims == 0) {
  392. type = access_type.innerType
  393. } else {
  394. type = new ArrayType(access_type.innerType, Math.max(0, access_type.dimensions - used_dims));
  395. }
  396. compatible = exp_type.isCompatible(type);
  397. } else {
  398. compatible = access_type.canAccept(exp_type, used_dims);
  399. }
  400. if(!compatible) {
  401. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(access_type, exp_type)) {
  402. const access_type_string_info = access_type.stringInfo();
  403. const access_type_info = access_type_string_info[0];
  404. const exp_type_string_info = exp_type.stringInfo();
  405. const exp_type_info = exp_type_string_info[0];
  406. throw ProcessorErrorFactory.incompatible_types_full(access_type_info.type, access_type_info.dim - used_dims, exp_type_info.type, exp_type_info.dim, exp.toString(), cmd.sourceInfo);
  407. }
  408. }
  409. return optional;
  410. } else if (cmd instanceof Assign) {
  411. // TODO - rework since there is no literal array assignment
  412. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  413. if(typeInfo === null) {
  414. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  415. }
  416. if(typeInfo.isConst) {
  417. throw ProcessorErrorFactory.invalid_const_assignment_full(cmd.id, cmd.sourceInfo);
  418. }
  419. const exp = cmd.expression;
  420. const exp_type = this.evaluateExpressionType(exp);
  421. if(exp_type instanceof ArrayType) {
  422. if(!(typeInfo.type instanceof ArrayType)) {
  423. // TODO better error message
  424. throw new Error("Cannot assign an array to a non-array variable ");
  425. }
  426. // Both are arrays...
  427. // if both don't have same dimensions and type, cannot perform assignment
  428. if(!exp_type.isCompatible(typeInfo.type)) {
  429. if(exp_type.dimensions === typeInfo.type.dimensions && !exp_type.innerType.isCompatible(typeInfo.type.innerType)) {
  430. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(typeInfo.type.innerType, exp_type.innerType)) {
  431. const stringInfo = typeInfo.type.stringInfo();
  432. const info = stringInfo[0];
  433. const exp_type_string_info = exp_type.stringInfo();
  434. const exp_type_info = exp_type_string_info[0];
  435. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, exp_type_info.type, exp_type_info.dim, exp.toString(), cmd.sourceInfo);
  436. }
  437. } else {
  438. switch(exp_type.dimensions) {
  439. case 1: {
  440. throw ProcessorErrorFactory.vector_to_matrix_attr(cmd.id, exp.toString(),cmd.sourceInfo);
  441. }
  442. case 2: {
  443. throw ProcessorErrorFactory.matrix_to_vector_attr(cmd.id, exp.toString(),cmd.sourceInfo);
  444. }
  445. }
  446. }
  447. }
  448. } else if(!exp_type.isCompatible(typeInfo.type)) {
  449. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(typeInfo.type, exp_type)) {
  450. const stringInfo = typeInfo.type.stringInfo();
  451. const info = stringInfo[0];
  452. const exp_type_string_info = exp_type.stringInfo();
  453. const exp_type_info = exp_type_string_info[0];
  454. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, exp_type_info.type, exp_type_info.dim, exp.toString(), cmd.sourceInfo);
  455. }
  456. }
  457. return optional;
  458. } else if (cmd instanceof Break) {
  459. return optional;
  460. } else if (cmd instanceof IfThenElse) {
  461. const resultType = this.evaluateExpressionType(cmd.condition);
  462. if (!resultType.isCompatible(Types.BOOLEAN)) {
  463. throw ProcessorErrorFactory.if_condition_type_full(cmd.condition.toString(), cmd.sourceInfo);
  464. }
  465. if(cmd.ifFalse instanceof IfThenElse) {
  466. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommand(type, cmd.ifFalse, optional);
  467. } else if(cmd.ifFalse != null) {
  468. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommands(type, cmd.ifFalse.commands,optional);
  469. } else {
  470. return this.checkCommands(type, cmd.ifTrue.commands, optional);
  471. }
  472. } else if (cmd instanceof FunctionCall) {
  473. let fun = null;
  474. if (cmd.isMainCall) {
  475. fun = this.getMainFunction();
  476. } else {
  477. fun = this.findFunction(cmd.id);
  478. }
  479. if(fun === null) {
  480. throw ProcessorErrorFactory.function_missing_full(cmd.id, cmd.sourceInfo);
  481. }
  482. this.assertParameters(fun, cmd.actualParameters);
  483. return optional;
  484. } else if (cmd instanceof Return) {
  485. const funcName = this.currentFunction.isMain ? LanguageDefinedFunction.getMainFunctionName() : this.currentFunction.name
  486. if (cmd.expression === null && !type.isCompatible(Types.VOID)) {
  487. const stringInfo = type.stringInfo();
  488. const info = stringInfo[0];
  489. throw ProcessorErrorFactory.invalid_void_return_full(funcName, info.type, info.dim, cmd.sourceInfo);
  490. } else if (cmd.expression !== null) {
  491. const resultType = this.evaluateExpressionType(cmd.expression);
  492. if (!type.isCompatible(resultType)) {
  493. if (!Config.enable_type_casting || !Store.canImplicitTypeCast(type, resultType)) {
  494. const stringInfo = type.stringInfo();
  495. const info = stringInfo[0];
  496. throw ProcessorErrorFactory.invalid_return_type_full(funcName, info.type, info.dim, cmd.sourceInfo);
  497. }
  498. }
  499. return true;
  500. } else {
  501. return true;
  502. }
  503. }
  504. }
  505. checkCommands (type, cmds, optional) {
  506. return cmds.reduce(
  507. (last, next) => this.checkCommand(type, next, optional) || last, optional
  508. );
  509. }
  510. /**
  511. *
  512. * @param {import('./../../ast/commands/function').Function} fun
  513. * @param {Expression[]} actualParametersList
  514. */
  515. assertParameters (fun, actualParametersList) {
  516. const parameterList = fun.formalParameters;
  517. if ((parameterList.length > actualParametersList.length)
  518. || (parameterList.length !== actualParametersList.length && !fun.hasVariadic())) {
  519. throw ProcessorErrorFactory.invalid_parameters_size_full(fun.name, actualParametersList.length,
  520. fun.formalParameters.length, null);
  521. }
  522. for (let i = 0, j = 0; i < parameterList.length && j < actualParametersList.length; i += 1, j += 1) {
  523. const formalParam = parameterList[i];
  524. if (formalParam.variadic && i + 1 !== parameterList.length) {
  525. throw "A function variadic parameter must be its last parameter!";
  526. }
  527. if (formalParam.variadic) {
  528. j = this.assertVariadicParameter(fun, formalParam, j, actualParametersList);
  529. } else {
  530. const param = actualParametersList[j];
  531. this.assertParameter(fun, formalParam, param);
  532. }
  533. }
  534. }
  535. evaluateVectorLiteralType (literal, type) {
  536. // console.log(literal);
  537. for (let i = 0; i < literal.value.length; i+=1) {
  538. const exp = literal.value[i];
  539. const expType = this.evaluateExpressionType(exp);
  540. let compatible = false;
  541. if(expType instanceof MultiType) {
  542. compatible = expType.isCompatible(type.innerType);
  543. } else {
  544. compatible = type.canAccept(expType, 1);
  545. }
  546. if (!compatible) {
  547. if (!Config.enable_type_casting || !Store.canImplicitTypeCast(type.innerType, expType)) {
  548. const stringInfo = type.stringInfo();
  549. const info = stringInfo[0];
  550. const result_string_info = expType.stringInfo();
  551. const result_info = result_string_info[0];
  552. throw ProcessorErrorFactory.incompatible_types_full(info.type, 0, result_info.type, result_info.dim, exp.toString(), literal.sourceInfo);
  553. }
  554. }
  555. }
  556. return type;
  557. }
  558. /**
  559. *
  560. * @param {import('./../../ast/commands/function').Function} fun
  561. * @param {import('./../../ast/commands/formalParameter').FormalParameter} formalParam
  562. * @param {number} index
  563. * @param {Expression[]} actualParametersList
  564. */
  565. assertVariadicParameter (fun, formalParam, index, actualParametersList) {
  566. let i;
  567. for (i = index; i < actualParametersList.length; i += 1) {
  568. this.assertParameter(fun, formalParam, actualParametersList[i]);
  569. }
  570. return i - 1;
  571. }
  572. /**
  573. *
  574. * @param {import('./../../ast/commands/function').Function} fun
  575. * @param {import('./../../ast/commands/formalParameter').FormalParameter} formalParam
  576. * @param {Expression} actualParameter
  577. */
  578. assertParameter (fun, formalParam, actualParameter) {
  579. // const id = formalParam.id;
  580. if (formalParam.byRef) {
  581. if (actualParameter instanceof VariableLiteral) {
  582. const variable = this.findSymbol(actualParameter.id, this.symbolMap);
  583. if (variable.isConst) {
  584. throw ProcessorErrorFactory.invalid_const_ref_full(fun.name, actualParameter.toString(), actualParameter.sourceInfo);
  585. }
  586. } else if (!(actualParameter instanceof VariableLiteral || actualParameter instanceof ArrayAccess)) {
  587. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, actualParameter.toString(), actualParameter.sourceInfo);
  588. }
  589. }
  590. const resultType = this.evaluateExpressionType(actualParameter);
  591. if (resultType instanceof MultiType && formalParam.type instanceof MultiType) {
  592. let shared = 0
  593. for (let j = 0; j < resultType.types.length; ++j) {
  594. const element = resultType.types[j];
  595. if (formalParam.type.types.indexOf(element) !== -1) {
  596. shared += 1;
  597. }
  598. }
  599. if (shared <= 0) {
  600. if (Config.enable_type_casting && !formalParam.byRef) {
  601. if (!resultType.isCompatible(Types.INTEGER) && !resultType.isCompatible(Types.REAL)
  602. || (formalParam.type.isCompatible(Types.INTEGER) || formalParam.type.isCompatible(Types.REAL))) {
  603. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, actualParameter.toString(),
  604. actualParameter.sourceInfo);
  605. }
  606. }
  607. }
  608. } else if (resultType instanceof MultiType) {
  609. if(!resultType.isCompatible(formalParam.type)) {
  610. if (Config.enable_type_casting && !formalParam.byRef) {
  611. if (!resultType.isCompatible(Types.INTEGER) && !resultType.isCompatible(Types.REAL)
  612. || (formalParam.type.isCompatible(Types.INTEGER) || formalParam.type.isCompatible(Types.REAL))) {
  613. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, actualParameter.toString(),
  614. actualParameter.sourceInfo);
  615. }
  616. }
  617. }
  618. } else if (!formalParam.type.isCompatible(resultType)) {
  619. if (Config.enable_type_casting && !formalParam.byRef) {
  620. if (!Store.canImplicitTypeCast(formalParam.type, resultType)) {
  621. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, actualParameter.toString(), actualParameter.sourceInfo);
  622. }
  623. }
  624. }
  625. }
  626. }