ivprog-visual-1.0.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Definição das classes utilizadas para armazenar o algoritmo
  2. // Não adicionar elementos ao tipoDados, pois existem componentes que dependem do seu tamanho e isso afetará seu funcionamento
  3. var tiposDados = Object.freeze({void:"void", integer:"integer", real:"real", text:"text", boolean:"boolean", vector:"vector"});
  4. var tiposComandos = Object.freeze({function:"function", comment:"comment", reader:"reader", writer:"writer", attribution:"attribution", iftrue:"iftrue",
  5. repeatNtimes:"repeatNtimes", whiletrue:"whiletrue", dowhiletrue:"dowhiletrue", switch:"switch", functioncall:"functioncall"});
  6. var allCommandsReference = [];
  7. var Variavel = function(tipo, nome, valor, dimensoes = 0, eh_constante = false, linhas = 0, colunas = 0) {
  8. this.tipo = tipo;
  9. this.nome = nome;
  10. this.valor = valor;
  11. this.dimensoes = dimensoes;
  12. this.eh_constante = eh_constante;
  13. this.linhas = linhas;
  14. this.colunas = colunas;
  15. this.id_command = getIdCommandControl();
  16. allCommandsReference[this.id_command] = this;
  17. };
  18. var Funcao = function(nome, tipo_retorno = tiposDados.void, dimensoes_retorno = 0, lista_parametros = null, eh_principal = false, esta_oculta = false, variaveis = null, comentario_funcao = null) {
  19. this.tipo = tiposComandos.function;
  20. this.nome = nome;
  21. this.tipo_retorno = tipo_retorno;
  22. this.dimensoes_retorno = dimensoes_retorno;
  23. this.lista_parametros = lista_parametros;
  24. this.eh_principal = eh_principal;
  25. this.esta_oculta = esta_oculta;
  26. this.variaveis = variaveis;
  27. this.comentario_funcao = comentario_funcao;
  28. this.comandos = null;
  29. this.id_command = getIdCommandControl();
  30. allCommandsReference[this.id_command] = this;
  31. };
  32. var Comentario = function(texto_comentario) {
  33. this.tipo = tiposComandos.comment;
  34. this.texto_comentario = texto_comentario;
  35. this.id_command = getIdCommandControl();
  36. allCommandsReference[this.id_command] = this;
  37. };
  38. var Leitura = function(variavel) {
  39. this.tipo = tiposComandos.reader;
  40. this.variavel = variavel;
  41. this.id_command = getIdCommandControl();
  42. allCommandsReference[this.id_command] = this;
  43. };
  44. var Escrita = function(conteudo) {
  45. this.tipo = tiposComandos.writer;
  46. this.conteudo = conteudo;
  47. this.id_command = getIdCommandControl();
  48. allCommandsReference[this.id_command] = this;
  49. };
  50. var Atribuicao = function(variavel, expressao) {
  51. this.tipo = tiposComandos.attribution;
  52. this.variavel = variavel;
  53. this.expressao = expressao;
  54. this.id_command = getIdCommandControl();
  55. allCommandsReference[this.id_command] = this;
  56. };
  57. var SeVerdadeiro = function(expressao, commands_block, commands_else) {
  58. this.tipo = tiposComandos.iftrue;
  59. this.expressao = expressao;
  60. this.commands_block = commands_block;
  61. this.commands_else = commands_else;
  62. this.id_command = getIdCommandControl();
  63. allCommandsReference[this.id_command] = this;
  64. };
  65. var RepitaNVezes = function(expressao1, expressao2, expressao3, commands_block) {
  66. this.tipo = tiposComandos.repeatNtimes;
  67. this.expressao1 = expressao1;
  68. this.expressao2 = expressao2;
  69. this.expressao3 = expressao3;
  70. this.commands_block = commands_block;
  71. this.id_command = getIdCommandControl();
  72. allCommandsReference[this.id_command] = this;
  73. };
  74. var EnquantoVerdadeiro = function(expressao, commands_block) {
  75. this.tipo = tiposComandos.whiletrue;
  76. this.expressao = expressao;
  77. this.commands_block = commands_block;
  78. this.id_command = getIdCommandControl();
  79. allCommandsReference[this.id_command] = this;
  80. };
  81. var FacaEnquantoVerdadeiro = function(expressao, commands_block) {
  82. this.tipo = tiposComandos.dowhiletrue;
  83. this.expressao = expressao;
  84. this.commands_block = commands_block;
  85. this.id_command = getIdCommandControl();
  86. allCommandsReference[this.id_command] = this;
  87. };
  88. var Escolha = function(variavel, lista_casos_e_blocos) {
  89. this.tipo = tiposComandos.switch;
  90. this.variavel = variavel;
  91. this.lista_casos_e_blocos = lista_casos_e_blocos;
  92. this.id_command = getIdCommandControl();
  93. allCommandsReference[this.id_command] = this;
  94. };
  95. var ChamadaFuncao = function(funcao, lista_parametros) {
  96. this.tipo = tiposComandos.functioncall;
  97. this.funcao = funcao;
  98. this.lista_parametros = lista_parametros;
  99. this.id_command = getIdCommandControl();
  100. allCommandsReference[this.id_command] = this;
  101. }
  102. var Programa = function () {
  103. this.funcoes = new Array();
  104. this.globais = new Array();
  105. };
  106. function adicionarFuncao(funcao) {
  107. programa.funcoes.push(funcao);
  108. }
  109. function adicionarVariavel(funcao, variavel) {
  110. if (programa.funcoes[funcao].variaveis == null) {
  111. programa.funcoes[funcao].variaveis = new Array();
  112. }
  113. programa.funcoes[funcao].variaveis.push(variavel);
  114. }
  115. var id_command_control = 0;
  116. function getIdCommandControl() {
  117. id_command_control ++;
  118. return id_command_control;
  119. }
  120. //var tex = i18n('text');
  121. // Adicionando a função principal automaticamente
  122. var programa = new Programa();
  123. var funcaoPrincipal = new Funcao(i18n("start"), tiposDados.void, 0, [], true);
  124. funcaoPrincipal.comentario_funcao = new Comentario(i18n('text_comment_main'));
  125. funcaoPrincipal.comandos = [];
  126. funcaoPrincipal.comandos.push(new Leitura(null));
  127. funcaoPrincipal.comandos.push(new Escrita(null));
  128. funcaoPrincipal.comandos.push(new Comentario("Comentario aqui..."));
  129. bloco = [];
  130. bloco.push(new Leitura(null));
  131. bloco.push(new Escrita(null));
  132. bloco.push(new Comentario(null));
  133. b_senao = [];
  134. b_senao.push(new Leitura(null));
  135. b_senao.push(new Comentario(null));
  136. senao = [];
  137. senao.push(new SeVerdadeiro(null, b_senao, b_senao));
  138. teste = [];
  139. bb_r = [];
  140. bb_r.push(new Comentario(null));
  141. teste.push(new RepitaNVezes(null, null, null, bb_r));
  142. funcaoPrincipal.comandos.push(new SeVerdadeiro(null, teste, b_senao));
  143. funcaoPrincipal.comandos.push(new SeVerdadeiro(null, bloco, senao));
  144. //funcaoPrincipal.lista_parametros.push(new Variavel(tiposDados.text, "args"));
  145. adicionarFuncao(funcaoPrincipal);
  146. var funcaoSomar = new Funcao("somar", tiposDados.integer, 0, [], false, false, null, new Comentario(i18n('text_comment_start')));
  147. funcaoSomar.lista_parametros.push(new Variavel(tiposDados.integer, "a"));
  148. funcaoSomar.lista_parametros.push(new Variavel(tiposDados.integer, "b"));
  149. //adicionarFuncao(funcaoSomar);