ivprog_elements.js 6.7 KB

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