diff options
| author | Stefan Eßer <se@FreeBSD.org> | 2023-01-28 19:59:18 +0000 |
|---|---|---|
| committer | Stefan Eßer <se@FreeBSD.org> | 2023-01-28 19:59:18 +0000 |
| commit | 0b671e8cf134e605567a6b8091958c1f1dfc5140 (patch) | |
| tree | e17587523032ecead9edbb65df0136e1d0d09a38 /include | |
| parent | 9471e6a0958eaea36dd38589b6a5d186e5a1fbf9 (diff) | |
Diffstat (limited to 'include')
| -rw-r--r-- | include/bc.h | 4 | ||||
| -rw-r--r-- | include/lang.h | 19 | ||||
| -rw-r--r-- | include/lex.h | 26 | ||||
| -rw-r--r-- | include/parse.h | 24 | ||||
| -rw-r--r-- | include/program.h | 50 | ||||
| -rw-r--r-- | include/status.h | 16 | ||||
| -rw-r--r-- | include/vector.h | 24 | ||||
| -rw-r--r-- | include/version.h | 2 | ||||
| -rw-r--r-- | include/vm.h | 27 |
9 files changed, 82 insertions, 110 deletions
diff --git a/include/bc.h b/include/bc.h index 06b2131c967b..5e879e83a987 100644 --- a/include/bc.h +++ b/include/bc.h @@ -97,13 +97,13 @@ typedef struct BcLexKeyword /// A macro for the number of keywords bc has. This has to be updated if any are /// added. This is for the redefined_kws field of the BcVm struct. -#define BC_LEX_NKWS (35) +#define BC_LEX_NKWS (37) #else // BC_ENABLE_EXTRA_MATH /// A macro for the number of keywords bc has. This has to be updated if any are /// added. This is for the redefined_kws field of the BcVm struct. -#define BC_LEX_NKWS (31) +#define BC_LEX_NKWS (33) #endif // BC_ENABLE_EXTRA_MATH diff --git a/include/lang.h b/include/lang.h index f7356c412396..396fc8a4b34c 100644 --- a/include/lang.h +++ b/include/lang.h @@ -178,6 +178,8 @@ typedef enum BcInst BC_INST_SCALE_FUNC, BC_INST_SQRT, BC_INST_ABS, + BC_INST_IS_NUMBER, + BC_INST_IS_STRING, #if BC_ENABLE_EXTRA_MATH /// Another builtin function. @@ -392,12 +394,6 @@ typedef struct BcFunc #endif // BC_ENABLED - /// The strings encountered in the function. - BcVec strs; - - /// The constants encountered in the function. - BcVec consts; - /// The function's name. const char* name; @@ -660,17 +656,6 @@ bc_result_free(void* result); void bc_array_expand(BcVec* a, size_t len); -/** - * Compare two BcId's and return the result. Since they are just comparing the - * names in the BcId, I return the result from strcmp() exactly. This is used by - * maps in their binary search. - * @param e1 The first id. - * @param e2 The second id. - * @return The result of strcmp() on the BcId's names. - */ -int -bc_id_cmp(const BcId* e1, const BcId* e2); - #if BC_ENABLED /** diff --git a/include/lex.h b/include/lex.h index 4f08b45d623f..160c0f114855 100644 --- a/include/lex.h +++ b/include/lex.h @@ -317,6 +317,12 @@ typedef enum BcLexType /// bc abs keyword. BC_LEX_KW_ABS, + /// bc is_number keyword. + BC_LEX_KW_IS_NUMBER, + + /// bc is_string keyword. + BC_LEX_KW_IS_STRING, + #if BC_ENABLE_EXTRA_MATH /// bc irand keyword. @@ -494,14 +500,8 @@ typedef struct BcLex /// string. BcVec str; - /// If this is true, the lexer is processing stdin and can ask for more data - /// if a string or comment are not properly terminated. - bool is_stdin; - - /// If this is true, the lexer is processing expressions from the - /// command-line and can ask for more data if a string or comment are not - /// properly terminated. - bool is_exprs; + /// The mode the lexer is in. + BcMode mode; } BcLex; @@ -531,14 +531,12 @@ bc_lex_file(BcLex* l, const char* file); /** * Sets the text the lexer will lex. - * @param l The lexer. - * @param text The text to lex. - * @param is_stdin True if the text is from stdin, false otherwise. - * @param is_exprs True if the text is from command-line expressions, false - * otherwise. + * @param l The lexer. + * @param text The text to lex. + * @param mode The mode to lex in. */ void -bc_lex_text(BcLex* l, const char* text, bool is_stdin, bool is_exprs); +bc_lex_text(BcLex* l, const char* text, BcMode mode); /** * Generic next function for the parser to call. It takes care of calling the diff --git a/include/parse.h b/include/parse.h index e692462395ca..ebf234c5f7f9 100644 --- a/include/parse.h +++ b/include/parse.h @@ -80,18 +80,6 @@ */ #define BC_PARSE_IS_INITED(p, prg) ((p)->prog == (prg)) -#if BC_ENABLED - -/** - * Returns true if the current parser state allows parsing, false otherwise. - * @param p The parser. - * @return True if parsing can proceed, false otherwise. - */ -#define BC_PARSE_CAN_PARSE(p) \ - ((p).l.t != BC_LEX_EOF && (p).l.t != BC_LEX_KW_DEFINE) - -#else // BC_ENABLED - /** * Returns true if the current parser state allows parsing, false otherwise. * @param p The parser. @@ -99,8 +87,6 @@ */ #define BC_PARSE_CAN_PARSE(p) ((p).l.t != BC_LEX_EOF) -#endif // BC_ENABLED - /** * Pushes the instruction @a i onto the bytecode vector for the current * function. @@ -268,14 +254,12 @@ bc_parse_pushName(const BcParse* p, char* name, bool var); /** * Sets the text that the parser will parse. - * @param p The parser. - * @param text The text to lex. - * @param is_stdin True if the text is from stdin, false otherwise. - * @param is_exprs True if the text is from command-line expressions, false - * otherwise. + * @param p The parser. + * @param text The text to lex. + * @param mode The mode to parse in. */ void -bc_parse_text(BcParse* p, const char* text, bool is_stdin, bool is_exprs); +bc_parse_text(BcParse* p, const char* text, BcMode mode); // References to const 0 and 1 strings for special cases. bc and dc have // specific instructions for 0 and 1 because they pop up so often and (in the diff --git a/include/program.h b/include/program.h index 3eaf568d66ac..62e867eb7fb8 100644 --- a/include/program.h +++ b/include/program.h @@ -87,11 +87,21 @@ typedef struct BcProgram /// The execution stack. BcVec stack; - /// A pointer to the current function's constants. - BcVec* consts; + /// The constants encountered in the program. They are global to the program + /// to prevent bad accesses when functions that used non-auto variables are + /// replaced. + BcVec consts; - /// A pointer to the current function's strings. - BcVec* strs; + /// The map of constants to go with consts. + BcVec const_map; + + /// The strings encountered in the program. They are global to the program + /// to prevent bad accesses when functions that used non-auto variables are + /// replaced. + BcVec strs; + + /// The map of strings to go with strs. + BcVec str_map; /// The array of functions. BcVec fns; @@ -343,22 +353,22 @@ bc_program_printStackDebug(BcProgram* p); /** * Returns the index of the variable or array in their respective arrays. - * @param p The program. - * @param id The BcId of the variable or array. - * @param var True if the search should be for a variable, false for an array. - * @return The index of the variable or array in the correct array. + * @param p The program. + * @param name The name of the variable or array. + * @param var True if the search should be for a variable, false for an array. + * @return The index of the variable or array in the correct array. */ size_t -bc_program_search(BcProgram* p, const char* id, bool var); +bc_program_search(BcProgram* p, const char* name, bool var); /** - * Adds a string to a function and returns the string's index in the function. - * @param p The program. - * @param str The string to add. - * @param fidx The index of the function to add to. + * Adds a string to the program and returns the string's index in the program. + * @param p The program. + * @param str The string to add. + * @return The string's index in the program. */ size_t -bc_program_addString(BcProgram* p, const char* str, size_t fidx); +bc_program_addString(BcProgram* p, const char* str); /** * Inserts a function into the program and returns the index of the function in @@ -571,6 +581,8 @@ extern const char bc_program_esc_seqs[]; &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ + &&lbl_BC_INST_IS_NUMBER, \ + &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_IRAND, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ @@ -665,6 +677,8 @@ extern const char bc_program_esc_seqs[]; &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ + &&lbl_BC_INST_IS_NUMBER, \ + &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_MAXIBASE, \ @@ -771,6 +785,8 @@ extern const char bc_program_esc_seqs[]; &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ + &&lbl_BC_INST_IS_NUMBER, \ + &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_IRAND, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ @@ -851,6 +867,8 @@ extern const char bc_program_esc_seqs[]; &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ + &&lbl_BC_INST_IS_NUMBER, \ + &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_MAXIBASE, \ @@ -923,6 +941,8 @@ extern const char bc_program_esc_seqs[]; &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ + &&lbl_BC_INST_IS_NUMBER, \ + &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_IRAND, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ @@ -992,6 +1012,8 @@ extern const char bc_program_esc_seqs[]; &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ + &&lbl_BC_INST_IS_NUMBER, \ + &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_MAXIBASE, \ diff --git a/include/status.h b/include/status.h index f478beb1a2d5..ce6251ab0c1d 100644 --- a/include/status.h +++ b/include/status.h @@ -658,6 +658,22 @@ typedef enum BcErr #endif // BC_ENABLED +/** + * The mode bc is in. This is basically what input it is processing. + */ +typedef enum BcMode +{ + /// Expressions mode. + BC_MODE_EXPRS, + + /// File mode. + BC_MODE_FILE, + + /// stdin mode. + BC_MODE_STDIN, + +} BcMode; + /// Do a longjmp(). This is what to use when activating an "exception", i.e., a /// longjmp(). With debug code, it will print the name of the function it jumped /// from. diff --git a/include/vector.h b/include/vector.h index 539b8a1ac292..bf79d30c36dd 100644 --- a/include/vector.h +++ b/include/vector.h @@ -59,9 +59,6 @@ typedef unsigned char uchar; */ typedef void (*BcVecFree)(void* ptr); -// Forward declaration. -struct BcId; - #if BC_LONG_BIT >= 64 /// An integer to shrink the size of a vector by using these instead of size_t. @@ -322,7 +319,7 @@ void bc_vec_free(void* vec); /** - * Attempts to insert an item into a map and returns true if it succeeded, false + * Attempts to insert an ID into a map and returns true if it succeeded, false * if the item already exists. * @param v The map vector to insert into. * @param name The name of the item to insert. This name is assumed to be owned @@ -449,25 +446,6 @@ bc_slabvec_print(BcVec* v, const char* func); /// A convenience macro for freeing a vector of slabs. #define bc_slabvec_free bc_vec_free -#if BC_ENABLED -#if DC_ENABLED - -/// Returns the set of slabs for the maps and the current calculator. -#define BC_VEC_MAP_SLABS (BC_IS_DC ? &vm->main_slabs : &vm->other_slabs) - -#else // DC_ENABLED - -/// Returns the set of slabs for the maps and the current calculator. -#define BC_VEC_MAP_SLABS (&vm->other_slabs) - -#endif // DC_ENABLED -#else // BC_ENABLED - -/// Returns the set of slabs for the maps and the current calculator. -#define BC_VEC_MAP_SLABS (&vm->main_slabs) - -#endif // BC_ENABLED - #ifndef _WIN32 /** diff --git a/include/version.h b/include/version.h index 4fbbe3c2acf4..2a3e6aa0f34c 100644 --- a/include/version.h +++ b/include/version.h @@ -37,6 +37,6 @@ #define BC_VERSION_H /// The current version. -#define VERSION 6.0.4 +#define VERSION 6.1.0 #endif // BC_VERSION_H diff --git a/include/vm.h b/include/vm.h index c800e476e228..44212eb03598 100644 --- a/include/vm.h +++ b/include/vm.h @@ -628,12 +628,8 @@ typedef struct BcVm /// True if EOF was encountered. bool eof; - /// True if bc is currently reading from stdin. - bool is_stdin; - - /// True if bc should clear its buffers. This is BcVm to fill a hole and - /// also to avoid clobber warnings from GCC. - bool clear; + /// The mode that the program is in. + uchar mode; #if BC_ENABLED @@ -759,16 +755,9 @@ typedef struct BcVm /// The number of items in the input buffer. size_t buf_len; - /// The slab for constants in the main function. This is separate for - /// garbage collection reasons. - BcVec main_const_slab; - - //// The slab for all other strings for the main function. - BcVec main_slabs; - - /// The slab for function names, strings in other functions, and constants - /// in other functions. - BcVec other_slabs; + /// The slabs vector for constants, strings, function names, and other + /// string-like things. + BcVec slabs; #if BC_ENABLED @@ -846,7 +835,7 @@ bc_vm_getTemp(void); void bc_vm_freeTemps(void); -#if !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB +#if !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY /** * Erases the flush argument if history does not exist because it does not @@ -854,12 +843,12 @@ bc_vm_freeTemps(void); */ #define bc_vm_putchar(c, t) bc_vm_putchar_impl(c) -#else // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB +#else // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY // This is here to satisfy a clang warning about recursive macros. #define bc_vm_putchar(c, t) bc_vm_putchar_impl(c, t) -#endif // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB +#endif // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY /** * Print to stdout with limited formating. |
