blob: 82af4bad56ecab23eaf693ad1516373fd37ebcfe [file] [log] [blame]
Gavin Howard5715b042018-02-12 16:11:42 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard5715b042018-02-12 16:11:42 -07003 *
Gavin Howardb5904bf2018-02-20 13:28:18 -07004 * Copyright 2018 Gavin D. Howard
Gavin Howard5715b042018-02-12 16:11:42 -07005 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070017 * *****************************************************************************
Gavin Howard5715b042018-02-12 16:11:42 -070018 *
19 * Definitions for bc programs.
20 *
21 */
22
Gavin Howard8a596d42018-01-15 15:46:01 -070023#ifndef BC_PROGRAM_H
24#define BC_PROGRAM_H
25
26#include <stdbool.h>
27#include <stdint.h>
28
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070029#include <bc.h>
Gavin Howardd79b7a12018-03-10 19:49:52 -070030#include <lang.h>
Gavin Howard364287c2018-01-30 16:14:44 -070031
Gavin Howard5a94ef62018-01-30 13:35:00 -070032#define BC_PROGRAM_BUF_SIZE (1024)
33
Gavin Howard8a596d42018-01-15 15:46:01 -070034typedef struct BcProgram {
35
Gavin Howard5a8949e2018-02-05 16:46:06 -070036 BcVec ip_stack;
Gavin Howard4dc42662018-01-17 15:17:47 -070037
Gavin Howardb5c77212018-02-14 17:12:34 -070038 size_t scale;
Gavin Howard5d74e962018-02-26 13:44:13 -070039
40 BcNum ibase;
41 size_t ibase_t;
42 BcNum obase;
43 size_t obase_t;
Gavin Howard6fc77692018-01-24 18:05:21 -070044
Gavin Howard4bc73ee2018-01-26 11:39:20 -070045 long base_max;
46 long dim_max;
47 long scale_max;
48 long string_max;
Gavin Howard88f39002018-01-24 16:53:07 -070049
Gavin Howard2534a812018-02-10 15:34:15 -070050 BcVec expr_stack;
51
Gavin Howard602178d2018-02-09 12:03:28 -070052 BcVec stack;
Gavin Howard4dc42662018-01-17 15:17:47 -070053
Gavin Howard23fa3f32018-02-05 14:45:39 -070054 BcVec funcs;
Gavin Howard8a596d42018-01-15 15:46:01 -070055
Gavin Howardd96bcae2018-02-07 19:16:19 -070056 BcVecO func_map;
57
Gavin Howard23fa3f32018-02-05 14:45:39 -070058 BcVec vars;
Gavin Howard8a596d42018-01-15 15:46:01 -070059
Gavin Howardd96bcae2018-02-07 19:16:19 -070060 BcVecO var_map;
61
Gavin Howard23fa3f32018-02-05 14:45:39 -070062 BcVec arrays;
Gavin Howard8a596d42018-01-15 15:46:01 -070063
Gavin Howardd96bcae2018-02-07 19:16:19 -070064 BcVecO array_map;
65
Gavin Howard5a8949e2018-02-05 16:46:06 -070066 BcVec strings;
67
Gavin Howarde46c6822018-02-08 20:05:39 -070068 BcVec constants;
69
Gavin Howard8d1f1db2018-02-23 11:29:41 -070070 const char *file;
Gavin Howard4dc42662018-01-17 15:17:47 -070071
Gavin Howardb5c77212018-02-14 17:12:34 -070072 BcNum last;
Gavin Howard5a94ef62018-01-30 13:35:00 -070073
Gavin Howardb5c77212018-02-14 17:12:34 -070074 BcNum zero;
75 BcNum one;
Gavin Howard361bfd32018-01-29 15:24:44 -070076
Gavin Howard8d1f1db2018-02-23 11:29:41 -070077 char *num_buf;
Gavin Howard364287c2018-01-30 16:14:44 -070078 size_t buf_size;
79
Gavin Howard8a596d42018-01-15 15:46:01 -070080} BcProgram;
81
Gavin Howard195706a2018-02-26 17:45:58 -070082#define BC_PROGRAM_CHECK_STACK(p) ((p)->stack.len > 1)
Gavin Howarda883ad92018-02-22 13:34:41 -070083#define BC_PROGRAM_CHECK_EXPR_STACK(p, l) ((p)->expr_stack.len >= (l))
84
Gavin Howard6fb635f2018-03-03 12:45:42 -070085#define BC_PROGRAM_MAIN_FUNC (0)
Gavin Howarded392aa2018-02-27 13:09:26 -070086#define BC_PROGRAM_READ_FUNC (1)
87
Gavin Howardf23448c2018-02-27 20:36:24 -070088#define BC_PROGRAM_SEARCH_VAR (1<<0)
89#define BC_PROGRAM_SEARCH_ARRAY_ONLY (1<<1)
90
Gavin Howard9bb13e22018-02-27 17:01:42 -070091typedef BcStatus (*BcProgramExecFunc)(BcProgram*);
92typedef unsigned long (*BcProgramBuiltInFunc)(BcNum*);
93typedef void (*BcNumInitFunc)(BcNum*);
Gavin Howarde9a135e2018-02-21 18:59:02 -070094
Gavin Howardd75aaec2018-03-10 20:33:39 -070095// ** Exclude start. **
Gavin Howard8d1f1db2018-02-23 11:29:41 -070096BcStatus bc_program_init(BcProgram *p);
97void bc_program_limits(BcProgram *p);
Gavin Howard8d1f1db2018-02-23 11:29:41 -070098BcStatus bc_program_var_add(BcProgram *p, char *name, size_t *idx);
99BcStatus bc_program_array_add(BcProgram *p, char *name, size_t *idx);
Gavin Howard67c38522018-02-27 16:00:49 -0700100BcStatus bc_program_print(BcProgram *p);
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700101void bc_program_free(BcProgram *program);
Gavin Howardd75aaec2018-03-10 20:33:39 -0700102// ** Exclude end. **
103
104BcStatus bc_program_func_add(BcProgram *p, char *name, size_t *idx);
105BcStatus bc_program_exec(BcProgram *p);
Gavin Howard8a596d42018-01-15 15:46:01 -0700106
Gavin Howardf456d372018-03-10 20:11:41 -0700107extern const char *bc_program_byte_fmt;
108extern const BcNumBinaryFunc bc_program_math_ops[];
109extern const char *bc_program_stdin_name;
110extern const char *bc_program_ready_prompt;
111extern const char *bc_program_sigint_msg;
112
Gavin Howard8a596d42018-01-15 15:46:01 -0700113#endif // BC_PROGRAM_H