| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315 |
- // iVProg - www.usp.br/line/ivprog
- // LInE - Free Education, Private Data
- // eslint-disable @typescript-eslint/no-use-before-define
- // globals $
- import { Types } from "./types";
- import * as Models from "./ivprog_elements";
- import { LocalizedStrings } from "./../services/localizedStringsService";
- import * as Utils from "./utils";
- // Code to main function
- export function generate () {
- $(".ivprog_visual_panel").find(".error_icon").remove();
- let code = LocalizedStrings.getUI("program") + " { ";
- code += globalsCode();
- code += "\n";
- let has_error = false;
- for (let i = 0; i < window.program_obj.functions.length; i++) {
- const n_code = functionsCode(window.program_obj.functions[i]);
- if (n_code == null) {
- has_error = true;
- }
- code += n_code;
- code += "\n";
- }
- code += "\n}";
- if (has_error) {
- return null;
- } else {
- return code;
- }
- }
- function functionsCode (function_obj) {
- let ret = "\n " + LocalizedStrings.getUI("function") + " "; // not \t - using fixed space to indentation
- const has_error = false;
- switch (function_obj.return_type) {
- case Types.INTEGER:
- ret += LocalizedStrings.getUI("type_integer");
- break;
- case Types.REAL:
- ret += LocalizedStrings.getUI("type_real");
- break;
- case Types.TEXT:
- ret += LocalizedStrings.getUI("type_text");
- break;
- case Types.BOOLEAN:
- ret += LocalizedStrings.getUI("type_boolean");
- break;
- case Types.CHAR:
- ret += LocalizedStrings.getUI("type_char");
- break;
- case Types.VOID:
- ret += LocalizedStrings.getUI("type_void");
- break;
- }
- ret += " ";
- if (function_obj.return_dimensions == 1) {
- ret += "[] ";
- } else if (function_obj.return_dimensions == 2) {
- ret += "[][] ";
- }
- if (function_obj.is_main) {
- ret += LocalizedStrings.getUI("start");
- } else {
- ret += function_obj.name;
- }
- ret += " ("; // open parameter to function
- for (let j = 0; j < function_obj.parameters_list.length; j++) {
- ret += parametersCode(function_obj.parameters_list[j]);
- if (j + 1 < function_obj.parameters_list.length) {
- ret += ", ";
- }
- }
- ret += ") {"; // close parameters to function
- for (let j = 0; j < function_obj.variables_list.length; j++) {
- ret += variablesCode(function_obj.variables_list[j]);
- }
- for (let j = 0; j < function_obj.commands.length; j++) {
- //D try {
- ret += commandsCode(function_obj.commands[j]);
- /* //D } catch (err) {
- has_error = true; console.error(err.message); var todos = $('body').find('.command_container');
- for (var i = 0; i < todos.length; i++) { if ($(todos[i]).data('command') == function_obj.commands[j]) {
- $( todos[i] ).prepend(' <i class="ui icon red exclamation triangle error_icon"></i> '); break;
- } } }*/
- }
- ret += "\n }"; // close function - "\n\t" using fixed space to indentation
- if (has_error) {
- return null;
- } else {
- return ret;
- }
- }
- function commandsCode (command_obj, indentation = 2) {
- let code = "";
- switch (command_obj.type) {
- case Models.COMMAND_TYPES.break:
- code = breaksCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.comment:
- code = commentsCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.reader:
- code = readersCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.writer:
- code = writersCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.functioncall:
- code = functioncallsCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.attribution:
- code = attributionsCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.whiletrue:
- code = whiletruesCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.dowhiletrue:
- code = doWhilesCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.iftrue:
- code = iftruesCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.repeatNtimes:
- code = repeatNtimesCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.switch:
- code = switchsCode(command_obj, indentation);
- break;
- case Models.COMMAND_TYPES.return:
- code = returnsCode(command_obj, indentation);
- break;
- }
- // IMPORTANT: do NOT add inline comment here!
- // Each specific function (whiletruesCode, etc) does this
- // To add here implies to DOUBLE the comment!
- // Add comment if, and only if, is a simple command (without blocks)
- const simpleCommands = [
- Models.COMMAND_TYPES.break,
- Models.COMMAND_TYPES.reader,
- Models.COMMAND_TYPES.writer,
- Models.COMMAND_TYPES.functioncall,
- Models.COMMAND_TYPES.attribution,
- Models.COMMAND_TYPES.return,
- ];
- if (simpleCommands.includes(command_obj.type)) {
- code = addInlineComment(code, command_obj);
- }
- return code;
- } // function commandsCode(command_obj, indentation = 2)
- function returnsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_return");
- if (command_obj.variable_value_menu) {
- try {
- ret += " " + elementExpressionCode(command_obj.variable_value_menu);
- //ret += ' ' + variableValueMenuCode(command_obj.variable_value_menu, true);
- } catch (err) {
- //Empty block
- }
- }
- return ret;
- }
- function breaksCode (_command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // "\t" - using fixed space to indentation
- }
- ret += LocalizedStrings.getUI("text_break");
- return ret;
- }
- // Write Switch code
- function switchsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_code_switch") + "(";
- ret += variableValueMenuCode(command_obj.variable);
- ret += ") { ";
- if (command_obj.cases) {
- for (let i = 0; i < command_obj.cases.length; i++) {
- ret += switchcasesCode(command_obj.cases[i], indentation + 1);
- }
- }
- ret += "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += "} ";
- return ret;
- }
- function switchcasesCode (switchcase, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_code_case") + " ";
- ret += variableValueMenuCode(switchcase.variable_value_menu);
- ret += " :";
- if (switchcase.commands_block) {
- for (let i = 0; i < switchcase.commands_block.length; i++) {
- ret += commandsCode(switchcase.commands_block[i], indentation + 1);
- }
- }
- return ret;
- }
- export function repeatNtimesHeader (command_obj) {
- let ret = "";
- if (command_obj.var_attribution) {
- ret += '<span class="repeatN_text_par_1">';
- ret += variableValueMenuCode(command_obj.var_attribution);
- ret += '</span>';
- //leo ret += ` ${LocalizedStrings.getUI("text_code_for_from")} `;
- ret += ' ' + LocalizedStrings.getUI("text_code_for_from") + ' ';
- ret += '<span class="repeatN_text_par_2">';
- ret += variableValueMenuCode(command_obj.expression1);
- ret += '</span>';
- }
- //D var ret1 = LocalizedStrings.getUI("text_code_for_from") + ' ';
- //D var ret2 = ` ${LocalizedStrings.getUI("text_code_for_from")} `;
- //D console.log("********** code_generator.js: repeatNtimesHeader(.): 1 ret=" + ret1 + "\n" + ret2 + "\n"); //leo remover xxxxxxxxxxx
- if (command_obj.expression2) {
- //leo ret += ` ${LocalizedStrings.getUI("text_code_for_to")} `;
- ret += ' ' + LocalizedStrings.getUI("text_code_for_to") + ' ';
- ret += '<span class="repeatN_text_par_3">';
- ret += variableValueMenuCode(command_obj.expression2);
- ret += '</span>';
- }
- if (command_obj.expression3) {
- //leo ret += ` ${LocalizedStrings.getUI("text_code_for_pass")} `;
- ret += ' ' + LocalizedStrings.getUI("text_code_for_pass") + ' ';
- ret += '<span class="repeatN_text_par_4">';
- switch (command_obj.expression3.itens[1]) {
- case Models.ARITHMETIC_TYPES.plus:
- ret += " +";
- break;
- case Models.ARITHMETIC_TYPES.minus:
- ret += " -";
- break;
- }
- ret += variableValueMenuCode(command_obj.expression3.itens[2]);
- ret += '</span>';
- }
- return ret;
- }
- function repeatNtimesCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_for") + " ";
- if (command_obj.var_attribution) {
- ret += variableValueMenuCode(command_obj.var_attribution);
- ret += ` ${LocalizedStrings.getUI("text_code_for_from")} `;
- ret += variableValueMenuCode(command_obj.expression1);
- }
- if (command_obj.expression2) {
- ret += ` ${LocalizedStrings.getUI("text_code_for_to")} `;
- ret += variableValueMenuCode(command_obj.expression2);
- }
- if (command_obj.expression3) {
- ret += ` ${LocalizedStrings.getUI("text_code_for_pass")} `;
- // To fix the bug UNDEFINED
- // The problem is inside the struture of expression3
- console.log("DEBUG expression3:", command_obj.expression3);
- // Try to access in more than one form
- let operator = null;
- let operand = null;
- // Verify if is ExpressionElement with items
- if (command_obj.expression3.itens) {
- operator = command_obj.expression3.itens[1];
- operand = command_obj.expression3.itens[2];
- }
- // Verify if has direct structure (what is "direct structure"?)
- else if (command_obj.expression3.operator) {
- operator = command_obj.expression3.operator;
- operand = command_obj.expression3.operand || command_obj.expression3.value;
- }
- // Try (directly) array
- else if (Array.isArray(command_obj.expression3)) {
- operator = command_obj.expression3[0];
- operand = command_obj.expression3[1];
- }
- // Render operator (build operator to be HTML presented)
- if (operator) {
- switch (operator) {
- case Models.ARITHMETIC_TYPES.plus:
- ret += "+";
- break;
- case Models.ARITHMETIC_TYPES.minus:
- ret += "-";
- break;
- default:
- ret += "+"; // fallback
- }
- } else {
- ret += "+"; // fallback
- }
- // Render operand (build operand to be HTML presented)
- if (operand) {
- ret += variableValueMenuCode(operand);
- } else {
- ret += "1"; // fallback
- }
- }
- ret += " {";
- // Add inline comment ONLY ONCE HERE
- if (command_obj.inlineComment) {
- ret += " // " + command_obj.inlineComment;
- }
- if (command_obj.commands_block) {
- for (let i = 0; i < command_obj.commands_block.length; i++) {
- ret += commandsCode(command_obj.commands_block[i], indentation + 1);
- }
- }
- ret += "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += "}";
- // here, do not add "another" inline comment!
- return ret;
- } // function repeatNtimesCode(command_obj, indentation)
- // Write IfElse code
- // Whenever "click" to see "textual code"
- function iftruesCode (command_obj, indentation) {
- let ret = "\n";
- //D console.log("code_generator.js!iftruesCode(.): "); //leo
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_if");
- if (command_obj.expression) {
- ret += "(";
- ret += elementExpressionCode(command_obj.expression);
- ret += ")";
- } else {
- //NOT necessary: Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI("inform_valid_expression"));
- }
- ret += " {";
- // Inline comment ONLY here
- if (command_obj.inlineComment) {
- ret += " // " + command_obj.inlineComment;
- }
- if (command_obj.commands_block) {
- for (let i = 0; i < command_obj.commands_block.length; i++) {
- ret += commandsCode(command_obj.commands_block[i], indentation + 1);
- }
- }
- ret += "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += "} " + LocalizedStrings.getUI("text_else") + " {";
- if (command_obj.commands_else) {
- for (let i = 0; i < command_obj.commands_else.length; i++) {
- ret += commandsCode(command_obj.commands_else[i], indentation + 1);
- }
- }
- ret += "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += "}";
- return ret;
- } // function iftruesCode(command_obj, indentation)
- function doWhilesCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_code_do") + " {";
- if (command_obj.inlineComment) {
- ret += " // " + command_obj.inlineComment;
- }
- if (command_obj.commands_block) {
- for (let i = 0; i < command_obj.commands_block.length; i++) {
- ret += commandsCode(command_obj.commands_block[i], indentation + 1);
- }
- }
- ret += "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += "} " + LocalizedStrings.getUI("text_code_do_until");
- //x if (!command_obj.expression) { Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI("inform_valid_expression")); }
- if (command_obj.expression) {
- ret += "(";
- ret += elementExpressionCode(command_obj.expression);
- ret += ")";
- }
- return ret;
- } // function doWhilesCode(command_obj, indentation)
- function whiletruesCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_code_while");
- //x if (!command_obj.expression) Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI("inform_valid_expression"));
- if (command_obj.expression) {
- ret += "(";
- ret += elementExpressionCode(command_obj.expression);
- ret += ")";
- }
- ret += " {";
- if (command_obj.inlineComment) {
- ret += " // " + command_obj.inlineComment;
- }
- if (command_obj.commands_block) {
- for (let i = 0; i < command_obj.commands_block.length; i++) {
- ret += commandsCode(command_obj.commands_block[i], indentation + 1);
- }
- }
- ret += "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += "}";
- return ret;
- } // function whiletruesCode(command_obj, indentation)
- function logicExpressionCode (expression) {
- let ret = "(";
- if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
- ret += logicExpressionCode(expression.first_operand);
- } else if (
- expression.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
- ) {
- ret += arithmeticExpressionCode(expression.first_operand);
- } else {
- ret += variableValueMenuCode(expression.first_operand);
- }
- if (expression.operator) {
- switch (expression.operator) {
- case Models.LOGIC_COMPARISON.equals_to:
- ret += " == ";
- break;
- case Models.LOGIC_COMPARISON.not_equals_to:
- ret += " != ";
- break;
- case Models.LOGIC_COMPARISON.and:
- ret += " && ";
- break;
- case Models.LOGIC_COMPARISON.or:
- ret += " || ";
- break;
- }
- if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
- ret += logicExpressionCode(expression.second_operand);
- } else if (
- expression.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
- ) {
- ret += arithmeticExpressionCode(expression.second_operand);
- } else {
- ret += variableValueMenuCode(expression.second_operand);
- }
- }
- ret += ")";
- return ret;
- } // function logicExpressionCode(expression)
- function arithmeticExpressionCode (expression) {
- let ret = "(";
- if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
- ret += logicExpressionCode(expression.first_operand);
- } else if (
- expression.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
- ) {
- ret += arithmeticExpressionCode(expression.first_operand);
- } else {
- ret += variableValueMenuCode(expression.first_operand);
- }
- switch (expression.operator) {
- case Models.ARITHMETIC_COMPARISON.greater_than:
- ret += " > ";
- break;
- case Models.ARITHMETIC_COMPARISON.less_than:
- ret += " < ";
- break;
- case Models.ARITHMETIC_COMPARISON.equals_to:
- ret += " == ";
- break;
- case Models.ARITHMETIC_COMPARISON.not_equals_to:
- ret += " != ";
- break;
- case Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to:
- ret += " >= ";
- break;
- case Models.ARITHMETIC_COMPARISON.less_than_or_equals_to:
- ret += " <= ";
- break;
- }
- if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
- ret += logicExpressionCode(expression.second_operand);
- } else if (
- expression.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
- ) {
- ret += arithmeticExpressionCode(expression.second_operand);
- } else {
- ret += variableValueMenuCode(expression.second_operand);
- }
- ret += ")";
- return ret;
- } // function arithmeticExpressionCode(expression)
- function attributionsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += variableValueMenuCode(command_obj.variable) + " <- ";
- // for (var i = 0; i < command_obj.expression.length; i++) { ret += elementExpressionCode(command_obj.expression[i]); }
- ret += elementExpressionCode(command_obj.expression);
- return ret;
- }
- export function elementExpressionCode (expression_obj) {
- let ret = "";
- for (let i = 0; i < expression_obj.length; i++) {
- if (expression_obj[i].type) {
- ret += variableValueMenuCode(expression_obj[i]);
- } else if (expression_obj[i].type_op) {
- switch (expression_obj[i].item) {
- case Models.ARITHMETIC_TYPES.plus:
- ret += " + ";
- break;
- case Models.ARITHMETIC_TYPES.minus:
- ret += " - ";
- break;
- case Models.ARITHMETIC_TYPES.multiplication:
- ret += " * ";
- break;
- case Models.ARITHMETIC_TYPES.division:
- ret += " / ";
- break;
- case Models.ARITHMETIC_TYPES.module:
- ret += " % ";
- break;
- case Models.LOGIC_COMPARISON.equals_to:
- ret += " == ";
- break;
- case Models.LOGIC_COMPARISON.not_equals_to:
- ret += " != ";
- break;
- case Models.LOGIC_COMPARISON.and:
- ret += " " + LocalizedStrings.getUI("logic_operator_and") + " ";
- break;
- case Models.LOGIC_COMPARISON.or:
- ret += " " + LocalizedStrings.getUI("logic_operator_or") + " ";
- break;
- case Models.LOGIC_COMPARISON.not:
- ret += " " + LocalizedStrings.getUI("logic_operator_not") + " ";
- break;
- case Models.ARITHMETIC_COMPARISON.greater_than:
- ret += " > ";
- break;
- case Models.ARITHMETIC_COMPARISON.less_than:
- ret += " < ";
- break;
- case Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to:
- ret += " >= ";
- break;
- case Models.ARITHMETIC_COMPARISON.less_than_or_equals_to:
- ret += " <= ";
- break;
- case Models.EXPRESSION_TYPES.write_sep:
- ret += ', " ", ';
- break;
- }
- } else {
- ret += " " + expression_obj[i] + " ";
- }
- }
- return ret;
- } // export function elementExpressionCode(expression_obj)
- function functioncallsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += variableValueMenuCode(command_obj.function_called);
- return ret;
- }
- // Presents "write(.)" command in iVProg graphical interface
- function readersCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_command_read") + "("; // open
- ret += variableValueMenuCode(command_obj.variable_value_menu);
- ret += ")"; // close
- return ret;
- }
- export function variableValueMenuCode (variable_obj, is_return = false) {
- let ret = "";
- try {
- if (variable_obj.function_called) {
- if (variable_obj.function_called.name) {
- ret += variable_obj.function_called.name + "("; // open
- } else {
- ret += LocalizedStrings.translateInternalFunction(variable_obj.function_called.identifier, variable_obj.function_called.category) + "(";
- }
- if (variable_obj.parameters_list) {
- for (let i = 0; i < variable_obj.parameters_list.length; i++) {
- ret += variableValueMenuCode(variable_obj.parameters_list[i]);
- if (i + 1 < variable_obj.parameters_list.length) {
- ret += ", ";
- }
- }
- }
- ret += ")"; // close
- //D console.log("********** code_generator.js: variableValueMenuCode(.): ret=" + ret); //leo remover xxxxxxxxxxx
- } else if (variable_obj.content.type) {
- ret += variable_obj.content.name;
- if (variable_obj.content.dimensions == 1 && variable_obj.dimensions != 1) {
- ret += "[" + variableValueMenuCode(variable_obj.column) + "]"; // array
- }
- if (variable_obj.content.dimensions == 2 && variable_obj.dimensions != 2 && variable_obj.reference_dimensions == 1) {
- ret += "[" + variableValueMenuCode(variable_obj.row) + "]"; // array
- } else if (variable_obj.content.dimensions == 2 && variable_obj.dimensions != 2) {
- ret += "[" + variableValueMenuCode(variable_obj.row) + "]"; // array
- ret += "[" + variableValueMenuCode(variable_obj.column) + "]"; // array
- }
- } else {
- const content = String(variable_obj.content);
- if (isNaN(content)) {
- // console.debug("Content length: ", content.length);
- // console.debug("Char code at 0", content.charCodeAt(0));
- const isBackslash = content.charCodeAt(0) === 92;
- if (content.length === 1 || (content.length === 2 && isBackslash)) {
- ret += `'${content}'`;
- } else {
- ret += '"' + variable_obj.content + '"';
- }
- } else if (content.length == 0) {
- ret += '""';
- } else if (content.trim().length == 0) {
- ret += '"' + content + '"';
- } else {
- ret += variable_obj.content;
- }
- }
- } catch (err) {
- var str = variable_obj==null || variable_obj==undefined ? "empty" : variable_obj.content; //D
- console.log("js/visualUI/code_generator.js!variableValueMenuCode(.): Error, variable_obj.content=" + str); //D
- if (!is_return) {
- Utils.renderErrorMessage(variable_obj.dom_object, LocalizedStrings.getUI("inform_valid_content"));
- throw err;
- }
- }
- return ret;
- } // export function variableValueMenuCode(variable_obj, is_return = false)
- // Write to file command "write(.)"
- function writersCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += LocalizedStrings.getUI("text_command_write") + "(";
- /*for (var i = 0; i < command_obj.content.length; i++) {
- ret += variableValueMenuCode(command_obj.content[i]);
- if ((i + 1) < command_obj.content.length) { ret += ' + ';}
- }*/
- ret += elementExpressionCode(command_obj.content);
- if (command_obj.newline) {
- ret += ', "\\n"';
- }
- ret += ") ";
- return ret;
- }
- function commentsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- }
- ret += "// ";
- //comment stars (Edilson)
- let commentText = "";
- if (command_obj.comment_text) {
- if (typeof command_obj.comment_text === "object" && command_obj.comment_text.content) {
- commentText = command_obj.comment_text.content;
- } else if (typeof command_obj.comment_text === "string") {
- commentText = command_obj.comment_text;
- }
- }
- if (!commentText) {
- commentText = command_obj.value || command_obj.comment || command_obj._text || "";
- }
- ret += commentText;
- //comments ends (Edilson)
- return ret;
- }
- function parametersCode (parameter_obj) {
- let ret = "";
- switch (parameter_obj.type) {
- case Types.INTEGER:
- ret += " " + LocalizedStrings.getUI("type_integer") + " ";
- break;
- case Types.REAL:
- ret += " " + LocalizedStrings.getUI("type_real") + " ";
- break;
- case Types.TEXT:
- ret += " " + LocalizedStrings.getUI("type_text") + " ";
- break;
- case Types.BOOLEAN:
- ret += " " + LocalizedStrings.getUI("type_boolean") + " ";
- break;
- case Types.CHAR:
- ret += " " + LocalizedStrings.getUI("type_char") + " ";
- break;
- }
- if (parameter_obj.reference)
- ret += "&";
- ret += parameter_obj.name + "";
- if (parameter_obj.dimensions == 1) {
- ret += " []";
- } else if (parameter_obj.dimensions == 2) {
- ret += " [][]";
- }
- return ret;
- }
- // Write variable declaration code
- function variablesCode (variable_obj) {
- if (variable_obj.type === "comment" || variable_obj.constructor.name === "Comment") {
- return commentsCode(variable_obj, 2);
- }
- let ret = "";
- const temp = variable_obj;
- ret += "\n "; // "\n\t\n" - using fixed space to indentation (adjust to the function indentation)
- if (temp.is_constant) {
- ret += "const ";
- }
- switch (temp.type) {
- case Types.INTEGER:
- ret += LocalizedStrings.getUI("type_integer") + " ";
- break;
- case Types.REAL:
- ret += LocalizedStrings.getUI("type_real") + " ";
- break;
- case Types.TEXT:
- ret += LocalizedStrings.getUI("type_text") + " ";
- break;
- case Types.BOOLEAN:
- ret += LocalizedStrings.getUI("type_boolean") + " ";
- break;
- case Types.CHAR:
- ret += LocalizedStrings.getUI("type_char") + " ";
- break;
- }
- ret += temp.name + " ";
- if (temp.dimensions == 1) {
- ret += "[" + temp.columns + "] ";
- switch (temp.type) {
- case Types.INTEGER:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- ret += temp.value[j];
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.REAL:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- ret += parseFloat(temp.value[j]).toFixed(2);
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.TEXT:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- ret += '"' + temp.value[j] + '"';
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.BOOLEAN:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- if (temp.value[j]) {
- ret += LocalizedStrings.getUI("logic_value_true");
- } else {
- ret += LocalizedStrings.getUI("logic_value_false");
- }
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.CHAR:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- ret += "'" + temp.value[j] + "'";
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- }
- } else if (temp.dimensions == 2) {
- ret += "[" + temp.rows + "][" + temp.columns + "] ";
- switch (temp.type) {
- case Types.INTEGER:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- ret += temp.value[j][k];
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.REAL:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- ret += parseFloat(temp.value[j][k]).toFixed(2);
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.TEXT:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- ret += '"' + temp.value[j][k] + '"';
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.BOOLEAN:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- if (temp.value[j][k]) {
- ret += LocalizedStrings.getUI("logic_value_true");
- } else {
- ret += LocalizedStrings.getUI("logic_value_false");
- }
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.CHAR:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- ret += "'" + temp.value[j][k] + "'";
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- }
- } else {
- switch (temp.type) {
- case Types.INTEGER:
- ret += "<- " + temp.value;
- break;
- case Types.REAL:
- ret += "<- " + parseFloat(temp.value).toFixed(2);
- break;
- case Types.TEXT:
- ret += '<- "' + temp.value + '"';
- break;
- case Types.BOOLEAN:
- ret += "<- ";
- if (temp.value) {
- ret += LocalizedStrings.getUI("logic_value_true");
- } else {
- ret += LocalizedStrings.getUI("logic_value_false");
- }
- break;
- case Types.CHAR:
- ret += "<- '" + temp.value + "'";
- break;
- }
- }
- ret = addInlineComment(ret, temp); //comments (Edilson)
- return ret;
- } // function variablesCode(variable_obj)
- function globalsCode () {
- let ret = "";
- if (window.program_obj.globals) {
- for (let i = 0; i < window.program_obj.globals.length; i++) {
- const temp = window.program_obj.globals[i];
- ret += " "; // do not use "\t" - better using 2 fixed spaces as indentation
- if (temp.is_constant) {
- ret += "const "; //TODO Internacionalizar - insert it in ./ivprog/i18n/ui.csv
- }
- switch (temp.type) {
- case Types.INTEGER:
- ret += LocalizedStrings.getUI("type_integer");
- break;
- case Types.REAL:
- ret += LocalizedStrings.getUI("type_real");
- break;
- case Types.TEXT:
- ret += LocalizedStrings.getUI("type_text");
- break;
- case Types.BOOLEAN:
- ret += LocalizedStrings.getUI("type_boolean");
- break;
- case Types.CHAR:
- ret += LocalizedStrings.getUI("type_char");
- break;
- }
- ret += " " + temp.name + " ";
- if (temp.dimensions == 1) {
- ret += "[" + temp.columns + "] ";
- switch (temp.type) {
- case Types.INTEGER:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- ret += temp.value[j];
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.REAL:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- ret += parseFloat(temp.value[j]).toFixed(2);
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.TEXT:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- ret += '"' + temp.value[j] + '"';
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.BOOLEAN:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- if (temp.value[j]) {
- ret += LocalizedStrings.getUI("logic_value_true");
- } else {
- ret += LocalizedStrings.getUI("logic_value_false");
- }
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.CHAR:
- ret += "<- {";
- for (let j = 0; j < temp.value.length; j++) {
- ret += "'" + temp.value[j] + "'";
- if (j + 1 < temp.value.length) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- }
- } else if (temp.dimensions == 2) {
- ret += "[" + temp.rows + "][" + temp.columns + "] ";
- switch (temp.type) {
- case Types.INTEGER:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- ret += temp.value[j][k];
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.REAL:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- ret += parseFloat(temp.value[j][k]).toFixed(2);
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.TEXT:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- ret += '"' + temp.value[j][k] + '"';
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.BOOLEAN:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- if (temp.value[j][k]) {
- ret += LocalizedStrings.getUI("logic_value_true");
- } else {
- ret += LocalizedStrings.getUI("logic_value_false");
- }
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- case Types.CHAR:
- ret += "<- {";
- for (let j = 0; j < temp.rows; j++) {
- ret += "{";
- for (let k = 0; k < temp.columns; k++) {
- ret += "'" + temp.value[j][k] + "'";
- if (k + 1 < temp.columns) {
- ret += ", ";
- }
- }
- ret += "}";
- if (j + 1 < temp.rows) {
- ret += ", ";
- }
- }
- ret += "}";
- break;
- }
- } else {
- switch (temp.type) {
- case Types.INTEGER:
- ret += "<- " + temp.value;
- break;
- case Types.REAL:
- ret += "<- " + parseFloat(temp.value).toFixed(2);
- break;
- case Types.TEXT:
- ret += '<- "' + temp.value + '"';
- break;
- case Types.BOOLEAN:
- ret += "<- ";
- if (temp.value) {
- ret += LocalizedStrings.getUI("logic_value_true");
- } else {
- ret += LocalizedStrings.getUI("logic_value_false");
- }
- break;
- case Types.CHAR:
- ret += "<- '" + temp.value + "'";
- break;
- }
- }
- }
- }
- return ret;
- } // function globalsCode()
- // Treat comments (Edilson)
- function addInlineComment (code, command_obj) {
- if (command_obj.inlineComment) {
- return code.trimEnd() + " // " + command_obj.inlineComment;
- }
- return code;
- }
|