2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
gbc_trans_code.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define _TRANS_CODE_C
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbc_compile.h"
|
|
|
|
#include "gbc_trans.h"
|
|
|
|
#include "gb_code.h"
|
|
|
|
#include "gb_limit.h"
|
|
|
|
|
|
|
|
/*#define DEBUG*/
|
|
|
|
|
2018-05-24 00:31:58 +02:00
|
|
|
ushort *TRANS_labels = NULL;
|
|
|
|
|
2018-05-17 01:13:56 +02:00
|
|
|
static FUNCTION *_func;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-14 17:03:14 +01:00
|
|
|
static CLASS_SYMBOL *add_local(int sym_index, TYPE type, int value, bool used)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
CLASS_SYMBOL *sym;
|
|
|
|
PARAM *loc;
|
2012-09-05 20:36:59 +02:00
|
|
|
bool warnings = JOB->warnings;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2019-08-22 03:13:58 +02:00
|
|
|
if (value > MAX_LOCAL_SYMBOL)
|
2014-04-05 13:02:17 +02:00
|
|
|
THROW("Too many local variables");
|
|
|
|
|
2018-05-17 01:13:56 +02:00
|
|
|
loc = ARRAY_add(&_func->local);
|
2012-01-15 09:54:57 +01:00
|
|
|
loc->index = sym_index;
|
|
|
|
loc->type = type;
|
|
|
|
loc->value = value;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-09-05 20:36:59 +02:00
|
|
|
if (used)
|
|
|
|
JOB->warnings = FALSE;
|
2015-01-16 01:31:59 +01:00
|
|
|
sym = CLASS_declare(JOB->class, sym_index, TK_VARIABLE, FALSE);
|
2012-09-05 20:36:59 +02:00
|
|
|
if (used)
|
|
|
|
JOB->warnings = warnings;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
sym->local.type = type;
|
|
|
|
sym->local.value = value;
|
2012-09-04 09:29:39 +02:00
|
|
|
sym->local_used = used;
|
2018-02-14 17:03:14 +01:00
|
|
|
|
|
|
|
return sym;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void create_local_from_param()
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
int i;
|
|
|
|
|
2012-09-04 09:29:39 +02:00
|
|
|
JOB->line--; // For line number of declaration
|
2015-11-18 02:29:28 +01:00
|
|
|
|
2018-05-17 01:13:56 +02:00
|
|
|
for (i = 0; i < _func->nparam; i++)
|
2012-01-15 09:54:57 +01:00
|
|
|
{
|
2018-05-17 01:13:56 +02:00
|
|
|
if (TYPE_get_id(_func->param[i].type) != T_VOID)
|
|
|
|
add_local(_func->param[i].index, _func->param[i].type, (i - _func->nparam), _func->param[i].ignore);
|
2012-01-15 09:54:57 +01:00
|
|
|
}
|
2015-11-18 02:29:28 +01:00
|
|
|
|
2012-09-04 09:29:39 +02:00
|
|
|
JOB->line++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_local()
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
int i;
|
|
|
|
CLASS_SYMBOL *sym;
|
|
|
|
|
2018-05-17 01:13:56 +02:00
|
|
|
for (i = 0; i < ARRAY_count(_func->local); i++)
|
2012-01-15 09:54:57 +01:00
|
|
|
{
|
2018-05-17 01:13:56 +02:00
|
|
|
sym = CLASS_get_symbol(JOB->class, _func->local[i].index);
|
2015-11-18 02:29:28 +01:00
|
|
|
|
2012-09-04 09:29:39 +02:00
|
|
|
if (!sym->local_used)
|
|
|
|
{
|
|
|
|
if (sym->local.value < 0)
|
|
|
|
COMPILE_print(MSG_WARNING, sym->local.line, "unused argument: &1", SYMBOL_get_name(&sym->symbol));
|
|
|
|
else
|
|
|
|
COMPILE_print(MSG_WARNING, sym->local.line, "unused variable: &1", SYMBOL_get_name(&sym->symbol));
|
|
|
|
}
|
2018-02-14 17:03:14 +01:00
|
|
|
else if (!sym->local_assigned)
|
|
|
|
{
|
|
|
|
if (sym->local.value >= 0)
|
|
|
|
COMPILE_print(MSG_WARNING, sym->local.line, "uninitialized variable: &1", SYMBOL_get_name(&sym->symbol));
|
|
|
|
}
|
2015-11-18 02:29:28 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
TYPE_clear(&sym->local.type);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool TRANS_local(void)
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
int sym_index;
|
|
|
|
TRANS_DECL decl;
|
2018-02-14 17:03:14 +01:00
|
|
|
PATTERN *pattern;
|
|
|
|
CLASS_SYMBOL *sym;
|
2012-01-15 09:54:57 +01:00
|
|
|
int f;
|
2015-01-16 01:31:59 +01:00
|
|
|
bool no_warning;
|
|
|
|
bool save_warnings;
|
2012-01-15 09:54:57 +01:00
|
|
|
|
|
|
|
if (!TRANS_is(RS_DIM))
|
|
|
|
return FALSE;
|
|
|
|
/*JOB->current++;
|
|
|
|
else if (!TRANS_check_declaration())
|
|
|
|
return FALSE;*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2011-10-16 21:24:01 +02:00
|
|
|
//many = FALSE;
|
2018-02-14 17:03:14 +01:00
|
|
|
pattern = JOB->current;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2015-01-16 01:31:59 +01:00
|
|
|
no_warning = TRANS_is(RS_LBRA);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!PATTERN_is_identifier(*JOB->current))
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
JOB->current++;
|
2015-01-16 01:31:59 +01:00
|
|
|
|
|
|
|
if (no_warning)
|
|
|
|
{
|
|
|
|
if (!PATTERN_is(*JOB->current, RS_RBRA))
|
|
|
|
THROW("Missing right brace");
|
|
|
|
JOB->current++;
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!TRANS_is(RS_COMMA))
|
|
|
|
break;
|
2011-10-16 21:24:01 +02:00
|
|
|
//many = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
f = TT_DO_NOT_CHECK_AS | TT_CAN_ARRAY | TT_CAN_NEW;
|
2010-05-19 14:43:57 +02:00
|
|
|
//if (!many)
|
|
|
|
// f |= TT_CAN_SQUARE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!TRANS_type(f, &decl))
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2018-02-14 17:03:14 +01:00
|
|
|
if (PATTERN_is(*pattern, RS_LBRA))
|
2015-01-16 01:31:59 +01:00
|
|
|
{
|
2018-02-14 17:03:14 +01:00
|
|
|
pattern++;
|
2015-01-16 01:31:59 +01:00
|
|
|
no_warning = TRUE;
|
|
|
|
save_warnings = JOB->warnings;
|
|
|
|
JOB->warnings = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
no_warning = FALSE;
|
|
|
|
|
2018-02-14 17:03:14 +01:00
|
|
|
sym_index = PATTERN_index(*pattern);
|
2018-05-17 01:13:56 +02:00
|
|
|
sym = add_local(sym_index, decl.type, _func->nlocal, FALSE);
|
2018-02-14 17:03:14 +01:00
|
|
|
pattern++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-01-16 01:31:59 +01:00
|
|
|
if (no_warning)
|
|
|
|
{
|
2018-02-14 17:03:14 +01:00
|
|
|
pattern++;
|
2015-01-16 01:31:59 +01:00
|
|
|
JOB->warnings = save_warnings;
|
|
|
|
}
|
|
|
|
|
2018-05-17 01:13:56 +02:00
|
|
|
_func->nlocal++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (TRANS_init_var(&decl))
|
2018-02-14 17:03:14 +01:00
|
|
|
{
|
2018-05-17 01:13:56 +02:00
|
|
|
CODE_pop_local(_func->nlocal - 1);
|
2018-02-14 17:03:14 +01:00
|
|
|
sym->local_assigned = TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-05-09 20:55:04 +02:00
|
|
|
if (COMP_verbose)
|
2007-12-30 17:41:49 +01:00
|
|
|
printf("LOCAL %s AS %s\n", TABLE_get_symbol_name(JOB->class->table, sym_index), TYPE_get_desc(decl.type));
|
|
|
|
|
2018-02-14 17:03:14 +01:00
|
|
|
if (!PATTERN_is(*pattern, RS_COMMA))
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
2018-02-14 17:03:14 +01:00
|
|
|
pattern++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*if (!many)
|
|
|
|
{
|
|
|
|
if (TRANS_init_var(&decl))
|
2018-05-17 01:13:56 +02:00
|
|
|
CODE_pop_local(_func->nlocal - 1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}*/
|
|
|
|
|
|
|
|
if (!TRANS_is(RS_COMMA))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 13:15:33 +01:00
|
|
|
|
|
|
|
int TRANS_loop_local(bool allow_arg)
|
|
|
|
{
|
|
|
|
int sym_index;
|
|
|
|
TRANS_DECL decl;
|
|
|
|
CLASS_SYMBOL *sym;
|
|
|
|
|
|
|
|
if (!PATTERN_is_identifier(*JOB->current))
|
2018-12-14 05:16:24 +01:00
|
|
|
THROW("Syntax error. Identifier expected");
|
2018-12-13 13:15:33 +01:00
|
|
|
|
|
|
|
sym_index = PATTERN_index(*JOB->current);
|
|
|
|
JOB->current++;
|
|
|
|
|
|
|
|
sym = CLASS_get_symbol(JOB->class, sym_index);
|
|
|
|
|
|
|
|
if (TRANS_type(TT_DO_NOT_CHECK_AS | TT_CAN_ARRAY, &decl))
|
|
|
|
{
|
|
|
|
if (!TYPE_compare(&sym->local.type, &decl.type))
|
|
|
|
{
|
|
|
|
add_local(sym_index, decl.type, _func->nlocal, FALSE);
|
|
|
|
_func->nlocal++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TYPE_is_null(sym->local.type))
|
2018-12-14 05:16:24 +01:00
|
|
|
{
|
|
|
|
if (TYPE_is_null(sym->global.type))
|
|
|
|
THROW("Unknown identifier: &1", TABLE_get_symbol_name(JOB->class->table, sym_index));
|
|
|
|
else
|
|
|
|
THROW("Loop variable cannot be global");
|
|
|
|
}
|
2018-12-13 13:15:33 +01:00
|
|
|
|
|
|
|
if (!allow_arg && sym->local.value < 0)
|
|
|
|
THROW("Loop variable cannot be an argument");
|
|
|
|
|
|
|
|
sym->local_assigned = TRUE;
|
|
|
|
sym->local_used = TRUE;
|
|
|
|
|
|
|
|
return sym->local.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-02 23:05:55 +01:00
|
|
|
void TRANS_stop(void)
|
|
|
|
{
|
|
|
|
if (TRANS_is(RS_EVENT))
|
|
|
|
CODE_stop_event();
|
|
|
|
else
|
|
|
|
CODE_stop();
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void TRANS_statement(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
static TRANS_STATEMENT statement[] = {
|
|
|
|
{ RS_EXIT, TRANS_break },
|
|
|
|
{ RS_BREAK, TRANS_break },
|
|
|
|
{ RS_CONTINUE, TRANS_continue },
|
|
|
|
{ RS_GOTO, TRANS_goto },
|
|
|
|
{ RS_RETURN, TRANS_return },
|
|
|
|
{ RS_PRINT, TRANS_print },
|
|
|
|
{ RS_INPUT, TRANS_input },
|
|
|
|
{ RS_WRITE, TRANS_write },
|
|
|
|
{ RS_READ, TRANS_read_old },
|
|
|
|
//{ RS_OPEN, TRANS_open },
|
|
|
|
{ RS_CLOSE, TRANS_close },
|
|
|
|
{ RS_SEEK, TRANS_seek },
|
|
|
|
{ RS_FLUSH, TRANS_flush },
|
|
|
|
{ RS_STOP, TRANS_stop },
|
|
|
|
{ RS_QUIT, TRANS_quit },
|
|
|
|
{ RS_EXEC, TRANS_exec },
|
|
|
|
{ RS_SHELL, TRANS_shell },
|
|
|
|
{ RS_WAIT, TRANS_wait },
|
|
|
|
{ RS_SLEEP, TRANS_sleep },
|
|
|
|
{ RS_KILL, TRANS_kill },
|
|
|
|
{ RS_MOVE, TRANS_move },
|
|
|
|
{ RS_INC, TRANS_inc },
|
|
|
|
{ RS_DEC, TRANS_dec },
|
|
|
|
{ RS_SWAP, TRANS_swap },
|
|
|
|
{ RS_MKDIR, TRANS_mkdir },
|
|
|
|
{ RS_RMDIR, TRANS_rmdir },
|
|
|
|
{ RS_COPY, TRANS_copy },
|
|
|
|
{ RS_RAISE, TRANS_raise },
|
|
|
|
{ RS_LINK, TRANS_link },
|
|
|
|
{ RS_LOCK, TRANS_lock },
|
|
|
|
{ RS_UNLOCK, TRANS_unlock },
|
|
|
|
{ RS_TRY, TRANS_try },
|
|
|
|
{ RS_LINE, TRANS_line_input },
|
|
|
|
{ RS_OUTPUT, TRANS_output_to },
|
|
|
|
{ RS_DEBUG, TRANS_debug },
|
2018-12-16 00:35:46 +01:00
|
|
|
{ RS_ASSERT, TRANS_assert },
|
2012-01-15 09:54:57 +01:00
|
|
|
{ RS_ERROR, TRANS_error },
|
|
|
|
{ RS_RANDOMIZE, TRANS_randomize },
|
|
|
|
{ RS_CHMOD, TRANS_chmod },
|
|
|
|
{ RS_CHOWN, TRANS_chown },
|
|
|
|
{ RS_CHGRP, TRANS_chgrp },
|
2012-03-04 00:57:36 +01:00
|
|
|
{ RS_GOSUB, TRANS_gosub },
|
2012-05-21 03:09:35 +02:00
|
|
|
{ RS_ON, TRANS_on_goto_gosub },
|
2012-01-15 09:54:57 +01:00
|
|
|
|
|
|
|
{ RS_NONE, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
PATTERN *look = JOB->current;
|
|
|
|
TRANS_STATEMENT *st;
|
|
|
|
COMP_INFO *info;
|
|
|
|
|
|
|
|
if (PATTERN_is_reserved(look[0]))
|
|
|
|
{
|
|
|
|
info = &COMP_res_info[PATTERN_index(*look)];
|
|
|
|
|
|
|
|
if (!info->func)
|
|
|
|
{
|
|
|
|
for (st = statement; st->id; st++)
|
|
|
|
{
|
|
|
|
if (PATTERN_is(look[0], st->id))
|
|
|
|
{
|
|
|
|
info->func = st->func;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!info->func)
|
|
|
|
info->func = TS_NO_SUBR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->func && info->func != TS_NO_SUBR)
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
(*info->func)();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (PATTERN_is_subr(look[0]) && (PATTERN_index(look[0]) == SUBR_Mid || PATTERN_index(look[0]) == SUBR_MidS))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_mid();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TRANS_affectation(FALSE))
|
|
|
|
TRANS_expression(TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void translate_body()
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
PATTERN *look;
|
2018-05-17 01:13:56 +02:00
|
|
|
bool is_proc = (TYPE_get_id(_func->type) == T_VOID);
|
2012-01-15 09:54:57 +01:00
|
|
|
bool test_newline;
|
2012-12-19 22:55:16 +01:00
|
|
|
//int line = JOB->line - 1;
|
2012-01-15 09:54:57 +01:00
|
|
|
bool just_got_select = FALSE;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
test_newline = TRUE;
|
|
|
|
CODE_allow_break();
|
|
|
|
|
2012-12-19 22:55:16 +01:00
|
|
|
FUNCTION_add_all_pos_line();
|
2012-01-15 09:54:57 +01:00
|
|
|
|
|
|
|
look = JOB->current;
|
|
|
|
|
|
|
|
if (PATTERN_is(look[0], RS_END))
|
|
|
|
if (TRANS_is_end_function(is_proc, &look[1]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (TRANS_newline())
|
|
|
|
test_newline = FALSE;
|
|
|
|
else if (!TRANS_local())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (test_newline)
|
|
|
|
if (!PATTERN_is_newline(*JOB->current))
|
|
|
|
THROW_UNEXPECTED(JOB->current);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TRANS_control_init();
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
test_newline = TRUE;
|
|
|
|
CODE_allow_break();
|
|
|
|
|
2012-12-19 22:55:16 +01:00
|
|
|
FUNCTION_add_all_pos_line();
|
2012-01-15 09:54:57 +01:00
|
|
|
|
|
|
|
look = JOB->current;
|
|
|
|
|
|
|
|
if (PATTERN_is(look[0], RS_END))
|
|
|
|
if (TRANS_is_end_function(is_proc, &look[1]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*if (PATTERN_is_newline(look[0]))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
JOB->line++;
|
|
|
|
continue;
|
|
|
|
}*/
|
|
|
|
if (TRANS_newline())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (just_got_select)
|
|
|
|
{
|
|
|
|
if (!PATTERN_is(look[0], RS_CASE) && !PATTERN_is(look[0], RS_DEFAULT))
|
|
|
|
THROW("Syntax error. CASE or DEFAULT expected after SELECT");
|
|
|
|
just_got_select = FALSE;
|
|
|
|
}
|
|
|
|
|
2018-12-11 02:40:14 +01:00
|
|
|
if (PATTERN_is(look[0], RS_DIM))
|
|
|
|
{
|
|
|
|
TRANS_local();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is_identifier(look[0]) && PATTERN_is(look[1], RS_COLON))
|
2012-01-15 09:54:57 +01:00
|
|
|
{
|
|
|
|
TRANS_label();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_IF))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_if();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_ELSE))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_else();
|
|
|
|
}
|
|
|
|
else if ((PATTERN_is(look[0], RS_END)
|
|
|
|
&& PATTERN_is(look[1], RS_IF))
|
|
|
|
|| PATTERN_is(look[0], RS_ENDIF))
|
|
|
|
{
|
|
|
|
if (PATTERN_is(look[0], RS_END))
|
|
|
|
JOB->current += 2;
|
|
|
|
else
|
|
|
|
JOB->current++;
|
|
|
|
|
|
|
|
TRANS_endif();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_DO))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_do(RS_DO);
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_WHILE))
|
|
|
|
{
|
|
|
|
TRANS_do(RS_WHILE);
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(*look, RS_REPEAT))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_do(RS_REPEAT);
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_LOOP))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_loop(RS_LOOP);
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_UNTIL))
|
|
|
|
{
|
|
|
|
TRANS_loop(RS_UNTIL);
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_WEND))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_loop(RS_WEND);
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_FOR))
|
|
|
|
{
|
|
|
|
if (PATTERN_is(look[1], RS_EACH))
|
|
|
|
{
|
|
|
|
JOB->current += 2;
|
|
|
|
TRANS_for_each();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_for();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_NEXT))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_next();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_SELECT))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_select();
|
|
|
|
just_got_select = TRUE;
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_CASE))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
if (PATTERN_is(look[1], RS_ELSE))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_default();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TRANS_case();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_DEFAULT))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_default();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_END)
|
|
|
|
&& PATTERN_is(look[1], RS_SELECT))
|
|
|
|
{
|
|
|
|
JOB->current += 2;
|
|
|
|
TRANS_end_select();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_FINALLY))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_finally();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_CATCH))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_catch();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(*look, RS_WITH))
|
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
TRANS_with();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_END)
|
|
|
|
&& PATTERN_is(look[1], RS_WITH))
|
|
|
|
{
|
|
|
|
JOB->current += 2;
|
|
|
|
TRANS_end_with();
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(look[0], RS_LET))
|
2010-07-14 18:33:29 +02:00
|
|
|
{
|
|
|
|
JOB->current++;
|
|
|
|
if (!TRANS_affectation(FALSE))
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
}
|
2012-01-15 09:54:57 +01:00
|
|
|
else
|
|
|
|
TRANS_statement();
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (next_newline)
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (PATTERN_is_NEWLINE(*JOB->current)
|
|
|
|
|| PATTERN_is_END(*JOB->current))
|
|
|
|
break;
|
|
|
|
JOB->current++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (test_newline)
|
|
|
|
if (!PATTERN_is_newline(*JOB->current))
|
|
|
|
THROW_UNEXPECTED(JOB->current);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TRANS_control_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void trans_call(const char *name, int nparam)
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
CLASS_SYMBOL *sym;
|
2010-05-25 13:19:00 +02:00
|
|
|
int index;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (!TABLE_find_symbol(JOB->class->table, name, strlen(name), &index))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
sym = (CLASS_SYMBOL *)TABLE_get_symbol(JOB->class->table, index);
|
2015-11-18 02:29:28 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (TYPE_get_kind(sym->global.type) != TK_FUNCTION)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-09-04 09:29:39 +02:00
|
|
|
sym->global_used = TRUE;
|
2015-11-18 02:29:28 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
CODE_push_global(sym->global.value, FALSE, TRUE);
|
|
|
|
CODE_call(nparam);
|
|
|
|
CODE_drop();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void TRANS_code(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
int i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
for (i = 0; i < ARRAY_count(JOB->class->function); i++)
|
|
|
|
{
|
2018-05-17 01:13:56 +02:00
|
|
|
_func = &JOB->class->function[i];
|
|
|
|
|
|
|
|
CODE_begin_function(_func);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-05-09 20:55:04 +02:00
|
|
|
if (COMP_verbose)
|
2018-06-09 22:42:35 +02:00
|
|
|
{
|
2018-05-17 01:13:56 +02:00
|
|
|
printf("Compiling %s()...\n", TABLE_get_symbol_name(JOB->class->table, _func->name));
|
2018-06-29 00:10:31 +02:00
|
|
|
if (_func->fast)
|
2018-06-09 22:42:35 +02:00
|
|
|
printf("Fast\n");
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-12-19 22:55:16 +01:00
|
|
|
/* Do not debug implicit or generated functions */
|
2018-05-17 01:13:56 +02:00
|
|
|
if (!_func->start || _func->name == NO_SYMBOL || TABLE_get_symbol_name(JOB->class->table, _func->name)[0] == '@')
|
2012-01-15 09:54:57 +01:00
|
|
|
JOB->nobreak = TRUE;
|
|
|
|
else
|
|
|
|
JOB->nobreak = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-05-17 01:13:56 +02:00
|
|
|
JOB->line = _func->line;
|
|
|
|
JOB->current = _func->start;
|
|
|
|
JOB->func = _func;
|
2012-12-19 22:55:16 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
/* fonction implicite ? */
|
2018-05-17 01:13:56 +02:00
|
|
|
if (!_func->start)
|
2012-01-15 09:54:57 +01:00
|
|
|
{
|
|
|
|
if ((i == FUNC_INIT_DYNAMIC) && (JOB->form != NULL))
|
|
|
|
{
|
|
|
|
//CODE_event(FALSE);
|
2016-06-07 02:08:11 +02:00
|
|
|
trans_call("@load", 0);
|
2012-01-15 09:54:57 +01:00
|
|
|
//CODE_event(TRUE);
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:55:16 +01:00
|
|
|
FUNCTION_add_last_pos_line();
|
2012-01-15 09:54:57 +01:00
|
|
|
CODE_op(C_RETURN, 0, 0, TRUE);
|
2020-05-09 20:55:04 +02:00
|
|
|
if (COMP_verbose)
|
2018-05-17 01:13:56 +02:00
|
|
|
CODE_dump(_func->code, _func->ncode);
|
2018-05-28 03:18:44 +02:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-17 01:13:56 +02:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
create_local_from_param();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
translate_body();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-04 00:57:36 +01:00
|
|
|
CODE_return(2); // Return from function, ignore Gosub stack
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-05-17 01:13:56 +02:00
|
|
|
CODE_end_function(_func);
|
2012-12-19 22:55:16 +01:00
|
|
|
FUNCTION_add_last_pos_line();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-05-17 01:13:56 +02:00
|
|
|
_func->stack = _func->nlocal + _func->nctrl + CODE_stack_usage;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-05-09 20:55:04 +02:00
|
|
|
if (COMP_verbose)
|
2012-01-15 09:54:57 +01:00
|
|
|
{
|
2018-05-17 01:13:56 +02:00
|
|
|
CODE_dump(_func->code, _func->ncode);
|
|
|
|
printf("%d local(s) %d control(s) ", _func->nlocal, _func->nctrl);
|
|
|
|
printf("%d stack\n", _func->stack);
|
2012-01-15 09:54:57 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
remove_local();
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
CLASS_check_properties(JOB->class);
|
2012-09-04 09:29:39 +02:00
|
|
|
CLASS_check_unused_global(JOB->class);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
JOB->func = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
bool TRANS_init_var(TRANS_DECL *decl)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
int i;
|
|
|
|
TRANS_ARRAY *array;
|
|
|
|
//PATTERN *save;
|
|
|
|
|
|
|
|
if (decl->is_new)
|
|
|
|
{
|
|
|
|
if (TYPE_is_array(decl->type) && decl->array.ndim > 0)
|
|
|
|
{
|
|
|
|
array = &decl->array;
|
|
|
|
|
|
|
|
if (TYPE_is_object(decl->type))
|
|
|
|
CODE_push_class(TYPE_get_value(decl->type));
|
|
|
|
else
|
|
|
|
CODE_push_number(TYPE_get_id(decl->type));
|
|
|
|
|
|
|
|
for (i = 0; i < array->ndim; i++)
|
|
|
|
CODE_push_number(array->dim[i]);
|
|
|
|
|
|
|
|
CODE_new(array->ndim + 1, TRUE, FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JOB->current = decl->init;
|
|
|
|
TRANS_new();
|
|
|
|
}
|
2012-12-22 01:23:51 +01:00
|
|
|
return TRUE;
|
2012-01-15 09:54:57 +01:00
|
|
|
}
|
|
|
|
else if (decl->init)
|
|
|
|
{
|
|
|
|
JOB->current = decl->init;
|
|
|
|
TRANS_expression(FALSE);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-12-22 01:23:51 +01:00
|
|
|
else
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2008-01-06 19:49:23 +01:00
|
|
|
void TRANS_init_object()
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void TRANS_init_optional(TRANS_PARAM *param)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
PATTERN *look = param->optional;
|
|
|
|
PATTERN *save;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (look == NULL)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
save = JOB->current;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (PATTERN_is(*look, RS_COMMA) || PATTERN_is(*look, RS_RBRA))
|
|
|
|
{
|
|
|
|
CODE_push_void();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!PATTERN_is(*look, RS_EQUAL))
|
|
|
|
THROW("Syntax error. Invalid optional parameter");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
look++;
|
|
|
|
JOB->current = look;
|
|
|
|
TRANS_expression(FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (!PATTERN_is(*JOB->current, RS_COMMA) && !PATTERN_is(*JOB->current, RS_RBRA))
|
|
|
|
THROW("Syntax error. Invalid optional parameter");
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
JOB->current = save;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|