1
0

semanticAnalyser.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. import { ProcessorErrorFactory } from './../error/processorErrorFactory';
  2. import { LanguageDefinedFunction } from './../definedFunctions';
  3. import { LanguageService } from './../../services/languageService';
  4. import { ArrayDeclaration, While, For, Switch, Case, Declaration, Assign, Break, IfThenElse, Return, ArrayIndexAssign } from '../../ast/commands';
  5. import { InfixApp, UnaryApp, FunctionCall, IntLiteral, RealLiteral, StringLiteral, BoolLiteral, VariableLiteral, ArrayLiteral, 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 { CompoundType } from '../../typeSystem/compoundType';
  10. import { MultiType } from '../../typeSystem/multiType';
  11. export class SemanticAnalyser {
  12. constructor(ast) {
  13. this.ast = ast;
  14. this.lexerClass = LanguageService.getCurrentLexer();
  15. const lexer = new this.lexerClass(null);
  16. this.literalNames = lexer.literalNames;
  17. this.symbolMap = null;
  18. }
  19. pushMap () {
  20. if(this.symbolMap === null) {
  21. this.symbolMap = {map:{}, next: null};
  22. } else {
  23. const n = {map:{}, next: this.symbolMap};
  24. this.symbolMap = n;
  25. }
  26. }
  27. popMap () {
  28. if(this.symbolMap !== null) {
  29. this.symbolMap = this.symbolMap.next;
  30. }
  31. }
  32. insertSymbol (id, typeInfo) {
  33. this.symbolMap.map[id] = typeInfo;
  34. }
  35. findSymbol (id, symMap) {
  36. if(!symMap.map[id]) {
  37. if(symMap.next) {
  38. return this.findSymbol(id, symMap.next);
  39. }
  40. return null;
  41. } else {
  42. return symMap.map[id];
  43. }
  44. }
  45. getMainFunction () {
  46. return this.ast.functions.find(v => v.isMain);
  47. }
  48. findFunction (name) {
  49. if(name.match(/^\$.+$/)) {
  50. const fun = LanguageDefinedFunction.getFunction(name);
  51. if(!!!fun) {
  52. throw ProcessorErrorFactory.unknown_command(name);
  53. }
  54. return fun;
  55. } else {
  56. const val = this.ast.functions.find( v => v.name === name);
  57. if (!!!val) {
  58. return null;
  59. }
  60. return val;
  61. }
  62. }
  63. analyseTree () {
  64. const globalVars = this.ast.global;
  65. this.pushMap();
  66. this.assertDeclarations(globalVars);
  67. const functions = this.ast.functions;
  68. const mainFunc = functions.filter((f) => f.name === null);
  69. if (mainFunc.length <= 0) {
  70. throw ProcessorErrorFactory.main_missing();
  71. }
  72. for (let i = 0; i < functions.length; i++) {
  73. const fun = functions[i];
  74. this.assertFunction(fun);
  75. }
  76. return this.ast;
  77. }
  78. assertDeclarations (list) {
  79. for (let i = 0; i < list.length; i++) {
  80. this.assertDeclaration(list[i]);
  81. }
  82. }
  83. assertDeclaration (declaration) {
  84. if (declaration instanceof ArrayDeclaration) {
  85. if(declaration.initial === null) {
  86. const lineType = this.evaluateExpressionType(declaration.lines);
  87. if (!lineType.isCompatible(Types.INTEGER)) {
  88. throw ProcessorErrorFactory.array_dimension_not_int_full(declaration.sourceInfo);
  89. }
  90. if (declaration.columns !== null) {
  91. const columnType = this.evaluateExpressionType(declaration.columns);
  92. if (!columnType.isCompatible(Types.INTEGER)) {
  93. throw ProcessorErrorFactory.array_dimension_not_int_full(declaration.sourceInfo);
  94. }
  95. }
  96. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type});
  97. return;
  98. }
  99. this.evaluateArrayLiteral(declaration.lines, declaration.columns, declaration.type, declaration.initial);
  100. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type});
  101. } else {
  102. if(declaration.initial === null) {
  103. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type});
  104. return;
  105. }
  106. const resultType = this.evaluateExpressionType(declaration.initial);
  107. if(resultType instanceof MultiType) {
  108. if(!resultType.isCompatible(declaration.type)) {
  109. const stringInfo = declaration.type.stringInfo();
  110. const info = stringInfo[0];
  111. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, declaration.sourceInfo);
  112. }
  113. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
  114. } else if(!declaration.type.isCompatible(resultType)) {
  115. const stringInfo = declaration.type.stringInfo();
  116. const info = stringInfo[0];
  117. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, declaration.sourceInfo);
  118. } else {
  119. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
  120. }
  121. }
  122. }
  123. evaluateExpressionType (expression) {
  124. if(expression instanceof UnaryApp) {
  125. const op = expression.op;
  126. const resultType = this.evaluateExpressionType(expression.left);
  127. return resultTypeAfterUnaryOp(op, resultType);
  128. } else if (expression instanceof InfixApp) {
  129. const op = expression.op;
  130. const resultTypeLeft = this.evaluateExpressionType(expression.left);
  131. const resultTypeRight = this.evaluateExpressionType(expression.right);
  132. return resultTypeAfterInfixOp(op, resultTypeLeft, resultTypeRight);
  133. } else if (expression instanceof Literal) {
  134. return this.evaluateLiteralType(expression);
  135. } else if (expression instanceof FunctionCall) {
  136. if (expression.isMainCall) {
  137. throw new Error("void return used in expression");
  138. }
  139. const fun = this.findFunction(expression.id);
  140. if(fun === null) {
  141. throw ProcessorErrorFactory.function_missing_full(expression.id, expression.sourceInfo);
  142. }
  143. if (fun.returnType.isCompatible(Types.VOID)) {
  144. throw new Error("void return used in expression");
  145. }
  146. this.assertParameters(fun, expression.actualParameters);
  147. return fun.returnType;
  148. } else if (expression instanceof ArrayAccess) {
  149. const arrayTypeInfo = this.findSymbol(expression.id, this.symbolMap);
  150. if(arrayTypeInfo === null) {
  151. throw ProcessorErrorFactory.symbol_not_found_full(expression.id, expression.sourceInfo);
  152. }
  153. if (!(arrayTypeInfo.type instanceof CompoundType)) {
  154. throw new Error("it's not an array");
  155. }
  156. const lineType = this.evaluateExpressionType(expression.line);
  157. if (!lineType.isCompatible(Types.INTEGER)) {
  158. throw ProcessorErrorFactory.array_dimension_not_int_full(expression.sourceInfo);
  159. }
  160. if (expression.column !== null) {
  161. if (arrayTypeInfo.columns === null) {
  162. throw new Error("it's not a matrix");
  163. }
  164. const columnType = this.evaluateExpressionType(expression.column);
  165. if(!columnType.isCompatible(Types.INTEGER)) {
  166. throw ProcessorErrorFactory.array_dimension_not_int_full(expression.sourceInfo);
  167. }
  168. }
  169. const arrType = arrayTypeInfo.type;
  170. if(expression.column !== null) {
  171. // indexing matrix
  172. return arrType.innerType;
  173. } else {
  174. if(arrayTypeInfo.columns === null) {
  175. return arrType.innerType;
  176. }
  177. return new CompoundType(arrType.innerType, 1);
  178. }
  179. }
  180. }
  181. evaluateLiteralType (literal) {
  182. if(literal instanceof IntLiteral) {
  183. return literal.type;
  184. } else if (literal instanceof RealLiteral) {
  185. return literal.type;
  186. } else if (literal instanceof StringLiteral) {
  187. return literal.type;
  188. } else if (literal instanceof BoolLiteral) {
  189. return literal.type;
  190. } else if (literal instanceof VariableLiteral) {
  191. const typeInfo = this.findSymbol(literal.id, this.symbolMap);
  192. if(typeInfo === null) {
  193. throw ProcessorErrorFactory.symbol_not_found_full(literal.id, literal.sourceInfo);
  194. }
  195. if (typeInfo.type instanceof CompoundType) {
  196. return typeInfo.type;
  197. }
  198. return typeInfo.type;
  199. } else {
  200. console.warn("Evaluating type only for an array literal...");
  201. let last = null;
  202. if(literal.value.length === 1) {
  203. last = this.evaluateExpressionType(literal.value[0]);
  204. } else {
  205. for (let i = 0; i < literal.value.length; i++) {
  206. const e = this.evaluateExpressionType(literal.value[i]);
  207. if(last === null) {
  208. last = e;
  209. } else if(!last.isCompatible(e)) {
  210. const strInfo = last.stringInfo();
  211. const info = strInfo[0];
  212. const strExp = literal.toString();
  213. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  214. }
  215. }
  216. }
  217. if(last instanceof CompoundType) {
  218. return new CompoundType(last.innerType, last.dimensions + 1);
  219. }
  220. return new CompoundType(last, 1);
  221. }
  222. }
  223. evaluateArrayLiteral (lines, columns, type, literal) {
  224. if (literal instanceof ArrayLiteral) {
  225. if (columns === null) {
  226. // it's a vector...
  227. const dimType = this.evaluateExpressionType(lines);
  228. if (!dimType.isCompatible(Types.INTEGER)) {
  229. throw ProcessorErrorFactory.array_dimension_not_int_full(literal.sourceInfo);
  230. }
  231. if ((lines instanceof IntLiteral) && !lines.value.eq(literal.value.length)) {
  232. throw new Error("invalid array size");
  233. }
  234. literal.value.reduce((last, next) => {
  235. const eType = this.evaluateExpressionType(next);
  236. if (!last.canAccept(eType)) {
  237. const strInfo = last.stringInfo();
  238. const info = strInfo[0];
  239. const strExp = literal.toString();
  240. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  241. }
  242. return last;
  243. }, type);
  244. return true;
  245. } else {
  246. const dimType = this.evaluateExpressionType(columns);
  247. if (!dimType.isCompatible(Types.INTEGER)) {
  248. throw ProcessorErrorFactory.array_dimension_not_int_full(literal.sourceInfo); }
  249. if ((columns instanceof IntLiteral) && !columns.value.eq(literal.value.length)) {
  250. throw new Error("invalid array size");
  251. }
  252. for (let i = 0; i < columns; i++) {
  253. const anotherArray = literal.value[i];
  254. this.evaluateArrayLiteral(lines, null, type, anotherArray)
  255. }
  256. }
  257. } else {
  258. const resultType = this.evaluateExpressionType(literal);
  259. if (!(resultType instanceof CompoundType)) {
  260. const strInfo = type.stringInfo();
  261. const info = strInfo[0];
  262. const strExp = literal.toString();
  263. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  264. }
  265. if (!type.isCompatible(resultType)) {
  266. const strInfo = type.stringInfo();
  267. const info = strInfo[0];
  268. const strExp = literal.toString();
  269. throw ProcessorErrorFactory.incompatible_types_array_full(strExp,info.type, info.dim, literal.sourceInfo);
  270. }
  271. return true;
  272. }
  273. }
  274. assertFunction (fun) {
  275. this.pushMap();
  276. fun.formalParameters.forEach(formalParam => {
  277. if(formalParam.type instanceof CompoundType) {
  278. if(formalParam.type.dimensions > 1) {
  279. this.insertSymbol(formalParam.id, {id: formalParam.id, lines: -1, columns: -1, type: formalParam.type});
  280. } else {
  281. this.insertSymbol(formalParam.id, {id: formalParam.id, lines: -1, columns: null, type: formalParam.type});
  282. }
  283. } else {
  284. this.insertSymbol(formalParam.id, {id: formalParam.id, type: formalParam.type});
  285. }
  286. })
  287. this.assertDeclarations(fun.variablesDeclarations);
  288. const optional = fun.returnType.isCompatible(Types.VOID);
  289. const valid = this.assertReturn(fun, optional);
  290. if (!valid) {
  291. throw new Error("function has no accessible return");
  292. }
  293. this.popMap();
  294. }
  295. assertReturn (fun, optional) {
  296. return fun.commands.reduce(
  297. (last, next) => this.checkCommand(fun.returnType, next, optional) || last, optional
  298. );
  299. }
  300. checkCommand (type, cmd, optional) {
  301. if (cmd instanceof While) {
  302. const resultType = this.evaluateExpressionType(cmd.expression);
  303. if (!resultType.isCompatible(Types.BOOLEAN)) {
  304. throw ProcessorErrorFactory.loop_condition_type_full(cmd.sourceInfo);
  305. }
  306. this.checkCommands(type, cmd.commands, optional);
  307. return false;
  308. } else if (cmd instanceof For) {
  309. this.checkCommand(type, cmd.assignment, optional);
  310. const resultType = this.evaluateExpressionType(cmd.condition);
  311. if (!resultType.isCompatible(Types.BOOLEAN)) {
  312. throw ProcessorErrorFactory.for_condition_type_full(cmd.sourceInfo);
  313. }
  314. this.checkCommand(type, cmd.increment, optional);
  315. this.checkCommands(type, cmd.commands, optional);
  316. return false;
  317. } else if (cmd instanceof Switch) {
  318. const sType = this.evaluateExpressionType(cmd.expression);
  319. let result = optional;
  320. let hasDefault = false;
  321. for (let i = 0; i < cmd.cases.length; i++) {
  322. const aCase = cmd.cases[i];
  323. if (aCase.expression !== null) {
  324. const caseType = this.evaluateExpressionType(aCase.expression);
  325. if (!sType.isCompatible(caseType)) {
  326. const strInfo = sType.stringInfo();
  327. const info = strInfo[0];
  328. const strExp = aCase.expression.toString();
  329. throw ProcessorErrorFactory.invalid_case_type_full(strExp, info.type, info.dim, aCase.sourceInfo);
  330. }
  331. } else {
  332. hasDefault = true;
  333. }
  334. result = result && this.checkCommands(type, aCase.commands, result);
  335. }
  336. return result && hasDefault;
  337. } else if (cmd instanceof ArrayIndexAssign) {
  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.type instanceof CompoundType)) {
  343. throw new Error(cmd.id + " is not an array.");
  344. }
  345. const exp = cmd.expression;
  346. const lineExp = cmd.line;
  347. const lineType = this.evaluateExpressionType(lineExp);
  348. if (!lineType.isCompatible(Types.INTEGER)) {
  349. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  350. }
  351. const columnExp = cmd.column;
  352. if (typeInfo.columns === null && columnExp !== null) {
  353. throw new Error(cmd.id + " is not a matrix");
  354. } else if (columnExp !== null) {
  355. const columnType = this.evaluateExpressionType(columnExp);
  356. if (!columnType.isCompatible(Types.INTEGER)) {
  357. throw ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo);
  358. }
  359. }
  360. // exp can be a arrayLiteral, a single value exp or an array access
  361. if(exp instanceof ArrayLiteral) {
  362. this.evaluateArrayLiteral(typeInfo.lines, (columnExp ? typeInfo.columns : null), typeInfo.type, exp);
  363. } else {
  364. // cannot properly evaluate since type system is poorly constructed
  365. }
  366. return optional;
  367. } else if (cmd instanceof Assign) {
  368. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  369. if(typeInfo === null) {
  370. throw ProcessorErrorFactory.symbol_not_found_full(cmd.id, cmd.sourceInfo);
  371. }
  372. const exp = cmd.expression;
  373. if(exp instanceof ArrayLiteral) {
  374. if(!(typeInfo.type instanceof CompoundType)) {
  375. const stringInfo = typeInfo.type.stringInfo();
  376. const info = stringInfo[0];
  377. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo);
  378. }
  379. this.evaluateArrayLiteral(typeInfo.lines, typeInfo.columns, typeInfo.type, exp);
  380. } else {
  381. const resultType = this.evaluateExpressionType(exp);
  382. if(!resultType.isCompatible(typeInfo.type)) {
  383. const stringInfo = typeInfo.type.stringInfo();
  384. const info = stringInfo[0];
  385. throw ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, cmd.sourceInfo);
  386. }
  387. }
  388. return optional;
  389. } else if (cmd instanceof Break) {
  390. return optional;
  391. } else if (cmd instanceof IfThenElse) {
  392. const resultType = this.evaluateExpressionType(cmd.condition);
  393. if (!resultType.isCompatible(Types.BOOLEAN)) {
  394. throw ProcessorErrorFactory.if_condition_type_full(cmd.sourceInfo);
  395. }
  396. if(cmd.ifFalse instanceof IfThenElse) {
  397. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommand(type, cmd.ifFalse, optional);
  398. } else {
  399. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommands(type, cmd.ifFalse.commands,optional);
  400. }
  401. } else if (cmd instanceof FunctionCall) {
  402. let fun = null;
  403. if (cmd.isMainCall) {
  404. fun = this.getMainFunction();
  405. } else {
  406. fun = this.findFunction(cmd.id);
  407. }
  408. if(fun === null) {
  409. throw ProcessorErrorFactory.function_missing_full(cmd.id, cmd.sourceInfo);
  410. }
  411. this.assertParameters(fun, cmd.actualParameters);
  412. return optional;
  413. } else if (cmd instanceof Return) {
  414. if (cmd.expression === null && !type.isCompatible(Types.VOID)) {
  415. throw new Error('invalid return type');
  416. } else if (cmd.expression !== null) {
  417. const resultType = this.evaluateExpressionType(cmd.expression);
  418. if (!type.isCompatible(resultType)) {
  419. console.log(resultType);
  420. throw new Error('invalid return type');
  421. } else {
  422. return true;
  423. }
  424. } else {
  425. return true;
  426. }
  427. }
  428. }
  429. checkCommands (type, cmds, optional) {
  430. return cmds.reduce(
  431. (last, next) => this.checkCommand(type, next, optional) || last, optional
  432. );
  433. }
  434. assertParameters (fun, actualParametersList) {
  435. if (fun.formalParameters.length !== actualParametersList.length) {
  436. throw new Error("wrong number of parameters...");
  437. }
  438. for (let i = 0; i < actualParametersList.length; i++) {
  439. const param = actualParametersList[i];
  440. const formalParam = fun.formalParameters[i];
  441. if(formalParam.byRef) {
  442. if (!(param instanceof VariableLiteral || param instanceof ArrayAccess)) {
  443. throw new Error("Invalid param type for ref");
  444. }
  445. }
  446. const resultType = this.evaluateExpressionType(param);
  447. if(resultType instanceof MultiType && formalParam.type instanceof MultiType) {
  448. let shared = 0
  449. for (let j = 0; j < resultType.types.length; j++) {
  450. const element = resultType.types[j];
  451. if(formalParam.type.types.indexOf(element) !== -1) {
  452. shared++;
  453. }
  454. }
  455. if(shared <= 0) {
  456. throw new Error(`Parameter ${formalParam.id} is not compatible with the value given.`);
  457. }
  458. } else if (resultType instanceof MultiType) {
  459. if(!resultType.isCompatible(formalParam.type)) {
  460. throw new Error(`Parameter ${formalParam.id} is not compatible with the value given.`);
  461. }
  462. } else if(!formalParam.type.isCompatible(resultType)) {
  463. throw new Error(`Parameter ${formalParam.id} is not compatible with the value given.`);
  464. }
  465. }
  466. }
  467. }