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, 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. this.checkCommand(type, cmd.assignment, optional);
  306. const resultType = this.evaluateExpressionType(cmd.condition);
  307. if (!resultType.isCompatible(Types.BOOLEAN)) {
  308. throw ProcessorErrorFactory.for_condition_type_full(cmd.condition.toString(), cmd.sourceInfo);
  309. }
  310. this.checkCommand(type, cmd.increment, optional);
  311. this.checkCommands(type, cmd.commands, optional);
  312. return false;
  313. } else if (cmd instanceof Switch) {
  314. const sType = this.evaluateExpressionType(cmd.expression);
  315. let result = optional;
  316. let hasDefault = false;
  317. for (let i = 0; i < cmd.cases.length; i++) {
  318. const aCase = cmd.cases[i];
  319. if (aCase.expression !== null) {
  320. const caseType = this.evaluateExpressionType(aCase.expression);
  321. if (!sType.isCompatible(caseType)) {
  322. const strInfo = sType.stringInfo();
  323. const info = strInfo[0];
  324. const strExp = aCase.expression.toString();
  325. throw ProcessorErrorFactory.invalid_case_type_full(strExp, info.type, info.dim, aCase.sourceInfo);
  326. }
  327. } else {
  328. hasDefault = true;
  329. }
  330. result = result && this.checkCommands(type, aCase.commands, result);
  331. }
  332. return result && hasDefault;
  333. } else if (cmd instanceof ArrayIndexAssign) {
  334. // TODO - rework!!!!!
  335. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  336. if(typeInfo === null) {
  337. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  338. }
  339. if(!(typeInfo.type instanceof ArrayType)) {
  340. throw ProcessorErrorFactory.invalid_array_access_full(cmd.id, cmd.sourceInfo);
  341. }
  342. const exp = cmd.expression;
  343. const lineExp = cmd.line;
  344. const lineType = this.evaluateExpressionType(lineExp);
  345. if (!lineType.isCompatible(Types.INTEGER)) {
  346. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  347. }
  348. const columnExp = cmd.column;
  349. if (typeInfo.columns === null && columnExp !== null) {
  350. throw ProcessorErrorFactory.invalid_matrix_access_full(cmd.id, cmd.sourceInfo);
  351. } else if (!typeInfo.type.isVector && columnExp == null) {
  352. throw new Error("Cannot assign to matrix line");
  353. } else if (columnExp !== null) {
  354. const columnType = this.evaluateExpressionType(columnExp);
  355. if (!columnType.isCompatible(Types.INTEGER)) {
  356. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  357. }
  358. }
  359. // exp a single value exp or an array access
  360. const exp_type = this.evaluateExpressionType(exp);
  361. if(exp_type instanceof ArrayType) {
  362. // TODO better error message
  363. throw new Error("Cannot assign a matrix/vector");
  364. }
  365. let compatible = false;
  366. if(exp_type instanceof MultiType) {
  367. compatible = exp_type.isCompatible(typeInfo.type.innerType);
  368. } else {
  369. compatible = typeInfo.type.canAccept(exp_type);
  370. }
  371. if(!compatible) {
  372. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(typeInfo.type.innerType, exp_type)) {
  373. throw new Error("invalid vector element type");
  374. }
  375. }
  376. return optional;
  377. } else if (cmd instanceof Assign) {
  378. // TODO - rework since there is no literal array assignment
  379. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  380. if(typeInfo === null) {
  381. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  382. }
  383. const exp = cmd.expression;
  384. const exp_type = this.evaluateExpressionType(exp);
  385. if(exp_type instanceof ArrayType) {
  386. if(!(typeInfo.type instanceof ArrayType)) {
  387. // TODO better error message
  388. throw new Error("Cannot assign an array to a non-array variable ");
  389. }
  390. // Both are arrays...
  391. // if both don't have same dimensions and type, cannot perform assignment
  392. if(!exp_type.isCompatible(typeInfo.type)) {
  393. if(exp_type.dimensions === typeInfo.type.dimensions && !exp_type.innerType.isCompatible(typeInfo.type.innerType)) {
  394. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(typeInfo.type.innerType, exp_type.innerType)) {
  395. const stringInfo = typeInfo.type.stringInfo();
  396. const info = stringInfo[0];
  397. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo);
  398. }
  399. } else {
  400. // TODO better error message
  401. throw new Error("Cannot assign a vector to matrix or matrix to vector");
  402. }
  403. }
  404. } else if(!exp_type.isCompatible(typeInfo.type)) {
  405. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(typeInfo.type, exp_type)) {
  406. const stringInfo = typeInfo.type.stringInfo();
  407. const info = stringInfo[0];
  408. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo);
  409. }
  410. }
  411. return optional;
  412. } else if (cmd instanceof Break) {
  413. return optional;
  414. } else if (cmd instanceof IfThenElse) {
  415. const resultType = this.evaluateExpressionType(cmd.condition);
  416. if (!resultType.isCompatible(Types.BOOLEAN)) {
  417. throw ProcessorErrorFactory.if_condition_type_full(cmd.condition.toString(), cmd.sourceInfo);
  418. }
  419. if(cmd.ifFalse instanceof IfThenElse) {
  420. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommand(type, cmd.ifFalse, optional);
  421. } else if(cmd.ifFalse != null) {
  422. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommands(type, cmd.ifFalse.commands,optional);
  423. } else {
  424. return this.checkCommands(type, cmd.ifTrue.commands, optional);
  425. }
  426. } else if (cmd instanceof FunctionCall) {
  427. let fun = null;
  428. if (cmd.isMainCall) {
  429. fun = this.getMainFunction();
  430. } else {
  431. fun = this.findFunction(cmd.id);
  432. }
  433. if(fun === null) {
  434. throw ProcessorErrorFactory.function_missing_full(cmd.id, cmd.sourceInfo);
  435. }
  436. this.assertParameters(fun, cmd.actualParameters);
  437. return optional;
  438. } else if (cmd instanceof Return) {
  439. const funcName = this.currentFunction.isMain ? LanguageDefinedFunction.getMainFunctionName() : this.currentFunction.name
  440. if (cmd.expression === null && !type.isCompatible(Types.VOID)) {
  441. const stringInfo = type.stringInfo();
  442. const info = stringInfo[0];
  443. throw ProcessorErrorFactory.invalid_void_return_full(funcName, info.type, info.dim, cmd.sourceInfo);
  444. } else if (cmd.expression !== null) {
  445. const resultType = this.evaluateExpressionType(cmd.expression);
  446. if (!type.isCompatible(resultType)) {
  447. const stringInfo = type.stringInfo();
  448. const info = stringInfo[0];
  449. throw ProcessorErrorFactory.invalid_return_type_full(funcName, info.type, info.dim, cmd.sourceInfo);
  450. } else {
  451. return true;
  452. }
  453. } else {
  454. return true;
  455. }
  456. }
  457. }
  458. checkCommands (type, cmds, optional) {
  459. return cmds.reduce(
  460. (last, next) => this.checkCommand(type, next, optional) || last, optional
  461. );
  462. }
  463. assertParameters (fun, actualParametersList) {
  464. if (fun.formalParameters.length !== actualParametersList.length) {
  465. throw ProcessorErrorFactory.invalid_parameters_size_full(fun.name, actualParametersList.length, fun.formalParameters.length, null);
  466. }
  467. for (let i = 0; i < actualParametersList.length; ++i) {
  468. const param = actualParametersList[i];
  469. const formalParam = fun.formalParameters[i];
  470. // const id = formalParam.id;
  471. if(formalParam.byRef) {
  472. if(param instanceof VariableLiteral) {
  473. const variable = this.findSymbol(param.id, this.symbolMap);
  474. if (variable.isConst) {
  475. throw ProcessorErrorFactory.invalid_const_ref_full(fun.name, param.toString(), param.sourceInfo);
  476. }
  477. } else if (!(param instanceof VariableLiteral || param instanceof ArrayAccess)) {
  478. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, param.toString(), param.sourceInfo);
  479. }
  480. }
  481. const resultType = this.evaluateExpressionType(param);
  482. if(resultType instanceof MultiType && formalParam.type instanceof MultiType) {
  483. let shared = 0
  484. for (let j = 0; j < resultType.types.length; ++j) {
  485. const element = resultType.types[j];
  486. if(formalParam.type.types.indexOf(element) !== -1) {
  487. shared += 1;
  488. }
  489. }
  490. if(shared <= 0) {
  491. if(Config.enable_type_casting && !formalParam.byRef) {
  492. if(resultType.isCompatible(Types.INTEGER) || resultType.isCompatible(Types.REAL)) {
  493. if(formalParam.type.isCompatible(Types.INTEGER) || formalParam.type.isCompatible(Types.REAL)) {
  494. continue;
  495. }
  496. }
  497. }
  498. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, param.toString(), param.sourceInfo);
  499. }
  500. } else if (resultType instanceof MultiType) {
  501. if(!resultType.isCompatible(formalParam.type)) {
  502. if(Config.enable_type_casting && !formalParam.byRef) {
  503. if(resultType.isCompatible(Types.INTEGER) || resultType.isCompatible(Types.REAL)) {
  504. if(formalParam.type.isCompatible(Types.INTEGER) || formalParam.type.isCompatible(Types.REAL)) {
  505. continue;
  506. }
  507. }
  508. }
  509. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, param.toString(), param.sourceInfo);
  510. }
  511. } else if(!formalParam.type.isCompatible(resultType)) {
  512. if(Config.enable_type_casting && !formalParam.byRef) {
  513. if (Store.canImplicitTypeCast(formalParam.type, resultType)) {
  514. continue;
  515. }
  516. }
  517. throw ProcessorErrorFactory.invalid_parameter_type_full(fun.name, param.toString(), param.sourceInfo);
  518. }
  519. }
  520. }
  521. evaluateVectorLiteralType (literal, type) {
  522. for(let i = 0; i < literal.value.length; i+=1) {
  523. const exp = literal.value[i];
  524. const expType = this.evaluateExpressionType(exp);
  525. let compatible = false;
  526. if(expType instanceof MultiType) {
  527. compatible = expType.isCompatible(type.innerType);
  528. } else {
  529. compatible = type.canAccept(expType);
  530. }
  531. if(!compatible) {
  532. // vector wrong type
  533. // TODO better error message
  534. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(type.innerType, expType)) {
  535. throw new Error("invalid vector element type");
  536. }
  537. }
  538. }
  539. return type;
  540. }
  541. }