semanticAnalyser.js 25 KB

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