semanticAnalyser.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 '../../ast/types';
  9. export class SemanticAnalyser {
  10. constructor(ast) {
  11. this.ast = ast;
  12. this.lexerClass = LanguageService.getCurrentLexer();
  13. const lexer = new this.lexerClass(null);
  14. this.literalNames = lexer.literalNames;
  15. this.symbolMap = null;
  16. }
  17. pushMap () {
  18. if(this.symbolMap === null) {
  19. this.symbolMap = {map:{}, next: null};
  20. } else {
  21. const n = {map:{}, next: this.symbolMap};
  22. this.symbolMap = n;
  23. }
  24. }
  25. popMap () {
  26. if(this.symbolMap !== null) {
  27. this.symbolMap = this.symbolMap.next;
  28. }
  29. }
  30. insertSymbol (id, typeInfo) {
  31. this.symbolMap.map[id] = typeInfo;
  32. }
  33. findSymbol (id, symMap) {
  34. if(!symMap.map[id]) {
  35. if(symMap.next) {
  36. return this.findSymbol(id, symMap.next);
  37. }
  38. throw new Error("variable not defined");
  39. } else {
  40. return symMap.map[id];
  41. }
  42. }
  43. findFunction (name) {
  44. if(name.match(/^\$.+$/)) {
  45. const fun = LanguageDefinedFunction.getFunction(name);
  46. if(!!!fun) {
  47. throw new Error("!!!Internal Error. Language defined function not implemented -> " + name + "!!!");
  48. }
  49. return fun;
  50. } else {
  51. const val = this.ast.functions.find( v => v.name === name);
  52. if (!!!val) {
  53. // TODO: better error message;
  54. throw new Error(`Function ${name} is not defined.`);
  55. }
  56. return val;
  57. }
  58. }
  59. analyseTree () {
  60. const globalVars = this.ast.global;
  61. this.pushMap();
  62. this.assertDeclarations(globalVars);
  63. const functions = this.ast.functions;
  64. const mainFunc = functions.filter((f) => f.name === null);
  65. if (mainFunc.length <= 0) {
  66. throw new Error("no main func...");
  67. } else if (mainFunc.length > 1) {
  68. throw new Error("only one main func...");
  69. }
  70. for (let i = 0; i < functions.length; i++) {
  71. const fun = functions[i];
  72. this.assertFunction(fun);
  73. }
  74. return this.ast;
  75. }
  76. assertDeclarations (list) {
  77. for (let i = 0; i < list.length; i++) {
  78. this.assertDeclaration(list[i]);
  79. }
  80. }
  81. assertDeclaration (declaration) {
  82. if (declaration instanceof ArrayDeclaration) {
  83. if(declaration.initial === null) {
  84. const lineType = this.evaluateExpressionType(declaration.lines);
  85. if (lineType !== Types.INTEGER) {
  86. throw new Error("dim must be int");
  87. }
  88. if (declaration.columns !== null) {
  89. const columnType = this.evaluateExpressionType(declaration.columns);
  90. if (columnType !== Types.INTEGER) {
  91. throw new Error("dim must be int");
  92. }
  93. }
  94. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type, subtype: declaration.subtype});
  95. return;
  96. }
  97. this.evaluateArrayLiteral(declaration.lines, declaration.columns, declaration.subtype, declaration.initial);
  98. this.insertSymbol(declaration.id, {id: declaration.id, lines: declaration.lines, columns: declaration.columns, type: declaration.type, subtype: declaration.subtype});
  99. } else {
  100. if(declaration.initial === null) {
  101. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type});
  102. return;
  103. }
  104. const resultType = this.evaluateExpressionType(declaration.initial);
  105. if(declaration.type !== resultType) {
  106. throw new Error('Invalid type');
  107. } else {
  108. this.insertSymbol(declaration.id, {id: declaration.id, type: declaration.type})
  109. }
  110. }
  111. }
  112. evaluateExpressionType (expression) {
  113. if(expression instanceof UnaryApp) {
  114. const op = expression.op;
  115. const resultType = this.evaluateExpressionType(expression.left);
  116. return resultTypeAfterUnaryOp(op, resultType);
  117. } else if (expression instanceof InfixApp) {
  118. const op = expression.op;
  119. const resultTypeLeft = this.evaluateExpressionType(expression.left);
  120. const resultTypeRight = this.evaluateExpressionType(expression.right);
  121. return resultTypeAfterInfixOp(op, resultTypeLeft, resultTypeRight);
  122. } else if (expression instanceof Literal) {
  123. return this.evaluateLiteralType(expression);
  124. } else if (expression instanceof FunctionCall) {
  125. const fun = this.findFunction(expression.id);
  126. if (fun.returnType === Types.VOID) {
  127. throw new Error("void return");
  128. }
  129. this.assertParameters(fun, expression.actualParameters);
  130. return fun.returnType;
  131. } else if (expression instanceof ArrayAccess) {
  132. const arrayTypeInfo = this.findSymbol(expression.id, this.symbolMap);
  133. if (arrayTypeInfo.type !== Types.ARRAY) {
  134. throw new Error("it's not an array");
  135. }
  136. const lineType = this.evaluateExpressionType(expression.line);
  137. if (lineType !== Types.INTEGER) {
  138. throw new Error("line must be integer");
  139. }
  140. if (expression.column != null) {
  141. if (arrayTypeInfo.columns === null) {
  142. throw new Error("it's not a matrix");
  143. }
  144. const columnType = this.evaluateExpressionType(expression.column);
  145. if(columnType !== Types.INTEGER) {
  146. throw new Error("column must be integer");
  147. }
  148. }
  149. return arrayTypeInfo.subtype;
  150. }
  151. }
  152. evaluateLiteralType (literal) {
  153. if(literal instanceof IntLiteral) {
  154. return literal.type;
  155. } else if (literal instanceof RealLiteral) {
  156. return literal.type;
  157. } else if (literal instanceof StringLiteral) {
  158. return literal.type;
  159. } else if (literal instanceof BoolLiteral) {
  160. return literal.type;
  161. } else if (literal instanceof VariableLiteral) {
  162. const typeInfo = this.findSymbol(literal.id, this.symbolMap);
  163. if (typeInfo.type === Types.ARRAY) {
  164. return typeInfo;
  165. }
  166. return typeInfo.type;
  167. } else {
  168. console.warn("Evaluating type only for an array literal...");
  169. return Types.UNDEFINED;
  170. }
  171. }
  172. evaluateArrayLiteral (lines, columns, subtype, literal) {
  173. if (literal instanceof ArrayLiteral) {
  174. if (columns === null) {
  175. // it's a vector...
  176. const dimType = this.evaluateExpressionType(lines);
  177. if (dimType !== Types.INTEGER) {
  178. throw new Error("dim must be int");
  179. }
  180. if ((lines instanceof IntLiteral) && !lines.value.eq(literal.value.length)) {
  181. throw new Error("invalid array size");
  182. }
  183. literal.value.reduce((last, next) => {
  184. const eType = this.evaluateExpressionType(next);
  185. if (eType !== last) {
  186. throw new Error("invalid array type");
  187. }
  188. return eType;
  189. }, subtype);
  190. return true;
  191. } else {
  192. const dimType = this.evaluateExpressionType(columns);
  193. if (dimType !== Types.INTEGER) {
  194. throw new Error("dim must be int");
  195. }
  196. if ((columns instanceof IntLiteral) && !columns.value.eq(literal.value.length)) {
  197. throw new Error("invalid array size");
  198. }
  199. for (let i = 0; i < columns; i++) {
  200. const anotherArray = literal.value[i];
  201. this.evaluateArrayLiteral(lines, null, subtype, anotherArray)
  202. }
  203. }
  204. } else {
  205. const resultType = this.evaluateExpressionType(literal);
  206. if (!resultType.subtype) {
  207. throw new Error("initial must be of type array");
  208. }
  209. if (resultType.subtype !== subtype) {
  210. throw new Error("invalid array type");
  211. } else if (resultType.lines !== lines) {
  212. throw new Error("invalid array size");
  213. } else if (resultType.columns !== columns) {
  214. throw new Error("invalid array size");
  215. }
  216. return true;
  217. }
  218. }
  219. assertFunction (fun) {
  220. this.pushMap();
  221. this.assertDeclarations(fun.variablesDeclarations);
  222. const optional = fun.returnType === Types.VOID;
  223. const valid = this.assertReturn(fun, optional);
  224. if (!valid) {
  225. throw new Error("function has no accessible return");
  226. }
  227. this.popMap();
  228. }
  229. assertReturn (fun, optional) {
  230. return fun.commands.reduce(
  231. (last, next) => this.checkCommand(fun.returnType, next, optional) || last, optional
  232. );
  233. }
  234. checkCommand (type, cmd, optional) {
  235. if (cmd instanceof While) {
  236. const resultType = this.evaluateExpressionType(cmd.expression);
  237. if (resultType !== Types.BOOLEAN) {
  238. throw new Error("condition not boolean");
  239. }
  240. this.checkCommands(type, cmd.commands, optional);
  241. return false;
  242. } else if (cmd instanceof For) {
  243. this.checkCommand(type, cmd.assignment, optional);
  244. const resultType = this.evaluateExpressionType(cmd.condition);
  245. if (resultType !== Types.BOOLEAN) {
  246. throw new Error("condition not boolean");
  247. }
  248. this.checkCommand(type, cmd.increment, optional);
  249. this.checkCommands(type, cmd.commands, optional);
  250. return false;
  251. } else if (cmd instanceof Switch) {
  252. const sType = this.evaluateExpressionType(cmd.expression);
  253. let result = optional;
  254. let hasDefault = false;
  255. for (let i = 0; i < cmd.cases.length; i++) {
  256. const aCase = cmd.cases[i];
  257. if (aCase.expression !== null) {
  258. const caseType = this.evaluateExpressionType(aCase.expression);
  259. if (sType !== caseType) {
  260. throw new Error("invalid type in case");
  261. }
  262. } else {
  263. hasDefault = true;
  264. }
  265. result = result && this.checkCommands(type, aCase.commands, result);
  266. }
  267. return result && hasDefault;
  268. } else if (cmd instanceof ArrayIndexAssign) {
  269. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  270. if(!typeInfo.subtype) {
  271. throw new Error(cmd.id + " is not an array.");
  272. }
  273. const exp = cmd.expression;
  274. const lineExp = cmd.line;
  275. const lineType = this.evaluateExpressionType(lineExp);
  276. if (lineType !== Types.INTEGER) {
  277. throw new Error("array dimension must be of type int");
  278. }
  279. const columnExp = cmd.column;
  280. if (typeInfo.columns === null && columnExp !== null) {
  281. throw new Error(cmd.id + " is not a matrix");
  282. } else if (columnExp !== null) {
  283. const columnType = this.evaluateExpressionType(columnExp);
  284. if (columnType !== Types.INTEGER) {
  285. throw new Error("array dimension must be of type int");
  286. }
  287. }
  288. // exp can be a arrayLiteral, a single value exp or an array access
  289. if(exp instanceof ArrayLiteral) {
  290. this.evaluateArrayLiteral(typeInfo.lines, (columnExp ? typeInfo.columns : null), typeInfo.subtype, exp);
  291. } else {
  292. // cannot properly evaluate since type system is poorly constructed
  293. }
  294. return optional;
  295. } else if (cmd instanceof Assign) {
  296. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  297. const exp = cmd.expression;
  298. if(exp instanceof ArrayLiteral) {
  299. if(!typeInfo.subtype) {
  300. throw new Error("type not compatible");
  301. }
  302. this.evaluateArrayLiteral(typeInfo.lines, typeInfo.columns, typeInfo.subtype, exp);
  303. } else {
  304. if(typeInfo.subtype) {
  305. throw new Error("type not compatible");
  306. }
  307. const resultType = this.evaluateExpressionType(exp);
  308. if(resultType !== typeInfo.type) {
  309. throw new Error("type not compatible");
  310. }
  311. }
  312. return optional;
  313. } else if (cmd instanceof Break) {
  314. return optional;
  315. } else if (cmd instanceof IfThenElse) {
  316. const resultType = this.evaluateExpressionType(cmd.condition);
  317. if (resultType !== Types.BOOLEAN) {
  318. throw new Error("condition not boolean");
  319. }
  320. console.log(cmd);
  321. if(cmd.ifFalse instanceof IfThenElse) {
  322. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommand(type, cmd.ifFalse, optional);
  323. } else {
  324. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommands(type, cmd.ifFalse.commands,optional);
  325. }
  326. } else if (cmd instanceof FunctionCall) {
  327. const fun = this.findFunction(cmd.id);
  328. this.assertParameters(fun, cmd.actualParameters);
  329. return optional;
  330. } else if (cmd instanceof Return) {
  331. if (cmd.expression === null && type !== Types.VOID) {
  332. throw new Error('invalid return type');
  333. } else if (cmd.expression !== null) {
  334. const resultType = this.evaluateExpressionType(cmd.expression);
  335. if (resultType !== type) {
  336. throw new Error('invalid return type');
  337. } else {
  338. return true;
  339. }
  340. } else {
  341. return true;
  342. }
  343. }
  344. }
  345. checkCommands (type, cmds, optional) {
  346. return cmds.reduce(
  347. (last, next) => this.checkCommand(type, next, optional) || last, optional
  348. );
  349. }
  350. assertParameters (fun, actualParametersList) {
  351. if (fun.formalParameters.length !== actualParametersList.length) {
  352. throw new Error("wrong number of parameters...");
  353. }
  354. for (let i = 0; i < actualParametersList.length; i++) {
  355. const param = actualParametersList[i];
  356. const formalParam = fun.formalParameters[i];
  357. if(formalParam.byRef) {
  358. if (!(param instanceof VariableLiteral || param instanceof ArrayAccess)) {
  359. throw new Error("Invalid param type");
  360. }
  361. }
  362. const resultType = this.evaluateExpressionType(param);
  363. switch (formalParam.dimensions) {
  364. case 1: {
  365. if (!resultType.subtype) {
  366. throw new Error("invalid param type");
  367. } else if (resultType.subtype !== formalParam.type) {
  368. throw new Error("invalid param type");
  369. } else if (resultType.lines === null || resultType.columns !== null) {
  370. throw new Error("invalid param type");
  371. }
  372. break;
  373. }
  374. case 2: {
  375. if (!resultType.subtype) {
  376. throw new Error("invalid param type");
  377. } else if (resultType.subtype !== formalParam.type) {
  378. throw new Error("invalid param type");
  379. } else if (resultType.lines === null || resultType.columns === null) {
  380. throw new Error("invalid param type");
  381. }
  382. break;
  383. }
  384. default: {
  385. if (resultType.subtype) {
  386. throw new Error("invalid param type");
  387. }
  388. if (formalParam.type !== Types.ALL && resultType !== formalParam.type) {
  389. throw new Error("invalid param type");
  390. }
  391. break;
  392. }
  393. }
  394. }
  395. }
  396. }