ivprog_elements.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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) {
  37. this.type = COMMAND_TYPES.reader;
  38. this.variable = variable;
  39. this.row = row;
  40. this.column = column;
  41. }
  42. }
  43. export class Writer {
  44. constructor (content) {
  45. this.type = COMMAND_TYPES.writer;
  46. this.content = content;
  47. }
  48. }
  49. export class Attribution {
  50. constructor (variable, expression) {
  51. this.type = COMMAND_TYPES.attribution;
  52. this.variable = variable;
  53. this.expression = expression;
  54. }
  55. }
  56. export class IfTrue {
  57. constructor (expression, commands_block, commands_else) {
  58. this.type = COMMAND_TYPES.iftrue;
  59. this.expression = expression;
  60. this.commands_block = commands_block;
  61. this.commands_else = commands_else;
  62. }
  63. }
  64. export class RepeatNTimes {
  65. constructor (expression1, expression2, expression3, commands_block) {
  66. this.type = COMMAND_TYPES.repeatNtimes;
  67. this.expression1 = expression1;
  68. this.expression2 = expression2;
  69. this.expression3 = expression3;
  70. this.commands_block = commands_block;
  71. }
  72. }
  73. export class WhileTrue {
  74. constructor (expression, commands_block) {
  75. this.type = COMMAND_TYPES.whiletrue;
  76. this.expression = expression;
  77. this.commands_block = commands_block;
  78. }
  79. }
  80. export class DoWhileTrue {
  81. constructor (expression, commands_block) {
  82. this.type = COMMAND_TYPES.dowhiletrue;
  83. this.expression = expression;
  84. this.commands_block = commands_block;
  85. }
  86. }
  87. export class Switch {
  88. constructor (variable, cases, commands_blocks) {
  89. this.type = COMMAND_TYPES.switch;
  90. this.variable = variable;
  91. this.cases = cases;
  92. this.commands_blocks = commands_blocks;
  93. }
  94. }
  95. export class FunctionCall {
  96. constructor (function_called, parameters_list) {
  97. this.type = COMMAND_TYPES.functioncall;
  98. this.function_called = function_called;
  99. this.parameters_list = parameters_list;
  100. }
  101. }
  102. export class Program {
  103. constructor () {
  104. this.functions = [];
  105. this.globals = [];
  106. }
  107. addFunction (function_to_add) {
  108. this.functions.push(function_to_add);
  109. }
  110. addVariable (function_to_receive, variable) {
  111. if (this.functions[function_to_receive].variable === null) {
  112. this.functions[function_to_receive].variables_list = [];
  113. }
  114. this.functions[function_to_receive].variables_list.push(variable);
  115. }
  116. addGlobal (variable) {
  117. this.globals.push(variable);
  118. }
  119. }