blob: dba6db4b43a1eb89779c3f7f7e6889cd1ebdacb4 [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 Howard707a4e32018-10-03 16:36:41 -060026#include <stdbool.h>
27
Gavin Howard29493062018-03-20 19:57:37 -060028#include <status.h>
Gavin Howard707a4e32018-10-03 16:36:41 -060029#include <lex.h>
30#include <parse.h>
Gavin Howard6839e4d2018-03-20 21:24:52 -060031
Gavin Howard40a085f2018-12-03 12:08:59 -070032#if BC_ENABLED
Gavin Howarddf4fe792018-10-03 16:48:30 -060033
Gavin Howardeb9a8222018-03-13 09:25:56 -060034// ** Exclude start. **
Gavin Howard773c86b2018-11-02 14:07:19 -060035// ** Busybox exclude start. **
Gavin Howarda84ad992018-12-03 19:11:06 -070036int bc_main(int argc, char **argv);
37
Gavin Howardd5551672018-09-22 19:52:42 -060038extern const char bc_help[];
Gavin Howard773c86b2018-11-02 14:07:19 -060039// ** Busybox exclude end. **
Gavin Howard707a4e32018-10-03 16:36:41 -060040// ** Exclude end. **
41
42typedef struct BcLexKeyword {
Gavin Howard707a4e32018-10-03 16:36:41 -060043 const char name[9];
Gavin Howard954f9b62018-12-19 15:22:20 -070044 const uchar len;
Gavin Howard707a4e32018-10-03 16:36:41 -060045 const bool posix;
Gavin Howard707a4e32018-10-03 16:36:41 -060046} BcLexKeyword;
47
48#define BC_LEX_KW_ENTRY(a, b, c) { .name = a, .len = (b), .posix = (c) }
49
50extern const BcLexKeyword bc_lex_kws[20];
51
52BcStatus bc_lex_token(BcLex *l);
53
Gavin Howard890d0c02018-10-30 16:34:50 -060054#define BC_PARSE_TOP_OP(p) (*((BcLexType*) bc_vec_top(&(p)->ops)))
Gavin Howard707a4e32018-10-03 16:36:41 -060055#define BC_PARSE_LEAF(p, rparen) \
56 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
57 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
58
59// We can calculate the conversion between tokens and exprs by subtracting the
Gavin Howard954f9b62018-12-19 15:22:20 -070060// position of the first operator in the lex enum and adding the position of
61// the first in the expr enum. Note: This only works for binary operators.
62#define BC_PARSE_TOKEN_INST(t) ((uchar) ((t) - BC_LEX_NEG + BC_INST_NEG))
Gavin Howard707a4e32018-10-03 16:36:41 -060063
64// ** Exclude start. **
Gavin Howard773c86b2018-11-02 14:07:19 -060065// ** Busybox exclude start. **
Gavin Howardad0ecfe2018-10-30 01:16:01 -060066void bc_parse_init(BcParse *p, struct BcProgram *prog, size_t func);
Gavin Howard7536dcf2018-12-15 19:27:09 -070067BcStatus bc_parse_expr(BcParse *p, uint8_t flags);
Gavin Howard773c86b2018-11-02 14:07:19 -060068// ** Busybox exclude end. **
Gavin Howard707a4e32018-10-03 16:36:41 -060069// ** Exclude end. **
70
Gavin Howard9d2497d2018-10-29 16:10:21 -060071BcStatus bc_parse_parse(BcParse *p);
Gavin Howard7536dcf2018-12-15 19:27:09 -070072
73BcStatus bc_parse_expr_error(BcParse *p, uint8_t flags, BcParseNext next);
74BcStatus bc_parse_expr_status(BcParse *p, uint8_t flags, BcParseNext next);
75BcStatus bc_parse_else(BcParse *p);
76BcStatus bc_parse_stmt(BcParse *p);
Gavin Howardff66e292018-03-28 12:47:58 -060077
Gavin Howarda73c11b2018-12-17 11:30:39 -070078extern const char* const bc_parse_const1;
Gavin Howardf89006a2018-10-29 13:01:51 -060079extern const bool bc_parse_exprs[];
80extern const BcOp bc_parse_ops[];
81extern const BcParseNext bc_parse_next_expr;
82extern const BcParseNext bc_parse_next_param;
83extern const BcParseNext bc_parse_next_print;
84extern const BcParseNext bc_parse_next_rel;
85extern const BcParseNext bc_parse_next_elem;
86extern const BcParseNext bc_parse_next_for;
87extern const BcParseNext bc_parse_next_read;
88
Gavin Howarddf4fe792018-10-03 16:48:30 -060089#endif // BC_ENABLED
90
Gavin Howard4ffe5a92018-09-26 20:58:31 -060091#endif // BC_BC_H