ivprog_elements.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { Types } from './../ast/types';
  2. export const COMMAND_TYPES = Object.freeze({function:"function", comment:"comment", reader:"reader", writer:"writer", attribution:"attribution", iftrue:"iftrue",
  3. repeatNtimes:"repeatNtimes", whiletrue:"whiletrue", dowhiletrue:"dowhiletrue", switch:"switch", functioncall:"functioncall"});
  4. export const ARITHMETIC_TYPES = Object.freeze({plus:"plus", minus:"minus", multiplication:"multiplication", division:"division", module:"module"});
  5. export class Variable {
  6. constructor (type, name, value, dimensions = 0, is_constant = false, rows = 0, columns = 0) {
  7. this.type = type;
  8. this.name = name;
  9. this.value = value;
  10. this.dimensions = dimensions;
  11. this.is_constant = is_constant;
  12. this.rows = rows;
  13. this.columns = columns;
  14. }
  15. }
  16. export class Function {
  17. constructor (name, return_type = Types.VOID, return_dimensions = 0, parameters_list = [], is_main = false, is_hidden = false, variables_list = [], function_comment = null) {
  18. this.type = COMMAND_TYPES.function;
  19. this.name = name;
  20. this.return_type = return_type;
  21. this.return_dimensions = return_dimensions;
  22. this.parameters_list = parameters_list;
  23. this.is_main = is_main;
  24. this.is_hidden = is_hidden;
  25. this.variables_list = variables_list;
  26. this.function_comment = function_comment;
  27. this.commands = [];
  28. }
  29. }
  30. export class Comment {
  31. constructor (comment_text) {
  32. this.type = COMMAND_TYPES.comment;
  33. this.comment_text = comment_text;
  34. }
  35. }
  36. export class Reader {
  37. constructor (variable = null, row = null, column = null, variable_value_menu = null) {
  38. this.type = COMMAND_TYPES.reader;
  39. this.variable = variable;
  40. this.row = row;
  41. this.column = column;
  42. this.variable_value_menu = variable_value_menu;
  43. }
  44. }
  45. export class Writer {
  46. constructor (content) {
  47. this.type = COMMAND_TYPES.writer;
  48. this.content = content;
  49. }
  50. }
  51. export class Attribution {
  52. constructor (variable, expression) {
  53. this.type = COMMAND_TYPES.attribution;
  54. this.variable = variable;
  55. this.expression = expression;
  56. }
  57. }
  58. export class Expression {
  59. constructor (operand1, operand2, operator) {
  60. this.operand1 = operand1;
  61. this.operand2 = operand2;
  62. this.operator = operator;
  63. }
  64. }
  65. export class IfTrue {
  66. constructor (expression, commands_block, commands_else) {
  67. this.type = COMMAND_TYPES.iftrue;
  68. this.expression = expression;
  69. this.commands_block = commands_block;
  70. this.commands_else = commands_else;
  71. }
  72. }
  73. export class RepeatNTimes {
  74. constructor (expression1, expression2, expression3, commands_block) {
  75. this.type = COMMAND_TYPES.repeatNtimes;
  76. this.expression1 = expression1;
  77. this.expression2 = expression2;
  78. this.expression3 = expression3;
  79. this.commands_block = commands_block;
  80. }
  81. }
  82. export class WhileTrue {
  83. constructor (expression, commands_block) {
  84. this.type = COMMAND_TYPES.whiletrue;
  85. this.expression = expression;
  86. this.commands_block = commands_block;
  87. }
  88. }
  89. export class DoWhileTrue {
  90. constructor (expression, commands_block) {
  91. this.type = COMMAND_TYPES.dowhiletrue;
  92. this.expression = expression;
  93. this.commands_block = commands_block;
  94. }
  95. }
  96. export class Switch {
  97. constructor (variable, cases, commands_blocks) {
  98. this.type = COMMAND_TYPES.switch;
  99. this.variable = variable;
  100. this.cases = cases;
  101. this.commands_blocks = commands_blocks;
  102. }
  103. }
  104. export class FunctionCall {
  105. constructor (function_called, parameters_list) {
  106. this.type = COMMAND_TYPES.functioncall;
  107. this.function_called = function_called;
  108. this.parameters_list = parameters_list;
  109. }
  110. }
  111. export class VariableValueMenu {
  112. constructor (variable_and_value = 7, content = null, row = null, column = null, include_constant = true) {
  113. this.variable_and_value = variable_and_value;
  114. this.content = content;
  115. this.row = row;
  116. this.column = column;
  117. this.include_constant = include_constant;
  118. }
  119. }
  120. export class Program {
  121. constructor () {
  122. this.functions = [];
  123. this.globals = [];
  124. }
  125. addFunction (function_to_add) {
  126. this.functions.push(function_to_add);
  127. }
  128. addVariable (function_to_receive, variable) {
  129. if (this.functions[function_to_receive].variable === null) {
  130. this.functions[function_to_receive].variables_list = [];
  131. }
  132. this.functions[function_to_receive].variables_list.push(variable);
  133. }
  134. addGlobal (variable) {
  135. this.globals.push(variable);
  136. }
  137. }