ivprog_elements.js 8.3 KB

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