semanticAnalyser.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. import { ProcessorErrorFactory } from "./../error/processorErrorFactory";
  2. import { LanguageDefinedFunction } from "./../definedFunctions";
  3. import {
  4. ArrayDeclaration,
  5. While,
  6. For,
  7. Switch,
  8. Assign,
  9. Break,
  10. IfThenElse,
  11. Return,
  12. ArrayIndexAssign,
  13. } from "../../ast/commands";
  14. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  15. import { Expression } from "./../../ast/expressions/expression";
  16. import {
  17. InfixApp,
  18. UnaryApp,
  19. FunctionCall,
  20. IntLiteral,
  21. RealLiteral,
  22. StringLiteral,
  23. BoolLiteral,
  24. VariableLiteral,
  25. ArrayAccess,
  26. CharLiteral,
  27. } from "../../ast/expressions";
  28. import { Literal } from "../../ast/expressions/literal";
  29. import {
  30. resultTypeAfterInfixOp,
  31. resultTypeAfterUnaryOp,
  32. } from "../compatibilityTable";
  33. import { Types } from "../../typeSystem/types";
  34. import { ArrayType } from "../../typeSystem/array_type";
  35. import { MultiType } from "../../typeSystem/multiType";
  36. import { Config } from "../../util/config";
  37. import { Store } from "../store/store";
  38. import { IVProgParser } from "../../ast/ivprogParser";
  39. export class SemanticAnalyser {
  40. static analyseFromSource (stringCode) {
  41. const parser = IVProgParser.createParser(stringCode);
  42. const semantic = new SemanticAnalyser(parser.parseTree());
  43. return semantic.analyseTree();
  44. }
  45. constructor (ast) {
  46. this.ast = ast;
  47. this.symbolMap = null;
  48. this.currentFunction = null;
  49. }
  50. pushMap () {
  51. if (this.symbolMap === null) {
  52. this.symbolMap = { map: {}, next: null };
  53. } else {
  54. const n = { map: {}, next: this.symbolMap };
  55. this.symbolMap = n;
  56. }
  57. }
  58. popMap () {
  59. if (this.symbolMap !== null) {
  60. this.symbolMap = this.symbolMap.next;
  61. }
  62. }
  63. insertSymbol (id, typeInfo) {
  64. this.symbolMap.map[id] = typeInfo;
  65. }
  66. findSymbol (id, symbol_map) {
  67. if (!symbol_map.map[id]) {
  68. if (symbol_map.next) {
  69. return this.findSymbol(id, symbol_map.next);
  70. }
  71. return null;
  72. } else {
  73. return symbol_map.map[id];
  74. }
  75. }
  76. getMainFunction () {
  77. return this.ast.functions.find((v) => v.isMain);
  78. }
  79. findFunction (name) {
  80. if (name.match(/^\$.+$/)) {
  81. const fun = LanguageDefinedFunction.getFunction(name);
  82. if (!fun) {
  83. throw ProcessorErrorFactory.not_implemented(name);
  84. }
  85. return fun;
  86. } else {
  87. const val = this.ast.functions.find((v) => v.name === name);
  88. if (!val) {
  89. return null;
  90. }
  91. return val;
  92. }
  93. }
  94. analyseTree () {
  95. const globalVars = this.ast.global;
  96. this.pushMap();
  97. this.assertDeclarations(globalVars);
  98. const functions = this.ast.functions;
  99. const mainFunc = functions.filter((f) => f.name === null);
  100. if (mainFunc.length <= 0) {
  101. throw ProcessorErrorFactory.main_missing();
  102. }
  103. for (let i = 0; i < functions.length; i++) {
  104. const fun = functions[i];
  105. this.assertFunction(fun);
  106. }
  107. return this.ast;
  108. }
  109. assertDeclarations (list) {
  110. for (let i = 0; i < list.length; i++) {
  111. this.assertDeclaration(list[i]);
  112. }
  113. }
  114. assertDeclaration (declaration) {
  115. if (declaration instanceof ArrayDeclaration) {
  116. this.assertArrayDeclaration(declaration);
  117. this.insertSymbol(declaration.id, {
  118. id: declaration.id,
  119. lines: declaration.lines,
  120. columns: declaration.columns,
  121. type: declaration.type,
  122. isConst: declaration.isConst,
  123. });
  124. } else {
  125. if (declaration.initial === null) {
  126. this.insertSymbol(declaration.id, {
  127. id: declaration.id,
  128. type: declaration.type,
  129. isConst: declaration.isConst,
  130. });
  131. return;
  132. }
  133. const resultType = this.evaluateExpressionType(declaration.initial);
  134. if (resultType instanceof MultiType) {
  135. if (!resultType.isCompatible(declaration.type)) {
  136. const stringInfo = declaration.type.stringInfo();
  137. const info = stringInfo[0];
  138. const result_string_info = resultType.stringInfo();
  139. const result_info = result_string_info[0];
  140. const exp = declaration.initial;
  141. throw ProcessorErrorFactory.incompatible_types_full(
  142. info.type,
  143. info.dim,
  144. result_info.type,
  145. result_info.dim,
  146. exp.toString(),
  147. declaration.sourceInfo
  148. );
  149. }
  150. this.insertSymbol(declaration.id, {
  151. id: declaration.id,
  152. type: declaration.type,
  153. isConst: declaration.isConst,
  154. });
  155. } else if (
  156. (!declaration.type.isCompatible(resultType) &&
  157. !Config.enable_type_casting) ||
  158. (!declaration.type.isCompatible(resultType) &&
  159. Config.enable_type_casting &&
  160. !Store.canImplicitTypeCast(declaration.type, resultType))
  161. ) {
  162. const stringInfo = declaration.type.stringInfo();
  163. const info = stringInfo[0];
  164. const result_string_info = resultType.stringInfo();
  165. const result_info = result_string_info[0];
  166. const exp = declaration.initial;
  167. throw ProcessorErrorFactory.incompatible_types_full(
  168. info.type,
  169. info.dim,
  170. result_info.type,
  171. result_info.dim,
  172. exp.toString(),
  173. declaration.sourceInfo
  174. );
  175. } else {
  176. this.insertSymbol(declaration.id, {
  177. id: declaration.id,
  178. type: declaration.type,
  179. isConst: declaration.isConst,
  180. });
  181. }
  182. }
  183. }
  184. assertArrayDeclaration (declaration) {
  185. if (declaration.initial === null) {
  186. const lineType = this.evaluateExpressionType(declaration.lines);
  187. if (!lineType.isCompatible(Types.INTEGER)) {
  188. throw ProcessorErrorFactory.array_dimension_not_int_full(
  189. declaration.sourceInfo
  190. );
  191. }
  192. if (declaration.columns !== null) {
  193. const columnType = this.evaluateExpressionType(declaration.columns);
  194. if (!columnType.isCompatible(Types.INTEGER)) {
  195. throw ProcessorErrorFactory.array_dimension_not_int_full(
  196. declaration.sourceInfo
  197. );
  198. }
  199. }
  200. } else {
  201. this.evaluateArrayLiteral(declaration);
  202. }
  203. this.insertSymbol(declaration.id, {
  204. id: declaration.id,
  205. lines: declaration.lines,
  206. columns: declaration.columns,
  207. type: declaration.type,
  208. });
  209. return;
  210. }
  211. evaluateExpressionType (expression) {
  212. // TODO: Throw operator error in case type == UNDEFINED
  213. if (expression instanceof UnaryApp) {
  214. const op = expression.op;
  215. const resultType = this.evaluateExpressionType(expression.left);
  216. const finalResult = resultTypeAfterUnaryOp(op, resultType);
  217. if (Types.UNDEFINED.isCompatible(finalResult)) {
  218. const stringInfo = resultType.stringInfo();
  219. const info = stringInfo[0];
  220. const expString = expression.toString();
  221. throw ProcessorErrorFactory.invalid_unary_op_full(
  222. expString,
  223. op,
  224. info.type,
  225. info.dim,
  226. expression.sourceInfo
  227. );
  228. }
  229. return finalResult;
  230. } else if (expression instanceof InfixApp) {
  231. const op = expression.op;
  232. const resultTypeLeft = this.evaluateExpressionType(expression.left);
  233. const resultTypeRight = this.evaluateExpressionType(expression.right);
  234. const finalResult = resultTypeAfterInfixOp(
  235. op,
  236. resultTypeLeft,
  237. resultTypeRight
  238. );
  239. if (Types.UNDEFINED.isCompatible(finalResult)) {
  240. const stringInfoLeft = resultTypeLeft.stringInfo();
  241. const infoLeft = stringInfoLeft[0];
  242. const stringInfoRight = resultTypeRight.stringInfo();
  243. const infoRight = stringInfoRight[0];
  244. const expString = expression.toString();
  245. throw ProcessorErrorFactory.invalid_infix_op_full(
  246. expString,
  247. op,
  248. infoLeft.type,
  249. infoLeft.dim,
  250. infoRight.type,
  251. infoRight.dim,
  252. expression.sourceInfo
  253. );
  254. }
  255. return finalResult;
  256. } else if (expression instanceof Literal) {
  257. return this.evaluateLiteralType(expression);
  258. } else if (expression instanceof FunctionCall) {
  259. if (expression.isMainCall) {
  260. throw ProcessorErrorFactory.void_in_expression_full(
  261. LanguageDefinedFunction.getMainFunctionName(),
  262. expression.sourceInfo
  263. );
  264. }
  265. const fun = this.findFunction(expression.id);
  266. if (fun === null) {
  267. throw ProcessorErrorFactory.function_missing_full(
  268. expression.id,
  269. expression.sourceInfo
  270. );
  271. }
  272. if (fun.returnType.isCompatible(Types.VOID)) {
  273. throw ProcessorErrorFactory.void_in_expression_full(
  274. expression.id,
  275. expression.sourceInfo
  276. );
  277. }
  278. this.assertParameters(fun, expression.actualParameters);
  279. return fun.returnType;
  280. } else if (expression instanceof ArrayAccess) {
  281. const arrayTypeInfo = this.findSymbol(expression.id, this.symbolMap);
  282. if (arrayTypeInfo === null) {
  283. throw ProcessorErrorFactory.symbol_not_found_full(
  284. expression.id,
  285. expression.sourceInfo
  286. );
  287. }
  288. if (!(arrayTypeInfo.type instanceof ArrayType)) {
  289. throw ProcessorErrorFactory.invalid_array_access_full(
  290. expression.id,
  291. expression.sourceInfo
  292. );
  293. }
  294. const lineType = this.evaluateExpressionType(expression.line);
  295. if (!lineType.isCompatible(Types.INTEGER)) {
  296. throw ProcessorErrorFactory.array_dimension_not_int_full(
  297. expression.sourceInfo
  298. );
  299. }
  300. if (expression.column !== null) {
  301. if (arrayTypeInfo.columns === null) {
  302. throw ProcessorErrorFactory.invalid_matrix_access_full(
  303. expression.id,
  304. expression.sourceInfo
  305. );
  306. }
  307. const columnType = this.evaluateExpressionType(expression.column);
  308. if (!columnType.isCompatible(Types.INTEGER)) {
  309. throw ProcessorErrorFactory.array_dimension_not_int_full(
  310. expression.sourceInfo
  311. );
  312. }
  313. }
  314. const arrType = arrayTypeInfo.type;
  315. if (expression.column !== null) {
  316. // indexing matrix
  317. return arrType.innerType;
  318. } else {
  319. if (arrayTypeInfo.columns === null) {
  320. return arrType.innerType;
  321. }
  322. return new ArrayType(arrType.innerType, 1);
  323. }
  324. }
  325. }
  326. evaluateLiteralType (literal) {
  327. if (literal instanceof IntLiteral) {
  328. return literal.type;
  329. } else if (literal instanceof RealLiteral) {
  330. return literal.type;
  331. } else if (literal instanceof StringLiteral) {
  332. return literal.type;
  333. } else if (literal instanceof BoolLiteral) {
  334. return literal.type;
  335. } else if (literal instanceof CharLiteral) {
  336. return literal.type;
  337. } else if (literal instanceof VariableLiteral) {
  338. const typeInfo = this.findSymbol(literal.id, this.symbolMap);
  339. if (typeInfo === null) {
  340. throw ProcessorErrorFactory.symbol_not_found_full(
  341. literal.id,
  342. literal.sourceInfo
  343. );
  344. }
  345. if (typeInfo.type instanceof ArrayType) {
  346. return typeInfo.type;
  347. }
  348. return typeInfo.type;
  349. } else {
  350. // console.warn("Evaluating type only for an array literal...");
  351. let last = null;
  352. if (literal.value.length === 1) {
  353. last = this.evaluateExpressionType(literal.value[0]);
  354. } else {
  355. for (let i = 0; i < literal.value.length; i++) {
  356. const e = this.evaluateExpressionType(literal.value[i]);
  357. if (last === null) {
  358. last = e;
  359. } else if (!last.isCompatible(e)) {
  360. const strInfo = last.stringInfo();
  361. const info = strInfo[0];
  362. const strExp = literal.toString();
  363. throw ProcessorErrorFactory.incompatible_types_array_full(
  364. strExp,
  365. info.type,
  366. info.dim,
  367. literal.sourceInfo
  368. );
  369. }
  370. }
  371. }
  372. if (last instanceof ArrayType) {
  373. return new ArrayType(last.innerType, last.dimensions + 1);
  374. }
  375. return new ArrayType(last, 1);
  376. }
  377. }
  378. evaluateArrayLiteral (arrayDeclaration) {
  379. const type = arrayDeclaration.type;
  380. const literal = arrayDeclaration.initial;
  381. // console.log(arrayDeclaration);
  382. if (arrayDeclaration.isVector) {
  383. this.evaluateVectorLiteralType(literal, type);
  384. } else {
  385. // TODO matrix type check
  386. for (let i = 0; i < literal.lines; ++i) {
  387. const line_literal = literal.value[i];
  388. this.evaluateVectorLiteralType(
  389. line_literal,
  390. new ArrayType(type.innerType, 1)
  391. );
  392. }
  393. }
  394. return true;
  395. }
  396. assertFunction (fun) {
  397. this.pushMap();
  398. this.currentFunction = fun;
  399. fun.formalParameters.forEach((formalParam) => {
  400. if (formalParam.type instanceof ArrayType) {
  401. if (formalParam.type.dimensions > 1) {
  402. this.insertSymbol(formalParam.id, {
  403. id: formalParam.id,
  404. lines: -1,
  405. columns: -1,
  406. type: formalParam.type,
  407. });
  408. } else {
  409. this.insertSymbol(formalParam.id, {
  410. id: formalParam.id,
  411. lines: -1,
  412. columns: null,
  413. type: formalParam.type,
  414. });
  415. }
  416. } else {
  417. this.insertSymbol(formalParam.id, {
  418. id: formalParam.id,
  419. type: formalParam.type,
  420. });
  421. }
  422. });
  423. this.assertDeclarations(fun.variablesDeclarations);
  424. const optional = fun.returnType.isCompatible(Types.VOID);
  425. const valid = this.assertReturn(fun, optional);
  426. if (!valid) {
  427. throw ProcessorErrorFactory.function_no_return(fun.name);
  428. }
  429. this.popMap();
  430. }
  431. assertReturn (fun, optional) {
  432. return fun.commands.reduce(
  433. (last, next) => this.checkCommand(fun.returnType, next, optional) || last,
  434. optional
  435. );
  436. }
  437. checkCommand (type, cmd, optional) {
  438. if (cmd instanceof While) {
  439. const resultType = this.evaluateExpressionType(cmd.expression);
  440. if (!resultType.isCompatible(Types.BOOLEAN)) {
  441. throw ProcessorErrorFactory.loop_condition_type_full(
  442. cmd.expression.toString(),
  443. cmd.sourceInfo
  444. );
  445. }
  446. this.checkCommands(type, cmd.commands, optional);
  447. return false;
  448. } else if (cmd instanceof For) {
  449. const var_type = this.evaluateExpressionType(cmd.for_id);
  450. if (!var_type.isCompatible(Types.INTEGER)) {
  451. throw ProcessorErrorFactory.invalid_for_variable(
  452. cmd.for_id,
  453. cmd.sourceInfo
  454. );
  455. }
  456. const from_type = this.evaluateExpressionType(cmd.for_from);
  457. if (!from_type.isCompatible(Types.INTEGER)) {
  458. throw ProcessorErrorFactory.invalid_for_from(
  459. cmd.for_from,
  460. cmd.sourceInfo
  461. );
  462. }
  463. const to_type = this.evaluateExpressionType(cmd.for_to);
  464. if (!to_type.isCompatible(Types.INTEGER)) {
  465. throw ProcessorErrorFactory.invalid_for_to(cmd.for_to, cmd.sourceInfo);
  466. }
  467. if (cmd.for_pass != null) {
  468. const pass_type = this.evaluateExpressionType(cmd.for_pass);
  469. if (!pass_type.isCompatible(Types.INTEGER)) {
  470. throw ProcessorErrorFactory.invalid_for_pass(
  471. cmd.for_pass,
  472. cmd.sourceInfo
  473. );
  474. }
  475. }
  476. this.checkCommands(type, cmd.commands, optional);
  477. return false;
  478. } else if (cmd instanceof Switch) {
  479. const sType = this.evaluateExpressionType(cmd.expression);
  480. let result = optional;
  481. let hasDefault = false;
  482. for (let i = 0; i < cmd.cases.length; i++) {
  483. const aCase = cmd.cases[i];
  484. if (aCase.expression !== null) {
  485. const caseType = this.evaluateExpressionType(aCase.expression);
  486. if (!sType.isCompatible(caseType)) {
  487. const strInfo = sType.stringInfo();
  488. const info = strInfo[0];
  489. const strExp = aCase.expression.toString();
  490. throw ProcessorErrorFactory.invalid_case_type_full(
  491. strExp,
  492. info.type,
  493. info.dim,
  494. aCase.sourceInfo
  495. );
  496. }
  497. } else {
  498. hasDefault = true;
  499. }
  500. result = result && this.checkCommands(type, aCase.commands, result);
  501. }
  502. return result && hasDefault;
  503. } else if (cmd instanceof ArrayIndexAssign) {
  504. // TODO - rework!!!!!
  505. let used_dims = 0;
  506. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  507. if (typeInfo === null) {
  508. throw ProcessorErrorFactory.symbol_not_found_full(
  509. cmd.id,
  510. cmd.sourceInfo
  511. );
  512. }
  513. if (typeInfo.isConst) {
  514. throw ProcessorErrorFactory.invalid_const_assignment_full(
  515. cmd.id,
  516. cmd.sourceInfo
  517. );
  518. }
  519. if (!(typeInfo.type instanceof ArrayType)) {
  520. throw ProcessorErrorFactory.invalid_array_access_full(
  521. cmd.id,
  522. cmd.sourceInfo
  523. );
  524. }
  525. const exp = cmd.expression;
  526. const lineExp = cmd.line;
  527. const lineType = this.evaluateExpressionType(lineExp);
  528. if (!lineType.isCompatible(Types.INTEGER)) {
  529. throw ProcessorErrorFactory.array_dimension_not_int_full(
  530. cmd.sourceInfo
  531. );
  532. }
  533. used_dims += 1;
  534. const columnExp = cmd.column;
  535. if (typeInfo.columns === null && columnExp !== null) {
  536. throw ProcessorErrorFactory.invalid_matrix_access_full(
  537. cmd.id,
  538. cmd.sourceInfo
  539. );
  540. } else if (columnExp !== null) {
  541. const columnType = this.evaluateExpressionType(columnExp);
  542. if (!columnType.isCompatible(Types.INTEGER)) {
  543. throw ProcessorErrorFactory.array_dimension_not_int_full(
  544. cmd.sourceInfo
  545. );
  546. }
  547. used_dims += 1;
  548. }
  549. // exp a single value exp or an array access
  550. const exp_type = this.evaluateExpressionType(exp);
  551. const access_type = typeInfo.type;
  552. let compatible = false;
  553. let type = access_type;
  554. if (exp_type instanceof MultiType) {
  555. if (access_type.dimensions - used_dims == 0) {
  556. type = access_type.innerType;
  557. } else {
  558. type = new ArrayType(
  559. access_type.innerType,
  560. Math.max(0, access_type.dimensions - used_dims)
  561. );
  562. }
  563. compatible = exp_type.isCompatible(type);
  564. } else {
  565. compatible = access_type.canAccept(exp_type, used_dims);
  566. }
  567. if (!compatible) {
  568. if (0 === access_type.dimensions - used_dims) {
  569. type = access_type.innerType; // enable a valid attempt to cast real <--> integer
  570. }
  571. if (
  572. !Config.enable_type_casting ||
  573. !Store.canImplicitTypeCast(type, exp_type)
  574. ) {
  575. const access_type_string_info = access_type.stringInfo();
  576. const access_type_info = access_type_string_info[0];
  577. const exp_type_string_info = exp_type.stringInfo();
  578. const exp_type_info = exp_type_string_info[0];
  579. throw ProcessorErrorFactory.incompatible_types_full(
  580. access_type_info.type,
  581. access_type_info.dim - used_dims,
  582. exp_type_info.type,
  583. exp_type_info.dim,
  584. exp.toString(),
  585. cmd.sourceInfo
  586. );
  587. }
  588. }
  589. return optional;
  590. } else if (cmd instanceof Assign) {
  591. // TODO - rework since there is no literal array assignment
  592. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  593. if (typeInfo === null) {
  594. throw ProcessorErrorFactory.symbol_not_found_full(
  595. cmd.id,
  596. cmd.sourceInfo
  597. );
  598. }
  599. if (typeInfo.isConst) {
  600. throw ProcessorErrorFactory.invalid_const_assignment_full(
  601. cmd.id,
  602. cmd.sourceInfo
  603. );
  604. }
  605. const exp = cmd.expression;
  606. const exp_type = this.evaluateExpressionType(exp);
  607. if (exp_type instanceof ArrayType) {
  608. if (!(typeInfo.type instanceof ArrayType)) {
  609. // TODO better error message
  610. throw new Error("Cannot assign an array to a non-array variable ");
  611. }
  612. // Both are arrays...
  613. // if both don't have same dimensions and type, cannot perform assignment
  614. if (!exp_type.isCompatible(typeInfo.type)) {
  615. if (
  616. exp_type.dimensions === typeInfo.type.dimensions &&
  617. !exp_type.innerType.isCompatible(typeInfo.type.innerType)
  618. ) {
  619. if (
  620. !Config.enable_type_casting ||
  621. !Store.canImplicitTypeCast(
  622. typeInfo.type.innerType,
  623. exp_type.innerType
  624. )
  625. ) {
  626. const stringInfo = typeInfo.type.stringInfo();
  627. const info = stringInfo[0];
  628. const exp_type_string_info = exp_type.stringInfo();
  629. const exp_type_info = exp_type_string_info[0];
  630. throw ProcessorErrorFactory.incompatible_types_full(
  631. info.type,
  632. info.dim,
  633. exp_type_info.type,
  634. exp_type_info.dim,
  635. exp.toString(),
  636. cmd.sourceInfo
  637. );
  638. }
  639. } else {
  640. switch (exp_type.dimensions) {
  641. case 1: {
  642. throw ProcessorErrorFactory.vector_to_matrix_attr(
  643. cmd.id,
  644. exp.toString(),
  645. cmd.sourceInfo
  646. );
  647. }
  648. case 2: {
  649. throw ProcessorErrorFactory.matrix_to_vector_attr(
  650. cmd.id,
  651. exp.toString(),
  652. cmd.sourceInfo
  653. );
  654. }
  655. }
  656. }
  657. }
  658. } else if (!exp_type.isCompatible(typeInfo.type)) {
  659. if (
  660. !Config.enable_type_casting ||
  661. !Store.canImplicitTypeCast(typeInfo.type, exp_type)
  662. ) {
  663. const stringInfo = typeInfo.type.stringInfo();
  664. const info = stringInfo[0];
  665. const exp_type_string_info = exp_type.stringInfo();
  666. const exp_type_info = exp_type_string_info[0];
  667. throw ProcessorErrorFactory.incompatible_types_full(
  668. info.type,
  669. info.dim,
  670. exp_type_info.type,
  671. exp_type_info.dim,
  672. exp.toString(),
  673. cmd.sourceInfo
  674. );
  675. }
  676. }
  677. return optional;
  678. } else if (cmd instanceof Break) {
  679. return optional;
  680. } else if (cmd instanceof IfThenElse) {
  681. const resultType = this.evaluateExpressionType(cmd.condition);
  682. if (!resultType.isCompatible(Types.BOOLEAN)) {
  683. throw ProcessorErrorFactory.if_condition_type_full(
  684. cmd.condition.toString(),
  685. cmd.sourceInfo
  686. );
  687. }
  688. if (cmd.ifFalse instanceof IfThenElse) {
  689. return (
  690. this.checkCommands(type, cmd.ifTrue.commands, optional) &&
  691. this.checkCommand(type, cmd.ifFalse, optional)
  692. );
  693. } else if (cmd.ifFalse != null) {
  694. return (
  695. this.checkCommands(type, cmd.ifTrue.commands, optional) &&
  696. this.checkCommands(type, cmd.ifFalse.commands, optional)
  697. );
  698. } else {
  699. return this.checkCommands(type, cmd.ifTrue.commands, optional);
  700. }
  701. } else if (cmd instanceof FunctionCall) {
  702. let fun = null;
  703. if (cmd.isMainCall) {
  704. fun = this.getMainFunction();
  705. } else {
  706. fun = this.findFunction(cmd.id);
  707. }
  708. if (fun === null) {
  709. throw ProcessorErrorFactory.function_missing_full(
  710. cmd.id,
  711. cmd.sourceInfo
  712. );
  713. }
  714. this.assertParameters(fun, cmd.actualParameters);
  715. return optional;
  716. } else if (cmd instanceof Return) {
  717. const funcName = this.currentFunction.isMain
  718. ? LanguageDefinedFunction.getMainFunctionName()
  719. : this.currentFunction.name;
  720. if (cmd.expression === null && !type.isCompatible(Types.VOID)) {
  721. const stringInfo = type.stringInfo();
  722. const info = stringInfo[0];
  723. throw ProcessorErrorFactory.invalid_void_return_full(
  724. funcName,
  725. info.type,
  726. info.dim,
  727. cmd.sourceInfo
  728. );
  729. } else if (cmd.expression !== null) {
  730. const resultType = this.evaluateExpressionType(cmd.expression);
  731. if (!type.isCompatible(resultType)) {
  732. if (
  733. !Config.enable_type_casting ||
  734. !Store.canImplicitTypeCast(type, resultType)
  735. ) {
  736. const stringInfo = type.stringInfo();
  737. const info = stringInfo[0];
  738. throw ProcessorErrorFactory.invalid_return_type_full(
  739. funcName,
  740. info.type,
  741. info.dim,
  742. cmd.sourceInfo
  743. );
  744. }
  745. }
  746. return true;
  747. } else {
  748. return true;
  749. }
  750. }
  751. }
  752. checkCommands (type, cmds, optional) {
  753. return cmds.reduce(
  754. (last, next) => this.checkCommand(type, next, optional) || last,
  755. optional
  756. );
  757. }
  758. /**
  759. *
  760. * @param {import('./../../ast/commands/function').Function} fun
  761. * @param {Expression[]} actualParametersList
  762. */
  763. assertParameters (fun, actualParametersList) {
  764. const parameterList = fun.formalParameters;
  765. if (
  766. parameterList.length > actualParametersList.length ||
  767. (parameterList.length !== actualParametersList.length &&
  768. !fun.hasVariadic())
  769. ) {
  770. throw ProcessorErrorFactory.invalid_parameters_size_full(
  771. fun.name,
  772. actualParametersList.length,
  773. fun.formalParameters.length,
  774. null
  775. );
  776. }
  777. for (
  778. let i = 0, j = 0;
  779. i < parameterList.length && j < actualParametersList.length;
  780. i += 1, j += 1
  781. ) {
  782. const formalParam = parameterList[i];
  783. if (formalParam.variadic && i + 1 !== parameterList.length) {
  784. throw "A function variadic parameter must be its last parameter!";
  785. }
  786. if (formalParam.variadic) {
  787. j = this.assertVariadicParameter(
  788. fun,
  789. formalParam,
  790. j,
  791. actualParametersList
  792. );
  793. } else {
  794. const param = actualParametersList[j];
  795. this.assertParameter(fun, formalParam, param);
  796. }
  797. }
  798. }
  799. evaluateVectorLiteralType (literal, type) {
  800. // console.log(literal);
  801. for (let i = 0; i < literal.value.length; i += 1) {
  802. const exp = literal.value[i];
  803. const expType = this.evaluateExpressionType(exp);
  804. let compatible = false;
  805. if (expType instanceof MultiType) {
  806. compatible = expType.isCompatible(type.innerType);
  807. } else {
  808. compatible = type.canAccept(expType, 1);
  809. }
  810. if (!compatible) {
  811. if (
  812. !Config.enable_type_casting ||
  813. !Store.canImplicitTypeCast(type.innerType, expType)
  814. ) {
  815. const stringInfo = type.stringInfo();
  816. const info = stringInfo[0];
  817. const result_string_info = expType.stringInfo();
  818. const result_info = result_string_info[0];
  819. throw ProcessorErrorFactory.incompatible_types_full(
  820. info.type,
  821. 0,
  822. result_info.type,
  823. result_info.dim,
  824. exp.toString(),
  825. literal.sourceInfo
  826. );
  827. }
  828. }
  829. }
  830. return type;
  831. }
  832. /**
  833. *
  834. * @param {import('./../../ast/commands/function').Function} fun
  835. * @param {import('./../../ast/commands/formalParameter').FormalParameter} formalParam
  836. * @param {number} index
  837. * @param {Expression[]} actualParametersList
  838. */
  839. assertVariadicParameter (fun, formalParam, index, actualParametersList) {
  840. let i;
  841. for (i = index; i < actualParametersList.length; i += 1) {
  842. this.assertParameter(fun, formalParam, actualParametersList[i]);
  843. }
  844. return i - 1;
  845. }
  846. /**
  847. *
  848. * @param {import('./../../ast/commands/function').Function} fun
  849. * @param {import('./../../ast/commands/formalParameter').FormalParameter} formalParam
  850. * @param {Expression} actualParameter
  851. */
  852. assertParameter (fun, formalParam, actualParameter) {
  853. // const id = formalParam.id;
  854. if (formalParam.byRef) {
  855. if (actualParameter instanceof VariableLiteral) {
  856. const variable = this.findSymbol(actualParameter.id, this.symbolMap);
  857. if (variable.isConst) {
  858. throw ProcessorErrorFactory.invalid_const_ref_full(
  859. fun.name,
  860. actualParameter.toString(),
  861. actualParameter.sourceInfo
  862. );
  863. }
  864. } else if (
  865. !(
  866. actualParameter instanceof VariableLiteral ||
  867. actualParameter instanceof ArrayAccess
  868. )
  869. ) {
  870. throw ProcessorErrorFactory.invalid_parameter_type_full(
  871. fun.name,
  872. actualParameter.toString(),
  873. actualParameter.sourceInfo
  874. );
  875. }
  876. }
  877. const resultType = this.evaluateExpressionType(actualParameter);
  878. if (
  879. resultType instanceof MultiType &&
  880. formalParam.type instanceof MultiType
  881. ) {
  882. let shared = 0;
  883. for (let j = 0; j < resultType.types.length; ++j) {
  884. const element = resultType.types[j];
  885. if (formalParam.type.types.indexOf(element) !== -1) {
  886. shared += 1;
  887. }
  888. }
  889. if (shared <= 0) {
  890. if (Config.enable_type_casting && !formalParam.byRef) {
  891. if (
  892. (!resultType.isCompatible(Types.INTEGER) &&
  893. !resultType.isCompatible(Types.REAL)) ||
  894. formalParam.type.isCompatible(Types.INTEGER) ||
  895. formalParam.type.isCompatible(Types.REAL)
  896. ) {
  897. throw ProcessorErrorFactory.invalid_parameter_type_full(
  898. fun.name,
  899. actualParameter.toString(),
  900. actualParameter.sourceInfo
  901. );
  902. }
  903. }
  904. }
  905. } else if (resultType instanceof MultiType) {
  906. if (!resultType.isCompatible(formalParam.type)) {
  907. if (Config.enable_type_casting && !formalParam.byRef) {
  908. if (
  909. (!resultType.isCompatible(Types.INTEGER) &&
  910. !resultType.isCompatible(Types.REAL)) ||
  911. formalParam.type.isCompatible(Types.INTEGER) ||
  912. formalParam.type.isCompatible(Types.REAL)
  913. ) {
  914. throw ProcessorErrorFactory.invalid_parameter_type_full(
  915. fun.name,
  916. actualParameter.toString(),
  917. actualParameter.sourceInfo
  918. );
  919. }
  920. }
  921. }
  922. } else if (!formalParam.type.isCompatible(resultType)) {
  923. if (Config.enable_type_casting && !formalParam.byRef) {
  924. if (!Store.canImplicitTypeCast(formalParam.type, resultType)) {
  925. throw ProcessorErrorFactory.invalid_parameter_type_full(
  926. fun.name,
  927. actualParameter.toString(),
  928. actualParameter.sourceInfo
  929. );
  930. }
  931. }
  932. }
  933. }
  934. }