ivprog_elements.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import * as VariableValueMenuManagement from './commands/variable_value_menu';
  2. import { Types } from './types';
  3. import WatchJS from 'melanke-watchjs';
  4. import * as AlgorithmManagement from './algorithm';
  5. export const COMMAND_TYPES = Object.freeze({function:"function", comment:"comment", reader:"reader", writer:"writer", attribution:"attribution", iftrue:"iftrue",
  6. repeatNtimes:"repeatNtimes", whiletrue:"whiletrue", dowhiletrue:"dowhiletrue", switch:"switch", switchcase:"switchcase", functioncall:"functioncall", break:"break",
  7. return:"return"});
  8. export const ARITHMETIC_TYPES = Object.freeze({plus:"plus", minus:"minus", multiplication:"multiplication", division:"division", module:"module", none:"none"});
  9. 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"});
  10. export const EXPRESSION_TYPES = Object.freeze({exp_conditional:"exp_conditional", exp_logic:"exp_logic", exp_arithmetic:"exp_arithmetic"});
  11. export const ARITHMETIC_COMPARISON = Object.freeze({greater_than:"greater_than", less_than:"less_than", equals_to:"equals_to", not_equals_to:"not_equals_to", greater_than_or_equals_to:"greater_than_or_equals_to", less_than_or_equals_to:"less_than_or_equals_to"});
  12. export const LOGIC_COMPARISON = Object.freeze({equals_to:"equals_to", not_equals_to:"not_equals_to", and:"and", or:"or"});
  13. export class Variable {
  14. constructor (type, name, value, dimensions = 0, is_constant = false, rows = 0, columns = 0) {
  15. this.type = type;
  16. this.name = name;
  17. this.value = value;
  18. this.dimensions = dimensions;
  19. this.is_constant = is_constant;
  20. this.rows = rows;
  21. this.columns = columns;
  22. }
  23. }
  24. export class Function {
  25. constructor (name, return_type = Types.VOID, return_dimensions = 0, parameters_list = [], is_main = false, is_hidden = false, variables_list = [], function_comment = null, commands = []) {
  26. this.type = COMMAND_TYPES.function;
  27. this.name = name;
  28. this.return_type = return_type;
  29. this.return_dimensions = return_dimensions;
  30. this.parameters_list = parameters_list;
  31. this.is_main = is_main;
  32. this.is_hidden = is_hidden;
  33. this.variables_list = variables_list;
  34. this.function_comment = function_comment;
  35. this.commands = commands;
  36. }
  37. }
  38. export class Comment {
  39. constructor (comment_text) {
  40. this.type = COMMAND_TYPES.comment;
  41. this.comment_text = comment_text;
  42. }
  43. }
  44. export class Break {
  45. constructor () {
  46. this.type = COMMAND_TYPES.break;
  47. }
  48. }
  49. export class Reader {
  50. constructor (variable_value_menu = new VariableValueMenu()) {
  51. this.type = COMMAND_TYPES.reader;
  52. this.variable_value_menu = variable_value_menu;
  53. }
  54. }
  55. export class Writer {
  56. constructor (content) {
  57. this.type = COMMAND_TYPES.writer;
  58. this.content = content;
  59. }
  60. }
  61. export class Attribution {
  62. constructor (variable, expression = []) {
  63. this.type = COMMAND_TYPES.attribution;
  64. this.variable = variable;
  65. this.expression = expression;
  66. }
  67. }
  68. export class ExpressionElement {
  69. constructor (type_exp, itens = []) {
  70. this.type_exp = type_exp;
  71. this.itens = itens;
  72. }
  73. }
  74. export class ConditionalExpression {
  75. constructor (expression) {
  76. this.type = EXPRESSION_TYPES.exp_conditional;
  77. this.expression = expression;
  78. }
  79. }
  80. export class LogicExpression {
  81. constructor (has_neg, first_operand, second_operand, operator) {
  82. this.type = EXPRESSION_TYPES.exp_logic;
  83. this.has_neg = has_neg;
  84. this.first_operand = first_operand;
  85. this.second_operand = second_operand;
  86. this.operator = operator;
  87. }
  88. }
  89. export class ArithmeticExpression {
  90. constructor (first_operand, second_operand, operator) {
  91. this.type = EXPRESSION_TYPES.exp_arithmetic;
  92. this.first_operand = first_operand;
  93. this.second_operand = second_operand;
  94. this.operator = operator;
  95. }
  96. }
  97. export class IfTrue {
  98. constructor (expression, commands_block, commands_else) {
  99. this.type = COMMAND_TYPES.iftrue;
  100. this.expression = expression;
  101. this.commands_block = commands_block;
  102. this.commands_else = commands_else;
  103. }
  104. }
  105. export class RepeatNTimes {
  106. constructor (var_attribution, var_incrementation, expression1, expression2, expression3, commands_block) {
  107. this.type = COMMAND_TYPES.repeatNtimes;
  108. this.var_attribution = var_attribution;
  109. this.var_incrementation = var_incrementation;
  110. this.expression1 = expression1;
  111. this.expression2 = expression2;
  112. this.expression3 = expression3;
  113. this.commands_block = commands_block;
  114. }
  115. }
  116. export class WhileTrue {
  117. constructor (expression, commands_block) {
  118. this.type = COMMAND_TYPES.whiletrue;
  119. this.expression = expression;
  120. this.commands_block = commands_block;
  121. }
  122. }
  123. export class DoWhileTrue {
  124. constructor (expression, commands_block) {
  125. this.type = COMMAND_TYPES.dowhiletrue;
  126. this.expression = expression;
  127. this.commands_block = commands_block;
  128. }
  129. }
  130. export class Switch {
  131. constructor (variable, cases = []) {
  132. this.type = COMMAND_TYPES.switch;
  133. this.variable = variable;
  134. this.cases = cases;
  135. }
  136. }
  137. export class Return {
  138. constructor (variable_value_menu) {
  139. this.type = COMMAND_TYPES.return;
  140. this.variable_value_menu = variable_value_menu;
  141. }
  142. }
  143. export class SwitchCase {
  144. constructor (variable_value_menu, commands_block = []) {
  145. this.type = COMMAND_TYPES.switchcase;
  146. this.variable_value_menu = variable_value_menu;
  147. this.commands_block = commands_block;
  148. }
  149. }
  150. export class FunctionCall {
  151. constructor (function_called, parameters_list) {
  152. this.type = COMMAND_TYPES.functioncall;
  153. this.function_called = function_called;
  154. this.parameters_list = parameters_list;
  155. }
  156. }
  157. export class VariableValueMenu {
  158. constructor (variable_and_value = 7, content = null, row = null, column = null, include_constant = true) {
  159. this.type = "var_value";
  160. this.variable_and_value = variable_and_value;
  161. this.content = content;
  162. this.row = row;
  163. this.column = column;
  164. this.include_constant = include_constant;
  165. }
  166. }
  167. export class FunctionCallMenu {
  168. constructor (function_called = null, parameters_list = []) {
  169. this.type = "function_call";
  170. this.function_called = function_called;
  171. this.parameters_list = parameters_list;
  172. }
  173. }
  174. export class Program {
  175. constructor () {
  176. this.functions = [];
  177. this.globals = [];
  178. }
  179. addFunction (function_to_add) {
  180. WatchJS.watch(function_to_add.parameters_list, function(){
  181. console.log("os parametros da função abaixo foram alterados:");
  182. console.log(function_to_add);
  183. }, 1);
  184. WatchJS.watch(function_to_add.variables_list, function(){
  185. console.log("as variáveis da função abaixo foram alteradas: ");
  186. console.log(function_to_add);
  187. AlgorithmManagement.renderAlgorithm();
  188. }, 1);
  189. this.functions.push(function_to_add);
  190. }
  191. addVariable (function_to_receive, variable) {
  192. if (this.functions[function_to_receive].variable === null) {
  193. this.functions[function_to_receive].variables_list = [];
  194. }
  195. this.functions[function_to_receive].variables_list.push(variable);
  196. }
  197. addGlobal (variable) {
  198. this.globals.push(variable);
  199. }
  200. }