ivprog_elements.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. reference = false
  71. ) {
  72. this.type = type;
  73. this.name = name;
  74. this.value = value;
  75. this.dimensions = dimensions;
  76. this.is_constant = is_constant;
  77. this.rows = rows;
  78. this.columns = columns;
  79. this.reference = reference;
  80. }
  81. }
  82. export class Function {
  83. constructor (
  84. name,
  85. return_type = Types.VOID,
  86. return_dimensions = 0,
  87. parameters_list = [],
  88. is_main = false,
  89. is_hidden = false,
  90. variables_list = [],
  91. function_comment = null,
  92. commands = []
  93. ) {
  94. this.type = COMMAND_TYPES.function;
  95. this.name = name;
  96. this.return_type = return_type;
  97. this.return_dimensions = return_dimensions;
  98. this.parameters_list = parameters_list;
  99. this.is_main = is_main;
  100. this.is_hidden = is_hidden;
  101. this.variables_list = variables_list;
  102. this.function_comment = function_comment;
  103. this.commands = commands;
  104. }
  105. }
  106. export class SystemFunction {
  107. constructor (
  108. identifier,
  109. return_type,
  110. return_dimensions,
  111. parameters_list,
  112. function_comment = null,
  113. category
  114. ) {
  115. this.type = COMMAND_TYPES.function;
  116. this.identifier = identifier;
  117. this.return_type = return_type;
  118. this.return_dimensions = return_dimensions;
  119. this.parameters_list = parameters_list;
  120. this.function_comment = function_comment;
  121. this.category = category;
  122. }
  123. }
  124. export class Comment {
  125. constructor (comment_text) {
  126. this.type = COMMAND_TYPES.comment;
  127. this.comment_text = comment_text;
  128. }
  129. }
  130. export class Break {
  131. constructor () {
  132. this.type = COMMAND_TYPES.break;
  133. }
  134. }
  135. export class Reader {
  136. constructor (variable_value_menu = new VariableValueMenu()) {
  137. this.type = COMMAND_TYPES.reader;
  138. this.variable_value_menu = variable_value_menu;
  139. }
  140. }
  141. export class Writer {
  142. constructor (content, newline = true) {
  143. this.type = COMMAND_TYPES.writer;
  144. this.content = content;
  145. this.newline = newline;
  146. }
  147. }
  148. export class Attribution {
  149. constructor (variable, expression = []) {
  150. this.type = COMMAND_TYPES.attribution;
  151. this.variable = variable;
  152. this.expression = expression;
  153. }
  154. }
  155. export class ExpressionOperator {
  156. constructor (type_op, item) {
  157. this.type_op = type_op; // Logic, Arithmetic OR Relational
  158. this.item = item;
  159. }
  160. }
  161. export class ExpressionElement {
  162. constructor (type_exp, itens = []) {
  163. this.type_exp = type_exp;
  164. this.itens = itens;
  165. }
  166. }
  167. export class ConditionalExpression {
  168. constructor (expression) {
  169. this.type = EXPRESSION_TYPES.exp_conditional;
  170. this.expression = expression;
  171. }
  172. }
  173. export class LogicExpression {
  174. constructor (has_neg, first_operand, second_operand, operator) {
  175. this.type = EXPRESSION_TYPES.exp_logic;
  176. this.has_neg = has_neg;
  177. this.first_operand = first_operand;
  178. this.second_operand = second_operand;
  179. this.operator = operator;
  180. }
  181. }
  182. export class ArithmeticExpression {
  183. constructor (first_operand, second_operand, operator) {
  184. this.type = EXPRESSION_TYPES.exp_arithmetic;
  185. this.first_operand = first_operand;
  186. this.second_operand = second_operand;
  187. this.operator = operator;
  188. }
  189. }
  190. export class IfTrue {
  191. constructor (expression, commands_block, commands_else) {
  192. this.type = COMMAND_TYPES.iftrue;
  193. this.expression = expression;
  194. this.commands_block = commands_block;
  195. this.commands_else = commands_else;
  196. }
  197. }
  198. export class RepeatNTimes {
  199. constructor (
  200. var_attribution,
  201. var_incrementation,
  202. expression1,
  203. expression2,
  204. expression3,
  205. commands_block
  206. ) {
  207. this.type = COMMAND_TYPES.repeatNtimes;
  208. this.var_attribution = var_attribution;
  209. this.var_incrementation = var_incrementation;
  210. this.expression1 = expression1;
  211. this.expression2 = expression2;
  212. this.expression3 = expression3;
  213. this.commands_block = commands_block;
  214. }
  215. }
  216. export class WhileTrue {
  217. constructor (expression, commands_block) {
  218. this.type = COMMAND_TYPES.whiletrue;
  219. this.expression = expression;
  220. this.commands_block = commands_block;
  221. }
  222. }
  223. export class DoWhileTrue {
  224. constructor (expression, commands_block) {
  225. this.type = COMMAND_TYPES.dowhiletrue;
  226. this.expression = expression;
  227. this.commands_block = commands_block;
  228. }
  229. }
  230. export class Switch {
  231. constructor (variable, cases = []) {
  232. this.type = COMMAND_TYPES.switch;
  233. this.variable = variable;
  234. this.cases = cases;
  235. }
  236. }
  237. export class Return {
  238. constructor (variable_value_menu) {
  239. this.type = COMMAND_TYPES.return;
  240. this.variable_value_menu = variable_value_menu;
  241. }
  242. }
  243. export class SwitchCase {
  244. constructor (variable_value_menu, commands_block = []) {
  245. this.type = COMMAND_TYPES.switchcase;
  246. this.variable_value_menu = variable_value_menu;
  247. this.commands_block = commands_block;
  248. }
  249. }
  250. export class FunctionCall {
  251. constructor (function_called, parameters_list) {
  252. this.type = COMMAND_TYPES.functioncall;
  253. this.function_called = function_called;
  254. this.parameters_list = parameters_list;
  255. }
  256. }
  257. export class VariableValueMenu {
  258. constructor (
  259. variable_and_value = 7,
  260. content = null,
  261. row = null,
  262. column = null,
  263. include_constant = true,
  264. dimensions = 0
  265. ) {
  266. this.type = "var_value";
  267. this.variable_and_value = variable_and_value;
  268. this.content = content;
  269. this.row = row;
  270. this.column = column;
  271. this.include_constant = include_constant;
  272. this.dimensions = dimensions;
  273. }
  274. }
  275. export class FunctionCallMenu {
  276. constructor (function_called = null, parameters_list = []) {
  277. this.type = "function_call";
  278. this.function_called = function_called;
  279. this.parameters_list = parameters_list;
  280. }
  281. }
  282. export class Program {
  283. constructor () {
  284. this.functions = [];
  285. this.globals = [];
  286. }
  287. addFunction (function_to_add) {
  288. WatchJS.watch(
  289. function_to_add.parameters_list,
  290. function () {
  291. if (window.insertContext) {
  292. setTimeout(function () {
  293. AlgorithmManagement.renderAlgorithm();
  294. }, 300);
  295. window.insertContext = false;
  296. } else {
  297. AlgorithmManagement.renderAlgorithm();
  298. }
  299. },
  300. 1
  301. );
  302. WatchJS.watch(
  303. function_to_add.variables_list,
  304. function () {
  305. if (window.insertContext) {
  306. setTimeout(function () {
  307. AlgorithmManagement.renderAlgorithm();
  308. }, 300);
  309. window.insertContext = false;
  310. } else {
  311. AlgorithmManagement.renderAlgorithm();
  312. }
  313. },
  314. 1
  315. );
  316. this.functions.push(function_to_add);
  317. }
  318. addVariable (function_to_receive, variable) {
  319. if (this.functions[function_to_receive].variable === null) {
  320. this.functions[function_to_receive].variables_list = [];
  321. }
  322. this.functions[function_to_receive].variables_list.push(variable);
  323. }
  324. addGlobal (variable) {
  325. this.globals.push(variable);
  326. }
  327. }