semanticAnalyser.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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, 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 { ArrayType } from '../../typeSystem/array_type';
  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. this.assertArrayDeclaration(declaration);
  95. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines,
  96. columns: declaration.columns, type: declaration.type, isConst: declaration.isConst});
  97. } else {
  98. if(declaration.initial === null) {
  99. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type, isConst: declaration.isConst});
  100. return;
  101. }
  102. const resultType = this.evaluateExpressionType(declaration.initial);
  103. if(resultType instanceof MultiType) {
  104. if(!resultType.isCompatible(declaration.type)) {
  105. const stringInfo = declaration.type.stringInfo();
  106. const info = stringInfo[0];
  107. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, declaration.sourceInfo);
  108. }
  109. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type, isConst: declaration.isConst})
  110. } else if((!declaration.type.isCompatible(resultType) && !Config.enable_type_casting)
  111. || (!declaration.type.isCompatible(resultType) && Config.enable_type_casting
  112. && !Store.canImplicitTypeCast(declaration.type, resultType))) {
  113. const stringInfo = declaration.type.stringInfo();
  114. const info = stringInfo[0];
  115. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, declaration.sourceInfo);
  116. } else {
  117. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type, isConst: declaration.isConst});
  118. }
  119. }
  120. }
  121. assertArrayDeclaration (declaration) {
  122. if(declaration.initial === null) {
  123. const lineType = this.evaluateExpressionType(declaration.lines);
  124. if (!lineType.isCompatible(Types.INTEGER)) {
  125. throw ProcessorErrorFactory.array_dimension_not_int_full(declaration.sourceInfo);
  126. }
  127. if (declaration.columns !== null) {
  128. const columnType = this.evaluateExpressionType(declaration.columns);
  129. if (!columnType.isCompatible(Types.INTEGER)) {
  130. throw ProcessorErrorFactory.array_dimension_not_int_full(declaration.sourceInfo);
  131. }
  132. }
  133. } else {
  134. this.evaluateArrayLiteral(declaration);
  135. }
  136. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type});
  137. return;
  138. }
  139. evaluateExpressionType (expression) {
  140. // TODO: Throw operator error in case type == UNDEFINED
  141. if(expression instanceof UnaryApp) {
  142. const op = expression.op;
  143. const resultType = this.evaluateExpressionType(expression.left);
  144. const finalResult = resultTypeAfterUnaryOp(op, resultType);
  145. if (Types.UNDEFINED.isCompatible(finalResult)) {
  146. const stringInfo = resultType.stringInfo();
  147. const info = stringInfo[0];
  148. const expString = expression.toString();
  149. throw ProcessorErrorFactory.invalid_unary_op_full(expString, op, info.type, info.dim, expression.sourceInfo);
  150. }
  151. return finalResult;
  152. } else if (expression instanceof InfixApp) {
  153. const op = expression.op;
  154. const resultTypeLeft = this.evaluateExpressionType(expression.left);
  155. const resultTypeRight = this.evaluateExpressionType(expression.right);
  156. const finalResult = resultTypeAfterInfixOp(op, resultTypeLeft, resultTypeRight);
  157. if (Types.UNDEFINED.isCompatible(finalResult)) {
  158. const stringInfoLeft = resultTypeLeft.stringInfo();
  159. const infoLeft = stringInfoLeft[0];
  160. const stringInfoRight = resultTypeRight.stringInfo();
  161. const infoRight = stringInfoRight[0];
  162. const expString = expression.toString();
  163. throw ProcessorErrorFactory.invalid_infix_op_full(expString,op, infoLeft.type, infoLeft.dim, infoRight.type, infoRight.dim, expression.sourceInfo);
  164. }
  165. return finalResult;
  166. } else if (expression instanceof Literal) {
  167. return this.evaluateLiteralType(expression);
  168. } else if (expression instanceof FunctionCall) {
  169. if (expression.isMainCall) {
  170. throw ProcessorErrorFactory.void_in_expression_full(LanguageDefinedFunction.getMainFunctionName(), expression.sourceInfo);
  171. }
  172. const fun = this.findFunction(expression.id);
  173. if(fun === null) {
  174. throw ProcessorErrorFactory.function_missing_full(expression.id, expression.sourceInfo);
  175. }
  176. if (fun.returnType.isCompatible(Types.VOID)) {
  177. throw ProcessorErrorFactory.void_in_expression_full(expression.id, expression.sourceInfo);
  178. }
  179. this.assertParameters(fun, expression.actualParameters);
  180. return fun.returnType;
  181. } else if (expression instanceof ArrayAccess) {
  182. const arrayTypeInfo = this.findSymbol(expression.id, this.symbolMap);
  183. if(arrayTypeInfo === null) {
  184. throw ProcessorErrorFactory.symbol_not_found_full(expression.id, expression.sourceInfo);
  185. }
  186. if (!(arrayTypeInfo.type instanceof ArrayType)) {
  187. throw ProcessorErrorFactory.invalid_array_access_full(expression.id, expression.sourceInfo);
  188. }
  189. const lineType = this.evaluateExpressionType(expression.line);
  190. if (!lineType.isCompatible(Types.INTEGER)) {
  191. throw ProcessorErrorFactory.array_dimension_not_int_full(expression.sourceInfo);
  192. }
  193. if (expression.column !== null) {
  194. if (arrayTypeInfo.columns === null) {
  195. throw ProcessorErrorFactory.invalid_matrix_access_full(expression.id, expression.sourceInfo);
  196. }
  197. const columnType = this.evaluateExpressionType(expression.column);
  198. if(!columnType.isCompatible(Types.INTEGER)) {
  199. throw ProcessorErrorFactory.array_dimension_not_int_full(expression.sourceInfo);
  200. }
  201. }
  202. const arrType = arrayTypeInfo.type;
  203. if(expression.column !== null) {
  204. // indexing matrix
  205. return arrType.innerType;
  206. } else {
  207. if(arrayTypeInfo.columns === null) {
  208. return arrType.innerType;
  209. }
  210. return new ArrayType(arrType.innerType, 1);
  211. }
  212. }
  213. }
  214. evaluateLiteralType (literal) {
  215. if(literal instanceof IntLiteral) {
  216. return literal.type;
  217. } else if (literal instanceof RealLiteral) {
  218. return literal.type;
  219. } else if (literal instanceof StringLiteral) {
  220. return literal.type;
  221. } else if (literal instanceof BoolLiteral) {
  222. return literal.type;
  223. } else if (literal instanceof VariableLiteral) {
  224. const typeInfo = this.findSymbol(literal.id, this.symbolMap);
  225. if(typeInfo === null) {
  226. throw ProcessorErrorFactory.symbol_not_found_full(literal.id, literal.sourceInfo);
  227. }
  228. if (typeInfo.type instanceof ArrayType) {
  229. return typeInfo.type;
  230. }
  231. return typeInfo.type;
  232. } else {
  233. console.warn("Evaluating type only for an array literal...");
  234. let last = null;
  235. if(literal.value.length === 1) {
  236. last = this.evaluateExpressionType(literal.value[0]);
  237. } else {
  238. for (let i = 0; i < literal.value.length; i++) {
  239. const e = this.evaluateExpressionType(literal.value[i]);
  240. if(last === null) {
  241. last = e;
  242. } else if(!last.isCompatible(e)) {
  243. const strInfo = last.stringInfo();
  244. const info = strInfo[0];
  245. const strExp = literal.toString();
  246. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  247. }
  248. }
  249. }
  250. if(last instanceof ArrayType) {
  251. return new ArrayType(last.innerType, last.dimensions + 1);
  252. }
  253. return new ArrayType(last, 1);
  254. }
  255. }
  256. evaluateArrayLiteral (arrayDeclaration) {
  257. const type = arrayDeclaration.type;
  258. const literal = arrayDeclaration.initial;
  259. // console.log(arrayDeclaration);
  260. if(arrayDeclaration.isVector) {
  261. this.evaluateVectorLiteralType(literal, type);
  262. } else {
  263. // TODO matrix type check
  264. for(let i = 0; i < literal.lines; ++i) {
  265. const line_literal = literal.value[i];
  266. this.evaluateVectorLiteralType(line_literal, new ArrayType(type.innerType, 1));
  267. }
  268. }
  269. return true;
  270. }
  271. assertFunction (fun) {
  272. this.pushMap();
  273. this.currentFunction = fun;
  274. fun.formalParameters.forEach(formalParam => {
  275. if(formalParam.type instanceof ArrayType) {
  276. if(formalParam.type.dimensions > 1) {
  277. this.insertSymbol(formalParam.id, {id: formalParam.id, lines: -1, columns: -1, type: formalParam.type});
  278. } else {
  279. this.insertSymbol(formalParam.id, {id: formalParam.id, lines: -1, columns: null, type: formalParam.type});
  280. }
  281. } else {
  282. this.insertSymbol(formalParam.id, {id: formalParam.id, type: formalParam.type});
  283. }
  284. })
  285. this.assertDeclarations(fun.variablesDeclarations);
  286. const optional = fun.returnType.isCompatible(Types.VOID);
  287. const valid = this.assertReturn(fun, optional);
  288. if (!valid) {
  289. throw ProcessorErrorFactory.function_no_return(fun.name);
  290. }
  291. this.popMap();
  292. }
  293. assertReturn (fun, optional) {
  294. return fun.commands.reduce(
  295. (last, next) => this.checkCommand(fun.returnType, next, optional) || last, optional
  296. );
  297. }
  298. checkCommand (type, cmd, optional) {
  299. if (cmd instanceof While) {
  300. const resultType = this.evaluateExpressionType(cmd.expression);
  301. if (!resultType.isCompatible(Types.BOOLEAN)) {
  302. throw ProcessorErrorFactory.loop_condition_type_full(cmd.expression.toString(), cmd.sourceInfo);
  303. }
  304. this.checkCommands(type, cmd.commands, optional);
  305. return false;
  306. } else if (cmd instanceof For) {
  307. console.log(cmd);
  308. const var_type = this.evaluateExpressionType(cmd.for_id);
  309. if (!var_type.isCompatible(Types.INTEGER)) {
  310. // TODO better error message
  311. throw new Error("A variavel do comando repita_para deve ser do tipo inteiro");
  312. }
  313. const from_type = this.evaluateExpressionType(cmd.for_from);
  314. if (!from_type.isCompatible(Types.INTEGER)) {
  315. // TODO better error message
  316. throw new Error("o paramentro 'de' do comando repita_para deve ser do tipo inteiro");
  317. }
  318. const to_type = this.evaluateExpressionType(cmd.for_to);
  319. if (!to_type.isCompatible(Types.INTEGER)) {
  320. // TODO better error message
  321. throw new Error("o paramentro 'ate' do comando repita_para deve ser do tipo inteiro");
  322. }
  323. if (cmd.for_pass != null) {
  324. const pass_type = this.evaluateExpressionType(cmd.for_pass);
  325. if (!pass_type.isCompatible(Types.INTEGER)) {
  326. // TODO better error message
  327. throw new Error("o paramentro 'passo' do comando repita_para deve ser do tipo inteiro");
  328. }
  329. }
  330. this.checkCommands(type, cmd.commands, optional);
  331. return false;
  332. } else if (cmd instanceof Switch) {
  333. const sType = this.evaluateExpressionType(cmd.expression);
  334. let result = optional;
  335. let hasDefault = false;
  336. for (let i = 0; i < cmd.cases.length; i++) {
  337. const aCase = cmd.cases[i];
  338. if (aCase.expression !== null) {
  339. const caseType = this.evaluateExpressionType(aCase.expression);
  340. if (!sType.isCompatible(caseType)) {
  341. const strInfo = sType.stringInfo();
  342. const info = strInfo[0];
  343. const strExp = aCase.expression.toString();
  344. throw ProcessorErrorFactory.invalid_case_type_full(strExp, info.type, info.dim, aCase.sourceInfo);
  345. }
  346. } else {
  347. hasDefault = true;
  348. }
  349. result = result && this.checkCommands(type, aCase.commands, result);
  350. }
  351. return result && hasDefault;
  352. } else if (cmd instanceof ArrayIndexAssign) {
  353. // TODO - rework!!!!!
  354. let used_dims = 0;
  355. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  356. if(typeInfo === null) {
  357. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  358. }
  359. if(typeInfo.isConst) {
  360. throw ProcessorErrorFactory.invalid_const_assignment_full(cmd.id, cmd.sourceInfo);
  361. }
  362. if(!(typeInfo.type instanceof ArrayType)) {
  363. throw ProcessorErrorFactory.invalid_array_access_full(cmd.id, cmd.sourceInfo);
  364. }
  365. const exp = cmd.expression;
  366. const lineExp = cmd.line;
  367. const lineType = this.evaluateExpressionType(lineExp);
  368. if (!lineType.isCompatible(Types.INTEGER)) {
  369. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  370. }
  371. used_dims += 1;
  372. const columnExp = cmd.column;
  373. if (typeInfo.columns === null && columnExp !== null) {
  374. throw ProcessorErrorFactory.invalid_matrix_access_full(cmd.id, cmd.sourceInfo);
  375. } else if (columnExp !== null) {
  376. const columnType = this.evaluateExpressionType(columnExp);
  377. if (!columnType.isCompatible(Types.INTEGER)) {
  378. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  379. }
  380. used_dims += 1;
  381. }
  382. // exp a single value exp or an array access
  383. const exp_type = this.evaluateExpressionType(exp);
  384. const access_type = typeInfo.type;
  385. let compatible = false;
  386. if(exp_type instanceof MultiType) {
  387. let type = access_type;
  388. if(access_type.dimensions - used_dims == 0) {
  389. type = access_type.innerType
  390. } else {
  391. type = new ArrayType(access_type.innerType, Math.max(0, access_type.dimensions - used_dims));
  392. }
  393. compatible = exp_type.isCompatible(type);
  394. } else {
  395. compatible = access_type.canAccept(exp_type, used_dims);
  396. }
  397. if(!compatible) {
  398. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(access_type, exp_type)) {
  399. throw new Error("invalid vector element type");
  400. }
  401. }
  402. return optional;
  403. } else if (cmd instanceof Assign) {
  404. // TODO - rework since there is no literal array assignment
  405. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  406. if(typeInfo === null) {
  407. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  408. }
  409. if(typeInfo.isConst) {
  410. throw ProcessorErrorFactory.invalid_const_assignment_full(cmd.id, cmd.sourceInfo);
  411. }
  412. const exp = cmd.expression;
  413. const exp_type = this.evaluateExpressionType(exp);
  414. if(exp_type instanceof ArrayType) {
  415. if(!(typeInfo.type instanceof ArrayType)) {
  416. // TODO better error message
  417. throw new Error("Cannot assign an array to a non-array variable ");
  418. }
  419. // Both are arrays...
  420. // if both don't have same dimensions and type, cannot perform assignment
  421. if(!exp_type.isCompatible(typeInfo.type)) {
  422. if(exp_type.dimensions === typeInfo.type.dimensions && !exp_type.innerType.isCompatible(typeInfo.type.innerType)) {
  423. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(typeInfo.type.innerType, exp_type.innerType)) {
  424. const stringInfo = typeInfo.type.stringInfo();
  425. const info = stringInfo[0];
  426. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo);
  427. }
  428. } else {
  429. // TODO better error message
  430. throw new Error("Cannot assign a vector to matrix or matrix to vector");
  431. }
  432. }
  433. } else if(!exp_type.isCompatible(typeInfo.type)) {
  434. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(typeInfo.type, exp_type)) {
  435. const stringInfo = typeInfo.type.stringInfo();
  436. const info = stringInfo[0];
  437. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo);
  438. }
  439. }
  440. return optional;
  441. } else if (cmd instanceof Break) {
  442. return optional;
  443. } else if (cmd instanceof IfThenElse) {
  444. const resultType = this.evaluateExpressionType(cmd.condition);
  445. if (!resultType.isCompatible(Types.BOOLEAN)) {
  446. throw ProcessorErrorFactory.if_condition_type_full(cmd.condition.toString(), cmd.sourceInfo);
  447. }
  448. if(cmd.ifFalse instanceof IfThenElse) {
  449. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommand(type, cmd.ifFalse, optional);
  450. } else if(cmd.ifFalse != null) {
  451. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommands(type, cmd.ifFalse.commands,optional);
  452. } else {
  453. return this.checkCommands(type, cmd.ifTrue.commands, optional);
  454. }
  455. } else if (cmd instanceof FunctionCall) {
  456. let fun = null;
  457. if (cmd.isMainCall) {
  458. fun = this.getMainFunction();
  459. } else {
  460. fun = this.findFunction(cmd.id);
  461. }
  462. if(fun === null) {
  463. throw ProcessorErrorFactory.function_missing_full(cmd.id, cmd.sourceInfo);
  464. }
  465. this.assertParameters(fun, cmd.actualParameters);
  466. return optional;
  467. } else if (cmd instanceof Return) {
  468. const funcName = this.currentFunction.isMain ? LanguageDefinedFunction.getMainFunctionName() : this.currentFunction.name
  469. if (cmd.expression === null && !type.isCompatible(Types.VOID)) {
  470. const stringInfo = type.stringInfo();
  471. const info = stringInfo[0];
  472. throw ProcessorErrorFactory.invalid_void_return_full(funcName, info.type, info.dim, cmd.sourceInfo);
  473. } else if (cmd.expression !== null) {
  474. const resultType = this.evaluateExpressionType(cmd.expression);
  475. if (!type.isCompatible(resultType)) {
  476. const stringInfo = type.stringInfo();
  477. const info = stringInfo[0];
  478. throw ProcessorErrorFactory.invalid_return_type_full(funcName, info.type, info.dim, cmd.sourceInfo);
  479. } else {
  480. return true;
  481. }
  482. } else {
  483. return true;
  484. }
  485. }
  486. }
  487. checkCommands (type, cmds, optional) {
  488. return cmds.reduce(
  489. (last, next) => this.checkCommand(type, next, optional) || last, optional
  490. );
  491. }
  492. assertParameters (fun, actualParametersList) {
  493. if (fun.formalParameters.length !== actualParametersList.length) {
  494. throw ProcessorErrorFactory.invalid_parameters_size_full(fun.name, actualParametersList.length, fun.formalParameters.length, null);
  495. }
  496. for (let i = 0; i < actualParametersList.length; ++i) {
  497. const param = actualParametersList[i];
  498. const formalParam = fun.formalParameters[i];
  499. // const id = formalParam.id;
  500. if(formalParam.byRef) {
  501. if(param instanceof VariableLiteral) {
  502. const variable = this.findSymbol(param.id, this.symbolMap);
  503. if (variable.isConst) {
  504. throw ProcessorErrorFactory.invalid_const_ref_full(fun.name, param.toString(), param.sourceInfo);
  505. }
  506. } else if (!(param instanceof VariableLiteral || param instanceof ArrayAccess)) {
  507. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, param.toString(), param.sourceInfo);
  508. }
  509. }
  510. const resultType = this.evaluateExpressionType(param);
  511. if(resultType instanceof MultiType && formalParam.type instanceof MultiType) {
  512. let shared = 0
  513. for (let j = 0; j < resultType.types.length; ++j) {
  514. const element = resultType.types[j];
  515. if(formalParam.type.types.indexOf(element) !== -1) {
  516. shared += 1;
  517. }
  518. }
  519. if(shared <= 0) {
  520. if(Config.enable_type_casting && !formalParam.byRef) {
  521. if(resultType.isCompatible(Types.INTEGER) || resultType.isCompatible(Types.REAL)) {
  522. if(formalParam.type.isCompatible(Types.INTEGER) || formalParam.type.isCompatible(Types.REAL)) {
  523. continue;
  524. }
  525. }
  526. }
  527. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, param.toString(), param.sourceInfo);
  528. }
  529. } else if (resultType instanceof MultiType) {
  530. if(!resultType.isCompatible(formalParam.type)) {
  531. if(Config.enable_type_casting && !formalParam.byRef) {
  532. if(resultType.isCompatible(Types.INTEGER) || resultType.isCompatible(Types.REAL)) {
  533. if(formalParam.type.isCompatible(Types.INTEGER) || formalParam.type.isCompatible(Types.REAL)) {
  534. continue;
  535. }
  536. }
  537. }
  538. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, param.toString(), param.sourceInfo);
  539. }
  540. } else if(!formalParam.type.isCompatible(resultType)) {
  541. if(Config.enable_type_casting && !formalParam.byRef) {
  542. if (Store.canImplicitTypeCast(formalParam.type, resultType)) {
  543. continue;
  544. }
  545. }
  546. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, param.toString(), param.sourceInfo);
  547. }
  548. }
  549. }
  550. evaluateVectorLiteralType (literal, type) {
  551. // console.log(literal);
  552. for(let i = 0; i < literal.value.length; i+=1) {
  553. const exp = literal.value[i];
  554. const expType = this.evaluateExpressionType(exp);
  555. let compatible = false;
  556. if(expType instanceof MultiType) {
  557. compatible = expType.isCompatible(type.innerType);
  558. } else {
  559. compatible = type.canAccept(expType, 1);
  560. }
  561. if(!compatible) {
  562. // vector wrong type
  563. // TODO better error message
  564. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(type.innerType, expType)) {
  565. throw new Error("invalid vector element type");
  566. }
  567. }
  568. }
  569. return type;
  570. }
  571. }