12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709 |
- import { Types } from './types';
- import * as Models from './ivprog_elements';
- import { LocalizedStrings } from './../services/localizedStringsService';
- import * as GlobalsManagement from './globals';
- import * as VariablesManagement from './variables';
- import * as CommandsManagement from './commands';
- import * as CodeManagement from './code_generator';
- import * as VariableValueMenu from './commands/variable_value_menu';
- import { DOMConsole } from './../io/domConsole';
- import { IVProgProcessor } from './../processor/ivprogProcessor';
- import WatchJS from 'melanke-watchjs';
- import { SemanticAnalyser } from '../processor/semantic/semanticAnalyser';
- import { IVProgAssessment } from '../assessment/ivprogAssessment';
- import * as AlgorithmManagement from './algorithm';
- import * as Utils from './utils';
- import { registerUserEvent, ActionTypes } from "./../services/userLog";
- import VersionInfo from './../../.ima_version.json';
- import * as TextEditor from "./text_editor";
- import { isValidIdentifier } from "./../util/utils";
- import { Modes } from '../processor/modes';
- var counter_new_functions = 0;
- var counter_new_parameters = 0;
- var ivprog_version = VersionInfo.version;
- const globalChangeListeners = [];
- const functionsChangeListeners = [];
- let proc = null;
- let domConsole = null;
- let _testCases = [];
- let isRunning = false;
- window.studentGrade = null;
- window.LocalizedStrings = LocalizedStrings;
- const program = new Models.Program();
- window.system_functions = [];
- // Adding math functions:
- window.system_functions.push(new Models.SystemFunction('$sin', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$cos', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$tan', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$sqrt', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$pow', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true), new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$log', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$abs', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$negate', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$invert', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$max', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$min', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- window.system_functions.push(new Models.SystemFunction('$rand', Types.REAL, 0, [],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
- // Adding text functions:
- window.system_functions.push(new Models.SystemFunction('$substring', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
- new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
- window.system_functions.push(new Models.SystemFunction('$length', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
- window.system_functions.push(new Models.SystemFunction('$uppercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
- window.system_functions.push(new Models.SystemFunction('$lowercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
- window.system_functions.push(new Models.SystemFunction('$charAt', Types.CHAR, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true), new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
- // Adding arrangement functions:
- window.system_functions.push(new Models.SystemFunction('$numElements', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 1)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
- window.system_functions.push(new Models.SystemFunction('$matrixLines', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 2)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
- window.system_functions.push(new Models.SystemFunction('$matrixColumns', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 2)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
- // Adding conversion functions:
- window.system_functions.push(new Models.SystemFunction('$isReal', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
- window.system_functions.push(new Models.SystemFunction('$isInt', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
- window.system_functions.push(new Models.SystemFunction('$isBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
- window.system_functions.push(new Models.SystemFunction('$castReal', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
- window.system_functions.push(new Models.SystemFunction('$castInt', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
- window.system_functions.push(new Models.SystemFunction('$castBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
- window.system_functions.push(new Models.SystemFunction('$castString', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
- window.system_functions.push(new Models.SystemFunction('$castChar', Types.CHAR, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
- null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
- console.log(' ___ ___ ________ \n / / / / / ____/ \n / / / / / / \n / / / / ______ ___ / /__ \n / / / / / \\ / / / ___/ \n / /______ / / / /\\ \\/ / / / \n / / / / / / \\ / / /____ \n/__________/ /___/ /___/ \\___/ /________/ \n\n Laboratório de Informática na Educação\n http://line.ime.usp.br');
- const mainFunction = new Models.Function(LocalizedStrings.getUI("start"), Types.VOID, 0, [], true, false);
- mainFunction.function_comment = new Models.Comment(LocalizedStrings.getUI('text_comment_main'));
- program.addFunction(mainFunction);
- window.program_obj = program;
- window.generator = CodeManagement.generate;
- window.runCodeAssessment = runCodeAssessment;
- window.renderAlgorithm = AlgorithmManagement.renderAlgorithm;
- window.insertContext = false;
- window.watchW = WatchJS;
- WatchJS.watch(window.program_obj.globals, function(){
- if (window.insertContext) {
- setTimeout(function() {
- AlgorithmManagement.renderAlgorithm();
- globalChangeListeners.forEach(x => x());
- }, 300);
- window.insertContext = false;
- } else {
- AlgorithmManagement.renderAlgorithm();
- globalChangeListeners.forEach(x => x());
- }
- }, 1);
- WatchJS.watch(window.program_obj.functions, function(){
- if (window.insertContext) {
- setTimeout(function(){
- AlgorithmManagement.renderAlgorithm();
- functionsChangeListeners.forEach( x => x());
- }, 300);
- window.insertContext = false;
- } else {
- AlgorithmManagement.renderAlgorithm();
- functionsChangeListeners.forEach( x => x());
- }
- }, 1);
- function addFunctionHandler () {
- if (window.program_obj.functions) {
- var in_use = false;
- do {
- in_use = false;
-
- var temp_name = LocalizedStrings.getUI('new_function') + '_' + counter_new_functions;
- window.program_obj.functions.forEach(function(el) {
- if (temp_name == el.name) {
- in_use = true;
- }
- })
- if (in_use)
- counter_new_functions++;
- } while (in_use);
- }
- const new_function = new Models.Function(LocalizedStrings.getUI("new_function") + "_" + counter_new_functions, Types.VOID, 0, [], false, false, [], new Models.Comment(LocalizedStrings.getUI('text_comment_start')));
- program.addFunction(new_function);
- counter_new_functions ++;
- window.insertContext = true;
- registerUserEvent(new_function.name, ActionTypes.INSERT_FUNCTION);
- // var newe = renderFunction(new_function);
- renderFunction(new_function);
- /*newe.css('display', 'none');
- newe.fadeIn();*/
- }
- function addParameter (function_obj, function_container/*, is_from_click = false*/) {
- if (function_obj.parameters_list == null) {
- function_obj.parameters_list = [];
- }
- if (function_obj.parameters_list) {
- var in_use = false;
- do {
- in_use = false;
-
- var temp_name = LocalizedStrings.getUI('new_parameter') + '_' + counter_new_parameters;
- function_obj.parameters_list.forEach(function(el) {
- if (temp_name == el.name) {
- in_use = true;
- }
- })
- if (in_use)
- counter_new_parameters++;
- } while (in_use);
- }
- const new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
- function_obj.parameters_list.push(new_parameter);
- counter_new_parameters ++;
- registerUserEvent(function_obj.name, ActionTypes.INSERT_FUNCTION_PARAM, new_parameter.name, Types.INTEGER, 0);
- //var newe = renderParameter(function_obj, new_parameter, function_container);
- renderParameter(function_obj, new_parameter, function_container);
- // if (is_from_click) {
- // newe.css('display', 'none');
- // newe.fadeIn();
- // }
- }
- function updateReturnType (function_obj, new_type, new_dimensions = 0) {
- registerUserEvent(function_obj.name, ActionTypes.CHANGE_FUNCTION_RETURN, new_type, new_dimensions);
- function_obj.return_type = new_type;
- function_obj.return_dimensions = new_dimensions;
- }
- function removeFunction (function_obj) {
- var index = program.functions.indexOf(function_obj);
- if (index > -1) {
- registerUserEvent(function_obj.name, ActionTypes.REMOVE_FUNCTION);
- program.functions.splice(index, 1);
- }
- }
- function minimizeFunction (function_obj) {
- function_obj.is_hidden = !function_obj.is_hidden;
- }
- function addHandlers (function_obj, function_container) {
- function_container.find('.ui.dropdown.function_return').dropdown({
- onChange: function(value, text, $selectedItem) {
- if ($selectedItem.data('dimensions')) {
- updateReturnType(function_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
- } else {
- updateReturnType(function_obj, Types[$selectedItem.data('type')]);
- }
- },
- selectOnKeydown: false
- });
- function_container.find( ".name_function_updated" ).on('click', function(e){
- enableNameFunctionUpdate(function_obj, function_container);
- });
- function_container.find( ".add_parameter_button" ).on('click', function(e){
- window.insertContext = true;
- addParameter(function_obj, function_container, true);
- });
- function_container.find('.menu_commands').dropdown({
- on: 'hover'
- });
- function_container.find('.menu_commands a').on('click', function(evt){
- if (function_obj.commands == null || function_obj.commands.length == 0) {
- function_obj.commands = [];
- var new_cmd = CommandsManagement.genericCreateCommand($(this).data('command'));
- function_obj.commands.push(new_cmd);
- CommandsManagement.renderCommand(new_cmd, function_container.find('.commands_list_div'), 3, function_obj);
- registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, $(this).data('command'), '/', 0);
- } else {
- CommandsManagement.createFloatingCommand(function_obj, function_container, $(this).data('command'), evt);
- }
- });
- function_container.find('.add_var_button_function').on('click', function(e){
- window.insertContext = true;
- VariablesManagement.addVariable(function_obj, function_container, true);
- });
- function_container.find('.remove_function_button').on('click', function(e){
- removeFunction(function_obj);
- function_container.fadeOut();
- });
- function_container.find('.minimize_function_button').on('click', function(e){
- minimizeFunction(function_obj);
- if (function_obj.is_hidden) {
- function_container.find(".add_var_button_function").toggle();
- function_container.find(".inline_add_command").toggle();
- function_container.find(".function_area").slideToggle();
- } else {
- function_container.find(".function_area").slideToggle(function(){
- function_container.find(".add_var_button_function").toggle();
- function_container.find(".inline_add_command").toggle();
- });
- }
-
- });
- }
- // Essa função imprime o tipo de retorno da função e cria o menu do tipo 'select' para alteração
- function renderFunctionReturn (function_obj, function_element) {
- var ret = '<div class="ui dropdown function_return">';
-
- if (function_obj.return_dimensions == 1) {
- ret += '<div class="text">'+ LocalizedStrings.getUI("vector") +': '+ LocalizedStrings.getUI(`type_${function_obj.return_type.toLowerCase()}`);
- ret += ' [ ] </div>';
- } else if (function_obj.return_dimensions == 2) {
- ret += '<div class="text">'+ LocalizedStrings.getUI("matrix") +': '+ LocalizedStrings.getUI(`type_${function_obj.return_type.toLowerCase()}`);
- ret += ' [ ] [ ] </div>';
- } else {
- ret += '<div class="text">'+LocalizedStrings.getUI(`type_${function_obj.return_type.toLowerCase()}`)+'</div>';
- }
- ret += '<div class="menu">';
- for (var tm in Types) {
- ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions < 1 ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
- }
- ret += '<div class="item ' + (function_obj.return_dimensions == 1 ? ' selected ' : '') + '">'
- + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('vector')
- + '<div class="menu">';
- for (var tm in Types) {
- if (tm == Types.VOID.toUpperCase()) {
- continue;
- }
- ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions == 1 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="1"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
- }
- ret += '</div></div>';
- ret += '<div class="item ' + (function_obj.return_dimensions == 2 ? ' selected ' : '') + '">'
- + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('matrix')
- + '<div class="menu">';
- for (var tm in Types) {
- if (tm == Types.VOID.toUpperCase()) {
- continue;
- }
- ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions == 2 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('matrix')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="2"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
- }
- ret += '</div></div>';
- ret += '</div></div>';
- ret = $(ret);
-
- function_element.find('.function_return').append(ret);
- }
- var cont = 0;
- export function renderFunction (function_obj) {
-
- var appender = '<div class="ui secondary segment function_div list-group-item function_cont_'+cont+'">';
- if (function_obj.function_comment) {
- //appender += renderComment(function_obj.function_comment, sequence, true, -1);
- }
-
- appender += '<span class="glyphicon glyphicon-move move_function" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>';
- appender += (function_obj.is_main ? '<div class="div_start_minimize_v"> </div>' : '<button class="ui icon button large remove_function_button"><i class="red icon times"></i></button>')
- + '<button class="ui icon button tiny minimize_function_button"><i class="icon window minimize"></i></button>';
- appender += '<div class="function_signature_div">'+LocalizedStrings.getUI("function")+' ';
- if (function_obj.is_main) {
- appender += '<div class="function_name_div"> ' + LocalizedStrings.getUI('type_void') + ' <span class="span_name_function" >'+function_obj.name+'</span> </div> '
- + ' <span class="parethesis_function">( </span> <div class="ui large labels parameters_list">';
- } else {
- appender += '<div class="ui function_return"></div>';
- appender += '<div class="function_name_div function_name_div_updated"><span class="span_name_function name_function_updated">'+function_obj.name+'</span> </div> '
- + ' <span class="parethesis_function"> ( </span> <i class="ui icon plus square outline add_parameter_button"></i> <div class="ui large labels parameters_list container_parameters_list">';
- }
-
- appender += '</div> <span class="parethesis_function"> ) </span> </div>'
- + (function_obj.is_hidden ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ');
- appender += '<div class="ui add_var_context add_var_button_function" style="float: left;"> <div class="ui icon button purple"> ';
- appender += '<i class="icons"><i class="icon superscript" style="margin-top: -2px;margin-bottom: 2px;margin-left: 1px;margin-right: 1px;"></i><i class="corner add icon inverted" style="font-size: 9px;padding-top: 6px;padding-left: 9px;"></i></i>';
- appender += '</div></div>';
- appender += '<div class="ui top attached segment variables_list_div"></div>';
- appender += '<div class="ui bottom attached segment commands_list_div commands_cont_'+cont+'">'
- + '<div class="ui rail" style="width: 35px; margin-left: -36px;"><div class="ui sticky sticky_cont_'+cont+'" style="top: 50px !important;">';
- appender += '<div class="ui icon button dropdown menu_commands orange" > '
- + '<i class="icons"><i class="icon code" style="margin-top: -1px;margin-bottom: 1px;margin-right: 0px;"></i><i class="corner add icon inverted" style="font-size: 9px;padding-top: 6px;padding-left: 9px;"></i></i>'
- + '<div class="menu"> ';
- appender += '<a class="item" data-command="'+Models.COMMAND_TYPES.reader+'"><i class="download icon"></i> ' +LocalizedStrings.getUI('text_read_var')+ '</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.writer+'"><i class="upload icon"></i> '+LocalizedStrings.getUI('text_write_var')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.comment+'"><i class="quote left icon"></i> '+LocalizedStrings.getUI('text_comment')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.attribution+'"><i class="arrow left icon"></i> '+LocalizedStrings.getUI('text_attribution')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.functioncall+'"><i class="hand point right icon"></i> '+LocalizedStrings.getUI('text_functioncall')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.iftrue+'" ><i class="random icon"></i> '+LocalizedStrings.getUI('text_iftrue')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.repeatNtimes+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_repeatNtimes')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.whiletrue+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_whiletrue')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.dowhiletrue+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_dowhiletrue')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.switch+'"><i class="list icon"></i> '+LocalizedStrings.getUI('text_switch')+'</a>'
- + '<a class="item" data-command="'+Models.COMMAND_TYPES.return+'"><i class="reply icon"></i> '+LocalizedStrings.getUI('text_btn_return')+'</a>'
- + '</div></div>';
- appender += '</div></div>'
- +'</div>';
- appender += '</div></div>';
- appender = $(appender);
- $('.all_functions').append(appender);
- appender.data('fun', function_obj);
- appender.find('.commands_list_div').data('fun', function_obj);
- renderFunctionReturn(function_obj, appender);
- addHandlers(function_obj, appender);
- // Rendering parameters:
- for (var j = 0; j < function_obj.parameters_list.length; j++) {
- renderParameter(function_obj, function_obj.parameters_list[j], appender);
- }
- // Rendering variables:
- for (var j = 0; j < function_obj.variables_list.length; j++) {
- VariablesManagement.renderVariable(appender, function_obj.variables_list[j], function_obj);
- }
- // Rendering commands:
- for (var j = 0; j < function_obj.commands.length; j++) {
- CommandsManagement.renderCommand(function_obj.commands[j], $(appender.find('.commands_list_div')[0]), 3, function_obj);
- }
- appender.find('.minimize_function_button').popup({
- content : LocalizedStrings.getUI("tooltip_hide_function"),
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('.move_function').popup({
- content : LocalizedStrings.getUI("btn_drag_and_drop_function"),
- delay: {
- show: 1000,
- hide: 0
- }
- });
- appender.find('.remove_function_button').popup({
- content : LocalizedStrings.getUI("btn_delete_function"),
- delay: {
- show: 750,
- hide: 0
- }
- });
-
- appender.find('.add_var_button_function').popup({
- content : LocalizedStrings.getUI("btn_add_var"),
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="attribution"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_attribution"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="functioncall"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_call_function"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="iftrue"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_if_else"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="comment"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_comments"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="reader"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_read"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="writer"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_write"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="repeatNtimes"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_repeat_n_times"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="whiletrue"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_repeat_whiletrue"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="dowhiletrue"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_repeat_dowhiletrue"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="switch"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_repeat_switch"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- appender.find('a[data-command="return"]').popup({
- content : LocalizedStrings.getUI("tooltip_menu_repeat_return"),
- position: 'right center',
- delay: {
- show: 750,
- hide: 0
- }
- });
- var function_index = program.functions.indexOf(function_obj);
- Sortable.create(appender.find(".variables_list_div")[0], {
- handle: '.ellipsis',
- animation: 100,
- ghostClass: 'ghost',
- group: 'local_vars_drag_' + function_index,
- onEnd: function (evt) {
- updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
- }
- });
- addSortableHandler(appender.find(".commands_list_div")[0], function_index);
- if (!function_obj.is_main) {
- Sortable.create(appender.find(".container_parameters_list")[0], {
- handle: '.ellipsis',
- animation: 100,
- ghostClass: 'ghost',
- group: 'parameters_drag_' + program.functions.indexOf(function_obj),
- onEnd: function (evt) {
- updateSequenceParameters(evt.oldIndex, evt.newIndex, function_obj);
- }
- });
- }
- if (function_obj.commands.length > 0) {
- var teste = '.ui.sticky.sticky_cont_'+cont;
- $(teste).sticky({
- context: '.ui.bottom.attached.segment.commands_list_div.commands_cont_'+cont,
- scrollContext: '.ivprog_visual_panel',
- observeChanges: true,
- offset: 40,
- onStick: function (evt) {
- $(teste).css('top', '20px', 'important');
- },
- onBottom: function (evt) {
- $(teste).css('top', '20px', 'important');
- },
- onUnstick: function (evt) {
- $(teste).css('top', '20px', 'important');
- },
- onReposition: function (evt) {
- $(teste).css('top', '20px', 'important');
- },
- onScroll: function (evt) {
- $(teste).css('top', '20px', 'important');
- if (!isVisible($(teste), $(teste).parent())) {
- $(teste).removeClass('fixed');
- }
- },
- onTop: function (evt) {
- $(teste).css('top', '20px', 'important');
- }
- });
- }
-
- cont ++;
- appender.find('.add_parameter_button').popup({
- content : LocalizedStrings.getUI("text_add_parameters"),
- delay: {
- show: 750,
- hide: 0
- }
- });
- return appender;
- }
- function isVisible (element, container) {
- var elementTop = $(element).offset().top,
- elementHeight = $(element).height(),
- containerTop = $(container).offset().top,
- containerHeight = $(container).height() - 30;
- return ((((elementTop - containerTop) + elementHeight) > 0)
- && ((elementTop - containerTop) < containerHeight));
- }
- window.evento_drag;
- function updateProgramObjDrag () {
- const nodes = Array.prototype.slice.call( $('.all_functions').children() );
- let function_index;
- var start_index;
- let function_obj;
- $(evento_drag.item).parentsUntil(".all_functions").each(function (index) {
- const ref = $(this);
- if (ref.hasClass('function_div')) {
- function_index = nodes.indexOf(this);
- start_index = index;
- function_obj = ref;
- }
- });
- const path_target = [];
- $(evento_drag.item).parentsUntil(".all_functions").each(function () {
- if ($(this).hasClass('command_container')) {
- path_target.push(this);
- }
- });
- if (path_target.length == 0) {
- //console.log('soltou na raiz, na posição: ' + evento_drag.newIndex + ' mas ainda não sei de onde saiu ');
- } else {
- //console.log('soltou dentro de algum bloco, sequência vem logo abaixo (de baixo pra cima): ');
- //console.log(path_target);
- }
- var index_each = [];
- var path_relative = [];
- for (var i = path_target.length - 1; i >= 0; i --) {
- if (i == (path_target.length - 1)) { // está na raiz
- var indice_na_raiz = function_obj.find('.command_container').index(path_target[i]);
- } else {
- if ($(path_target[i + 1]).hasClass('iftrue')) {
- if ($(path_target[i]).parent().hasClass('commands_if')) {
- path_relative.push('if');
- index_each.push($(path_target[i]).parent().find('.command_container').index(path_target[i]));
- } else {
- path_relative.push('else');
- index_each.push($(path_target[i]).parent().find('.command_container').index(path_target[i]));
- }
- } else if ($(path_target[i + 1]).hasClass('dowhiletrue')) {
- path_relative.push('dowhiletrue');
- index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
- } else if ($(path_target[i + 1]).hasClass('repeatNtimes')) {
- path_relative.push('repeatNtimes');
- index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
- } else if ($(path_target[i + 1]).hasClass('whiletrue')) {
- path_relative.push('whiletrue');
- index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
- } else if ($(path_target[i + 1]).hasClass('switch')) {
- path_relative.push('switch');
- //index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
- }
- }
- }
- // var index_in_block = -1;
- const is_in_else = $(evento_drag.item).parent().hasClass('commands_else');
- // index_in_block = $(evento_drag.item).parent().find('.command_container').index(evento_drag.item);
- const is_in_case_switch = $(evento_drag.item).parent().hasClass('case_commands_block');
-
- // encontrar o elemento na árvore:
- var command_start_point = window.program_obj.functions[function_index].commands[indice_na_raiz];
- var block_to_insert = command_start_point;
- for (var i = 0; i < index_each.length; i++) {
- if (path_relative[i] == "else") {
- block_to_insert = block_to_insert.commands_else[index_each[i]];
- } else if (path_relative[i] == "switch") {
- } else {
- block_to_insert = block_to_insert.commands_block[index_each[i]]
- }
- }
- //console.log('command_start_point', command_start_point);
- //console.log('block_to_insert', block_to_insert);
- // agora tem que alocar o comando na árvore, mas considerar as quatro situações:
- // (1) se está em um else ou (2) se está em switch ou (3) será um caso padrão ou (4) se será na raiz.
-
- if (path_target.length == 0) { // soltou na raiz:
- window.program_obj.functions[function_index].commands.splice(evento_drag.newIndex - 1, 0, command_in_drag);
- } else if (is_in_else) {
- if (block_to_insert.commands_else) {
- block_to_insert.commands_else.splice(evento_drag.newIndex, 0, command_in_drag);
- } else {
- block_to_insert.commands_else = [];
- block_to_insert.commands_else.push(command_in_drag);
- }
- } else if (is_in_case_switch) {
- } else {
- // verificar se tem alguma coisa no bloco:
- if (block_to_insert.commands_block) {
- block_to_insert.commands_block.splice(evento_drag.newIndex, 0, command_in_drag);
- } else {
- block_to_insert.commands_block = [];
- block_to_insert.commands_block.push(command_in_drag);
- }
- }
- window.draging = false;
- renderAlgorithm();
-
- }
- function prepareDragHandler (evt) {
- window.draging = true;
- var nodes = Array.prototype.slice.call( $('.all_functions').children() );
- var function_index;
- var function_obj;
- $(evt.item).parentsUntil(".all_functions").each(function (index) {
- if ($(this).hasClass('function_div')) {
- function_index = nodes.indexOf(this);
- function_obj = window.program_obj.functions[function_index];
- }
- });
- command_in_drag = $(evt.item).data("command");
- //console.log('$(evt.item).parent(): ');
- //console.log($(evt.item).parent());
- // descobrir qual das quatro situações:
- if ($(evt.item).parent().hasClass('commands_list_div')) { // está na raiz:
- if (function_obj.commands.indexOf(command_in_drag) > -1) {
- function_obj.commands.splice(function_obj.commands.indexOf(command_in_drag), 1);
- }
- } else if ($(evt.item).parent().hasClass('commands_else')) { // está no else:
- if ($(evt.item).parent().data('command').commands_else.indexOf(command_in_drag) > -1) {
- $(evt.item).parent().data('command').commands_else.splice($(evt.item).parent().data('command').commands_else.indexOf(command_in_drag), 1);
- }
- } else if ($(evt.item).parent().hasClass('case_commands_block')) { // está em um switch:
- } else { // caso padrão:
- if ($(evt.item).parent().data('command').commands_block.indexOf(command_in_drag) > -1) {
- $(evt.item).parent().data('command').commands_block.splice($(evt.item).parent().data('command').commands_block.indexOf(command_in_drag), 1);
- }
- }
- }
- var command_in_drag;
- function addSortableHandler (element, id_function) {
-
- var n_group = 'commands_drag_' + id_function;
- Sortable.create(element, {
- handle: '.command_drag',
- ghostClass: 'ghost',
- animation: 300,
- group: {name: n_group},
- onEnd: function (evt) {
- var nodes = Array.prototype.slice.call( $('.all_functions').children() );
- var function_index;
- var function_obj;
- $(evt.item).parentsUntil(".all_functions").each(function (index) {
- if ($(this).hasClass('function_div')) {
- function_index = nodes.indexOf(this);
- function_obj = window.program_obj.functions[function_index];
- }
- });
- registerUserEvent(function_obj.name, ActionTypes.MOVE_COMMAND, $(evt.item).data('command').type, '/', 'from: ' + evt.oldIndex + ' to: ' + evt.newIndex);
- //updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
- var itemEl = evt.item; // dragged HTMLElement
- evt.to; // target list
- evt.from; // previous list
- evt.oldIndex; // element's old index within old parent
- evt.newIndex; // element's new index within new parent
- //console.log('::EVT::');
- //console.log(evt);
- window.evento_drag = evt;
- try {
- updateProgramObjDrag();
- } catch (e) {
- console.error(e);
- window.draging = false;
- }
- },
- onStart: function (evt) {
- //console.log("START::EVT::");
- //console.log(evt);
- //console.log("\n\ncommand_in_drag");
- try {
- prepareDragHandler(evt);
- } catch (e) {
- window.draging = false;
- }
- }
- });
- element = $(element);
- element.find(".iftrue").each(function( index ) {
- addSortableHandler($(this).find(".block_commands")[0], id_function);
- addSortableHandler($(this).find(".block_commands")[1], id_function);
- });
- element.find(".repeatNtimes").each(function( index ) {
- addSortableHandler($(this).find(".block_commands")[0], id_function);
- });
- element.find(".dowhiletrue").each(function( index ) {
- addSortableHandler($(this).find(".block_commands")[0], id_function);
- });
- element.find(".whiletrue").each(function( index ) {
- addSortableHandler($(this).find(".block_commands")[0], id_function);
- });
- element.find(".switch").each(function( index ) {
- $(this).find(".case_div").each(function( index ) {
- addSortableHandler($(this).find(".case_commands_block")[0], id_function);
- });
- });
- }
- export function initVisualUI () {
- // MUST USE CONST, LET, OR VAR !!!!!!
- // const mainDiv = $('#visual-main-div');
- // fill mainDiv with functions and globals...
- // renderAlgorithm()...
- domConsole = new DOMConsole("ivprog-term-div");
- domConsole.hide();
- $(document.getElementById("ivprog-term-div")).draggable()
- $('.add_function_button').on('click', () => {
- addFunctionHandler();
- }).popup({
- content : LocalizedStrings.getUI("text_add_function"),
- delay: {
- show: 750,
- hide: 0
- }
- });
- $('.add_global_button').on('click', () => {
- window.insertContext = true;
- GlobalsManagement.addGlobal(program, true);
- });
- $('#run_button').on('click', function () {
- showStopButton();
- runCode();
- });
- $('#download_file_button').on('click', function () {
- downloadFile();
- });
- $('#upload_file_button').on('click', function () {
- $('#ivph_file').click().off('change').change(uploadFile);
- });
- $('#stop_button').on('click', function () {
- showRunButton();
- stopExecution();
- });
- $('.visual_coding_button').on('click', () => {
- toggleVisualCoding();
- });
- $('.textual_coding_button').on('click', () => {
- toggleTextualCoding();
- });
- $('.assessment').on('click', () => {
- is_iassign = true;
- runCodeAssessment();
- });
- $('.div_toggle_console').on('click', () => {
- toggleConsole();
- });
- $('.expand_button').on('click', () => {
- full_screen();
- });
- $('.help_button').on('click', () => {
- window.open('https://www.usp.br/line/ivprog/', '_blank');
- });
- $('.main_title h2').prop('title', LocalizedStrings.getUI('text_ivprog_description'));
- var time_show = 750;
- $('.visual_coding_button').popup({
- content : LocalizedStrings.getUI("tooltip_visual"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.textual_coding_button').popup({
- content : LocalizedStrings.getUI("tooltip_textual"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.upload_file_button').popup({
- content : LocalizedStrings.getUI("tooltip_upload"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.download_file_button').popup({
- content : LocalizedStrings.getUI("tooltip_download"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.undo_button').popup({
- content : LocalizedStrings.getUI("tooltip_undo"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.redo_button').popup({
- content : LocalizedStrings.getUI("tooltip_redo"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.run_button').popup({
- content : LocalizedStrings.getUI("tooltip_run"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.assessment_button').popup({
- content : LocalizedStrings.getUI("tooltip_evaluate"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.help_button').popup({
- content : LocalizedStrings.getUI("tooltip_help") + ' - ' + LocalizedStrings.getUI("text_ivprog_version") + ' ' + ivprog_version,
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.add_global_button').popup({
- content : LocalizedStrings.getUI("tooltip_add_global"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- $('.div_toggle_console').popup({
- content : LocalizedStrings.getUI("tooltip_console"),
- delay: {
- show: time_show,
- hide: 0
- }
- });
- Sortable.create(listWithHandle, {
- handle: '.glyphicon-move',
- animation: 100,
- ghostClass: 'ghost',
- group: 'functions_divs_drag',
- onEnd: function (evt) {
- updateSequenceFunction(evt.oldIndex, evt.newIndex);
- }
- });
- var listGlobalsHandle = document.getElementById("listGlobalsHandle");
- Sortable.create(listGlobalsHandle, {
- handle: '.ellipsis',
- animation: 100,
- ghostClass: 'ghost',
- group: 'globals_divs_drag',
- onEnd: function (evt) {
- updateSequenceGlobals(evt.oldIndex, evt.newIndex);
- }
- });
- }
- export function setTestCases (testCases) {
- _testCases = testCases;
- }
- export function getTestCases () {
- // Deep clone of test cases to avoid unauthorized modification
- // TODO: It may be not possible to use this once custom test are fully implemented
- return JSON.parse(JSON.stringify(_testCases));
- }
- var is_iassign = false;
- function updateSequenceParameters (oldIndex, newIndex, function_obj) {
- function_obj.parameters_list.splice(newIndex, 0, function_obj.parameters_list.splice(oldIndex, 1)[0]);
- }
- function updateSequenceLocals (oldIndex, newIndex, function_obj) {
- function_obj.variables_list.splice(newIndex, 0, function_obj.variables_list.splice(oldIndex, 1)[0]);
- }
- function updateSequenceGlobals (oldIndex, newIndex) {
- program_obj.globals.splice(newIndex, 0, program_obj.globals.splice(oldIndex, 1)[0]);
- }
- function updateSequenceFunction (oldIndex, newIndex) {
- program_obj.functions.splice(newIndex, 0, program_obj.functions.splice(oldIndex, 1)[0]);
- }
- function runCodeAssessment () {
- if (isRunning) {
- // cannot run assessment or it's already running
- return -1;
- }
-
- let strCode = null;
- is_iassign = false;
- window.studentGrade = null;
- if (settingsProgrammingTypes == "textual") {
- strCode = TextEditor.getCode();
- } else {
- strCode = CodeManagement.generate();
- }
- if (strCode == null) {
- // No code, so no assessment
- return -1;
- }
- toggleConsole(true);
- try {
- const data = SemanticAnalyser.analyseFromSource(strCode);
- isRunning = true;
- const runner = new IVProgAssessment(data, _testCases, domConsole);
- runner.runTest().then(grade => {
- if (!is_iassign) {
- parent.getEvaluationCallback(grade);
- } else {
- is_iassign = false;
- }
- isRunning = false;
- }).catch( err => {
- console.log(err);
- isRunning = false;
- });
- } catch (error) {
- isRunning = false;
- domConsole.err(error.message);
- console.log(error);
- if (!is_iassign) {
- parent.getEvaluationCallback(0);
- } else {
- is_iassign = false;
- }
- }
- return 0;
- }
- function runCode () {
- if (isRunning) {
- return;
- }
- let strCode = null;
- if (settingsProgrammingTypes == "textual") {
- strCode = TextEditor.getCode();
- } else {
- strCode = CodeManagement.generate();
- }
- if (strCode == null) {
- return;
- }
-
- toggleConsole(true);
- // if(domConsole == null)
- // domConsole = new DOMConsole("#ivprog-term");
- //$("#ivprog-term").slideDown(500);
- try {
- const data = SemanticAnalyser.analyseFromSource(strCode);
- proc = new IVProgProcessor(data);
- proc.registerInput(domConsole);
- proc.registerOutput(domConsole);
- $("#ivprog-term").addClass('ivprog-term-active');
- isRunning = true;
- proc.interpretAST().then( _ => {
- scheduleCall(() => {
- if(domConsole.pending_writes.length == 0) {
- if(proc.mode === Modes.ABORT) {
- domConsole.info(LocalizedStrings.getMessage("aborted_execution"));
- } else {
- domConsole.info(LocalizedStrings.getMessage("success_execution"));
- }
- $("#ivprog-term").removeClass('ivprog-term-active');
- isRunning = false;
- proc = null;
- showRunButton();
- return true;
- }
- return false;
- },100);
- }).catch(err => {
- scheduleCall(() => {
- if(domConsole.pending_writes.length == 0) {
- if(err instanceof Error) {
- domConsole.err(err.message);
- } else {
- domConsole.err(err);
- }
- $("#ivprog-term").removeClass('ivprog-term-active');
- isRunning = false;
- proc = null;
- showRunButton();
- return true;
- }
- return false;
- },100);
- })
- } catch (error) {
- scheduleCall(() => {
- if(domConsole.pending_writes.length == 0) {
- isRunning = false;
- proc = null;
- showRunButton();
- domConsole.err(error.message);
- console.log(error);
- return true;
- }
- return false;
- },100);
- }
-
- }
- function toggleConsole (is_running) {
- if (is_running) {
- $('.ivprog-term-div').css('display', 'block');
- $('#ivprog-term').css('min-height', '160px');
- if(domConsole != null)
- domConsole.focus();
- //$('#ivprog-term').css('margin-top', '-170px');
- return;
- } else {
- domConsole.hide();
- return;
- }
- if ($('#ivprog-term').css('min-height') == '160px') {
- // esconder
- $('.ivprog-term-div').css('display', 'none');
- $('#ivprog-term').css('min-height', '0');
- //$('#ivprog-term').css('margin-top', '-30px');
- //$('#ivprog-term').css('padding', '5px');
- } else {
- // mostrar
- $('.ivprog-term-div').css('display', 'block');
- $('#ivprog-term').css('min-height', '160px');
- //$('#ivprog-term').css('margin-top', '-170px');
- }
- }
- // function waitToCloseConsole () {
- // domConsole.info("Aperte qualquer tecla para fechar...");
- // const p = new Promise((resolve, _) => {
- // domConsole.requestInput(resolve, true);
- // });
- // p.then( _ => {
- // domConsole.dispose();
- // domConsole = null;
- // $("#ivprog-term").hide();
- // })
- // }
- function toggleTextualCoding () {
- let code = null;
- if (settingsProgrammingTypes != "textual") {
- code = CodeManagement.generate();
- if (code == null) {
- return;
- }
- }
- $('.ivprog_visual_panel').css('display', 'none');
- $('.ivprog_textual_panel').css('display', 'block');
- $('.ivprog_textual_panel').removeClass('loading');
- TextEditor.updateEditor();
- if (code != null)
- TextEditor.setCode(code);
- //$('.ivprog_textual_code').text(code);
- $('.visual_coding_button').removeClass('active');
- $('.textual_coding_button').addClass('active');
- }
- function toggleVisualCoding () {
- $('.ivprog_textual_panel').addClass('loading');
- $('.ivprog_textual_panel').css('display', 'none');
- $('.ivprog_visual_panel').css('display', 'block');
- $('.textual_coding_button').removeClass('active');
- $('.visual_coding_button').addClass('active');
- }
- function removeParameter (function_obj, parameter_obj, parameter_container) {
- registerUserEvent(parameter_obj.name, ActionTypes.REMOVE_FUNCTION_PARAM, function_obj.name);
- var index = function_obj.parameters_list.indexOf(parameter_obj);
- if (index > -1) {
- window.insertContext = true;
- function_obj.parameters_list.splice(index, 1);
- }
- $(parameter_container).fadeOut();
- }
- function updateParameterType (parameter_obj, new_type, function_name, new_dimensions = 0) {
- registerUserEvent(parameter_obj.name, ActionTypes.CHANGE_PARAM_TYPE, function_name, new_type, new_dimensions);
- parameter_obj.type = new_type;
- parameter_obj.dimensions = new_dimensions;
- if (new_dimensions > 0) {
- parameter_obj.rows = new_dimensions;
- parameter_obj.columns = 2;
- }
- }
- function renderParameter (function_obj, parameter_obj, function_container) {
- let ret = "";
- ret += '<div class="ui label function_name_parameter pink"><i class="ui icon ellipsis vertical inverted"></i>';
- if (parameter_obj.reference)
- ret += '<input type="checkbox" checked class="by_reference">';
- else
- ret += '<input type="checkbox" class="by_copy">';
- ret += '<div class="ui dropdown parameter_type">';
- if (parameter_obj.dimensions > 0) {
- ret += '<div class="text">'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(`type_${parameter_obj.type.toLowerCase()}`);
- if (parameter_obj.dimensions == 1) {
- ret += ' [ ] ';
- } else {
- ret += ' [ ] [ ] ';
- }
- ret += '</div>';
- } else {
- ret += '<div class="text">'+LocalizedStrings.getUI(`type_${parameter_obj.type.toLowerCase()}`)+'</div>';
- }
- ret += '<div class="menu">';
-
- for (const tm in Types) {
- if (tm == Types.VOID.toUpperCase()) {
- continue;
- }
- ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() && parameter_obj.dimensions == 0 ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
- }
- ret += '<div class="item ' + (parameter_obj.dimensions == 1 ? ' selected ' : '') + '">'
- + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('vector')
- + '<div class="menu">';
- for (var tm in Types) {
- if (tm == Types.VOID.toUpperCase()) {
- continue;
- }
- ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() && parameter_obj.dimensions == 1 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="1"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
- }
- ret += '</div></div>';
- ret += '<div class="item ' + (parameter_obj.dimensions == 2 ? ' selected ' : '') + '">'
- + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('matrix')
- + '<div class="menu">';
- for (var tm in Types) {
- if (tm == Types.VOID.toUpperCase()) {
- continue;
- }
- ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() && parameter_obj.dimensions == 2 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('matrix')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="2"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
- }
- ret += '</div></div>';
- ret += '</div></div>';
- ret += '<div class="parameter_div_edit"><span class="span_name_parameter label_enable_name_parameter">'+parameter_obj.name+'</span></div> ';
- ret += ' <i class="yellow inverted icon times remove_parameter"></i></div>';
- ret = $(ret);
-
- function_container.find('.container_parameters_list').append(ret);
- ret.find('.remove_parameter').on('click', function(e){
- removeParameter(function_obj, parameter_obj, ret);
- });
-
- ret.find('.ui.dropdown.parameter_type').dropdown({
- onChange: function(_, __, $selectedItem) {
- if ($selectedItem.data('dimensions')) {
- updateParameterType(parameter_obj, Types[$selectedItem.data('type')], function_obj.name, $selectedItem.data('dimensions'));
- } else {
- updateParameterType(parameter_obj, Types[$selectedItem.data('type')], function_obj.name);
- }
- },
- selectOnKeydown: false
- });
- ret.find('.parameter_div_edit').on('click', function(e){
- registerUserEvent(function_obj.name, ActionTypes.ENTER_CHANGE_PARAM_NAME, parameter_obj.name);
- enableNameParameterUpdate(parameter_obj, ret, function_obj);
- });
- ret.find('input:checkbox').change(function() {
- parameter_obj.reference = this.checked
- AlgorithmManagement.renderAlgorithm();
- });
- ret.find('.by_reference').popup({
- content : LocalizedStrings.getUI("text_parameter_reference"),
- delay: {
- show: 750,
- hide: 0
- }
- });
- ret.find('.by_copy').popup({
- content : LocalizedStrings.getUI("text_parameter_copy"),
- delay: {
- show: 750,
- hide: 0
- }
- });
- return ret;
- }
- function updateParameterName (parameter_var, new_name, parameter_obj_dom, function_obj) {
-
- if (parameter_var.name == new_name) {
- return;
- }
- if (isValidIdentifier(new_name)) {
- if (variableNameAlreadyExists(new_name, function_obj)) {
- Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getError('inform_valid_param_duplicated', [new_name, function_obj.name]));
- } else {
- registerUserEvent(function_obj.name, ActionTypes.RENAME_FUNCTION_PARAM, parameter_var.name, new_name);
- parameter_var.name = new_name;
- }
- } else {
- Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getError('inform_valid_identifier'));
- }
- }
- function variableNameAlreadyExists (name_var, function_obj) {
- if (function_obj.parameters_list) {
- for (var i = 0; i < function_obj.parameters_list.length; i++) {
- if (function_obj.parameters_list[i].name == name_var) {
- return true;
- }
- }
- }
- if (function_obj.variables_list) {
- for (var i = 0; i < function_obj.variables_list.length; i++) {
- if (function_obj.variables_list[i].name == name_var) {
- return true;
- }
- }
- }
- return false;
- }
- function updateFunctionName (function_var, new_name, function_obj_dom) {
-
- if (function_var.name == new_name) {
- return;
- }
-
- if (isValidIdentifier(new_name)) {
- if (functionNameAlreadyExists(new_name)) {
- Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getError('inform_valid_function_duplicated', [new_name]));
- } else {
- registerUserEvent(function_var.name, ActionTypes.RENAME_FUNCTION, new_name);
- function_var.name = new_name;
- }
- } else {
- Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getError('inform_valid_identifier'));
- }
- }
- function functionNameAlreadyExists (function_name) {
- for (var i = 0; i < window.program_obj.functions.length; i++) {
- if (window.program_obj.functions[i].name == function_name) {
- return true;
- }
- }
- return false;
- }
- var opened_name_parameter = false;
- var opened_input_parameter = null;
- function enableNameParameterUpdate (parameter_obj, parent_node, function_obj) {
- if (opened_name_parameter) {
- opened_input_parameter.focus();
- return;
- }
- opened_name_parameter = true;
- parent_node = $(parent_node);
- var input_field;
- parent_node.find('.span_name_parameter').text('');
- input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+parameter_obj.name+"' />" );
- input_field.insertBefore(parent_node.find('.span_name_parameter'));
- input_field.on('input', function() {
- var inputWidth = input_field.textWidth()+10;
- opened_input_parameter = input_field;
- input_field.focus();
- var tmpStr = input_field.val();
- input_field.val('');
- input_field.val(tmpStr);
- input_field.css({
- width: inputWidth
- })
- }).trigger('input');
- input_field.focusout(function() {
- /// update array:
- if (input_field.val().trim()) {
- updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
- parent_node.find('.span_name_parameter').text(parameter_obj.name);
- }
- input_field.off();
- input_field.remove();
- /// update elements:
- opened_name_parameter = false;
- opened_input_parameter = false;
- });
- input_field.on('keydown', function(e) {
- var code = e.keyCode || e.which;
- if(code == 13) {
- if (input_field.val().trim()) {
- updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
- parent_node.find('.span_name_parameter').text(parameter_obj.name);
- }
- input_field.off();
- input_field.remove();
- /// update elements:
- opened_name_parameter = false;
- opened_input_parameter = false;
- }
- if(code == 27) {
- parent_node.find('.span_name_parameter').text(parameter_obj.name);
- input_field.off();
- input_field.remove();
- /// update elements:
- opened_name_parameter = false;
- opened_input_parameter = false;
- }
- });
- input_field.select();
- }
- var opened_name_function = false;
- var opened_input = null;
- var previousPadding = null;
- function enableNameFunctionUpdate (function_obj, parent_node) {
- if (opened_name_function) {
- opened_input.focus();
- return;
- }
- parent_node = $(parent_node);
- parent_node.find('.span_name_function').text('');
- var input_field;
- if (!previousPadding) {
- previousPadding = parent_node.find('.span_name_function').css('padding-left');
- }
- parent_node.find('.span_name_function').css('padding-left', '0');
- parent_node.find('.span_name_function').css('padding-right', '0');
-
- input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+function_obj.name+"' />" );
- input_field.insertBefore(parent_node.find('.span_name_function'));
- input_field.on('input', function() {
- var inputWidth = input_field.textWidth()+10;
- opened_input = input_field;
- input_field.focus();
- var tmpStr = input_field.val();
- input_field.val('');
- input_field.val(tmpStr);
- input_field.css({
- width: inputWidth
- })
- }).trigger('input');
- input_field.focusout(function() {
- /// update array:
- if (input_field.val().trim()) {
- updateFunctionName(function_obj, input_field.val().trim(), parent_node);
- }
- input_field.off();
- input_field.remove();
- parent_node.find('.span_name_function').css('padding-left', previousPadding);
- parent_node.find('.span_name_function').css('padding-right', previousPadding);
- parent_node.find('.span_name_function').text(function_obj.name);
- /// update elements:
- opened_name_function = false;
- opened_input = false;
- });
- input_field.on('keydown', function(e) {
- var code = e.keyCode || e.which;
- if(code == 13) {
- if (input_field.val().trim()) {
- updateFunctionName(function_obj, input_field.val().trim(), parent_node);
- }
- input_field.off();
- input_field.remove();
- parent_node.find('.span_name_function').css('padding-left', previousPadding);
- parent_node.find('.span_name_function').css('padding-right', previousPadding);
- parent_node.find('.span_name_function').text(function_obj.name);
- /// update elements:
- opened_name_function = false;
- opened_input = false;
- }
- if(code == 27) {
- input_field.off();
- input_field.remove();
- parent_node.find('.span_name_function').css('padding-left', previousPadding);
- parent_node.find('.span_name_function').css('padding-right', previousPadding);
- parent_node.find('.span_name_function').text(function_obj.name);
- /// update elements:
- opened_name_function = false;
- opened_input = false;
- }
- });
- input_field.select();
-
- }
- export function addFunctionChangeListener (callback) {
- functionsChangeListeners.push(callback);
- return functionsChangeListeners.length - 1;
- }
- export function addGlobalChangeListener (callback) {
- globalChangeListeners.push(callback);
- return globalChangeListeners.length - 1;
- }
- export function removeGlobalListener (index) {
- globalChangeListeners.splice(index, 1);
- }
- export function removeFunctionListener (index) {
- functionsChangeListeners.splice(index);
- }
- function scheduleCall (callback, time) {
- const scheduled_call = () => {
- const id = setInterval(() => {
- if(callback()) {
- clearInterval(id);
- }
- }, time)
- }
- scheduled_call();
- }
- function showRunButton () {
- document.getElementById('run_button').style.display = 'inline';
- document.getElementById('stop_button').style.display = 'none';
- }
- function showStopButton () {
- document.getElementById('run_button').style.display = 'none';
- document.getElementById('stop_button').style.display = 'inline';
- }
- function stopExecution () {
- domConsole.clearPendingWrites();
- domConsole.cancelPendingInputRequests();
- if(!isRunning) {
- return;
- }
- proc.mode = Modes.ABORT;
- }
- function downloadFile() {
-
- var contentToSend = '{}\n::algorithm::';
- contentToSend = generator();
- var date = new Date();
- var temp = date.toISOString().split('T')[0] + "_" + date.toTimeString().split(' ')[0].replaceAll(':', '-');
- var blob = new Blob([contentToSend],
- { type: "text/plain;charset=utf-8" });
-
- saveAs(blob, "ivprog-exported_" + temp + ".ivph");
- }
- function uploadFile (evt) {
- var oFReader = new FileReader();
- oFReader.readAsText(document.getElementById("ivph_file").files[0]);
- oFReader.onload = function (oFREvent) {
- var txt = oFREvent.target.result;
- try {
- ivprogCore.setPreviousAlgorithm(txt);
- window.renderAlgorithm();
- }
- catch (e) {
- showInvalidFile();
- }
- };
- }
|