semanticAnalyser.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 } 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 Assign) {
  269. const typeInfo = this.findSymbol(cmd.id, this.symbolMap);
  270. const exp = cmd.expression;
  271. if(exp instanceof ArrayLiteral) {
  272. if(!typeInfo.subtype) {
  273. throw new Error("type not compatible");
  274. }
  275. this.evaluateArrayLiteral(typeInfo.lines, typeInfo.columns, typeInfo.subtype, exp);
  276. } else {
  277. if(typeInfo.subtype) {
  278. throw new Error("type not compatible");
  279. }
  280. const resultType = this.evaluateExpressionType(exp);
  281. if(resultType !== typeInfo.type) {
  282. throw new Error("type not compatible");
  283. }
  284. }
  285. return optional;
  286. } else if (cmd instanceof Break) {
  287. return optional;
  288. } else if (cmd instanceof IfThenElse) {
  289. const resultType = this.evaluateExpressionType(cmd.condition);
  290. if (resultType !== Types.BOOLEAN) {
  291. throw new Error("condition not boolean");
  292. }
  293. console.log(cmd);
  294. if(cmd.ifFalse instanceof IfThenElse) {
  295. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommand(type, cmd.ifFalse, optional);
  296. } else {
  297. return this.checkCommands(type, cmd.ifTrue.commands, optional) && this.checkCommands(type, cmd.ifFalse.commands,optional);
  298. }
  299. } else if (cmd instanceof FunctionCall) {
  300. const fun = this.findFunction(cmd.id);
  301. this.assertParameters(fun, cmd.actualParameters);
  302. return optional;
  303. } else if (cmd instanceof Return) {
  304. if (cmd.expression === null && type !== Types.VOID) {
  305. throw new Error('invalid return type');
  306. } else if (cmd.expression !== null) {
  307. const resultType = this.evaluateExpressionType(cmd.expression);
  308. if (resultType !== type) {
  309. throw new Error('invalid return type');
  310. } else {
  311. return true;
  312. }
  313. } else {
  314. return true;
  315. }
  316. }
  317. }
  318. checkCommands (type, cmds, optional) {
  319. return cmds.reduce(
  320. (last, next) => this.checkCommand(type, next, optional) || last, optional
  321. );
  322. }
  323. assertParameters (fun, actualParametersList) {
  324. if (fun.formalParameters.length !== actualParametersList.length) {
  325. throw new Error("wrong number of parameters...");
  326. }
  327. for (let i = 0; i < actualParametersList.length; i++) {
  328. const param = actualParametersList[i];
  329. const formalParam = fun.formalParameters[i];
  330. if(formalParam.byRef && !(param instanceof VariableLiteral)) {
  331. throw new Error("Invalid param type");
  332. }
  333. const resultType = this.evaluateExpressionType(param);
  334. switch (formalParam.dimensions) {
  335. case 1: {
  336. if (!resultType.subtype) {
  337. throw new Error("invalid param type");
  338. } else if (resultType.subtype !== formalParam.type) {
  339. throw new Error("invalid param type");
  340. } else if (resultType.lines === null || resultType.columns !== null) {
  341. throw new Error("invalid param type");
  342. }
  343. break;
  344. }
  345. case 2: {
  346. if (!resultType.subtype) {
  347. throw new Error("invalid param type");
  348. } else if (resultType.subtype !== formalParam.type) {
  349. throw new Error("invalid param type");
  350. } else if (resultType.lines === null || resultType.columns === null) {
  351. throw new Error("invalid param type");
  352. }
  353. break;
  354. }
  355. default: {
  356. if (resultType.subtype) {
  357. throw new Error("invalid param type");
  358. }
  359. if (formalParam.type !== Types.ALL && resultType !== formalParam.type) {
  360. throw new Error("invalid param type");
  361. }
  362. break;
  363. }
  364. }
  365. }
  366. }
  367. }