ivprog_elements.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import { Types } from "./types";
  2. import WatchJS from "melanke-watchjs";
  3. import * as AlgorithmManagement from "./algorithm";
  4. export const COMMAND_TYPES = Object.freeze({
  5. function: "function",
  6. comment: "comment",
  7. reader: "reader",
  8. writer: "writer",
  9. attribution: "attribution",
  10. iftrue: "iftrue",
  11. repeatNtimes: "repeatNtimes",
  12. whiletrue: "whiletrue",
  13. dowhiletrue: "dowhiletrue",
  14. switch: "switch",
  15. switchcase: "switchcase",
  16. functioncall: "functioncall",
  17. break: "break",
  18. return: "return",
  19. });
  20. export const ARITHMETIC_TYPES = Object.freeze({
  21. plus: "plus",
  22. minus: "minus",
  23. multiplication: "multiplication",
  24. division: "division",
  25. module: "module",
  26. none: "none",
  27. });
  28. export const EXPRESSION_ELEMENTS = Object.freeze({
  29. exp_op_exp: "exp_op_exp",
  30. op_exp: "op_exp",
  31. par_exp_par: "par_exp_par",
  32. start_point: "start_point",
  33. });
  34. export const EXPRESSION_TYPES = Object.freeze({
  35. exp_conditional: "exp_conditional",
  36. exp_logic: "exp_logic",
  37. exp_arithmetic: "exp_arithmetic",
  38. write_sep: "write_separator",
  39. });
  40. export const ARITHMETIC_COMPARISON = Object.freeze({
  41. greater_than: "greater_than",
  42. less_than: "less_than",
  43. equals_to: "equals_to",
  44. not_equals_to: "not_equals_to",
  45. greater_than_or_equals_to: "greater_than_or_equals_to",
  46. less_than_or_equals_to: "less_than_or_equals_to",
  47. });
  48. export const LOGIC_COMPARISON = Object.freeze({
  49. equals_to: "equals_to",
  50. not_equals_to: "not_equals_to",
  51. and: "and",
  52. or: "or",
  53. not: "not",
  54. });
  55. export const SYSTEM_FUNCTIONS_CATEGORIES = Object.freeze({
  56. math: "$mathLib",
  57. text: "$strLib",
  58. arrangement: "$arrayLib",
  59. conversion: "$langLib",
  60. });
  61. export class Variable {
  62. constructor (
  63. type,
  64. name,
  65. value,
  66. dimensions = 0,
  67. is_constant = false,
  68. rows = 0,
  69. columns = 0
  70. ) {
  71. this.type = type;
  72. this.name = name;
  73. this.value = value;
  74. this.dimensions = dimensions;
  75. this.is_constant = is_constant;
  76. this.rows = rows;
  77. this.columns = columns;
  78. }
  79. }
  80. export class Function {
  81. constructor (
  82. name,
  83. return_type = Types.VOID,
  84. return_dimensions = 0,
  85. parameters_list = [],
  86. is_main = false,
  87. is_hidden = false,
  88. variables_list = [],
  89. function_comment = null,
  90. commands = []
  91. ) {
  92. this.type = COMMAND_TYPES.function;
  93. this.name = name;
  94. this.return_type = return_type;
  95. this.return_dimensions = return_dimensions;
  96. this.parameters_list = parameters_list;
  97. this.is_main = is_main;
  98. this.is_hidden = is_hidden;
  99. this.variables_list = variables_list;
  100. this.function_comment = function_comment;
  101. this.commands = commands;
  102. }
  103. }
  104. export class SystemFunction {
  105. constructor (
  106. identifier,
  107. return_type,
  108. return_dimensions,
  109. parameters_list,
  110. function_comment = null,
  111. category
  112. ) {
  113. this.type = COMMAND_TYPES.function;
  114. this.identifier = identifier;
  115. this.return_type = return_type;
  116. this.return_dimensions = return_dimensions;
  117. this.parameters_list = parameters_list;
  118. this.function_comment = function_comment;
  119. this.category = category;
  120. }
  121. }
  122. export class Comment {
  123. constructor (comment_text) {
  124. this.type = COMMAND_TYPES.comment;
  125. this.comment_text = comment_text;
  126. }
  127. }
  128. export class Break {
  129. constructor () {
  130. this.type = COMMAND_TYPES.break;
  131. }
  132. }
  133. export class Reader {
  134. constructor (variable_value_menu = new VariableValueMenu()) {
  135. this.type = COMMAND_TYPES.reader;
  136. this.variable_value_menu = variable_value_menu;
  137. }
  138. }
  139. export class Writer {
  140. constructor (content, newline = true) {
  141. this.type = COMMAND_TYPES.writer;
  142. this.content = content;
  143. this.newline = newline;
  144. }
  145. }
  146. export class Attribution {
  147. constructor (variable, expression = []) {
  148. this.type = COMMAND_TYPES.attribution;
  149. this.variable = variable;
  150. this.expression = expression;
  151. }
  152. }
  153. export class ExpressionOperator {
  154. constructor (type_op, item) {
  155. this.type_op = type_op; // Logic, Arithmetic OR Relational
  156. this.item = item;
  157. }
  158. }
  159. export class ExpressionElement {
  160. constructor (type_exp, itens = []) {
  161. this.type_exp = type_exp;
  162. this.itens = itens;
  163. }
  164. }
  165. export class ConditionalExpression {
  166. constructor (expression) {
  167. this.type = EXPRESSION_TYPES.exp_conditional;
  168. this.expression = expression;
  169. }
  170. }
  171. export class LogicExpression {
  172. constructor (has_neg, first_operand, second_operand, operator) {
  173. this.type = EXPRESSION_TYPES.exp_logic;
  174. this.has_neg = has_neg;
  175. this.first_operand = first_operand;
  176. this.second_operand = second_operand;
  177. this.operator = operator;
  178. }
  179. }
  180. export class ArithmeticExpression {
  181. constructor (first_operand, second_operand, operator) {
  182. this.type = EXPRESSION_TYPES.exp_arithmetic;
  183. this.first_operand = first_operand;
  184. this.second_operand = second_operand;
  185. this.operator = operator;
  186. }
  187. }
  188. export class IfTrue {
  189. constructor (expression, commands_block, commands_else) {
  190. this.type = COMMAND_TYPES.iftrue;
  191. this.expression = expression;
  192. this.commands_block = commands_block;
  193. this.commands_else = commands_else;
  194. }
  195. }
  196. export class RepeatNTimes {
  197. constructor (
  198. var_attribution,
  199. var_incrementation,
  200. expression1,
  201. expression2,
  202. expression3,
  203. commands_block
  204. ) {
  205. this.type = COMMAND_TYPES.repeatNtimes;
  206. this.var_attribution = var_attribution;
  207. this.var_incrementation = var_incrementation;
  208. this.expression1 = expression1;
  209. this.expression2 = expression2;
  210. this.expression3 = expression3;
  211. this.commands_block = commands_block;
  212. }
  213. }
  214. export class WhileTrue {
  215. constructor (expression, commands_block) {
  216. this.type = COMMAND_TYPES.whiletrue;
  217. this.expression = expression;
  218. this.commands_block = commands_block;
  219. }
  220. }
  221. export class DoWhileTrue {
  222. constructor (expression, commands_block) {
  223. this.type = COMMAND_TYPES.dowhiletrue;
  224. this.expression = expression;
  225. this.commands_block = commands_block;
  226. }
  227. }
  228. export class Switch {
  229. constructor (variable, cases = []) {
  230. this.type = COMMAND_TYPES.switch;
  231. this.variable = variable;
  232. this.cases = cases;
  233. }
  234. }
  235. export class Return {
  236. constructor (variable_value_menu) {
  237. this.type = COMMAND_TYPES.return;
  238. this.variable_value_menu = variable_value_menu;
  239. }
  240. }
  241. export class SwitchCase {
  242. constructor (variable_value_menu, commands_block = []) {
  243. this.type = COMMAND_TYPES.switchcase;
  244. this.variable_value_menu = variable_value_menu;
  245. this.commands_block = commands_block;
  246. }
  247. }
  248. export class FunctionCall {
  249. constructor (function_called, parameters_list) {
  250. this.type = COMMAND_TYPES.functioncall;
  251. this.function_called = function_called;
  252. this.parameters_list = parameters_list;
  253. }
  254. }
  255. export class VariableValueMenu {
  256. constructor (
  257. variable_and_value = 7,
  258. content = null,
  259. row = null,
  260. column = null,
  261. include_constant = true,
  262. dimensions = 0
  263. ) {
  264. this.type = "var_value";
  265. this.variable_and_value = variable_and_value;
  266. this.content = content;
  267. this.row = row;
  268. this.column = column;
  269. this.include_constant = include_constant;
  270. this.dimensions = dimensions;
  271. }
  272. }
  273. export class FunctionCallMenu {
  274. constructor (function_called = null, parameters_list = []) {
  275. this.type = "function_call";
  276. this.function_called = function_called;
  277. this.parameters_list = parameters_list;
  278. }
  279. }
  280. export class Program {
  281. constructor () {
  282. this.functions = [];
  283. this.globals = [];
  284. }
  285. addFunction (function_to_add) {
  286. WatchJS.watch(
  287. function_to_add.parameters_list,
  288. function () {
  289. if (window.insertContext) {
  290. setTimeout(function () {
  291. AlgorithmManagement.renderAlgorithm();
  292. }, 300);
  293. window.insertContext = false;
  294. } else {
  295. AlgorithmManagement.renderAlgorithm();
  296. }
  297. },
  298. 1
  299. );
  300. WatchJS.watch(
  301. function_to_add.variables_list,
  302. function () {
  303. if (window.insertContext) {
  304. setTimeout(function () {
  305. AlgorithmManagement.renderAlgorithm();
  306. }, 300);
  307. window.insertContext = false;
  308. } else {
  309. AlgorithmManagement.renderAlgorithm();
  310. }
  311. },
  312. 1
  313. );
  314. this.functions.push(function_to_add);
  315. }
  316. addVariable (function_to_receive, variable) {
  317. if (this.functions[function_to_receive].variable === null) {
  318. this.functions[function_to_receive].variables_list = [];
  319. }
  320. this.functions[function_to_receive].variables_list.push(variable);
  321. }
  322. addGlobal (variable) {
  323. this.globals.push(variable);
  324. }
  325. }