123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305 |
- /* 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;
- }
- 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>';
- ret += ` ${LocalizedStrings.getUI("text_code_for_from")} `;
- ret += '<span class="repeatN_text_par_2">';
- ret += variableValueMenuCode(command_obj.expression1);
- ret += '</span>';
- }
- if (command_obj.expression2) {
- 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) {
- 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";
- }
- 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";
- }
- 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;
- }
- 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;
- }
- 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;
- }
- 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 + " ( ";
- } 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 &&
- variable_obj.reference_dimensions == 1
- ) {
- ret += " [ " + variableValueMenuCode(variable_obj.row) + " ] ";
- } else 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 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;
- }
- 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;
- }
- if (parameter_obj.reference)
- ret += "&";
- 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 += 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 () {
- 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 += 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;
- }
|