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