Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 5 | * |
| 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 Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 18 | * |
| 19 | * Definitions for bc programs. |
| 20 | * |
| 21 | */ |
| 22 | |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 23 | #ifndef BC_PROGRAM_H |
| 24 | #define BC_PROGRAM_H |
| 25 | |
| 26 | #include <stdbool.h> |
| 27 | #include <stdint.h> |
| 28 | |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 29 | #include <bc.h> |
Gavin Howard | d79b7a1 | 2018-03-10 19:49:52 -0700 | [diff] [blame] | 30 | #include <lang.h> |
Gavin Howard | 364287c | 2018-01-30 16:14:44 -0700 | [diff] [blame] | 31 | |
Gavin Howard | 5a94ef6 | 2018-01-30 13:35:00 -0700 | [diff] [blame] | 32 | #define BC_PROGRAM_BUF_SIZE (1024) |
| 33 | |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 34 | typedef struct BcProgram { |
| 35 | |
Gavin Howard | 5a8949e | 2018-02-05 16:46:06 -0700 | [diff] [blame] | 36 | BcVec ip_stack; |
Gavin Howard | 4dc4266 | 2018-01-17 15:17:47 -0700 | [diff] [blame] | 37 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 38 | size_t scale; |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 39 | |
| 40 | BcNum ibase; |
| 41 | size_t ibase_t; |
| 42 | BcNum obase; |
| 43 | size_t obase_t; |
Gavin Howard | 6fc7769 | 2018-01-24 18:05:21 -0700 | [diff] [blame] | 44 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 45 | long base_max; |
| 46 | long dim_max; |
| 47 | long scale_max; |
| 48 | long string_max; |
Gavin Howard | 88f3900 | 2018-01-24 16:53:07 -0700 | [diff] [blame] | 49 | |
Gavin Howard | 2534a81 | 2018-02-10 15:34:15 -0700 | [diff] [blame] | 50 | BcVec expr_stack; |
| 51 | |
Gavin Howard | 602178d | 2018-02-09 12:03:28 -0700 | [diff] [blame] | 52 | BcVec stack; |
Gavin Howard | 4dc4266 | 2018-01-17 15:17:47 -0700 | [diff] [blame] | 53 | |
Gavin Howard | 23fa3f3 | 2018-02-05 14:45:39 -0700 | [diff] [blame] | 54 | BcVec funcs; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 55 | |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 56 | BcVecO func_map; |
| 57 | |
Gavin Howard | 23fa3f3 | 2018-02-05 14:45:39 -0700 | [diff] [blame] | 58 | BcVec vars; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 59 | |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 60 | BcVecO var_map; |
| 61 | |
Gavin Howard | 23fa3f3 | 2018-02-05 14:45:39 -0700 | [diff] [blame] | 62 | BcVec arrays; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 63 | |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 64 | BcVecO array_map; |
| 65 | |
Gavin Howard | 5a8949e | 2018-02-05 16:46:06 -0700 | [diff] [blame] | 66 | BcVec strings; |
| 67 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 68 | BcVec constants; |
| 69 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 70 | const char *file; |
Gavin Howard | 4dc4266 | 2018-01-17 15:17:47 -0700 | [diff] [blame] | 71 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 72 | BcNum last; |
Gavin Howard | 5a94ef6 | 2018-01-30 13:35:00 -0700 | [diff] [blame] | 73 | |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 74 | BcNum zero; |
| 75 | BcNum one; |
Gavin Howard | 361bfd3 | 2018-01-29 15:24:44 -0700 | [diff] [blame] | 76 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 77 | char *num_buf; |
Gavin Howard | 364287c | 2018-01-30 16:14:44 -0700 | [diff] [blame] | 78 | size_t buf_size; |
| 79 | |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 80 | } BcProgram; |
| 81 | |
Gavin Howard | 195706a | 2018-02-26 17:45:58 -0700 | [diff] [blame] | 82 | #define BC_PROGRAM_CHECK_STACK(p) ((p)->stack.len > 1) |
Gavin Howard | a883ad9 | 2018-02-22 13:34:41 -0700 | [diff] [blame] | 83 | #define BC_PROGRAM_CHECK_EXPR_STACK(p, l) ((p)->expr_stack.len >= (l)) |
| 84 | |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 85 | #define BC_PROGRAM_MAIN_FUNC (0) |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 86 | #define BC_PROGRAM_READ_FUNC (1) |
| 87 | |
Gavin Howard | f23448c | 2018-02-27 20:36:24 -0700 | [diff] [blame] | 88 | #define BC_PROGRAM_SEARCH_VAR (1<<0) |
| 89 | #define BC_PROGRAM_SEARCH_ARRAY_ONLY (1<<1) |
| 90 | |
Gavin Howard | 9bb13e2 | 2018-02-27 17:01:42 -0700 | [diff] [blame] | 91 | typedef BcStatus (*BcProgramExecFunc)(BcProgram*); |
| 92 | typedef unsigned long (*BcProgramBuiltInFunc)(BcNum*); |
| 93 | typedef void (*BcNumInitFunc)(BcNum*); |
Gavin Howard | e9a135e | 2018-02-21 18:59:02 -0700 | [diff] [blame] | 94 | |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 95 | // ** Exclude start. ** |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 96 | BcStatus bc_program_init(BcProgram *p); |
| 97 | void bc_program_limits(BcProgram *p); |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 98 | BcStatus bc_program_var_add(BcProgram *p, char *name, size_t *idx); |
| 99 | BcStatus bc_program_array_add(BcProgram *p, char *name, size_t *idx); |
Gavin Howard | 67c3852 | 2018-02-27 16:00:49 -0700 | [diff] [blame] | 100 | BcStatus bc_program_print(BcProgram *p); |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 101 | void bc_program_free(BcProgram *program); |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 102 | // ** Exclude end. ** |
| 103 | |
| 104 | BcStatus bc_program_func_add(BcProgram *p, char *name, size_t *idx); |
| 105 | BcStatus bc_program_exec(BcProgram *p); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 106 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 107 | extern const char *bc_program_byte_fmt; |
| 108 | extern const BcNumBinaryFunc bc_program_math_ops[]; |
| 109 | extern const char *bc_program_stdin_name; |
| 110 | extern const char *bc_program_ready_prompt; |
| 111 | extern const char *bc_program_sigint_msg; |
| 112 | |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 113 | #endif // BC_PROGRAM_H |