blob: b2fee664fa6897f2242bb553ddbb0e97484203fe [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 *
Gavin Howard4ffe5a92018-09-26 20:58:31 -060019 * Definitions for bc.
Gavin Howard5715b042018-02-12 16:11:42 -070020 *
21 */
22
Gavin Howard4ffe5a92018-09-26 20:58:31 -060023#ifndef BC_BC_H
24#define BC_BC_H
Gavin Howard8a596d42018-01-15 15:46:01 -070025
Gavin Howardc1902aa2018-12-20 15:54:28 -070026#include <limits.h>
Gavin Howard707a4e32018-10-03 16:36:41 -060027#include <stdbool.h>
28
Gavin Howard29493062018-03-20 19:57:37 -060029#include <status.h>
Gavin Howard707a4e32018-10-03 16:36:41 -060030#include <lex.h>
31#include <parse.h>
Gavin Howard6839e4d2018-03-20 21:24:52 -060032
Gavin Howard40a085f2018-12-03 12:08:59 -070033#if BC_ENABLED
Gavin Howarddf4fe792018-10-03 16:48:30 -060034
Gavin Howardeb9a8222018-03-13 09:25:56 -060035// ** Exclude start. **
Gavin Howard773c86b2018-11-02 14:07:19 -060036// ** Busybox exclude start. **
Gavin Howarda84ad992018-12-03 19:11:06 -070037int bc_main(int argc, char **argv);
38
Gavin Howardd5551672018-09-22 19:52:42 -060039extern const char bc_help[];
Gavin Howard773c86b2018-11-02 14:07:19 -060040// ** Busybox exclude end. **
Gavin Howard707a4e32018-10-03 16:36:41 -060041// ** Exclude end. **
42
43typedef struct BcLexKeyword {
Gavin Howardc1902aa2018-12-20 15:54:28 -070044 uchar data;
Gavin Howard707a4e32018-10-03 16:36:41 -060045 const char name[9];
Gavin Howard707a4e32018-10-03 16:36:41 -060046} BcLexKeyword;
47
Gavin Howardc1902aa2018-12-20 15:54:28 -070048#define BC_LEX_KW_ENTRY(a, b, c) \
49 { .data = (b) & ~(BC_LEX_CHAR_MSB(1)) | BC_LEX_CHAR_MSB(c),.name = a }
Gavin Howard707a4e32018-10-03 16:36:41 -060050
51extern const BcLexKeyword bc_lex_kws[20];
52
53BcStatus bc_lex_token(BcLex *l);
54
Gavin Howard890d0c02018-10-30 16:34:50 -060055#define BC_PARSE_TOP_OP(p) (*((BcLexType*) bc_vec_top(&(p)->ops)))
Gavin Howard707a4e32018-10-03 16:36:41 -060056#define BC_PARSE_LEAF(p, rparen) \
57 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
58 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
59
60// We can calculate the conversion between tokens and exprs by subtracting the
Gavin Howard954f9b62018-12-19 15:22:20 -070061// position of the first operator in the lex enum and adding the position of
62// the first in the expr enum. Note: This only works for binary operators.
63#define BC_PARSE_TOKEN_INST(t) ((uchar) ((t) - BC_LEX_NEG + BC_INST_NEG))
Gavin Howard707a4e32018-10-03 16:36:41 -060064
65// ** Exclude start. **
Gavin Howard773c86b2018-11-02 14:07:19 -060066// ** Busybox exclude start. **
Gavin Howardad0ecfe2018-10-30 01:16:01 -060067void bc_parse_init(BcParse *p, struct BcProgram *prog, size_t func);
Gavin Howard7536dcf2018-12-15 19:27:09 -070068BcStatus bc_parse_expr(BcParse *p, uint8_t flags);
Gavin Howard773c86b2018-11-02 14:07:19 -060069// ** Busybox exclude end. **
Gavin Howard707a4e32018-10-03 16:36:41 -060070// ** Exclude end. **
71
Gavin Howard9d2497d2018-10-29 16:10:21 -060072BcStatus bc_parse_parse(BcParse *p);
Gavin Howard7536dcf2018-12-15 19:27:09 -070073
74BcStatus bc_parse_expr_error(BcParse *p, uint8_t flags, BcParseNext next);
75BcStatus bc_parse_expr_status(BcParse *p, uint8_t flags, BcParseNext next);
76BcStatus bc_parse_else(BcParse *p);
77BcStatus bc_parse_stmt(BcParse *p);
Gavin Howardff66e292018-03-28 12:47:58 -060078
Gavin Howard10667092018-12-20 11:52:55 -070079#if BC_ENABLE_SIGNALS
80extern const char bc_sig_msg[];
Gavin Howard10667092018-12-20 11:52:55 -070081#endif // BC_ENABLE_SIGNALS
82
Gavin Howarda73c11b2018-12-17 11:30:39 -070083extern const char* const bc_parse_const1;
Gavin Howardf89006a2018-10-29 13:01:51 -060084extern const bool bc_parse_exprs[];
Gavin Howardc1902aa2018-12-20 15:54:28 -070085extern const uchar bc_parse_ops[];
Gavin Howardf89006a2018-10-29 13:01:51 -060086extern const BcParseNext bc_parse_next_expr;
87extern const BcParseNext bc_parse_next_param;
88extern const BcParseNext bc_parse_next_print;
89extern const BcParseNext bc_parse_next_rel;
90extern const BcParseNext bc_parse_next_elem;
91extern const BcParseNext bc_parse_next_for;
92extern const BcParseNext bc_parse_next_read;
93
Gavin Howarddf4fe792018-10-03 16:48:30 -060094#endif // BC_ENABLED
95
Gavin Howard4ffe5a92018-09-26 20:58:31 -060096#endif // BC_BC_H