1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282 |
- /* 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";
- 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\t" + LocalizedStrings.getUI("function") + " ";
- 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 += " ( ";
- 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 += " ) {";
- 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++) {
- //try {
- ret += commandsCode(function_obj.commands[j]);
- /*} 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\t}";
- 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";
- }
- 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";
- }
- ret += LocalizedStrings.getUI("text_break");
- return ret;
- }
- function switchsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- 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";
- }
- ret += "} ";
- return ret;
- }
- function switchcasesCode (switchcase, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- 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;
- }
- function repeatNtimesCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- 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]);
- }
- /*if (command_obj.var_incrementation) {
- ret += variableValueMenuCode(command_obj.var_incrementation);
- ret += ' <- ';
- ret += variableValueMenuCode(command_obj.expression3.itens[0]);
- switch (command_obj.expression3.itens[1]) {
- 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;
- }
- 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";
- }
- ret += "}";
- return ret;
- }
- function iftruesCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- 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";
- }
- 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";
- }
- ret += "}";
- return ret;
- }
- function doWhilesCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- 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";
- }
- 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 whiletruesCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- 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";
- }
- ret += "}";
- return ret;
- }
- 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 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 attributionsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- 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;
- }
- 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;
- }
- function functioncallsCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- ret += variableValueMenuCode(command_obj.function_called);
- return ret;
- }
- function readersCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- ret += LocalizedStrings.getUI("text_command_read") + " ( ";
- ret += variableValueMenuCode(command_obj.variable_value_menu);
- ret += " ) ";
- return ret;
- }
- 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 + " ( ";
- } 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 += " )";
- } 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) + " ] ";
- }
- if (
- variable_obj.content.dimensions == 2 &&
- variable_obj.dimensions != 2
- ) {
- ret += " [ " + variableValueMenuCode(variable_obj.row) + " ] ";
- ret += " [ " + variableValueMenuCode(variable_obj.column) + " ] ";
- }
- } 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 {
- ret += variable_obj.content;
- }
- }
- } catch (err) {
- if (!is_return) {
- Utils.renderErrorMessage(
- variable_obj.dom_object,
- LocalizedStrings.getUI("inform_valid_content")
- );
- throw err;
- }
- }
- return ret;
- }
- function writersCode (command_obj, indentation) {
- let ret = "\n";
- for (let i = 0; i < indentation; i++) {
- ret += "\t";
- }
- 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";
- }
- 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;
- }
- ret += parameter_obj.name + "";
- if (parameter_obj.dimensions == 1) {
- ret += " []";
- } else if (parameter_obj.dimensions == 2) {
- ret += " [][]";
- }
- return ret;
- }
- function variablesCode (variable_obj) {
- let ret = "";
- const temp = variable_obj;
- ret += "\n\t\t";
- 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 += 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 += 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 += "<- " + 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 () {
- 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 += "\n\t";
- 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 += 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 += 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 += "<- " + 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;
- }
|