1
0

ivprog_elements.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import { Types } from './types';
  2. import WatchJS from 'melanke-watchjs';
  3. import * as AlgorithmManagement from './algorithm';
  4. export const COMMAND_TYPES = Object.freeze({function:"function", comment:"comment", reader:"reader", writer:"writer", attribution:"attribution", iftrue:"iftrue",
  5. repeatNtimes:"repeatNtimes", whiletrue:"whiletrue", dowhiletrue:"dowhiletrue", switch:"switch", switchcase:"switchcase", functioncall:"functioncall", break:"break",
  6. return:"return"});
  7. export const ARITHMETIC_TYPES = Object.freeze({plus:"plus", minus:"minus", multiplication:"multiplication", division:"division", module:"module", none:"none"});
  8. 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"});
  9. export const EXPRESSION_TYPES = Object.freeze({exp_conditional:"exp_conditional", exp_logic:"exp_logic", exp_arithmetic:"exp_arithmetic"});
  10. 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"});
  11. export const LOGIC_COMPARISON = Object.freeze({equals_to:"equals_to", not_equals_to:"not_equals_to", and:"and", or:"or", not:"not"});
  12. export const SYSTEM_FUNCTIONS_CATEGORIES = Object.freeze({math:'$mathLib', text:'$strLib', arrangement:'$arrayLib', conversion:'$langLib'});
  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 SystemFunction {
  39. constructor (identifier, return_type, return_dimensions, parameters_list, function_comment = null, category) {
  40. this.type = COMMAND_TYPES.function;
  41. this.identifier = identifier;
  42. this.return_type = return_type;
  43. this.return_dimensions = return_dimensions;
  44. this.parameters_list = parameters_list;
  45. this.function_comment = function_comment;
  46. this.category = category;
  47. }
  48. }
  49. export class Comment {
  50. constructor (comment_text) {
  51. this.type = COMMAND_TYPES.comment;
  52. this.comment_text = comment_text;
  53. }
  54. }
  55. export class Break {
  56. constructor () {
  57. this.type = COMMAND_TYPES.break;
  58. }
  59. }
  60. export class Reader {
  61. constructor (variable_value_menu = new VariableValueMenu()) {
  62. this.type = COMMAND_TYPES.reader;
  63. this.variable_value_menu = variable_value_menu;
  64. }
  65. }
  66. export class Writer {
  67. constructor (content) {
  68. this.type = COMMAND_TYPES.writer;
  69. this.content = content;
  70. }
  71. }
  72. export class Attribution {
  73. constructor (variable, expression = []) {
  74. this.type = COMMAND_TYPES.attribution;
  75. this.variable = variable;
  76. this.expression = expression;
  77. }
  78. }
  79. export class ExpressionOperator {
  80. constructor (type_op, item) {
  81. this.type_op = type_op; // Logic, Arithmetic OR Relational
  82. this.item = item;
  83. }
  84. }
  85. export class ExpressionElement {
  86. constructor (type_exp, itens = []) {
  87. this.type_exp = type_exp;
  88. this.itens = itens;
  89. }
  90. }
  91. export class ConditionalExpression {
  92. constructor (expression) {
  93. this.type = EXPRESSION_TYPES.exp_conditional;
  94. this.expression = expression;
  95. }
  96. }
  97. export class LogicExpression {
  98. constructor (has_neg, first_operand, second_operand, operator) {
  99. this.type = EXPRESSION_TYPES.exp_logic;
  100. this.has_neg = has_neg;
  101. this.first_operand = first_operand;
  102. this.second_operand = second_operand;
  103. this.operator = operator;
  104. }
  105. }
  106. export class ArithmeticExpression {
  107. constructor (first_operand, second_operand, operator) {
  108. this.type = EXPRESSION_TYPES.exp_arithmetic;
  109. this.first_operand = first_operand;
  110. this.second_operand = second_operand;
  111. this.operator = operator;
  112. }
  113. }
  114. export class IfTrue {
  115. constructor (expression, commands_block, commands_else) {
  116. this.type = COMMAND_TYPES.iftrue;
  117. this.expression = expression;
  118. this.commands_block = commands_block;
  119. this.commands_else = commands_else;
  120. }
  121. }
  122. export class RepeatNTimes {
  123. constructor (var_attribution, var_incrementation, expression1, expression2, expression3, commands_block) {
  124. this.type = COMMAND_TYPES.repeatNtimes;
  125. this.var_attribution = var_attribution;
  126. this.var_incrementation = var_incrementation;
  127. this.expression1 = expression1;
  128. this.expression2 = expression2;
  129. this.expression3 = expression3;
  130. this.commands_block = commands_block;
  131. }
  132. }
  133. export class WhileTrue {
  134. constructor (expression, commands_block) {
  135. this.type = COMMAND_TYPES.whiletrue;
  136. this.expression = expression;
  137. this.commands_block = commands_block;
  138. }
  139. }
  140. export class DoWhileTrue {
  141. constructor (expression, commands_block) {
  142. this.type = COMMAND_TYPES.dowhiletrue;
  143. this.expression = expression;
  144. this.commands_block = commands_block;
  145. }
  146. }
  147. export class Switch {
  148. constructor (variable, cases = []) {
  149. this.type = COMMAND_TYPES.switch;
  150. this.variable = variable;
  151. this.cases = cases;
  152. }
  153. }
  154. export class Return {
  155. constructor (variable_value_menu) {
  156. this.type = COMMAND_TYPES.return;
  157. this.variable_value_menu = variable_value_menu;
  158. }
  159. }
  160. export class SwitchCase {
  161. constructor (variable_value_menu, commands_block = []) {
  162. this.type = COMMAND_TYPES.switchcase;
  163. this.variable_value_menu = variable_value_menu;
  164. this.commands_block = commands_block;
  165. }
  166. }
  167. export class FunctionCall {
  168. constructor (function_called, parameters_list) {
  169. this.type = COMMAND_TYPES.functioncall;
  170. this.function_called = function_called;
  171. this.parameters_list = parameters_list;
  172. }
  173. }
  174. export class VariableValueMenu {
  175. constructor (variable_and_value = 7, content = null, row = null, column = null, include_constant = true, dimensions = 0) {
  176. this.type = "var_value";
  177. this.variable_and_value = variable_and_value;
  178. this.content = content;
  179. this.row = row;
  180. this.column = column;
  181. this.include_constant = include_constant;
  182. this.dimensions = dimensions;
  183. }
  184. }
  185. export class FunctionCallMenu {
  186. constructor (function_called = null, parameters_list = []) {
  187. this.type = "function_call";
  188. this.function_called = function_called;
  189. this.parameters_list = parameters_list;
  190. }
  191. }
  192. export class Program {
  193. constructor () {
  194. this.functions = [];
  195. this.globals = [];
  196. }
  197. addFunction (function_to_add) {
  198. WatchJS.watch(function_to_add.parameters_list, function(){
  199. if (window.insertContext) {
  200. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  201. window.insertContext = false;
  202. } else {
  203. AlgorithmManagement.renderAlgorithm();
  204. }
  205. }, 1);
  206. WatchJS.watch(function_to_add.variables_list, function(){
  207. if (window.insertContext) {
  208. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  209. window.insertContext = false;
  210. } else {
  211. AlgorithmManagement.renderAlgorithm();
  212. }
  213. }, 1);
  214. this.functions.push(function_to_add);
  215. }
  216. addVariable (function_to_receive, variable) {
  217. if (this.functions[function_to_receive].variable === null) {
  218. this.functions[function_to_receive].variables_list = [];
  219. }
  220. this.functions[function_to_receive].variables_list.push(variable);
  221. }
  222. addGlobal (variable) {
  223. this.globals.push(variable);
  224. }
  225. }