controllers.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. var deferred = Deferred();
  2. // To avoid several load, 'isContentEmpty' and 'countEmpty' are declared in 'js/app.js'
  3. // var isContentEmpty = 0;
  4. //
  5. var countEmpty = 0;
  6. function StartCtrl(){
  7. //D alert('1: StartCtrl! ' + countEmpty);
  8. }
  9. function CommCtrl($scope, $rootScope){
  10. $scope.valor = -12345; // mark of emptiness
  11. $scope.getSource = function(){
  12. return JSON.stringify($rootScope.getSource());
  13. }
  14. $scope.getEvaluation = function(){
  15. return $rootScope.getEvaluation();
  16. }
  17. //D alert('2: CommCtrl! ' + countEmpty);
  18. }
  19. // @calledby: ./js/app.js : ivProgApp.config(...)?
  20. // This provides the actions 'getEvaluation', 'run=true'
  21. // This function is called several times (to each element)
  22. function IvProgCreateCtrl($scope, $rootScope, IvProgSource, $filter) {
  23. //alert('3: IvProgCreateCtrl! ' + $scope + ', ' + $rootScope + ', ' + $filter);
  24. $rootScope.trackAction = function(action){
  25. //D alert('<ivprogh5>js/controllers.js: action=' + action);
  26. if(iLMparameters.iLM_PARAM_ServerToGetAnswerURL!=null && iLMparameters.iLM_PARAM_ServerToGetAnswerURL!=''){
  27. $.post(iLMparameters.iLM_PARAM_ServerToGetAnswerURL+"&track=1", { trackingData: "html=1;"+action }, function(d){});
  28. }
  29. }
  30. // iLM : getAnswer()
  31. $rootScope.getSource = function(){
  32. //D alert('<ivprogh5>js/controllers.js: rootScope.getSource');
  33. $rootScope.trackAction("getSource");
  34. return {
  35. mapping: $rootScope.mapping,
  36. src: $scope.program,
  37. testCases: $scope.testCases
  38. };
  39. }
  40. // iLM : getEvaluation()
  41. $rootScope.getEvaluation = function(){
  42. //D alert('<ivprogh5>js/controllers.js: rootScope.getEvaluation()');
  43. $rootScope.trackAction("getEvaluation");
  44. $scope.run(true);
  45. }
  46. $rootScope.itemCount = 0;
  47. $scope.vars = [];
  48. $scope.params = [];
  49. $scope.testCases = [];
  50. $scope.addTestCase = function(){
  51. $rootScope.trackAction("addTestCase");
  52. $scope.testCases.push({ input: "", output: "", currentIndex: 0 });
  53. }
  54. $scope.removeTestCase = function(i){
  55. $rootScope.trackAction("removeTestCase");
  56. $scope.testCases.splice(i, 1);
  57. }
  58. $rootScope.mapping = {};
  59. $scope.getTeste = function(){
  60. return 1;
  61. }
  62. // undo - redo control
  63. $scope.historyStack = -1;
  64. $scope.actionsHistory = [];
  65. $scope.addSnap = true;
  66. $scope.takeSnap = function(friendlyName, applying, sp){
  67. if(sp){
  68. $scope.actionsHistory.splice($scope.historyStack, $scope.actionsHistory.length-$scope.historyStack);
  69. }
  70. $scope.actionsHistory.push({name: friendlyName, src: JSON.stringify($scope.program)});
  71. $scope.historyStack = $scope.actionsHistory.length;
  72. }
  73. $rootScope.snapshot = function(friendlyName, applying){
  74. if(!applying){
  75. $scope.$apply(function(){
  76. $scope.takeSnap(friendlyName, applying, true);
  77. });
  78. }else{
  79. $scope.takeSnap(friendlyName, applying, true);
  80. }
  81. $scope.addSnap = true;
  82. }
  83. $scope.undo = function(){
  84. if($scope.historyStack>0){
  85. if($scope.addSnap){
  86. // salvando o estado atual
  87. $scope.takeSnap('', 1);
  88. $scope.historyStack--;
  89. $scope.addSnap = false;
  90. }
  91. $scope.historyStack--;
  92. var obj = JSON.parse($scope.actionsHistory[$scope.historyStack].src);
  93. $scope.program = obj;
  94. }
  95. }
  96. $scope.redo = function(){
  97. if($scope.historyStack < $scope.actionsHistory.length-1){
  98. $scope.historyStack++;
  99. var obj = JSON.parse($scope.actionsHistory[$scope.historyStack].src);
  100. $scope.program = obj;
  101. }
  102. }
  103. $scope.currentFunction = 0;
  104. $scope.program = {
  105. programName: "firstProgram",
  106. functions: [
  107. {
  108. isMain: true,
  109. name: "Principal",
  110. vars: {},
  111. params: {},
  112. type: "main", // int, void, float
  113. nodes:[]
  114. }/*,
  115. {
  116. isMain: false,
  117. name: "fatorial",
  118. vars: {},
  119. varss: {
  120. "var_1":
  121. { name: 'newVar1', type: 'int', initialValue: 0, id: "var_1" }
  122. },
  123. params: {},
  124. type: "int", // int, void, float
  125. nodes:[],
  126. nodess: [
  127. {
  128. id: "attr_1",
  129. type: "attr",
  130. name: "attr",
  131. parent: null,
  132. variable: "",
  133. exp: []
  134. }
  135. ]
  136. }*/
  137. ]
  138. }; // $scope.program
  139. $scope.setCurrentFunction = function(ind){
  140. $scope.currentFunction = ind;
  141. }
  142. $scope.addElVar = function(v){
  143. v.push({
  144. t: "var",
  145. v: "",
  146. o: "",
  147. p: ''//v
  148. });
  149. }
  150. $scope.addElVal = function(v){
  151. v.push({
  152. t: "val",
  153. v: 0,
  154. o: "",
  155. p: ''//v
  156. });
  157. }
  158. $scope.addElExpB = function(v){
  159. v.push({
  160. t: "expB",
  161. v: {
  162. op1: {
  163. t: "v",
  164. v: ""
  165. },
  166. op2: {
  167. t: "v",
  168. v: ""
  169. },
  170. op: ">"
  171. },
  172. o: "&&",
  173. p: ''//v
  174. });
  175. }
  176. $scope.isolar = function(item){
  177. item.t = "exp";
  178. item.v = "";
  179. item.exp = [];
  180. }
  181. $scope.addExp = function(parent){
  182. parent.push({ t: "val", v: "a", o: "+"});
  183. }
  184. $scope.getTemplate = function(x){
  185. return 'partials/elements/'+x.type+'.html'+"?t="+cacheTime;
  186. }
  187. $scope.addParam = function(){
  188. //var ind = $scope.params.length;
  189. var ind = $scope.programs[$scope.currentProgram].functions[0].params.length;
  190. //$scope.params.push({ name: 'newParam'+ind, type: 'int', initialValue: 0 } );
  191. $scope.programs[$scope.currentProgram].functions[0].params.push({ name: 'newParam'+ind, type: 'int', initialValue: 0 } );
  192. }
  193. $scope.removeParam = function(v){
  194. $scope.params.splice($scope.params.indexOf(v), 1);
  195. }
  196. $scope.varSetType = function(v, type){
  197. $rootScope.trackAction("changeVarType");
  198. var previousType = v.type;
  199. v.type = type;
  200. if(type=="string"){
  201. v.initialValue = "Olá mundo!";
  202. }else if(type=="float"){
  203. v.initialValue = 1.0;
  204. }else if(type=="int"){
  205. v.initialValue = 1;
  206. }else if(type=="boolean"){
  207. v.initialValue = true;
  208. }
  209. $scope.checkChangeTypeConsequences(v, $scope.program.functions[$scope.currentFunction].nodes, previousType);
  210. }
  211. // When the variable type is change this could be have consequences, verify them!
  212. $scope.checkChangeTypeConsequences = function(variable, where, previous){
  213. angular.forEach(where, function(item, key){
  214. if(item.type=="attr"){
  215. if(item.variable==variable.id){
  216. if(variable.type!=previous){
  217. var compatibility = ["int", "float"];
  218. if((compatibility.indexOf(variable.type)==-1)||(compatibility.indexOf(previous)==-1)){
  219. if(where[key].exp.length>0){
  220. where[key].exp = [];
  221. }
  222. }
  223. }
  224. }
  225. }
  226. if(item.nodes && item.nodes.length>0){
  227. $scope.checkChangeTypeConsequences(variable, item.nodes);
  228. }
  229. });
  230. } // $scope.checkChangeTypeConsequences = function(variable, where, previous)
  231. $scope.addVar = function(){
  232. $rootScope.trackAction("addVar");
  233. // TODO: checar se alterou o valor
  234. $rootScope.snapshot('Variável adicionada', true);
  235. var ind = $scope.itemCount;
  236. var id = "var"+$scope.itemCount++;
  237. $scope.program.functions[$scope.currentFunction].vars[id] = ({ name: 'newVar'+ind, type: 'int', initialValue: 1, id: id });
  238. }
  239. $scope.removeVarRec = function(nodes, id){
  240. $rootScope.trackAction("removeVar");
  241. angular.forEach(nodes, function(node, key){
  242. if(node.type=="write"){
  243. if(node.variable==id){
  244. node.variable = '';
  245. }
  246. }
  247. if(node.type!="attr"){
  248. if(node.nodes.length>0){
  249. $scope.removeVarRec(node.nodes, id);
  250. }
  251. }
  252. });
  253. }
  254. $scope.removeVar = function(v){
  255. $rootScope.trackAction("removeVar");
  256. $rootScope.snapshot('Variável removida', true);
  257. $scope.removeVarRec($scope.program.functions[$scope.currentFunction].nodes, v.id);
  258. delete $scope.program.functions[$scope.currentFunction].vars[v.id];
  259. }
  260. $scope.removeItem = function(parent, item){
  261. $rootScope.trackAction("removeItem");
  262. parentId = parent;
  263. // TODO: tratar para os outros functions
  264. if(parent=="root_0"){
  265. parent = $scope.program.functions[0].nodes;
  266. }else{
  267. parent = $rootScope.mapping[parent].nodes;
  268. }
  269. if($.isArray(parent)) {
  270. parent.splice(parent.indexOf(item),1);
  271. }
  272. if($rootScope.mapping[parentId]){
  273. var p1 = $rootScope.mapping[parentId].nodes1;
  274. if($.isArray(p1)) {
  275. p1.splice(p1.indexOf(item),1);
  276. }
  277. var p2 = $rootScope.mapping[parentId].nodes2;
  278. if($.isArray(p2)) {
  279. p2.splice(p2.indexOf(item),1);
  280. }
  281. }
  282. delete $rootScope.mapping[item.id];
  283. }
  284. $scope.isValidAttr = function(attr){
  285. var isValid = true;
  286. angular.forEach(attr, function(a, k){
  287. if(a.type=="var"){
  288. }
  289. });
  290. return false;
  291. }
  292. $scope.sortableOptions = {
  293. handle: '.handle',
  294. placeholder: "apps",
  295. connectWith: ".apps-container"
  296. };
  297. $scope.delete = function(data) {
  298. $rootScope.trackAction("delete");
  299. data.nodes = [];
  300. };
  301. $scope.run = function(useTestCases){
  302. cleanOutput();
  303. $rootScope.trackAction("run="+useTestCases);
  304. if(!$scope.validateEverything($scope.program)){
  305. writer("<i class='fa fa-exclamation-triangle'></i> Existem campos vazios. Preencha os campos com borda vermelha para executar o algoritmo corretamente.", false);
  306. }else{
  307. if(useTestCases){
  308. totalCasesEvaluated = 0;
  309. totalCasesPassed = 0;
  310. totalTestCases = $scope.testCases.length;
  311. angular.forEach($scope.testCases, function(item, key){
  312. $scope.testCases[key].currentIndex = 0;
  313. })
  314. testCases = $scope.testCases;
  315. var code = "";
  316. angular.forEach($scope.testCases, function(item, key){
  317. code += $scope.genCode($scope.program, true, key);
  318. });
  319. console.log(code);
  320. window.eval(code);
  321. }else{
  322. $rootScope.trackAction("run");
  323. var code = $scope.genCode($scope.program, false, 0);
  324. window.eval(code);
  325. }
  326. $("#valor").unbind('keydown');
  327. $("#valor").keydown(function( event ) {
  328. if ( event.which == 13 ) {
  329. $('#readData').modal('hide');
  330. var valor = $("#valor").val();
  331. $("#valor").val("");
  332. deferred.call(valor);
  333. event.preventDefault();
  334. }
  335. });
  336. $("#btnOk").unbind('click');
  337. $("#btnOk").click(function(){
  338. $('#readData').modal('hide');
  339. var valor = $("#valor").val();
  340. $("#valor").val("");
  341. deferred.call(valor);
  342. });
  343. }
  344. } // $scope.run = function(useTestCases)
  345. $scope.clearOutput = function(){
  346. $rootScope.trackAction("cleanOutput");
  347. $(".output").html("");
  348. }
  349. $scope.validateEverything = function(funcs){
  350. $(".node-with-error").removeClass("node-with-error");
  351. var ret = true;
  352. angular.forEach(funcs.functions, function(func, key){
  353. ret = ret && $scope.validateNode(func.nodes, func.vars);
  354. });
  355. return ret;
  356. }
  357. $scope.validateNode = function(nodes, vars){
  358. var ret = true;
  359. angular.forEach(nodes, function(node, key){
  360. if (node.type=="write"){
  361. if(node.variable==""){
  362. $("#node"+node.id).find(".select").addClass("node-with-error");
  363. ret = false;
  364. }
  365. }
  366. if(node.type=="read"){
  367. if(node.variable==""){
  368. $("#node"+node.id).find(".select").addClass("node-with-error");
  369. ret = false;
  370. }
  371. }
  372. if(node.type=="for"){
  373. if(node.forType==1){
  374. if((node.limitType=="var")&&(node.limit=="")){
  375. $("#node"+node.id).find(".for1").find(".select").addClass("node-with-error");
  376. ret = false;
  377. }
  378. }
  379. if(node.forType==2){
  380. if((node.limitType=="var")&&(node.limit=="")){
  381. $("#node"+node.id).find(".for1").find(".select").addClass("node-with-error");
  382. ret = false;
  383. }
  384. if(node.using==""){
  385. $("#node"+node.id).find(".for2").addClass("node-with-error");
  386. ret = false;
  387. }
  388. }
  389. if(node.forType==3){
  390. if((node.limitType=="var")&&(node.limit=="")){
  391. $("#node"+node.id).find(".for1").find(".select").addClass("node-with-error");
  392. ret = false;
  393. }
  394. if(node.using==""){
  395. $("#node"+node.id).find(".for2").addClass("node-with-error");
  396. ret = false;
  397. }
  398. if((node.initialType=="var")&&(node.initial=="")){
  399. ret = false;
  400. }
  401. if((node.initialType=="val")&&(node.limitType=="val")&&(node.initial>node.limit)){
  402. ret = false;
  403. }
  404. if((node.stepType=="var")&&(node.step=="")){
  405. $("#node"+node.id).find(".for3").find(".select").addClass("node-with-error");
  406. ret = false;
  407. }
  408. }
  409. }
  410. });
  411. return ret;
  412. } // $scope.validateNode = function(nodes, vars)
  413. $scope.genCode = function(funcs, useTestCases, testCaseIndex){
  414. var strCode = "var t"+testCaseIndex+" = function(){";
  415. var i = 0;
  416. angular.forEach(funcs.functions, function(func, key){
  417. if(i++==0){
  418. strCode+= "function "+func.name+"(){";
  419. angular.forEach(func.vars, function(variable, key){
  420. if(variable.type=="string"){
  421. strCode+="var var_"+variable.id+" = \""+variable.initialValue+"\";";
  422. }else{
  423. strCode+="var var_"+variable.id+" = "+variable.initialValue+";";
  424. }
  425. });
  426. strCode+= 'next(function(){';
  427. if(useTestCases){
  428. strCode+='resetTestCase('+testCaseIndex+');';
  429. }
  430. //strCode+='/*return deferred;*/';
  431. strCode+='})';
  432. // correcao automatica - false
  433. strCode+=$scope.genNode(useTestCases, func.nodes, func.vars, testCaseIndex);
  434. if(useTestCases){
  435. // correcao automatica
  436. strCode+= '.next(function(){';
  437. //strCode+=' console.log("OUT "+getOutput());'
  438. strCode+=' endTest('+testCaseIndex+');'
  439. if(($scope.testCases.length>testCaseIndex)){
  440. strCode += 't'+(testCaseIndex+1)+'();';
  441. }
  442. strCode+='});';
  443. }
  444. strCode+= "}";
  445. if(func.type=="main"){
  446. strCode+=func.name+"()";
  447. }
  448. }
  449. });
  450. strCode+="}; ";
  451. if(testCaseIndex==0){
  452. strCode+=" t"+testCaseIndex+"();";
  453. }
  454. return strCode;
  455. } // $scope.genCode = function(funcs, useTestCases, testCaseIndex)
  456. $scope.genNode = function(isEvaluating, nodes, vars, testCaseIndex){
  457. var strCode = "";
  458. angular.forEach(nodes, function(node, key){
  459. if(node.type=="write"){
  460. if(node.variable!=''){
  461. var v = $scope.program.functions[$scope.currentFunction].vars[node.variable];
  462. strCode += ".next(function(){";
  463. if(v.type=="boolean"){
  464. strCode+="if(var_"+node.variable+"){ writer('Verdadeiro', "+isEvaluating+"); }else{ writer('Falso', "+isEvaluating+"); }";
  465. }else{
  466. if(v.type=="float"){
  467. strCode += "if(isPutDecimalNeeded(parseFloat(var_"+node.variable+"))){";
  468. strCode += "writer(";
  469. strCode += "var_"+node.variable;
  470. strCode += "+'.0',"+isEvaluating+");";
  471. strCode += "}else{";
  472. strCode += "writer(";
  473. strCode += "var_"+node.variable;
  474. strCode += ","+isEvaluating+");";
  475. strCode += "}";
  476. }else{
  477. strCode += "writer(";
  478. strCode += "var_"+node.variable;
  479. strCode += ","+isEvaluating+");";
  480. }
  481. }
  482. strCode += "})";
  483. }
  484. }
  485. if(node.type=="while"){
  486. // while
  487. strCode+= '.next(function(){';
  488. //strCode+= 'var i'+node.id+ ' = 0;';
  489. strCode+= 'function loop'+node.id+'(){';
  490. strCode+= ' return next(function(){})'; // apenas para poder encadear
  491. if(node.nodes.length>0){
  492. strCode+= $scope.genNode(isEvaluating, node.nodes, vars, testCaseIndex);
  493. }
  494. strCode+=' .next(function(){';
  495. //strCode+=' ++i'+node.id+';';
  496. strCode+=' if('+$scope.genExp(node.exp, 'boolean')+'){';
  497. strCode+=' return loop'+node.id+'();';
  498. strCode+=' }'
  499. strCode+=' });';
  500. strCode+='}';
  501. strCode+=' if('+$scope.genExp(node.exp, 'boolean')+'){';
  502. strCode+='return loop'+node.id+'();';
  503. strCode+='}';
  504. strCode+='})';
  505. }
  506. if(node.type=="for"){
  507. if(node.forType==1){
  508. // for simples
  509. strCode+= '.next(function(){';
  510. strCode+= 'var i'+node.id+ ' = 0;';
  511. strCode+= 'function loop'+node.id+'(){';
  512. strCode+= ' return next(function(){})'; // apenas para poder encadear
  513. if(node.nodes.length>0){
  514. strCode+= $scope.genNode(isEvaluating, node.nodes, vars, testCaseIndex);
  515. }
  516. strCode+=' .next(function(){';
  517. strCode+=' ++i'+node.id+';';
  518. if(node.limitType=="val"){
  519. strCode+=' if(i'+node.id+'<'+node.limit+'){';
  520. }else{
  521. strCode+=' if(i'+node.id+'<'+' var_'+node.limit+'){';
  522. }
  523. strCode+=' return loop'+node.id+'();';
  524. strCode+=' }'
  525. strCode+=' });';
  526. strCode+='}';
  527. if(node.limitType=="val"){
  528. strCode+=' if(i'+node.id+'<'+node.limit+'){';
  529. }else{
  530. strCode+=' if(i'+node.id+'<'+' var_'+node.limit+'){';
  531. }
  532. strCode+='return loop'+node.id+'();';
  533. strCode+='}';
  534. strCode+='})';
  535. }else if(node.forType==2){
  536. // for mediano
  537. strCode+= '.next(function(){';
  538. strCode+= ' var_'+node.using+ ' = 0;';
  539. strCode+= 'function loop'+node.id+'(){';
  540. strCode+= ' return next(function(){})'; // apenas para poder encadear
  541. if(node.nodes.length>0){
  542. strCode+= $scope.genNode(isEvaluating, node.nodes, vars, testCaseIndex);
  543. }
  544. strCode+=' .next(function(){';
  545. strCode+=' ++var_'+node.using+';';
  546. if(node.limitType=="val"){
  547. strCode+=' if(var_'+node.using+'<'+node.limit+'){';
  548. }else{
  549. strCode+=' if(var_'+node.using+'<'+' var_'+node.limit+'){';
  550. }
  551. strCode+=' return loop'+node.id+'();';
  552. strCode+=' }'
  553. strCode+=' });';
  554. strCode+='}';
  555. if(node.limitType=="val"){
  556. strCode+=' if(var_'+node.using+'<'+node.limit+'){';
  557. }else{
  558. strCode+=' if(var_'+node.using+'<'+' var_'+node.limit+'){';
  559. }
  560. strCode+='return loop'+node.id+'();';
  561. strCode+='}';
  562. strCode+='})';
  563. }else if(node.forType==3){
  564. // for hard rs
  565. strCode+= '.next(function(){';
  566. if(node.initialType=="val"){
  567. strCode+= ' var_'+node.using+ ' = '+node.initial+';';
  568. }else{
  569. strCode+= ' var_'+node.using+ ' = var_'+node.initial+';';
  570. }
  571. strCode+= 'function loop'+node.id+'(){';
  572. strCode+= ' return next(function(){})'; // apenas para poder encadear
  573. if(node.nodes.length>0){
  574. strCode+= $scope.genNode(isEvaluating, node.nodes, vars, testCaseIndex);
  575. }
  576. strCode+=' .next(function(){';
  577. if(node.stepType=="val"){
  578. strCode+=' var_'+node.using+'+= '+node.step+';';
  579. }else{
  580. strCode+=' var_'+node.using+'+= var_'+node.step+';';
  581. }
  582. if(node.limitType=="val"){
  583. strCode+=' if(var_'+node.using+'<'+node.limit+'){';
  584. }else{
  585. strCode+=' if(var_'+node.using+'<'+' var_'+node.limit+'){';
  586. }
  587. strCode+=' return loop'+node.id+'();';
  588. strCode+=' }'
  589. strCode+=' });';
  590. strCode+='}';
  591. if(node.limitType=="val"){
  592. strCode+=' if(var_'+node.using+'<'+node.limit+'){';
  593. }else{
  594. strCode+=' if(var_'+node.using+'<'+' var_'+node.limit+'){';
  595. }
  596. strCode+='return loop'+node.id+'();';
  597. strCode+='}';
  598. strCode+='})';
  599. }
  600. }
  601. if(node.type=="attr"){
  602. if(node.variable!=""){
  603. strCode+= '.next(function () {';
  604. strCode+=" var_"+node.variable+"=";
  605. if(vars[node.variable].type=="int"){
  606. strCode+="parseInt("+$scope.genExp(node.exp, vars[node.variable].type)+")";
  607. }else if(vars[node.variable].type=="float"){
  608. strCode+="parseFloat("+$scope.genExp(node.exp, vars[node.variable].type)+")";
  609. }else{
  610. strCode+=" ("+$scope.genExp(node.exp, vars[node.variable].type)+")";
  611. }
  612. strCode+=" ;";
  613. strCode+= '})';
  614. }
  615. }
  616. if(node.type=="read"){
  617. var v = $scope.program.functions[$scope.currentFunction].vars[node.variable];
  618. if(!isEvaluating){
  619. strCode+= '.next(function () {';
  620. strCode+= ' $("#msgRead").html("'+node.message+'");';
  621. strCode+= ' $("#readData").modal();';
  622. strCode+= ' $("#valor").focus();';
  623. strCode+= ' return deferred;';
  624. strCode+= '}).';
  625. strCode+= 'next(function(a){';
  626. strCode+= ' console.log("Valor lido: "+a);';
  627. strCode+= '/* '+v.type+' */';
  628. }else{
  629. strCode+= '.next(function () {';
  630. strCode+= ' var a = readerInput('+testCaseIndex+');';
  631. }
  632. if(v.type=="int"){
  633. strCode+= " var_"+node.variable +" = parseInt(a);";
  634. }else if(v.type=="float"){
  635. strCode+= " var_"+node.variable +" = parseFloat(a); /* pq cai aqui */";
  636. }else if(v.type=="boolean"){
  637. // tratar boolean depois
  638. strCode+= " var_"+node.variable +" = a;";
  639. }else if(v.type=="string"){
  640. // tratar boolean depois
  641. strCode+= " var_"+node.variable +" = a;";
  642. }else{
  643. strCode+= " var_"+node.variable +" = a; ";
  644. }
  645. strCode+= '})';
  646. }
  647. if(node.type=="if"){
  648. strCode+= '.next(function () {';
  649. strCode+= 'if('+$scope.genExp(node.exp, 'boolean')+'){';
  650. strCode+= 'return next(function () {})'+$scope.genNode(isEvaluating, node.nodes1, vars, testCaseIndex);
  651. strCode+= '}else{';
  652. strCode+= 'return next(function () {})'+$scope.genNode(isEvaluating, node.nodes2, vars, testCaseIndex);
  653. strCode+= '}';
  654. strCode+= '})';
  655. }
  656. });
  657. return strCode;
  658. }
  659. $scope.genExp = function(exp, type){
  660. var strCode = "";
  661. //console.log(exp);
  662. angular.forEach(exp, function(ex, key){
  663. if(ex.t == "var"){
  664. strCode+=" var_"+ex.v+" ";
  665. }else if(ex.t == "val"){
  666. if(type=="string"){
  667. strCode+=" \" "+ex.v+"\" ";
  668. }else{
  669. strCode+=" "+ex.v+" ";
  670. }
  671. }else if(ex.t=="exp"){
  672. strCode+=" ( "+$scope.genExp(ex.v, type)+" ) ";
  673. }else if(ex.t=="expB"){
  674. strCode+=" ( "+$scope.genExp(ex.v, type)+" ) ";
  675. }else if(ex.t=="op"){
  676. strCode+= ex.v;
  677. }else if(ex.t=="opB"){
  678. strCode+= ex.v;
  679. }
  680. });
  681. return strCode;
  682. }
  683. $scope.changeForType = function(node, v){
  684. node.forType +=v;
  685. }
  686. $scope.changeForValue = function(node){
  687. node.isValue = !node.isValue;
  688. if(!node.isValue){
  689. node.simpleVariable = "";
  690. }
  691. writer(node.isValue, false);
  692. }
  693. $scope.childrenVisible = function(node){
  694. node.isChildrenVisible = !node.isChildrenVisible;
  695. }
  696. $scope.add = function(parent, parentId, type, name) {
  697. $rootScope.trackAction("add;type="+type);
  698. var newNode = {
  699. id: $scope.itemCount++,
  700. type: type,
  701. name: name,
  702. nodes: [],
  703. parent: parentId
  704. };
  705. // especifico de cada estrutura
  706. if(type=="if"){
  707. newNode.id = "if_"+newNode.id;
  708. newNode.exp = [/*
  709. { t: 'expB',
  710. v: [{"t":"val","v":""},{"t":"opB","v":""},{"t":"val","v":""}]
  711. }*/
  712. ];
  713. newNode.isChildrenVisible = true;
  714. newNode.nodes1 = [];
  715. newNode.nodes2 = [];
  716. }
  717. if(type=="read"){
  718. newNode.id = "read_"+newNode.id;
  719. newNode.message = "Por favor digite um valor:";
  720. newNode.variable = "";
  721. }
  722. if(type=="write"){
  723. newNode.id = "write_"+newNode.id;
  724. newNode.variable = "";
  725. }
  726. if(type=="while"){
  727. newNode.id = "while_"+newNode.id;
  728. newNode.exp = [];
  729. newNode.isChildrenVisible = true;
  730. newNode.nodes = [];
  731. }
  732. if(type=="for"){
  733. newNode.id = "for_"+newNode.id;
  734. newNode.forType = 1; // 1 SIMPLE, 2 +-, 3 COMPLETE
  735. newNode.initial = 1;
  736. newNode.initialType = "val";
  737. newNode.limit = 5;
  738. newNode.limitType = "val";
  739. newNode.using = "";
  740. newNode.step = 1;
  741. newNode.stepType = "val";
  742. newNode.isChildrenVisible = true;
  743. newNode.times = 5;
  744. newNode.timesType = 5;
  745. newNode.simple = true;
  746. newNode.isValue = true;
  747. newNode.simpleVariable = "";
  748. newNode.initialValue = 0;
  749. newNode.endValue = 5;
  750. newNode.increment = 1;
  751. newNode.variable = "";
  752. }
  753. if(type=="attr"){
  754. newNode.id = "attr_"+newNode.id;
  755. newNode.variable = "";
  756. //newNode.exp = [];
  757. /*newNode.exp = {
  758. op1: '',
  759. op1T : '',
  760. op: '',
  761. op2: '',
  762. op2T: ''
  763. };*/
  764. delete newNode.nodes;
  765. newNode.exp = [];
  766. newNode.isLocked = false;
  767. }
  768. parent.push(newNode);
  769. $rootScope.mapping[newNode.id] = newNode;
  770. }; // $scope.add = function(parent, parentId, type, name)
  771. $scope.save = function(){
  772. $.post('save.php', { src: JSON.stringify($scope.program) }, function(id) {
  773. $("body").append("<iframe src='get.php?id=" + id + "' style='display: none;' ></iframe>");
  774. });
  775. }
  776. if(iLMparameters.iLM_PARAM_Assignment!=null){
  777. //DEBUG: see if exists file under the HTML tag 'iLM_PARAM_Assignment' (that is used here in 'iLMparameters.iLM_PARAM_Assignment')
  778. //D writer('Entrou: ' + iLMparameters.iLM_PARAM_Assignment + '');
  779. //TODO NAO esta entrando no GET abaixo
  780. $.get(iLMparameters.iLM_PARAM_Assignment, function(d){ // load each element 'd'
  781. //DEBUG: writer('OK: 1 ' + iLMparameters.iLM_PARAM_Assignment + ': ');
  782. if(d!=null){
  783. $scope.mapping = d.mapping;
  784. $scope.program = d.src;
  785. $scope.testCases = d.testCases;
  786. $scope.$apply()
  787. //DEBUG:writer('OK: 2 ' + iLMparameters.iLM_PARAM_Assignment + ': ');// alert(d.src);
  788. }
  789. else writer('Erro: nao consegui ler o conteudo de ' + iLMparameters.iLM_PARAM_Assignment);
  790. }, "json");
  791. } // if(iLMparameters.iLM_PARAM_Assignment!=null)
  792. else writer('Erro: parametro \'iLM_PARAM_Assignment\' esta vazio: ' + iLMparameters.iLM_PARAM_Assignment);
  793. // Help developers
  794. if (iLMparameters==null) { // iVProgH5 not been used as an iLM?
  795. writer('There is no address defined to send the answer!');
  796. writer('There is not source code to load!');
  797. }
  798. else {
  799. if (iLMparameters.iLM_PARAM_Assignment==null || iLMparameters.iLM_PARAM_Assignment.indexOf("http")==-1) {
  800. strAux = '';
  801. if (iLMparameters.iLM_PARAM_Assignment!=null && iLMparameters.iLM_PARAM_Assignment!='')
  802. strAux = 'Try to load the file under ' + iLMparameters.iLM_PARAM_Assignment + '';
  803. else
  804. strAux = 'There is not source code to load! (' + iLMparameters.iLM_PARAM_Assignment + ')';
  805. writer(strAux);
  806. }
  807. if (iLMparameters.iLM_PARAM_ServerToGetAnswerURL==null || iLMparameters.iLM_PARAM_ServerToGetAnswerURL=='') {
  808. strAux = '';
  809. if (iLMparameters.iLM_PARAM_ServerToGetAnswerURL!=null && iLMparameters.iLM_PARAM_ServerToGetAnswerURL!='')
  810. strAux = ' (' + iLMparameters.iLM_PARAM_ServerToGetAnswerURL + ')';
  811. writer('There is no address defined to send the answer!' + strAux);
  812. }
  813. }
  814. //DEBUG x var code = getSource(); // $scope.genCode($scope.program, false, 0);
  815. //x alert(' src=' + code);
  816. } // function IvProgCreateCtrl($scope, $rootScope, IvProgSource, $filter)
  817. function IvProgAbertoCtrl($scope){
  818. //D alert('controllers.js: IvProgAbertoCtrl: isContentEmpty=' + isContentEmpty + ', countEmpty=' + countEmpty + ': ' + $scope);
  819. $scope.delete = function(data) {
  820. data.nodes = [];
  821. };
  822. $scope.add = function(data) {
  823. var post = data.nodes.length + 1;
  824. var newName = data.name + '-' + post;
  825. data.nodes.push({name: newName,nodes: []});
  826. };
  827. $scope.tree = [{name: "Node", nodes: []}];
  828. }