ivprog_elements.js 4.0 KB

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