12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211 |
- // 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) {
- switch (command_obj.type) {
- case Models.COMMAND_TYPES.break:
- return breaksCode(command_obj, indentation);
- case Models.COMMAND_TYPES.comment:
- return commentsCode(command_obj, indentation);
- case Models.COMMAND_TYPES.reader:
- return readersCode(command_obj, indentation);
- case Models.COMMAND_TYPES.writer:
- return writersCode(command_obj, indentation);
- case Models.COMMAND_TYPES.functioncall:
- return functioncallsCode(command_obj, indentation);
- case Models.COMMAND_TYPES.attribution:
- return attributionsCode(command_obj, indentation);
- case Models.COMMAND_TYPES.whiletrue:
- return whiletruesCode(command_obj, indentation);
- case Models.COMMAND_TYPES.dowhiletrue:
- return doWhilesCode(command_obj, indentation);
- case Models.COMMAND_TYPES.iftrue:
- return iftruesCode(command_obj, indentation);
- case Models.COMMAND_TYPES.repeatNtimes:
- return repeatNtimesCode(command_obj, indentation);
- case Models.COMMAND_TYPES.switch:
- return switchsCode(command_obj, indentation);
- case Models.COMMAND_TYPES.return:
- return returnsCode(command_obj, indentation);
- }
- }
- function returnsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to indentation
- }
- ret += "} ";
- return ret;
- }
- function switchcasesCode (switchcase, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to 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")} `;
- 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 += " { ";
- 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 += " "; // "\t" - using fixed space to indentation
- }
- ret += "}";
- return ret;
- }
- // Write IfElse code
- function iftruesCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += " "; // "\t" - using fixed space to indentation
- }
- ret += LocalizedStrings.getUI("text_if");
- if (!command_obj.expression) {
- Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI("inform_valid_expression"));
- } else {
- ret += "(";
- ret += elementExpressionCode(command_obj.expression);
- ret += ")";
- }
- /*switch (command_obj.expression.expression.type) {
- case Models.EXPRESSION_TYPES.exp_logic: ret += logicExpressionCode(command_obj.expression.expression); break;
- case Models.EXPRESSION_TYPES.exp_arithmetic: ret += arithmeticExpressionCode(command_obj.expression.expression); break;
- }*/
- ret += " { ";
- 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 += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to indentation
- }
- ret += LocalizedStrings.getUI("text_code_do") + " { ";
- 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 += " "; // "\t" - using fixed space to indentation
- }
- ret += "} " + LocalizedStrings.getUI("text_code_do_until");
- if (!command_obj.expression) {
- Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI("inform_valid_expression"));
- }
- /*switch (command_obj.expression.expression.type) {
- case Models.EXPRESSION_TYPES.exp_logic: ret += logicExpressionCode(command_obj.expression.expression); break;
- case Models.EXPRESSION_TYPES.exp_arithmetic: ret += arithmeticExpressionCode(command_obj.expression.expression); break;
- }*/
- 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 += " "; // "\t" - using fixed space to indentation
- }
- ret += LocalizedStrings.getUI("text_code_while");
- if (!command_obj.expression) {
- Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI("inform_valid_expression"));
- }
- /*switch (command_obj.expression.expression.type) {
- case Models.EXPRESSION_TYPES.exp_logic: ret += logicExpressionCode(command_obj.expression.expression); break;
- case Models.EXPRESSION_TYPES.exp_arithmetic: ret += arithmeticExpressionCode(command_obj.expression.expression); break;
- }*/
- if (command_obj.expression) {
- ret += "(";
- ret += elementExpressionCode(command_obj.expression);
- ret += ")";
- }
- ret += " { ";
- 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 += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to 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) {
- 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 += " "; // "\t" - using fixed space to 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 += " "; // "\t" - using fixed space to indentation
- }
- ret += "// ";
- ret += command_obj.comment_text.content;
- 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) {
- 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 + " ";
- //D console.log("********** code_generator.js: variablesCode(.): ret=|" + ret + "|"); //leo remover xxxxxxxxxxx
- 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 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 += " "; // "\t" - using fixed space to 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()
|