1
0

semanticAnalyser.js 26 KB

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