ivprog_elements.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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", not:"not"});
  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 ExpressionOperator {
  81. constructor (type_op, item) {
  82. this.type_op = type_op; // Logic, Arithmetic OR Relational
  83. this.item = item;
  84. }
  85. }
  86. export class ExpressionElement {
  87. constructor (type_exp, itens = []) {
  88. this.type_exp = type_exp;
  89. this.itens = itens;
  90. }
  91. }
  92. export class ConditionalExpression {
  93. constructor (expression) {
  94. this.type = EXPRESSION_TYPES.exp_conditional;
  95. this.expression = expression;
  96. }
  97. }
  98. export class LogicExpression {
  99. constructor (has_neg, first_operand, second_operand, operator) {
  100. this.type = EXPRESSION_TYPES.exp_logic;
  101. this.has_neg = has_neg;
  102. this.first_operand = first_operand;
  103. this.second_operand = second_operand;
  104. this.operator = operator;
  105. }
  106. }
  107. export class ArithmeticExpression {
  108. constructor (first_operand, second_operand, operator) {
  109. this.type = EXPRESSION_TYPES.exp_arithmetic;
  110. this.first_operand = first_operand;
  111. this.second_operand = second_operand;
  112. this.operator = operator;
  113. }
  114. }
  115. export class IfTrue {
  116. constructor (expression, commands_block, commands_else) {
  117. this.type = COMMAND_TYPES.iftrue;
  118. this.expression = expression;
  119. this.commands_block = commands_block;
  120. this.commands_else = commands_else;
  121. }
  122. }
  123. export class RepeatNTimes {
  124. constructor (var_attribution, var_incrementation, expression1, expression2, expression3, commands_block) {
  125. this.type = COMMAND_TYPES.repeatNtimes;
  126. this.var_attribution = var_attribution;
  127. this.var_incrementation = var_incrementation;
  128. this.expression1 = expression1;
  129. this.expression2 = expression2;
  130. this.expression3 = expression3;
  131. this.commands_block = commands_block;
  132. }
  133. }
  134. export class WhileTrue {
  135. constructor (expression, commands_block) {
  136. this.type = COMMAND_TYPES.whiletrue;
  137. this.expression = expression;
  138. this.commands_block = commands_block;
  139. }
  140. }
  141. export class DoWhileTrue {
  142. constructor (expression, commands_block) {
  143. this.type = COMMAND_TYPES.dowhiletrue;
  144. this.expression = expression;
  145. this.commands_block = commands_block;
  146. }
  147. }
  148. export class Switch {
  149. constructor (variable, cases = []) {
  150. this.type = COMMAND_TYPES.switch;
  151. this.variable = variable;
  152. this.cases = cases;
  153. }
  154. }
  155. export class Return {
  156. constructor (variable_value_menu) {
  157. this.type = COMMAND_TYPES.return;
  158. this.variable_value_menu = variable_value_menu;
  159. }
  160. }
  161. export class SwitchCase {
  162. constructor (variable_value_menu, commands_block = []) {
  163. this.type = COMMAND_TYPES.switchcase;
  164. this.variable_value_menu = variable_value_menu;
  165. this.commands_block = commands_block;
  166. }
  167. }
  168. export class FunctionCall {
  169. constructor (function_called, parameters_list) {
  170. this.type = COMMAND_TYPES.functioncall;
  171. this.function_called = function_called;
  172. this.parameters_list = parameters_list;
  173. }
  174. }
  175. export class VariableValueMenu {
  176. constructor (variable_and_value = 7, content = null, row = null, column = null, include_constant = true, dimensions = 0) {
  177. this.type = "var_value";
  178. this.variable_and_value = variable_and_value;
  179. this.content = content;
  180. this.row = row;
  181. this.column = column;
  182. this.include_constant = include_constant;
  183. this.dimensions = dimensions;
  184. }
  185. }
  186. export class FunctionCallMenu {
  187. constructor (function_called = null, parameters_list = []) {
  188. this.type = "function_call";
  189. this.function_called = function_called;
  190. this.parameters_list = parameters_list;
  191. }
  192. }
  193. export class Program {
  194. constructor () {
  195. this.functions = [];
  196. this.globals = [];
  197. }
  198. addFunction (function_to_add) {
  199. WatchJS.watch(function_to_add.parameters_list, function(){
  200. if (window.insertContext) {
  201. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  202. window.insertContext = false;
  203. } else {
  204. AlgorithmManagement.renderAlgorithm();
  205. }
  206. }, 1);
  207. WatchJS.watch(function_to_add.variables_list, function(){
  208. if (window.insertContext) {
  209. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  210. window.insertContext = false;
  211. } else {
  212. AlgorithmManagement.renderAlgorithm();
  213. }
  214. }, 1);
  215. this.functions.push(function_to_add);
  216. }
  217. addVariable (function_to_receive, variable) {
  218. if (this.functions[function_to_receive].variable === null) {
  219. this.functions[function_to_receive].variables_list = [];
  220. }
  221. this.functions[function_to_receive].variables_list.push(variable);
  222. }
  223. addGlobal (variable) {
  224. this.globals.push(variable);
  225. }
  226. }