semanticAnalyser.js 23 KB

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