1
0

ivprog_elements.js 5.2 KB

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