ivprog_elements.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 const SYSTEM_FUNCTIONS_CATEGORIES = Object.freeze({math:"math", text:"text_t", arrangement:"arrangement", conversion:"conversion"});
  14. export class Variable {
  15. constructor (type, name, value, dimensions = 0, is_constant = false, rows = 0, columns = 0) {
  16. this.type = type;
  17. this.name = name;
  18. this.value = value;
  19. this.dimensions = dimensions;
  20. this.is_constant = is_constant;
  21. this.rows = rows;
  22. this.columns = columns;
  23. }
  24. }
  25. export class Function {
  26. constructor (name, return_type = Types.VOID, return_dimensions = 0, parameters_list = [], is_main = false, is_hidden = false, variables_list = [], function_comment = null, commands = []) {
  27. this.type = COMMAND_TYPES.function;
  28. this.name = name;
  29. this.return_type = return_type;
  30. this.return_dimensions = return_dimensions;
  31. this.parameters_list = parameters_list;
  32. this.is_main = is_main;
  33. this.is_hidden = is_hidden;
  34. this.variables_list = variables_list;
  35. this.function_comment = function_comment;
  36. this.commands = commands;
  37. }
  38. }
  39. export class SystemFunction {
  40. constructor (identifier, return_type, return_dimensions, parameters_list, function_comment = null, category) {
  41. this.type = COMMAND_TYPES.function;
  42. this.identifier = identifier;
  43. this.return_type = return_type;
  44. this.return_dimensions = return_dimensions;
  45. this.parameters_list = parameters_list;
  46. this.function_comment = function_comment;
  47. this.category = category;
  48. }
  49. }
  50. export class Comment {
  51. constructor (comment_text) {
  52. this.type = COMMAND_TYPES.comment;
  53. this.comment_text = comment_text;
  54. }
  55. }
  56. export class Break {
  57. constructor () {
  58. this.type = COMMAND_TYPES.break;
  59. }
  60. }
  61. export class Reader {
  62. constructor (variable_value_menu = new VariableValueMenu()) {
  63. this.type = COMMAND_TYPES.reader;
  64. this.variable_value_menu = variable_value_menu;
  65. }
  66. }
  67. export class Writer {
  68. constructor (content) {
  69. this.type = COMMAND_TYPES.writer;
  70. this.content = content;
  71. }
  72. }
  73. export class Attribution {
  74. constructor (variable, expression = []) {
  75. this.type = COMMAND_TYPES.attribution;
  76. this.variable = variable;
  77. this.expression = expression;
  78. }
  79. }
  80. export class ExpressionElement {
  81. constructor (type_exp, itens = []) {
  82. this.type_exp = type_exp;
  83. this.itens = itens;
  84. }
  85. }
  86. export class ConditionalExpression {
  87. constructor (expression) {
  88. this.type = EXPRESSION_TYPES.exp_conditional;
  89. this.expression = expression;
  90. }
  91. }
  92. export class LogicExpression {
  93. constructor (has_neg, first_operand, second_operand, operator) {
  94. this.type = EXPRESSION_TYPES.exp_logic;
  95. this.has_neg = has_neg;
  96. this.first_operand = first_operand;
  97. this.second_operand = second_operand;
  98. this.operator = operator;
  99. }
  100. }
  101. export class ArithmeticExpression {
  102. constructor (first_operand, second_operand, operator) {
  103. this.type = EXPRESSION_TYPES.exp_arithmetic;
  104. this.first_operand = first_operand;
  105. this.second_operand = second_operand;
  106. this.operator = operator;
  107. }
  108. }
  109. export class IfTrue {
  110. constructor (expression, commands_block, commands_else) {
  111. this.type = COMMAND_TYPES.iftrue;
  112. this.expression = expression;
  113. this.commands_block = commands_block;
  114. this.commands_else = commands_else;
  115. }
  116. }
  117. export class RepeatNTimes {
  118. constructor (var_attribution, var_incrementation, expression1, expression2, expression3, commands_block) {
  119. this.type = COMMAND_TYPES.repeatNtimes;
  120. this.var_attribution = var_attribution;
  121. this.var_incrementation = var_incrementation;
  122. this.expression1 = expression1;
  123. this.expression2 = expression2;
  124. this.expression3 = expression3;
  125. this.commands_block = commands_block;
  126. }
  127. }
  128. export class WhileTrue {
  129. constructor (expression, commands_block) {
  130. this.type = COMMAND_TYPES.whiletrue;
  131. this.expression = expression;
  132. this.commands_block = commands_block;
  133. }
  134. }
  135. export class DoWhileTrue {
  136. constructor (expression, commands_block) {
  137. this.type = COMMAND_TYPES.dowhiletrue;
  138. this.expression = expression;
  139. this.commands_block = commands_block;
  140. }
  141. }
  142. export class Switch {
  143. constructor (variable, cases = []) {
  144. this.type = COMMAND_TYPES.switch;
  145. this.variable = variable;
  146. this.cases = cases;
  147. }
  148. }
  149. export class Return {
  150. constructor (variable_value_menu) {
  151. this.type = COMMAND_TYPES.return;
  152. this.variable_value_menu = variable_value_menu;
  153. }
  154. }
  155. export class SwitchCase {
  156. constructor (variable_value_menu, commands_block = []) {
  157. this.type = COMMAND_TYPES.switchcase;
  158. this.variable_value_menu = variable_value_menu;
  159. this.commands_block = commands_block;
  160. }
  161. }
  162. export class FunctionCall {
  163. constructor (function_called, parameters_list) {
  164. this.type = COMMAND_TYPES.functioncall;
  165. this.function_called = function_called;
  166. this.parameters_list = parameters_list;
  167. }
  168. }
  169. export class VariableValueMenu {
  170. constructor (variable_and_value = 7, content = null, row = null, column = null, include_constant = true) {
  171. this.type = "var_value";
  172. this.variable_and_value = variable_and_value;
  173. this.content = content;
  174. this.row = row;
  175. this.column = column;
  176. this.include_constant = include_constant;
  177. }
  178. }
  179. export class FunctionCallMenu {
  180. constructor (function_called = null, parameters_list = []) {
  181. this.type = "function_call";
  182. this.function_called = function_called;
  183. this.parameters_list = parameters_list;
  184. }
  185. }
  186. export class Program {
  187. constructor () {
  188. this.functions = [];
  189. this.globals = [];
  190. }
  191. addFunction (function_to_add) {
  192. WatchJS.watch(function_to_add.parameters_list, function(){
  193. if (window.insertContext) {
  194. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  195. window.insertContext = false;
  196. } else {
  197. AlgorithmManagement.renderAlgorithm();
  198. }
  199. }, 1);
  200. WatchJS.watch(function_to_add.variables_list, function(){
  201. if (window.insertContext) {
  202. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  203. window.insertContext = false;
  204. } else {
  205. AlgorithmManagement.renderAlgorithm();
  206. }
  207. }, 1);
  208. this.functions.push(function_to_add);
  209. }
  210. addVariable (function_to_receive, variable) {
  211. if (this.functions[function_to_receive].variable === null) {
  212. this.functions[function_to_receive].variables_list = [];
  213. }
  214. this.functions[function_to_receive].variables_list.push(variable);
  215. }
  216. addGlobal (variable) {
  217. this.globals.push(variable);
  218. }
  219. }