semanticAnalyser.js 23 KB

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