ivprog_elements.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 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 Reader {
  42. constructor (variable_value_menu = new VariableValueMenu()) {
  43. this.type = COMMAND_TYPES.reader;
  44. this.variable_value_menu = variable_value_menu;
  45. }
  46. }
  47. export class Writer {
  48. constructor (content) {
  49. this.type = COMMAND_TYPES.writer;
  50. this.content = content;
  51. }
  52. }
  53. export class Attribution {
  54. constructor (variable, expression = []) {
  55. this.type = COMMAND_TYPES.attribution;
  56. this.variable = variable;
  57. this.expression = expression;
  58. }
  59. }
  60. export class ExpressionElement {
  61. constructor (type_exp, itens = []) {
  62. this.type_exp = type_exp;
  63. this.itens = itens;
  64. }
  65. }
  66. export class ConditionalExpression {
  67. constructor (expression) {
  68. this.type = EXPRESSION_TYPES.exp_conditional;
  69. this.expression = expression;
  70. }
  71. }
  72. export class LogicExpression {
  73. constructor (has_neg, first_operand, second_operand, operator) {
  74. this.type = EXPRESSION_TYPES.exp_logic;
  75. this.has_neg = has_neg;
  76. this.first_operand = first_operand;
  77. this.second_operand = second_operand;
  78. this.operator = operator;
  79. }
  80. }
  81. export class ArithmeticExpression {
  82. constructor (first_operand, second_operand, operator) {
  83. this.type = EXPRESSION_TYPES.exp_arithmetic;
  84. this.first_operand = first_operand;
  85. this.second_operand = second_operand;
  86. this.operator = operator;
  87. }
  88. }
  89. export class IfTrue {
  90. constructor (expression, commands_block, commands_else) {
  91. this.type = COMMAND_TYPES.iftrue;
  92. this.expression = expression;
  93. this.commands_block = commands_block;
  94. this.commands_else = commands_else;
  95. }
  96. }
  97. export class RepeatNTimes {
  98. constructor (var_attribution, expression1, expression2, expression3, commands_block) {
  99. this.type = COMMAND_TYPES.repeatNtimes;
  100. this.var_attribution = var_attribution;
  101. this.expression1 = expression1;
  102. this.expression2 = expression2;
  103. this.expression3 = expression3;
  104. this.commands_block = commands_block;
  105. }
  106. }
  107. export class WhileTrue {
  108. constructor (expression, commands_block) {
  109. this.type = COMMAND_TYPES.whiletrue;
  110. this.expression = expression;
  111. this.commands_block = commands_block;
  112. }
  113. }
  114. export class DoWhileTrue {
  115. constructor (expression, commands_block) {
  116. this.type = COMMAND_TYPES.dowhiletrue;
  117. this.expression = expression;
  118. this.commands_block = commands_block;
  119. }
  120. }
  121. export class Switch {
  122. constructor (variable, cases, commands_blocks) {
  123. this.type = COMMAND_TYPES.switch;
  124. this.variable = variable;
  125. this.cases = cases;
  126. this.commands_blocks = commands_blocks;
  127. }
  128. }
  129. export class FunctionCall {
  130. constructor (function_called, parameters_list) {
  131. this.type = COMMAND_TYPES.functioncall;
  132. this.function_called = function_called;
  133. this.parameters_list = parameters_list;
  134. }
  135. }
  136. export class VariableValueMenu {
  137. constructor (variable_and_value = 7, content = null, row = null, column = null, include_constant = true) {
  138. this.type = "var_value";
  139. this.variable_and_value = variable_and_value;
  140. this.content = content;
  141. this.row = row;
  142. this.column = column;
  143. this.include_constant = include_constant;
  144. }
  145. }
  146. export class FunctionCallMenu {
  147. constructor (function_called = null, parameters_list = []) {
  148. this.type = "function_call";
  149. this.function_called = function_called;
  150. this.parameters_list = parameters_list;
  151. }
  152. }
  153. export class Program {
  154. constructor () {
  155. this.functions = [];
  156. this.globals = [];
  157. }
  158. addFunction (function_to_add) {
  159. WatchJS.watch(function_to_add.parameters_list, function(){
  160. console.log("os parametros da função abaixo foram alterados:");
  161. console.log(function_to_add);
  162. }, 1);
  163. WatchJS.watch(function_to_add.variables_list, function(){
  164. console.log("as variáveis da função abaixo foram alteradas: ");
  165. console.log(function_to_add);
  166. }, 1);
  167. this.functions.push(function_to_add);
  168. }
  169. addVariable (function_to_receive, variable) {
  170. if (this.functions[function_to_receive].variable === null) {
  171. this.functions[function_to_receive].variables_list = [];
  172. }
  173. this.functions[function_to_receive].variables_list.push(variable);
  174. }
  175. addGlobal (variable) {
  176. this.globals.push(variable);
  177. }
  178. }