2011-01-06 07:32:53 +00:00
|
|
|
%{
|
|
|
|
/*
|
2011-01-24 23:54:02 +00:00
|
|
|
qp-parse.y
|
2011-01-06 07:32:53 +00:00
|
|
|
|
2011-01-24 23:54:02 +00:00
|
|
|
parser for quakepascal
|
2011-01-06 07:32:53 +00:00
|
|
|
|
2011-01-24 23:54:02 +00:00
|
|
|
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
2011-01-06 07:32:53 +00:00
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
2011-01-24 23:54:02 +00:00
|
|
|
Date: 2011/01/06
|
2011-01-06 07:32:53 +00: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
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-01-06 11:27:32 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2011-01-13 05:58:16 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "tools/qfcc/include/codespace.h"
|
|
|
|
#include "tools/qfcc/include/diagnostic.h"
|
|
|
|
#include "tools/qfcc/include/expr.h"
|
|
|
|
#include "tools/qfcc/include/function.h"
|
|
|
|
#include "tools/qfcc/include/qfcc.h"
|
|
|
|
#include "tools/qfcc/include/reloc.h"
|
|
|
|
#include "tools/qfcc/include/shared.h"
|
|
|
|
#include "tools/qfcc/include/symtab.h"
|
|
|
|
#include "tools/qfcc/include/type.h"
|
2011-01-06 07:32:53 +00:00
|
|
|
|
|
|
|
#define YYDEBUG 1
|
|
|
|
#define YYERROR_VERBOSE 1
|
|
|
|
#undef YYERROR_VERBOSE
|
|
|
|
|
2012-10-26 07:01:41 +00:00
|
|
|
extern char *qp_yytext;
|
2011-01-06 07:32:53 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
yyerror (const char *s)
|
|
|
|
{
|
|
|
|
#ifdef YYERROR_VERBOSE
|
2012-10-26 07:01:41 +00:00
|
|
|
error (0, "%s %s\n", qp_yytext, s);
|
2011-01-06 07:32:53 +00:00
|
|
|
#else
|
2012-10-26 07:01:41 +00:00
|
|
|
error (0, "%s before %s", s, qp_yytext);
|
2011-01-06 07:32:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_error (void)
|
|
|
|
{
|
2012-10-26 07:01:41 +00:00
|
|
|
error (0, "parse error before %s", qp_yytext);
|
2011-01-06 07:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PARSE_ERROR do { parse_error (); YYERROR; } while (0)
|
|
|
|
|
|
|
|
int yylex (void);
|
|
|
|
|
|
|
|
%}
|
|
|
|
|
|
|
|
%union {
|
|
|
|
int op;
|
|
|
|
struct def_s *def;
|
|
|
|
struct hashtab_s *def_list;
|
|
|
|
struct type_s *type;
|
|
|
|
struct typedef_s *typename;
|
|
|
|
struct expr_s *expr;
|
|
|
|
struct function_s *function;
|
|
|
|
struct switch_block_s *switch_block;
|
|
|
|
struct param_s *param;
|
|
|
|
struct struct_s *strct;
|
2011-01-13 05:58:16 +00:00
|
|
|
struct symtab_s *symtab;
|
|
|
|
struct symbol_s *symbol;
|
2011-01-27 05:17:12 +00:00
|
|
|
int storage;
|
2011-01-06 07:32:53 +00:00
|
|
|
}
|
|
|
|
|
2012-11-04 01:27:41 +00:00
|
|
|
// these tokens are common between qc and qp
|
|
|
|
%left LOW
|
2011-01-06 07:32:53 +00:00
|
|
|
%nonassoc IFX
|
|
|
|
%nonassoc ELSE
|
2011-01-06 14:13:18 +00:00
|
|
|
%nonassoc BREAK_PRIMARY
|
|
|
|
%nonassoc ';'
|
|
|
|
%nonassoc CLASS_NOT_CATEGORY
|
|
|
|
%nonassoc STORAGEX
|
|
|
|
|
2012-10-26 10:06:10 +00:00
|
|
|
%left COMMA
|
2020-03-20 02:47:01 +00:00
|
|
|
%right <op> '=' ASX
|
2011-01-06 14:13:18 +00:00
|
|
|
%right '?' ':'
|
|
|
|
%left OR
|
|
|
|
%left AND
|
|
|
|
%left '|'
|
|
|
|
%left '^'
|
|
|
|
%left '&'
|
|
|
|
%left EQ NE
|
2022-01-09 05:02:16 +00:00
|
|
|
%left LT GT GE LE
|
2022-01-30 01:56:15 +00:00
|
|
|
%token NAND NOR XNOR
|
2012-11-04 01:27:41 +00:00
|
|
|
// end of tokens common between qc and qp
|
2011-01-06 07:32:53 +00:00
|
|
|
|
2011-01-13 05:58:16 +00:00
|
|
|
%left <op> RELOP
|
|
|
|
%left <op> ADDOP
|
|
|
|
%left <op> MULOP
|
2011-01-06 07:32:53 +00:00
|
|
|
%right UNARY
|
|
|
|
|
2011-01-13 05:58:16 +00:00
|
|
|
%token <type> TYPE TYPE_NAME
|
|
|
|
%token <symbol> ID
|
2012-12-18 04:59:45 +00:00
|
|
|
%token <expr> VALUE
|
2011-01-06 07:32:53 +00:00
|
|
|
|
|
|
|
%token PROGRAM VAR ARRAY OF FUNCTION PROCEDURE PBEGIN END IF THEN ELSE
|
2011-01-12 14:39:59 +00:00
|
|
|
%token WHILE DO RANGE ASSIGNOP NOT ELLIPSIS
|
2011-01-30 13:16:59 +00:00
|
|
|
%token RETURN
|
2011-01-06 07:32:53 +00:00
|
|
|
|
2011-01-13 05:58:16 +00:00
|
|
|
%type <type> type standard_type
|
|
|
|
%type <symbol> program_head identifier_list subprogram_head
|
2011-01-30 12:42:10 +00:00
|
|
|
%type <symtab> param_scope
|
2011-01-13 05:58:16 +00:00
|
|
|
%type <param> arguments parameter_list
|
2012-12-01 07:37:38 +00:00
|
|
|
%type <expr> compound_statement else optional_statements statement_list
|
2011-01-13 05:58:16 +00:00
|
|
|
%type <expr> statement procedure_statement
|
|
|
|
%type <expr> expression_list expression unary_expr primary variable name
|
|
|
|
%type <op> sign
|
2011-01-06 07:32:53 +00:00
|
|
|
|
|
|
|
%{
|
2020-03-20 02:48:01 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
build_dotmain (symbol_t *program)
|
|
|
|
{
|
|
|
|
symbol_t *dotmain = new_symbol (".main");
|
|
|
|
expr_t *code;
|
|
|
|
expr_t *exitcode;
|
|
|
|
|
|
|
|
dotmain->params = 0;
|
2022-01-18 04:21:06 +00:00
|
|
|
dotmain->type = parse_params (&type_int, 0);
|
2020-03-20 02:48:01 +00:00
|
|
|
dotmain->type = find_type (dotmain->type);
|
|
|
|
dotmain = function_symbol (dotmain, 0, 1);
|
|
|
|
|
|
|
|
exitcode = new_symbol_expr (symtab_lookup (current_symtab, "ExitCode"));
|
|
|
|
|
2021-09-24 10:49:55 +00:00
|
|
|
current_func = begin_function (dotmain, 0, current_symtab, 0,
|
|
|
|
current_storage);
|
2022-01-21 01:20:02 +00:00
|
|
|
current_symtab = current_func->locals;
|
2020-03-20 02:48:01 +00:00
|
|
|
code = new_block_expr ();
|
|
|
|
append_expr (code, function_expr (new_symbol_expr (program), 0));
|
|
|
|
append_expr (code, return_expr (current_func, exitcode));
|
|
|
|
build_code_function (dotmain, 0, code);
|
|
|
|
}
|
|
|
|
|
2022-02-01 07:08:58 +00:00
|
|
|
static symbol_t *
|
|
|
|
function_value (function_t *func)
|
|
|
|
{
|
|
|
|
symbol_t *ret = 0;
|
|
|
|
if (func->type->t.func.type) {
|
|
|
|
ret = symtab_lookup (func->locals, ".ret");
|
|
|
|
if (!ret || ret->table != func->locals) {
|
|
|
|
ret = new_symbol_type (".ret", func->type->t.func.type);
|
|
|
|
initialize_def (ret, 0, func->locals->space, sc_local,
|
|
|
|
func->locals);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static expr_t *
|
|
|
|
function_return (function_t *func)
|
|
|
|
{
|
|
|
|
symbol_t *ret = function_value (func);
|
|
|
|
expr_t *ret_val = 0;
|
|
|
|
if (ret) {
|
|
|
|
ret_val = new_symbol_expr (ret);
|
|
|
|
}
|
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
2011-01-06 07:32:53 +00:00
|
|
|
%}
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
program
|
2011-01-06 14:13:18 +00:00
|
|
|
: program_head
|
2011-01-06 07:32:53 +00:00
|
|
|
declarations
|
|
|
|
subprogram_declarations
|
2011-01-06 14:13:18 +00:00
|
|
|
compound_statement '.'
|
|
|
|
{
|
2011-01-19 13:19:26 +00:00
|
|
|
symtab_t *st = current_symtab;
|
2011-01-13 05:58:16 +00:00
|
|
|
// move the symbol for the program name to the end of the list
|
|
|
|
symtab_removesymbol (current_symtab, $1);
|
|
|
|
symtab_addsymbol (current_symtab, $1);
|
2011-01-18 03:37:12 +00:00
|
|
|
|
2021-09-24 10:49:55 +00:00
|
|
|
current_func = begin_function ($1, 0, current_symtab, 0,
|
|
|
|
current_storage);
|
2022-01-21 01:20:02 +00:00
|
|
|
current_symtab = current_func->locals;
|
2011-01-18 23:38:09 +00:00
|
|
|
build_code_function ($1, 0, $4);
|
2011-01-19 13:19:26 +00:00
|
|
|
current_symtab = st;
|
2011-01-18 23:38:09 +00:00
|
|
|
|
2020-03-20 02:48:01 +00:00
|
|
|
build_dotmain ($1);
|
2011-01-19 13:19:26 +00:00
|
|
|
current_symtab = st;
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
2011-01-06 14:13:18 +00:00
|
|
|
program_head
|
2011-01-30 12:42:10 +00:00
|
|
|
: PROGRAM { current_symtab = pr.symtab; }
|
|
|
|
ID '(' opt_identifier_list ')' ';'
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
2011-01-30 12:42:10 +00:00
|
|
|
$$ = $3;
|
2011-01-18 03:37:12 +00:00
|
|
|
|
2020-03-20 02:48:01 +00:00
|
|
|
// FIXME need units and standard units
|
|
|
|
{
|
|
|
|
symbol_t *sym = new_symbol ("ExitCode");
|
2022-01-18 04:21:06 +00:00
|
|
|
sym->type = &type_int;
|
2022-01-24 03:48:02 +00:00
|
|
|
initialize_def (sym, 0, current_symtab->space, sc_global,
|
|
|
|
current_symtab);
|
2020-03-20 02:48:01 +00:00
|
|
|
if (sym->s.def) {
|
|
|
|
sym->s.def->nosave = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-01-13 05:58:16 +00:00
|
|
|
$$->type = parse_params (&type_void, 0);
|
2012-08-06 23:10:29 +00:00
|
|
|
$$->type = find_type ($$->type);
|
2011-01-18 03:37:12 +00:00
|
|
|
$$ = function_symbol ($$, 0, 1);
|
2011-01-06 14:13:18 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
2011-01-13 05:58:16 +00:00
|
|
|
opt_identifier_list
|
|
|
|
: /* empty */
|
|
|
|
| identifier_list
|
|
|
|
;
|
|
|
|
|
2011-01-06 07:32:53 +00:00
|
|
|
identifier_list
|
2011-01-30 12:42:10 +00:00
|
|
|
: ID { $$ = check_redefined ($1); }
|
2011-01-06 07:32:53 +00:00
|
|
|
| identifier_list ',' ID
|
2011-01-06 11:27:32 +00:00
|
|
|
{
|
2011-01-13 05:58:16 +00:00
|
|
|
symbol_t **s;
|
|
|
|
$$ = $1;
|
2011-01-30 12:42:10 +00:00
|
|
|
$3 = check_redefined ($3);
|
2011-01-13 05:58:16 +00:00
|
|
|
for (s = &$$; *s; s = &(*s)->next)
|
|
|
|
;
|
|
|
|
*s = $3;
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
declarations
|
|
|
|
: declarations VAR identifier_list ':' type ';'
|
2011-01-06 11:27:32 +00:00
|
|
|
{
|
2011-01-13 05:58:16 +00:00
|
|
|
while ($3) {
|
|
|
|
symbol_t *next = $3->next;
|
2018-10-15 13:11:20 +00:00
|
|
|
$3->type = $5;
|
2022-01-24 03:48:02 +00:00
|
|
|
initialize_def ($3, 0, current_symtab->space, current_storage,
|
|
|
|
current_symtab);
|
2011-01-13 05:58:16 +00:00
|
|
|
$3 = next;
|
|
|
|
}
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
| /* empty */
|
|
|
|
;
|
|
|
|
|
|
|
|
type
|
2011-01-13 05:58:16 +00:00
|
|
|
: standard_type
|
2012-12-18 04:59:45 +00:00
|
|
|
| ARRAY '[' VALUE RANGE VALUE ']' OF standard_type
|
2011-01-06 07:32:53 +00:00
|
|
|
{
|
2022-01-18 04:21:06 +00:00
|
|
|
$$ = based_array_type ($8, expr_int ($3), expr_int ($5));
|
2011-01-06 07:32:53 +00:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
standard_type
|
2011-01-13 05:58:16 +00:00
|
|
|
: TYPE
|
|
|
|
| TYPE_NAME
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
subprogram_declarations
|
|
|
|
: subprogram_declarations subprogram_declaration
|
|
|
|
| /* emtpy */
|
|
|
|
;
|
|
|
|
|
|
|
|
subprogram_declaration
|
2011-01-06 11:27:32 +00:00
|
|
|
: subprogram_head ';'
|
|
|
|
{
|
2011-01-27 05:17:12 +00:00
|
|
|
$<storage>$ = current_storage;
|
2021-09-24 10:49:55 +00:00
|
|
|
current_func = begin_function ($1, 0, current_symtab, 0,
|
|
|
|
current_storage);
|
2022-01-21 01:20:02 +00:00
|
|
|
current_symtab = current_func->locals;
|
2012-12-02 01:11:30 +00:00
|
|
|
current_storage = sc_local;
|
2022-02-01 07:08:58 +00:00
|
|
|
function_value (current_func);
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
|
|
|
declarations compound_statement ';'
|
|
|
|
{
|
2022-02-01 07:08:58 +00:00
|
|
|
append_expr ($5, new_return_expr (function_return (current_func)));
|
2011-01-18 23:38:09 +00:00
|
|
|
build_code_function ($1, 0, $5);
|
2022-01-21 01:20:02 +00:00
|
|
|
current_symtab = current_func->parameters->parent;
|
2011-01-27 05:17:12 +00:00
|
|
|
current_storage = $<storage>3;
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
2012-12-18 04:59:45 +00:00
|
|
|
| subprogram_head ASSIGNOP '#' VALUE ';'
|
2011-01-17 13:33:33 +00:00
|
|
|
{
|
2021-09-24 10:49:55 +00:00
|
|
|
build_builtin_function ($1, $4, 0, current_storage);
|
2011-01-17 13:33:33 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
subprogram_head
|
|
|
|
: FUNCTION ID arguments ':' standard_type
|
2011-01-06 11:27:32 +00:00
|
|
|
{
|
2011-01-13 05:58:16 +00:00
|
|
|
$$ = $2;
|
|
|
|
if ($$->table == current_symtab) {
|
|
|
|
error (0, "%s redefined", $$->name);
|
|
|
|
} else {
|
|
|
|
$$->params = $3;
|
|
|
|
$$->type = parse_params ($5, $3);
|
2012-08-06 23:10:29 +00:00
|
|
|
$$->type = find_type ($$->type);
|
2011-01-18 03:37:12 +00:00
|
|
|
$$ = function_symbol ($$, 0, 1);
|
2011-01-13 05:58:16 +00:00
|
|
|
}
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
| PROCEDURE ID arguments
|
2011-01-06 11:27:32 +00:00
|
|
|
{
|
2011-01-13 05:58:16 +00:00
|
|
|
$$ = $2;
|
|
|
|
if ($$->table == current_symtab) {
|
|
|
|
error (0, "%s redefined", $$->name);
|
|
|
|
} else {
|
|
|
|
$$->params = $3;
|
|
|
|
$$->type = parse_params (&type_void, $3);
|
2012-08-06 23:10:29 +00:00
|
|
|
$$->type = find_type ($$->type);
|
2011-01-18 03:37:12 +00:00
|
|
|
$$ = function_symbol ($$, 0, 1);
|
2011-01-13 05:58:16 +00:00
|
|
|
}
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
arguments
|
2011-01-30 12:42:10 +00:00
|
|
|
: '(' param_scope parameter_list ')'
|
|
|
|
{
|
|
|
|
$$ = $3;
|
|
|
|
current_symtab = $2;
|
|
|
|
}
|
|
|
|
| '(' param_scope parameter_list ';' ELLIPSIS ')'
|
2011-01-12 14:39:59 +00:00
|
|
|
{
|
2011-01-30 12:42:10 +00:00
|
|
|
$$ = param_append_identifiers ($3, 0, 0);
|
|
|
|
current_symtab = $2;
|
2011-01-12 14:39:59 +00:00
|
|
|
}
|
2011-01-13 05:58:16 +00:00
|
|
|
| '(' ELLIPSIS ')' { $$ = new_param (0, 0, 0); }
|
|
|
|
| /* emtpy */ { $$ = 0; }
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
2011-01-30 12:42:10 +00:00
|
|
|
param_scope
|
|
|
|
: /* empty */
|
|
|
|
{
|
|
|
|
$$ = current_symtab;
|
|
|
|
current_symtab = new_symtab (current_symtab, stab_local);
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2011-01-06 07:32:53 +00:00
|
|
|
parameter_list
|
|
|
|
: identifier_list ':' type
|
2011-01-06 11:27:32 +00:00
|
|
|
{
|
2011-01-13 05:58:16 +00:00
|
|
|
$$ = param_append_identifiers (0, $1, $3);
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
| parameter_list ';' identifier_list ':' type
|
2011-01-06 11:27:32 +00:00
|
|
|
{
|
2011-01-13 05:58:16 +00:00
|
|
|
$$ = param_append_identifiers ($1, $3, $5);
|
2011-01-06 11:27:32 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
compound_statement
|
2011-01-13 05:58:16 +00:00
|
|
|
: PBEGIN optional_statements END { $$ = $2; }
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
optional_statements
|
2011-01-13 05:58:16 +00:00
|
|
|
: statement_list opt_semi
|
|
|
|
| /* emtpy */ { $$ = 0; }
|
|
|
|
;
|
|
|
|
|
|
|
|
opt_semi
|
|
|
|
: ';'
|
|
|
|
| /* empty */
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
statement_list
|
|
|
|
: statement
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
|
|
|
$$ = new_block_expr ();
|
|
|
|
append_expr ($$, $1);
|
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
| statement_list ';' statement
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
|
|
|
$$ = $1;
|
|
|
|
append_expr ($$, $3);
|
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
statement
|
|
|
|
: variable ASSIGNOP expression
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
2011-01-13 05:58:16 +00:00
|
|
|
$$ = $1;
|
2022-02-01 07:08:58 +00:00
|
|
|
if ($$->type == ex_symbol && $$->e.symbol->sy_type == sy_func) {
|
|
|
|
if ($$->e.symbol->s.func != current_func) {
|
|
|
|
$$ = error ($$, "cannot assign to other function");
|
|
|
|
} else {
|
|
|
|
symbol_t *ret = function_value (current_func);
|
|
|
|
if (!ret) {
|
|
|
|
$$ = error ($$, "cannot assign to procedure");
|
|
|
|
} else {
|
|
|
|
$$ = new_symbol_expr (ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-13 05:58:16 +00:00
|
|
|
$$ = assign_expr ($$, $3);
|
2011-01-06 14:13:18 +00:00
|
|
|
}
|
2011-01-13 05:58:16 +00:00
|
|
|
| procedure_statement
|
|
|
|
| compound_statement
|
2012-12-01 07:37:38 +00:00
|
|
|
| IF expression THEN statement else statement
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
2012-12-20 01:02:00 +00:00
|
|
|
$$ = build_if_statement (0, $2, $4, $5, $6);
|
2011-01-06 14:13:18 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
| IF expression THEN statement %prec IFX
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
2012-12-20 01:02:00 +00:00
|
|
|
$$ = build_if_statement (0, $2, $4, 0, 0);
|
2011-01-06 14:13:18 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
| WHILE expression DO statement
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
2013-06-25 01:46:51 +00:00
|
|
|
$$ = build_while_statement (0, $2, $4,
|
2011-01-17 13:33:33 +00:00
|
|
|
new_label_expr (),
|
|
|
|
new_label_expr ());
|
2011-01-06 14:13:18 +00:00
|
|
|
}
|
2011-01-30 13:16:59 +00:00
|
|
|
| RETURN
|
|
|
|
{
|
2022-02-01 07:08:58 +00:00
|
|
|
$$ = return_expr (current_func, function_return (current_func));
|
2011-01-30 13:16:59 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
2012-12-01 07:37:38 +00:00
|
|
|
else
|
|
|
|
: ELSE
|
|
|
|
{
|
2020-03-20 02:48:01 +00:00
|
|
|
// this is only to get the the file and line number info
|
2012-12-01 07:37:38 +00:00
|
|
|
$$ = new_nil_expr ();
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2011-01-06 07:32:53 +00:00
|
|
|
variable
|
2011-01-13 05:58:16 +00:00
|
|
|
: name
|
|
|
|
| name '[' expression ']' { $$ = array_expr ($1, $3); }
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
procedure_statement
|
2011-01-13 05:58:16 +00:00
|
|
|
: name { $$ = function_expr ($1, 0); }
|
|
|
|
| name '(' expression_list ')' { $$ = function_expr ($1, $3); }
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
expression_list
|
|
|
|
: expression
|
|
|
|
| expression_list ',' expression
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
|
|
|
$$ = $3;
|
2011-01-13 05:58:16 +00:00
|
|
|
$$->next = $1;
|
2011-01-06 14:13:18 +00:00
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
unary_expr
|
|
|
|
: primary
|
2011-01-13 05:58:16 +00:00
|
|
|
| sign unary_expr %prec UNARY { $$ = unary_expr ($1, $2); }
|
|
|
|
| NOT expression %prec UNARY { $$ = unary_expr ('!', $2); }
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
primary
|
|
|
|
: variable
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
|
|
|
$$ = $1;
|
2011-01-13 05:58:16 +00:00
|
|
|
if ($$->type == ex_symbol && extract_type ($$) == ev_func)
|
|
|
|
$$ = function_expr ($$, 0);
|
2011-01-06 14:13:18 +00:00
|
|
|
}
|
2012-12-18 04:59:45 +00:00
|
|
|
| VALUE
|
2011-01-13 05:58:16 +00:00
|
|
|
| name '(' expression_list ')' { $$ = function_expr ($1, $3); }
|
|
|
|
| '(' expression ')' { $$ = $2; }
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
expression
|
|
|
|
: unary_expr
|
2011-01-13 05:58:16 +00:00
|
|
|
| expression RELOP expression { $$ = binary_expr ($2, $1, $3); }
|
2011-01-06 07:32:53 +00:00
|
|
|
| expression ADDOP expression
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
|
|
|
if ($2 == 'o')
|
|
|
|
$$ = bool_expr (OR, new_label_expr (), $1, $3);
|
|
|
|
else
|
|
|
|
$$ = binary_expr ($2, $1, $3);
|
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
| expression MULOP expression
|
2011-01-06 14:13:18 +00:00
|
|
|
{
|
|
|
|
if ($2 == 'd')
|
|
|
|
$2 = '/';
|
|
|
|
else if ($2 == 'm')
|
|
|
|
$2 = '%';
|
|
|
|
if ($2 == 'a')
|
|
|
|
$$ = bool_expr (AND, new_label_expr (), $1, $3);
|
|
|
|
else
|
|
|
|
$$ = binary_expr ($2, $1, $3);
|
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
sign
|
|
|
|
: ADDOP
|
|
|
|
{
|
2011-01-13 05:58:16 +00:00
|
|
|
if ($$ == 'o')
|
2011-01-06 07:32:53 +00:00
|
|
|
PARSE_ERROR;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2011-01-06 14:13:18 +00:00
|
|
|
name
|
2011-01-17 13:33:33 +00:00
|
|
|
: ID
|
|
|
|
{
|
|
|
|
if (!$1->table) {
|
|
|
|
error (0, "%s undefined", $1->name);
|
2022-01-18 04:21:06 +00:00
|
|
|
$1->type = &type_int;
|
2011-01-17 13:33:33 +00:00
|
|
|
symtab_addsymbol (current_symtab, $1);
|
|
|
|
}
|
|
|
|
$$ = new_symbol_expr ($1);
|
|
|
|
}
|
2011-01-06 07:32:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|