|
Packit |
70b277 |
/* This file is part of GNU bc.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Copyright (C) 1991-1994, 1997, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
This program is free software; you can redistribute it and/or modify
|
|
Packit |
70b277 |
it under the terms of the GNU General Public License as published by
|
|
Packit |
70b277 |
the Free Software Foundation; either version 3 of the License , or
|
|
Packit |
70b277 |
(at your option) any later version.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
This program is distributed in the hope that it will be useful,
|
|
Packit |
70b277 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
70b277 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
70b277 |
GNU General Public License for more details.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
You should have received a copy of the GNU General Public License
|
|
Packit |
70b277 |
along with this program; see the file COPYING. If not, see
|
|
Packit |
70b277 |
<http://www.gnu.org/licenses>.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
You may contact the author by:
|
|
Packit |
70b277 |
e-mail: philnelson@acm.org
|
|
Packit |
70b277 |
us-mail: Philip A. Nelson
|
|
Packit |
70b277 |
Computer Science Department, 9062
|
|
Packit |
70b277 |
Western Washington University
|
|
Packit |
70b277 |
Bellingham, WA 98226-9062
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
*************************************************************************/
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* bcdefs.h: The single file to include all constants and type definitions. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Include the configuration file. */
|
|
Packit |
70b277 |
#include "config.h"
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Standard includes for all files. */
|
|
Packit |
70b277 |
#include <stdio.h>
|
|
Packit |
70b277 |
#include <sys/types.h>
|
|
Packit |
70b277 |
#include <ctype.h>
|
|
Packit |
70b277 |
#ifdef HAVE_STRING_H
|
|
Packit |
70b277 |
#include <string.h>
|
|
Packit |
70b277 |
#else
|
|
Packit |
70b277 |
#include <strings.h>
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
#ifdef HAVE_LIMITS_H
|
|
Packit |
70b277 |
#include <limits.h>
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#if defined(LIBEDIT)
|
|
Packit |
70b277 |
#include <histedit.h>
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#if defined(READLINE)
|
|
Packit |
70b277 |
#include <readline/readline.h>
|
|
Packit |
70b277 |
#include <readline/history.h>
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Initialization magic ... */
|
|
Packit |
70b277 |
#ifdef _GLOBAL_C
|
|
Packit |
70b277 |
#define EXTERN
|
|
Packit |
70b277 |
#define INIT(x) = x
|
|
Packit |
70b277 |
#else
|
|
Packit |
70b277 |
#define EXTERN extern
|
|
Packit |
70b277 |
#define INIT(x)
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Include the other definitions. */
|
|
Packit |
70b277 |
#include "const.h"
|
|
Packit |
70b277 |
#include "number.h"
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* These definitions define all the structures used in
|
|
Packit |
70b277 |
code and data storage. This includes the representation of
|
|
Packit |
70b277 |
labels. The "guiding" principle is to make structures that
|
|
Packit |
70b277 |
take a minimum of space when unused but can be built to contain
|
|
Packit |
70b277 |
the full structures. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Labels are first. Labels are generated sequentially in functions
|
|
Packit |
70b277 |
and full code. They just "point" to a single bye in the code. The
|
|
Packit |
70b277 |
"address" is the byte number. The byte number is used to get an
|
|
Packit |
70b277 |
actual character pointer. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct bc_label_group
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
unsigned long l_adrs [ BC_LABEL_GROUP ];
|
|
Packit |
70b277 |
struct bc_label_group *l_next;
|
|
Packit |
70b277 |
} bc_label_group;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Argument list. Recorded in the function so arguments can
|
|
Packit |
70b277 |
be checked at call time. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct arg_list
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
int av_name;
|
|
Packit |
70b277 |
int arg_is_var; /* Extension ... variable parameters. */
|
|
Packit |
70b277 |
struct arg_list *next;
|
|
Packit |
70b277 |
} arg_list;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Each function has its own code segments and labels. There can be
|
|
Packit |
70b277 |
no jumps between functions so labels are unique to a function. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
char f_defined; /* Is this function defined yet. */
|
|
Packit |
70b277 |
char f_void; /* Is this function a void function. */
|
|
Packit |
70b277 |
char *f_body;
|
|
Packit |
70b277 |
size_t f_body_size; /* Size of body. Power of 2. */
|
|
Packit |
70b277 |
size_t f_code_size;
|
|
Packit |
70b277 |
bc_label_group *f_label;
|
|
Packit |
70b277 |
arg_list *f_params;
|
|
Packit |
70b277 |
arg_list *f_autos;
|
|
Packit |
70b277 |
} bc_function;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Code addresses. */
|
|
Packit |
70b277 |
typedef struct {
|
|
Packit |
70b277 |
unsigned int pc_func;
|
|
Packit |
70b277 |
unsigned int pc_addr;
|
|
Packit |
70b277 |
} program_counter;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Variables are "pushable" (auto) and thus we need a stack mechanism.
|
|
Packit |
70b277 |
This is built into the variable record. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct bc_var
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
bc_num v_value;
|
|
Packit |
70b277 |
struct bc_var *v_next;
|
|
Packit |
70b277 |
} bc_var;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* bc arrays can also be "auto" variables and thus need the same
|
|
Packit |
70b277 |
kind of stacking mechanisms. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct bc_array_node
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
union
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
bc_num n_num [NODE_SIZE];
|
|
Packit |
70b277 |
struct bc_array_node *n_down [NODE_SIZE];
|
|
Packit |
70b277 |
} n_items;
|
|
Packit |
70b277 |
} bc_array_node;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct bc_array
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
bc_array_node *a_tree;
|
|
Packit |
70b277 |
short a_depth;
|
|
Packit |
70b277 |
} bc_array;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct bc_var_array
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
bc_array *a_value;
|
|
Packit |
70b277 |
char a_param;
|
|
Packit |
70b277 |
struct bc_var_array *a_next;
|
|
Packit |
70b277 |
} bc_var_array;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* For the stacks, execution and function, we need records to allow
|
|
Packit |
70b277 |
for arbitrary size. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct estack_rec {
|
|
Packit |
70b277 |
bc_num s_num;
|
|
Packit |
70b277 |
struct estack_rec *s_next;
|
|
Packit |
70b277 |
} estack_rec;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct fstack_rec {
|
|
Packit |
70b277 |
int s_val;
|
|
Packit |
70b277 |
struct fstack_rec *s_next;
|
|
Packit |
70b277 |
} fstack_rec;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* The following are for the name tree. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct id_rec {
|
|
Packit |
70b277 |
char *id; /* The program name. */
|
|
Packit |
70b277 |
/* A name == 0 => nothing assigned yet. */
|
|
Packit |
70b277 |
int a_name; /* The array variable name (number). */
|
|
Packit |
70b277 |
int f_name; /* The function name (number). */
|
|
Packit |
70b277 |
int v_name; /* The variable name (number). */
|
|
Packit |
70b277 |
short balance; /* For the balanced tree. */
|
|
Packit |
70b277 |
struct id_rec *left, *right; /* Tree pointers. */
|
|
Packit |
70b277 |
} id_rec;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* A list of files to process. */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
typedef struct file_node {
|
|
Packit |
70b277 |
char *name;
|
|
Packit |
70b277 |
struct file_node *next;
|
|
Packit |
70b277 |
} file_node;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Macro Definitions */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#if defined(LIBEDIT)
|
|
Packit |
70b277 |
#define HISTORY_SIZE(n) history(hist, &histev, H_SETSIZE, n)
|
|
Packit |
70b277 |
#define UNLIMIT_HISTORY history(hist, &histev, H_SETSIZE, INT_MAX)
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#if defined(READLINE)
|
|
Packit |
70b277 |
#define HISTORY_SIZE(n) stifle_history(n)
|
|
Packit |
70b277 |
#define UNLIMIT_HISTORY unstifle_history()
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Now the global variable declarations. */
|
|
Packit |
70b277 |
#include "global.h"
|