semanticAnalyser.js 23 KB

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