1
0

ivprog_elements.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { Types } from './../ast/types';
  2. import WatchJS from 'melanke-watchjs';
  3. export const COMMAND_TYPES = Object.freeze({function:"function", comment:"comment", reader:"reader", writer:"writer", attribution:"attribution", iftrue:"iftrue",
  4. repeatNtimes:"repeatNtimes", whiletrue:"whiletrue", dowhiletrue:"dowhiletrue", switch:"switch", functioncall:"functioncall"});
  5. export const ARITHMETIC_TYPES = Object.freeze({plus:"plus", minus:"minus", multiplication:"multiplication", division:"division", module:"module"});
  6. export class Variable {
  7. constructor (type, name, value, dimensions = 0, is_constant = false, rows = 0, columns = 0) {
  8. this.type = type;
  9. this.name = name;
  10. this.value = value;
  11. this.dimensions = dimensions;
  12. this.is_constant = is_constant;
  13. this.rows = rows;
  14. this.columns = columns;
  15. }
  16. }
  17. export class Function {
  18. constructor (name, return_type = Types.VOID, return_dimensions = 0, parameters_list = [], is_main = false, is_hidden = false, variables_list = [], function_comment = null, commands = []) {
  19. this.type = COMMAND_TYPES.function;
  20. this.name = name;
  21. this.return_type = return_type;
  22. this.return_dimensions = return_dimensions;
  23. this.parameters_list = parameters_list;
  24. this.is_main = is_main;
  25. this.is_hidden = is_hidden;
  26. this.variables_list = variables_list;
  27. this.function_comment = function_comment;
  28. this.commands = commands;
  29. }
  30. }
  31. export class Comment {
  32. constructor (comment_text) {
  33. this.type = COMMAND_TYPES.comment;
  34. this.comment_text = comment_text;
  35. }
  36. }
  37. export class Reader {
  38. constructor (variable_value_menu = new VariableValueMenu()) {
  39. this.type = COMMAND_TYPES.reader;
  40. this.variable_value_menu = variable_value_menu;
  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 Expression {
  57. constructor (operand1, operand2, operator) {
  58. this.operand1 = operand1;
  59. this.operand2 = operand2;
  60. this.operator = operator;
  61. }
  62. }
  63. export class ExpressionNode {
  64. constructor (parent_node, content, left_node, right_node) {
  65. this.parent_node = parent_node;
  66. this.content = content;
  67. this.left_node = left_node;
  68. this.right_node = right_node;
  69. }
  70. }
  71. export class IfTrue {
  72. constructor (expression, commands_block, commands_else) {
  73. this.type = COMMAND_TYPES.iftrue;
  74. this.expression = expression;
  75. this.commands_block = commands_block;
  76. this.commands_else = commands_else;
  77. }
  78. }
  79. export class RepeatNTimes {
  80. constructor (expression1, expression2, expression3, commands_block) {
  81. this.type = COMMAND_TYPES.repeatNtimes;
  82. this.expression1 = expression1;
  83. this.expression2 = expression2;
  84. this.expression3 = expression3;
  85. this.commands_block = commands_block;
  86. }
  87. }
  88. export class WhileTrue {
  89. constructor (expression, commands_block) {
  90. this.type = COMMAND_TYPES.whiletrue;
  91. this.expression = expression;
  92. this.commands_block = commands_block;
  93. }
  94. }
  95. export class DoWhileTrue {
  96. constructor (expression, commands_block) {
  97. this.type = COMMAND_TYPES.dowhiletrue;
  98. this.expression = expression;
  99. this.commands_block = commands_block;
  100. }
  101. }
  102. export class Switch {
  103. constructor (variable, cases, commands_blocks) {
  104. this.type = COMMAND_TYPES.switch;
  105. this.variable = variable;
  106. this.cases = cases;
  107. this.commands_blocks = commands_blocks;
  108. }
  109. }
  110. export class FunctionCall {
  111. constructor (function_called, parameters_list) {
  112. this.type = COMMAND_TYPES.functioncall;
  113. this.function_called = function_called;
  114. this.parameters_list = parameters_list;
  115. }
  116. }
  117. export class VariableValueMenu {
  118. constructor (variable_and_value = 7, content = null, row = null, column = null, include_constant = true) {
  119. this.type = "var_value";
  120. this.variable_and_value = variable_and_value;
  121. this.content = content;
  122. this.row = row;
  123. this.column = column;
  124. this.include_constant = include_constant;
  125. }
  126. }
  127. export class FunctionCallMenu {
  128. constructor (function_called = null, parameters_list = []) {
  129. this.type = "function_call";
  130. this.function_called = function_called;
  131. this.parameters_list = parameters_list;
  132. }
  133. }
  134. export class Program {
  135. constructor () {
  136. this.functions = [];
  137. this.globals = [];
  138. }
  139. addFunction (function_to_add) {
  140. WatchJS.watch(function_to_add.parameters_list, function(){
  141. console.log("os parametros da função abaixo foram alterados:");
  142. console.log(function_to_add);
  143. }, 1);
  144. WatchJS.watch(function_to_add.variables_list, function(){
  145. console.log("as variáveis da função abaixo foram alteradas: ");
  146. console.log(function_to_add);
  147. }, 1);
  148. this.functions.push(function_to_add);
  149. }
  150. addVariable (function_to_receive, variable) {
  151. if (this.functions[function_to_receive].variable === null) {
  152. this.functions[function_to_receive].variables_list = [];
  153. }
  154. this.functions[function_to_receive].variables_list.push(variable);
  155. }
  156. addGlobal (variable) {
  157. this.globals.push(variable);
  158. }
  159. }