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's parser. |
| 20 | * |
| 21 | */ |
| 22 | |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 23 | #ifndef BC_PARSE_H |
| 24 | #define BC_PARSE_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 <vector.h> |
| 30 | #include <program.h> |
| 31 | #include <lex.h> |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 32 | |
Gavin Howard | ac54c14 | 2018-01-16 01:48:45 -0700 | [diff] [blame] | 33 | #define BC_PARSE_TOP_FLAG_PTR(parse) \ |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 34 | ((uint8_t*) bc_vec_top(&(parse)->flags)) |
Gavin Howard | ac54c14 | 2018-01-16 01:48:45 -0700 | [diff] [blame] | 35 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 36 | #define BC_PARSE_TOP_FLAG(parse) \ |
| 37 | (*(BC_PARSE_TOP_FLAG_PTR(parse))) |
Gavin Howard | d133fb5 | 2018-01-16 14:44:40 -0700 | [diff] [blame] | 38 | |
Gavin Howard | 03d2795 | 2018-01-15 18:48:17 -0700 | [diff] [blame] | 39 | #define BC_PARSE_FLAG_FUNC_INNER (0x01) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 40 | |
| 41 | #define BC_PARSE_FUNC_INNER(parse) \ |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 42 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 43 | |
Gavin Howard | 03d2795 | 2018-01-15 18:48:17 -0700 | [diff] [blame] | 44 | #define BC_PARSE_FLAG_FUNC (0x02) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 45 | |
| 46 | #define BC_PARSE_FUNC(parse) \ |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 47 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 48 | |
Gavin Howard | 1e6793b | 2018-03-06 13:40:48 -0700 | [diff] [blame] | 49 | #define BC_PARSE_FLAG_BODY (0x04) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 50 | |
Gavin Howard | 1e6793b | 2018-03-06 13:40:48 -0700 | [diff] [blame] | 51 | #define BC_PARSE_BODY(parse) \ |
| 52 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 53 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 54 | #define BC_PARSE_FLAG_LOOP (0x08) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 55 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 56 | #define BC_PARSE_LOOP(parse) \ |
| 57 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 58 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 59 | #define BC_PARSE_FLAG_LOOP_INNER (0x10) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 60 | |
| 61 | #define BC_PARSE_LOOP_INNER(parse) \ |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 62 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 63 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 64 | #define BC_PARSE_FLAG_IF (0x20) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 65 | |
| 66 | #define BC_PARSE_IF(parse) \ |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 67 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 68 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 69 | #define BC_PARSE_FLAG_ELSE (0x40) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 70 | |
| 71 | #define BC_PARSE_ELSE(parse) \ |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 72 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 73 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 74 | #define BC_PARSE_FLAG_IF_END (0x80) |
| 75 | |
| 76 | #define BC_PARSE_IF_END(parse) \ |
| 77 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END) |
| 78 | |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 79 | #define BC_PARSE_CAN_EXEC(parse) \ |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 80 | (!(BC_PARSE_TOP_FLAG(parse) & (BC_PARSE_FLAG_FUNC_INNER | \ |
| 81 | BC_PARSE_FLAG_FUNC | \ |
Gavin Howard | 1e6793b | 2018-03-06 13:40:48 -0700 | [diff] [blame] | 82 | BC_PARSE_FLAG_BODY | \ |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 83 | BC_PARSE_FLAG_LOOP | \ |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 84 | BC_PARSE_FLAG_LOOP_INNER | \ |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 85 | BC_PARSE_FLAG_IF | \ |
| 86 | BC_PARSE_FLAG_ELSE | \ |
Gavin Howard | 1e6793b | 2018-03-06 13:40:48 -0700 | [diff] [blame] | 87 | BC_PARSE_FLAG_IF_END))) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 88 | |
| 89 | // We can calculate the conversion between tokens and exprs |
| 90 | // by subtracting the position of the first operator in the |
| 91 | // lex enum and adding the position of the first in the expr |
Gavin Howard | bde073f | 2018-02-26 17:48:36 -0700 | [diff] [blame] | 92 | // enum. Note: This only works for binary operators. |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 93 | #define BC_PARSE_TOKEN_TO_EXPR(type) ((type) - BC_LEX_OP_POWER + BC_EXPR_POWER) |
| 94 | |
| 95 | typedef struct BcOp { |
| 96 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 97 | uint8_t prec; |
| 98 | bool left; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 99 | |
| 100 | } BcOp; |
| 101 | |
| 102 | typedef struct BcParse { |
| 103 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 104 | BcLex lex; |
| 105 | BcLexToken token; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 106 | |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 107 | BcVec flags; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 108 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 109 | BcVec exit_labels; |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 110 | |
Gavin Howard | e46c682 | 2018-02-08 20:05:39 -0700 | [diff] [blame] | 111 | BcVec cond_labels; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 112 | |
Gavin Howard | 26a5aeb | 2018-02-05 14:33:40 -0700 | [diff] [blame] | 113 | BcVec ops; |
Gavin Howard | de38211 | 2018-01-16 20:53:57 -0700 | [diff] [blame] | 114 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 115 | BcProgram *program; |
Gavin Howard | d96bcae | 2018-02-07 19:16:19 -0700 | [diff] [blame] | 116 | size_t func; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 117 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 118 | uint32_t num_braces; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 119 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 120 | bool auto_part; |
Gavin Howard | ac54c14 | 2018-01-16 01:48:45 -0700 | [diff] [blame] | 121 | |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 122 | } BcParse; |
| 123 | |
Gavin Howard | b4d6049 | 2018-02-28 13:33:36 -0700 | [diff] [blame] | 124 | #define BC_PARSE_EXPR_POSIX_REL (1<<0) |
| 125 | #define BC_PARSE_EXPR_PRINT (1<<1) |
| 126 | #define BC_PARSE_EXPR_NO_CALL (1<<2) |
| 127 | #define BC_PARSE_EXPR_NO_READ (1<<3) |
| 128 | |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 129 | // ** Exclude start. ** |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 130 | BcStatus bc_parse_init(BcParse *parse, BcProgram *program); |
| 131 | BcStatus bc_parse_file(BcParse *parse, const char *file); |
| 132 | BcStatus bc_parse_text(BcParse *parse, const char *text); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 133 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 134 | BcStatus bc_parse_parse(BcParse *parse); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 135 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 136 | void bc_parse_free(BcParse *parse); |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 137 | // ** Exclude end. ** |
| 138 | |
| 139 | BcStatus bc_parse_expr(BcParse *parse, BcVec *code, uint8_t flags); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 140 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 141 | extern const bool bc_parse_token_exprs[]; |
| 142 | extern const BcOp bc_parse_ops[]; |
| 143 | extern const uint8_t bc_parse_insts[]; |
| 144 | |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 145 | #endif // BC_PARSE_H |