Browse Source

Format all C code

Signed-off-by: Gavin Howard <gavin@yzena.com>
master
Gavin Howard 3 months ago
parent
commit
f147e34c8e
Signed by: gavin
GPG Key ID: C08038BDF280D33E
  1. 64
      gen/strgen.c
  2. 3
      include/args.h
  3. 65
      include/bc.h
  4. 258
      include/bcl.h
  5. 15
      include/dc.h
  6. 45
      include/file.h
  7. 58
      include/history.h
  8. 91
      include/lang.h
  9. 49
      include/lex.h
  10. 104
      include/library.h
  11. 157
      include/num.h
  12. 24
      include/opt.h
  13. 47
      include/parse.h
  14. 1044
      include/program.h
  15. 48
      include/rand.h
  16. 12
      include/read.h
  17. 106
      include/status.h
  18. 106
      include/vector.h
  19. 141
      include/vm.h
  20. 40
      scripts/bitfuncgen.c
  21. 56
      src/args.c
  22. 5
      src/bc.c
  23. 90
      src/bc_lex.c
  24. 692
      src/bc_parse.c
  25. 837
      src/data.c
  26. 5
      src/dc.c
  27. 70
      src/dc_lex.c
  28. 66
      src/dc_parse.c
  29. 152
      src/file.c
  30. 610
      src/history.c
  31. 120
      src/lang.c
  32. 145
      src/lex.c
  33. 393
      src/library.c
  34. 11
      src/main.c
  35. 1169
      src/num.c
  36. 142
      src/opt.c
  37. 85
      src/parse.c
  38. 1020
      src/program.c
  39. 192
      src/rand.c
  40. 77
      src/read.c
  41. 242
      src/vector.c
  42. 492
      src/vm.c
  43. 69
      tests/bcl.c

64
gen/strgen.c

@ -45,12 +45,14 @@
#include <libgen.h>
#endif // _WIN32
// clang-format off
// This is exactly what it looks like. It just slaps a simple license header on
// the generated C source file.
static const char* const bc_gen_header =
"// Copyright (c) 2018-2021 Gavin D. Howard and contributors.\n"
"// Licensed under the 2-clause BSD license.\n"
"// *** AUTOMATICALLY GENERATED FROM %s. DO NOT MODIFY. ***\n\n";
// clang-format on
// These are just format strings used to generate the C source.
static const char* const bc_gen_label = "const char *%s = \"%s\";\n\n";
@ -77,8 +79,9 @@ static const char* const bc_gen_name_extern = "extern const char %s[];\n\n";
* @param filename The name of the file.
* @param mode The mode to open the file in.
*/
static void open_file(FILE** f, const char* filename, const char* mode) {
static void
open_file(FILE** f, const char* filename, const char* mode)
{
#ifndef _WIN32
*f = fopen(filename, mode);
@ -112,8 +115,9 @@ static void open_file(FILE** f, const char* filename, const char* mode) {
* @param name The actual label text, which is a filename.
* @return Positive if no error, negative on error, just like *printf().
*/
static int output_label(FILE* out, const char* label, const char* name) {
static int
output_label(FILE* out, const char* label, const char* name)
{
#ifndef _WIN32
return fprintf(out, bc_gen_label, label, name);
@ -125,7 +129,10 @@ static int output_label(FILE* out, const char* label, const char* name) {
int ret;
// This loop counts how many backslashes there are in the label.
for (i = 0; i < len; ++i) count += (name[i] == '\\');
for (i = 0; i < len; ++i)
{
count += (name[i] == '\\');
}
buf = (char*) malloc(len + 1 + count);
if (buf == NULL) return -1;
@ -136,11 +143,12 @@ static int output_label(FILE* out, const char* label, const char* name) {
// label byte-for-byte, unless it encounters a backslash, in which case, it
// copies the backslash twice to have it escaped properly in the string
// literal.
for (i = 0; i < len; ++i) {
for (i = 0; i < len; ++i)
{
buf[i + count] = name[i];
if (name[i] == '\\') {
if (name[i] == '\\')
{
count += 1;
buf[i + count] = name[i];
}
@ -202,17 +210,19 @@ static int output_label(FILE* out, const char* label, const char* name) {
* All text files that are transformed have license comments. This program finds
* the end of that comment and strips it out as well.
*/
int main(int argc, char *argv[]) {
FILE *in;
FILE *out;
char *label;
char *define;
char *name;
int
main(int argc, char* argv[])
{
FILE* in;
FILE* out;
char* label;
char* define;
char* name;
int c, count, slashes, err = IO_ERR;
bool has_label, has_define, remove_tabs;
if (argc < 4) {
if (argc < 4)
{
printf("usage: %s input output name [label [define [remove_tabs]]]\n",
argv[0]);
return INVALID_PARAMS;
@ -244,29 +254,34 @@ int main(int argc, char *argv[]) {
c = count = slashes = 0;
// This is where the end of the license comment is found.
while (slashes < 2 && (c = fgetc(in)) >= 0) {
while (slashes < 2 && (c = fgetc(in)) >= 0)
{
slashes += (slashes == 1 && c == '/' && fgetc(in) == '\n');
slashes += (!slashes && c == '/' && fgetc(in) == '*');
}
// The file is invalid if the end of the license comment could not be found.
if (c < 0) {
if (c < 0)
{
err = INVALID_INPUT_FILE;
goto err;
}
// Do not put extra newlines at the beginning of the char array.
while ((c = fgetc(in)) == '\n');
while ((c = fgetc(in)) == '\n')
{
continue;
}
// This loop is what generates the actual char array. It counts how many
// chars it has printed per line in order to insert newlines at appropriate
// places. It also skips tabs if they should be removed.
while (c >= 0) {
while (c >= 0)
{
int val;
if (!remove_tabs || c != '\t') {
if (!remove_tabs || c != '\t')
{
if (!count && fputc('\t', out) == EOF) goto err;
val = fprintf(out, "%d,", c);
@ -274,7 +289,8 @@ int main(int argc, char *argv[]) {
count += val;
if (count > MAX_WIDTH) {
if (count > MAX_WIDTH)
{
count = 0;
if (fputc('\n', out) == EOF) goto err;
}

3
include/args.h

@ -47,7 +47,8 @@
* @param exit_exprs True if bc/dc should exit when there are expressions,
* false otherwise.
*/
void bc_args(int argc, char *argv[], bool exit_exprs);
void
bc_args(int argc, char* argv[], bool exit_exprs);
// A reference to the list of long options.
extern const BcOptLong bc_args_lopt[];

65
include/bc.h

@ -49,7 +49,8 @@
* The main function for bc. It just sets variables and passes its arguments
* through to @a bc_vm_boot().
*/
void bc_main(int argc, char *argv[]);
void
bc_main(int argc, char* argv[]);
// These are references to the help text, the library text, and the "filename"
// for the library.
@ -66,8 +67,8 @@ extern const char* bc_lib2_name;
/**
* A struct containing information about a bc keyword.
*/
typedef struct BcLexKeyword {
typedef struct BcLexKeyword
{
/// Holds the length of the keyword along with a bit that, if set, means the
/// keyword is used in POSIX bc.
uchar data;
@ -87,8 +88,10 @@ typedef struct BcLexKeyword {
#define BC_LEX_KW_LEN(kw) ((size_t) ((kw)->data & ~(BC_LEX_CHAR_MSB(1))))
/// A macro to easily build a keyword entry. See bc_lex_kws in src/data.c.
#define BC_LEX_KW_ENTRY(a, b, c) \
{ .data = ((b) & ~(BC_LEX_CHAR_MSB(1))) | BC_LEX_CHAR_MSB(c), .name = a }
#define BC_LEX_KW_ENTRY(a, b, c) \
{ \
.data = ((b) & ~(BC_LEX_CHAR_MSB(1))) | BC_LEX_CHAR_MSB(c), .name = a \
}
#if BC_ENABLE_EXTRA_MATH
@ -113,7 +116,8 @@ extern const size_t bc_lex_kws_len;
* @a BcLexNext.)
* @param l The lexer.
*/
void bc_lex_token(BcLex *l);
void
bc_lex_token(BcLex* l);
// The following section is for flags needed when parsing bc code. These flags
// are complicated, but necessary. Why you ask? Because bc's standard is awful.
@ -144,49 +148,49 @@ void bc_lex_token(BcLex *l);
// flag stack. All `p` arguments are pointers to a BcParse.
// This flag is set if the parser has seen a left brace.
#define BC_PARSE_FLAG_BRACE (UINTMAX_C(1)<<0)
#define BC_PARSE_FLAG_BRACE (UINTMAX_C(1) << 0)
#define BC_PARSE_BRACE(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_BRACE)
// This flag is set if the parser is parsing inside of the braces of a function
// body.
#define BC_PARSE_FLAG_FUNC_INNER (UINTMAX_C(1)<<1)
#define BC_PARSE_FLAG_FUNC_INNER (UINTMAX_C(1) << 1)
#define BC_PARSE_FUNC_INNER(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_FUNC_INNER)
// This flag is set if the parser is parsing a function. It is different from
// the one above because it is set if it is parsing a function body *or* header,
// not just if it's parsing a function body.
#define BC_PARSE_FLAG_FUNC (UINTMAX_C(1)<<2)
#define BC_PARSE_FLAG_FUNC (UINTMAX_C(1) << 2)
#define BC_PARSE_FUNC(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_FUNC)
// This flag is set if the parser is expecting to parse a body, whether of a
// function, an if statement, or a loop.
#define BC_PARSE_FLAG_BODY (UINTMAX_C(1)<<3)
#define BC_PARSE_FLAG_BODY (UINTMAX_C(1) << 3)
#define BC_PARSE_BODY(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_BODY)
// This flag is set if bc is parsing a loop. This is important because the break
// and continue keywords are only valid inside of a loop.
#define BC_PARSE_FLAG_LOOP (UINTMAX_C(1)<<4)
#define BC_PARSE_FLAG_LOOP (UINTMAX_C(1) << 4)
#define BC_PARSE_LOOP(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_LOOP)
// This flag is set if bc is parsing the body of a loop. It is different from
// the one above the same way @a BC_PARSE_FLAG_FUNC_INNER is different from
// @a BC_PARSE_FLAG_FUNC.
#define BC_PARSE_FLAG_LOOP_INNER (UINTMAX_C(1)<<5)
#define BC_PARSE_FLAG_LOOP_INNER (UINTMAX_C(1) << 5)
#define BC_PARSE_LOOP_INNER(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_LOOP_INNER)
// This flag is set if bc is parsing an if statement.
#define BC_PARSE_FLAG_IF (UINTMAX_C(1)<<6)
#define BC_PARSE_FLAG_IF (UINTMAX_C(1) << 6)
#define BC_PARSE_IF(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_IF)
// This flag is set if bc is parsing an else statement. This is important
// because of "else if" constructions, among other things.
#define BC_PARSE_FLAG_ELSE (UINTMAX_C(1)<<7)
#define BC_PARSE_FLAG_ELSE (UINTMAX_C(1) << 7)
#define BC_PARSE_ELSE(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_ELSE)
// This flag is set if bc just finished parsing an if statement and its body.
// It tells the parser that it can probably expect an else statement next. This
// flag is, thus, one of the most subtle.
#define BC_PARSE_FLAG_IF_END (UINTMAX_C(1)<<8)
#define BC_PARSE_FLAG_IF_END (UINTMAX_C(1) << 8)
#define BC_PARSE_IF_END(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_IF_END)
/**
@ -230,7 +234,7 @@ void bc_lex_token(BcLex *l);
* @param t The token to return operator data for.
* @return The operator data for @a t.
*/
#define BC_PARSE_OP_DATA(t) bc_parse_ops[((t) - BC_LEX_OP_INC)]
#define BC_PARSE_OP_DATA(t) bc_parse_ops[((t) -BC_LEX_OP_INC)]
/**
* Returns non-zero if operator @a op is left associative, zero otherwise.
@ -261,7 +265,7 @@ void bc_lex_token(BcLex *l);
* @param e8 The eighth bit.
* @return An expression entry for bc_parse_exprs[].
*/
#define BC_PARSE_EXPR_ENTRY(e1, e2, e3, e4, e5, e6, e7, e8) \
#define BC_PARSE_EXPR_ENTRY(e1, e2, e3, e4, e5, e6, e7, e8) \
((UINTMAX_C(e1) << 7) | (UINTMAX_C(e2) << 6) | (UINTMAX_C(e3) << 5) | \
(UINTMAX_C(e4) << 4) | (UINTMAX_C(e5) << 3) | (UINTMAX_C(e6) << 2) | \
(UINTMAX_C(e7) << 1) | (UINTMAX_C(e8) << 0))
@ -272,7 +276,7 @@ void bc_lex_token(BcLex *l);
* @return True if i is an expression token, false otherwise.
*/
#define BC_PARSE_EXPR(i) \
(bc_parse_exprs[(((i) & (uchar) ~(0x07)) >> 3)] & (1 << (7 - ((i) & 0x07))))
(bc_parse_exprs[(((i) & (uchar) ~(0x07)) >> 3)] & (1 << (7 - ((i) &0x07))))
/**
* Returns the operator (by lex token) that is at the top of the operator
@ -337,7 +341,7 @@ void bc_lex_token(BcLex *l);
* @param t The token to turn into an instruction.
* @return The token as an instruction.
*/
#define BC_PARSE_TOKEN_INST(t) ((uchar) ((t) - BC_LEX_NEG + BC_INST_NEG))
#define BC_PARSE_TOKEN_INST(t) ((uchar) ((t) -BC_LEX_NEG + BC_INST_NEG))
/**
* Returns true if the token is a bc keyword.
@ -353,8 +357,8 @@ void bc_lex_token(BcLex *l);
///
/// Obviously, @a len is the number of tokens in the @a tokens array. If more
/// than 4 is needed in the future, @a tokens will have to be changed.
typedef struct BcParseNext {
typedef struct BcParseNext
{
/// The number of tokens in the tokens array.
uchar len;
@ -368,13 +372,15 @@ typedef struct BcParseNext {
/// A macro to generate a BcParseNext literal from BcParseNext data. See
/// src/data.c for examples.
#define BC_PARSE_NEXT(a, ...) \
{ .len = (uchar) (a), BC_PARSE_NEXT_TOKENS(__VA_ARGS__) }
#define BC_PARSE_NEXT(a, ...) \
{ \
.len = (uchar) (a), BC_PARSE_NEXT_TOKENS(__VA_ARGS__) \
}
/// A status returned by @a bc_parse_expr_err(). It can either return success or
/// an error indicating an empty expression.
typedef enum BcParseStatus {
typedef enum BcParseStatus
{
BC_PARSE_STATUS_SUCCESS,
BC_PARSE_STATUS_EMPTY_EXPR,
@ -387,14 +393,16 @@ typedef enum BcParseStatus {
* @param flags Flags that define the requirements that the parsed code must
* meet or an error will result. See @a BcParseExpr for more info.
*/
void bc_parse_expr(BcParse *p, uint8_t flags);
void
bc_parse_expr(BcParse* p, uint8_t flags);
/**
* The @a BcParseParse function for bc. (See include/parse.h for a definition of
* @a BcParseParse.)
* @param p The parser.
*/
void bc_parse_parse(BcParse *p);
void
bc_parse_parse(BcParse* p);
/**
* Ends a series of if statements. This is to ensure that full parses happen
@ -403,7 +411,8 @@ void bc_parse_parse(BcParse *p);
* function definition, we know we can add an empty else clause.
* @param p The parser.
*/
void bc_parse_endif(BcParse *p);
void
bc_parse_endif(BcParse* p);
/// References to the signal message and its length.
extern const char bc_sig_msg[];

258
include/bcl.h

@ -73,7 +73,7 @@
#define STDOUT_FILENO _fileno(stdout)
#define STDERR_FILENO _fileno(stderr)
#define ssize_t SSIZE_T
#define S_ISDIR(m) ((m) & _S_IFDIR)
#define S_ISDIR(m) ((m) & (_S_IFDIR))
#define O_RDONLY _O_RDONLY
#define stat _stat
#define fstat _fstat
@ -129,8 +129,8 @@ typedef uint32_t BclRandInt;
#if BC_ENABLE_LIBRARY
typedef enum BclError {
typedef enum BclError
{
BCL_ERROR_NONE,
BCL_ERROR_INVALID_NUM,
@ -151,8 +151,8 @@ typedef enum BclError {
} BclError;
typedef struct BclNumber {
typedef struct BclNumber
{
size_t i;
} BclNumber;
@ -161,81 +161,179 @@ struct BclCtxt;
typedef struct BclCtxt* BclContext;
void bcl_handleSignal(void);
bool bcl_running(void);
BclError bcl_init(void);
void bcl_free(void);
bool bcl_abortOnFatalError(void);
void bcl_setAbortOnFatalError(bool abrt);
bool bcl_leadingZeroes(void);
void bcl_setLeadingZeroes(bool leadingZeroes);
void bcl_gc(void);
BclError bcl_pushContext(BclContext ctxt);
void bcl_popContext(void);
BclContext bcl_context(void);
BclContext bcl_ctxt_create(void);
void bcl_ctxt_free(BclContext ctxt);
void bcl_ctxt_freeNums(BclContext ctxt);
size_t bcl_ctxt_scale(BclContext ctxt);
void bcl_ctxt_setScale(BclContext ctxt, size_t scale);
size_t bcl_ctxt_ibase(BclContext ctxt);
void bcl_ctxt_setIbase(BclContext ctxt, size_t ibase);
size_t bcl_ctxt_obase(BclContext ctxt);
void bcl_ctxt_setObase(BclContext ctxt, size_t obase);
BclError bcl_err(BclNumber n);
BclNumber bcl_num_create(void);
void bcl_num_free(BclNumber n);
bool bcl_num_neg(BclNumber n);
void bcl_num_setNeg(BclNumber n, bool neg);
size_t bcl_num_scale(BclNumber n);
BclError bcl_num_setScale(BclNumber n, size_t scale);
size_t bcl_num_len(BclNumber n);
BclError bcl_copy(BclNumber d, BclNumber s);
BclNumber bcl_dup(BclNumber s);
BclError bcl_bigdig(BclNumber n, BclBigDig *result);
BclNumber bcl_bigdig2num(BclBigDig val);
BclNumber bcl_add(BclNumber a, BclNumber b);
BclNumber bcl_sub(BclNumber a, BclNumber b);
BclNumber bcl_mul(BclNumber a, BclNumber b);
BclNumber bcl_div(BclNumber a, BclNumber b);
BclNumber bcl_mod(BclNumber a, BclNumber b);
BclNumber bcl_pow(BclNumber a, BclNumber b);
BclNumber bcl_lshift(BclNumber a, BclNumber b);
BclNumber bcl_rshift(BclNumber a, BclNumber b);
BclNumber bcl_sqrt(BclNumber a);
BclError bcl_divmod(BclNumber a, BclNumber b, BclNumber *c, BclNumber *d);
BclNumber bcl_modexp(BclNumber a, BclNumber b, BclNumber c);
ssize_t bcl_cmp(BclNumber a, BclNumber b);
void bcl_zero(BclNumber n);
void bcl_one(BclNumber n);
BclNumber bcl_parse(const char *restrict val);
char* bcl_string(BclNumber n);
BclNumber bcl_irand(BclNumber a);
BclNumber bcl_frand(size_t places);
BclNumber bcl_ifrand(BclNumber a, size_t places);
BclError bcl_rand_seedWithNum(BclNumber n);
BclError bcl_rand_seed(unsigned char seed[BCL_SEED_SIZE]);
void bcl_rand_reseed(void);
BclNumber bcl_rand_seed2num(void);
BclRandInt bcl_rand_int(void);
BclRandInt bcl_rand_bounded(BclRandInt bound);
void
bcl_handleSignal(void);
bool
bcl_running(void);
BclError
bcl_init(void);
void
bcl_free(void);
bool
bcl_abortOnFatalError(void);
void
bcl_setAbortOnFatalError(bool abrt);
bool
bcl_leadingZeroes(void);
void
bcl_setLeadingZeroes(bool leadingZeroes);
void
bcl_gc(void);
BclError
bcl_pushContext(BclContext ctxt);
void
bcl_popContext(void);
BclContext
bcl_context(void);
BclContext
bcl_ctxt_create(void);
void
bcl_ctxt_free(BclContext ctxt);
void
bcl_ctxt_freeNums(BclContext ctxt);
size_t
bcl_ctxt_scale(BclContext ctxt);
void
bcl_ctxt_setScale(BclContext ctxt, size_t scale);
size_t
bcl_ctxt_ibase(BclContext ctxt);
void
bcl_ctxt_setIbase(BclContext ctxt, size_t ibase);
size_t
bcl_ctxt_obase(BclContext ctxt);
void
bcl_ctxt_setObase(BclContext ctxt, size_t obase);
BclError
bcl_err(BclNumber n);
BclNumber
bcl_num_create(void);
void
bcl_num_free(BclNumber n);
bool
bcl_num_neg(BclNumber n);
void
bcl_num_setNeg(BclNumber n, bool neg);
size_t
bcl_num_scale(BclNumber n);
BclError
bcl_num_setScale(BclNumber n, size_t scale);
size_t
bcl_num_len(BclNumber n);
BclError
bcl_copy(BclNumber d, BclNumber s);
BclNumber
bcl_dup(BclNumber s);
BclError
bcl_bigdig(BclNumber n, BclBigDig* result);
BclNumber
bcl_bigdig2num(BclBigDig val);
BclNumber
bcl_add(BclNumber a, BclNumber b);
BclNumber
bcl_sub(BclNumber a, BclNumber b);
BclNumber
bcl_mul(BclNumber a, BclNumber b);
BclNumber
bcl_div(BclNumber a, BclNumber b);
BclNumber
bcl_mod(BclNumber a, BclNumber b);
BclNumber
bcl_pow(BclNumber a, BclNumber b);
BclNumber
bcl_lshift(BclNumber a, BclNumber b);
BclNumber
bcl_rshift(BclNumber a, BclNumber b);
BclNumber
bcl_sqrt(BclNumber a);
BclError
bcl_divmod(BclNumber a, BclNumber b, BclNumber* c, BclNumber* d);
BclNumber
bcl_modexp(BclNumber a, BclNumber b, BclNumber c);
ssize_t
bcl_cmp(BclNumber a, BclNumber b);
void
bcl_zero(BclNumber n);
void
bcl_one(BclNumber n);
BclNumber
bcl_parse(const char* restrict val);
char*
bcl_string(BclNumber n);
BclNumber
bcl_irand(BclNumber a);
BclNumber
bcl_frand(size_t places);
BclNumber
bcl_ifrand(BclNumber a, size_t places);
BclError
bcl_rand_seedWithNum(BclNumber n);
BclError
bcl_rand_seed(unsigned char seed[BCL_SEED_SIZE]);
void
bcl_rand_reseed(void);
BclNumber
bcl_rand_seed2num(void);
BclRandInt
bcl_rand_int(void);
BclRandInt
bcl_rand_bounded(BclRandInt bound);
#endif // BC_ENABLE_LIBRARY

15
include/dc.h

@ -46,7 +46,8 @@
* The main function for dc. It just sets variables and passes its arguments
* through to @a bc_vm_boot().
*/
void dc_main(int argc, char *argv[]);
void
dc_main(int argc, char* argv[]);
// A reference to the dc help text.
extern const char dc_help[];
@ -56,7 +57,8 @@ extern const char dc_help[];
* @a BcLexNext.)
* @param l The lexer.
*/
void dc_lex_token(BcLex *l);
void
dc_lex_token(BcLex* l);
/**
* Returns true if the negative char `_` should be treated as a command or not.
@ -66,7 +68,8 @@ void dc_lex_token(BcLex *l);
* @return True if a negative should be treated as a command, false if it
* should be treated as a negative sign on a number.
*/
bool dc_lex_negCommand(BcLex *l);
bool
dc_lex_negCommand(BcLex* l);
// References to the signal message and its length.
extern const char dc_sig_msg[];
@ -88,7 +91,8 @@ extern const uint8_t dc_parse_insts[];
* @a BcParseParse.)
* @param p The parser.
*/
void dc_parse_parse(BcParse *p);
void
dc_parse_parse(BcParse* p);
/**
* The @a BcParseExpr function for dc. (See include/parse.h for a definition of
@ -97,7 +101,8 @@ void dc_parse_parse(BcParse *p);
* @param flags Flags that define the requirements that the parsed code must
* meet or an error will result. See @a BcParseExpr for more info.
*/
void dc_parse_expr(BcParse *p, uint8_t flags);
void
dc_parse_expr(BcParse* p, uint8_t flags);
#endif // DC_ENABLED

45
include/file.h

@ -48,8 +48,8 @@
#include <stdio.h>
/// The file struct.
typedef struct BcFile {
typedef struct BcFile
{
// The file. This is here simply to make the line lib code as compatible
// with the existing code as possible.
FILE* f;
@ -59,13 +59,13 @@ typedef struct BcFile {
#else // BC_ENABLE_LINE_LIB
/// The file struct.
typedef struct BcFile {
typedef struct BcFile
{
// The actual file descriptor.
int fd;
// The buffer for the file.
char *buf;
char* buf;
// The length (number of actual chars) in the buffer.
size_t len;
@ -82,8 +82,8 @@ typedef struct BcFile {
/// Types of flushing. These are important because of history and printing
/// strings without newlines, something that users could use as their own
/// prompts.
typedef enum BcFlushType {
typedef enum BcFlushType
{
/// Do not clear the stored partial line, but don't add to it.
BC_FLUSH_NO_EXTRAS_NO_CLEAR,
@ -118,7 +118,8 @@ typedef enum BcFlushType {
* @param f The file to initialize.
* @param file The stdio file.
*/
void bc_file_init(BcFile *f, FILE* file);
void
bc_file_init(BcFile* f, FILE* file);
#else // BC_ENABLE_LINE_LIB
@ -129,7 +130,8 @@ void bc_file_init(BcFile *f, FILE* file);
* @param buf The buffer for the file.
* @param cap The capacity of the buffer.
*/
void bc_file_init(BcFile *f, int fd, char *buf, size_t cap);
void
bc_file_init(BcFile* f, int fd, char* buf, size_t cap);
#endif // BC_ENABLE_LINE_LIB
@ -137,7 +139,8 @@ void bc_file_init(BcFile *f, int fd, char *buf, size_t cap);
* Frees a file, including flushing it.
* @param f The file to free.
*/
void bc_file_free(BcFile *f);
void
bc_file_free(BcFile* f);
/**
* Print a char into the file.
@ -145,7 +148,8 @@ void bc_file_free(BcFile *f);
* @param type The flush type.
* @param c The character to write.
*/
void bc_file_putchar(BcFile *restrict f, BcFlushType type, uchar c);
void
bc_file_putchar(BcFile* restrict f, BcFlushType type, uchar c);
/**
* Flush and return an error if it failed. This is meant to be used when needing
@ -155,14 +159,16 @@ void bc_file_putchar(BcFile *restrict f, BcFlushType type, uchar c);
* @param type The flush type.
* @return A status indicating if an error occurred.
*/
BcStatus bc_file_flushErr(BcFile *restrict f, BcFlushType type);
BcStatus
bc_file_flushErr(BcFile* restrict f, BcFlushType type);
/**
* Flush and throw an error on failure.
* @param f The file to flush.
* @param type The flush type.
*/
void bc_file_flush(BcFile *restrict f, BcFlushType type);
void
bc_file_flush(BcFile* restrict f, BcFlushType type);
/**
* Write the contents of buf to the file.
@ -171,22 +177,24 @@ void bc_file_flush(BcFile *restrict f, BcFlushType type);
* @param buf The buffer whose contents will be written to the file.
* @param n The length of buf.
*/
void bc_file_write(BcFile *restrict f, BcFlushType type,
const char *buf, size_t n);
void
bc_file_write(BcFile* restrict f, BcFlushType type, const char* buf, size_t n);
/**
* Write to the file like fprintf would. This is very rudimentary.
* @param f The file to flush.
* @param fmt The format string.
*/
void bc_file_printf(BcFile *restrict f, const char *fmt, ...);
void
bc_file_printf(BcFile* restrict f, const char* fmt, ...);
/**
* Write to the file like vfprintf would. This is very rudimentary.
* @param f The file to flush.
* @param fmt The format string.
*/
void bc_file_vprintf(BcFile *restrict f, const char *fmt, va_list args);
void
bc_file_vprintf(BcFile* restrict f, const char* fmt, va_list args);
/**
* Write str to the file.
@ -194,7 +202,8 @@ void bc_file_vprintf(BcFile *restrict f, const char *fmt, va_list args);
* @param type The flush type.
* @param str The string to write to the file.
*/
void bc_file_puts(BcFile *restrict f, BcFlushType type, const char *str);
void
bc_file_puts(BcFile* restrict f, BcFlushType type, const char* str);
#if BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB

58
include/history.h

@ -123,13 +123,13 @@ extern volatile sig_atomic_t bc_history_inlinelib;
/**
* The history struct for editline.
*/
typedef struct BcHistory {
typedef struct BcHistory
{
/// A place to store the current line.
EditLine *el;
EditLine* el;
/// The history.
History *hist;
History* hist;
/// Whether the terminal is bad. This is more or less not used.
bool badTerm;
@ -151,10 +151,10 @@ extern const size_t bc_history_editrc_len;
/**
* The history struct for readline.
*/
typedef struct BcHistory {
typedef struct BcHistory
{
/// A place to store the current line.
char *line;
char* line;
/// Whether the terminal is bad. This is more or less not used.
bool badTerm;
@ -185,7 +185,7 @@ typedef struct BcHistory {
#include <conio.h>
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#define strcasecmp _stricmp
#endif // _WIN32
@ -233,9 +233,12 @@ typedef struct BcHistory {
#define BC_HISTORY_DEBUG_BUF_SIZE (1024)
// clang-format off
#define lndebug(...) \
do { \
if (bc_history_debug_fp.fd == 0) { \
do \
{ \
if (bc_history_debug_fp.fd == 0) \
{ \
bc_history_debug_buf = bc_vm_malloc(BC_HISTORY_DEBUG_BUF_SIZE); \
bc_file_init(&bc_history_debug_fp, \
open("/tmp/lndebug.txt", O_APPEND), \
@ -248,15 +251,17 @@ typedef struct BcHistory {
} \
bc_file_printf(&bc_history_debug_fp, ", " __VA_ARGS__); \
bc_file_flush(&bc_history_debug_fp); \
} while (0)
} \
while (0)
#else // BC_DEBUG_CODE
#define lndebug(fmt, ...)
#endif // BC_DEBUG_CODE
// clang-format on
/// An enum of useful actions. To understand what these mean, check terminal
/// emulators for their shortcuts or the VT100 codes.
typedef enum BcHistoryAction {
typedef enum BcHistoryAction
{
BC_ACTION_NULL = 0,
BC_ACTION_CTRL_A = 1,
BC_ACTION_CTRL_B = 2,
@ -279,7 +284,7 @@ typedef enum BcHistoryAction {
BC_ACTION_CTRL_Z = 26,
BC_ACTION_ESC = 27,
BC_ACTION_CTRL_BSLASH = 28,
BC_ACTION_BACKSPACE = 127
BC_ACTION_BACKSPACE = 127
} BcHistoryAction;
@ -287,8 +292,8 @@ typedef enum BcHistoryAction {
* This represents the state during line editing. We pass this state
* to functions implementing specific editing functionalities.
*/
typedef struct BcHistory {
typedef struct BcHistory
{
/// Edited line buffer.
BcVec buf;
@ -299,7 +304,7 @@ typedef struct BcHistory {
BcVec extras;
/// Prompt to display.
const char *prompt;
const char* prompt;
/// Prompt length.
size_t plen;
@ -355,10 +360,11 @@ typedef struct BcHistory {
* Frees strings used by history.
* @param str The string to free.
*/
void bc_history_string_free(void *str);
void
bc_history_string_free(void* str);
// A list of terminals that don't work.
extern const char *bc_history_bad_terms[];
extern const char* bc_history_bad_terms[];
// A tab in history and its length.
extern const char bc_history_tab[];
@ -377,13 +383,14 @@ extern const size_t bc_history_combo_chars_len;
// Debug data.
extern BcFile bc_history_debug_fp;
extern char *bc_history_debug_buf;
extern char* bc_history_debug_buf;
/**
* A function to print keycodes for debugging.
* @param h The history data.
*/
void bc_history_printKeyCodes(BcHistory* h);
void
bc_history_printKeyCodes(BcHistory* h);
#endif // BC_DEBUG_CODE
@ -406,19 +413,22 @@ void bc_history_printKeyCodes(BcHistory* h);
* leave the terminal in raw mode or in some other half-baked
* state.
*/
BcStatus bc_history_line(BcHistory *h, BcVec *vec, const char *prompt);
BcStatus
bc_history_line(BcHistory* h, BcVec* vec, const char* prompt);
/**
* Initialize history data.
* @param h The struct to initialize.
*/
void bc_history_init(BcHistory *h);
void
bc_history_init(BcHistory* h);
/**
* Free history data (and recook the terminal).
* @param h The struct to free.
*/
void bc_history_free(BcHistory *h);
void
bc_history_free(BcHistory* h);
#endif // BC_ENABLE_HISTORY

91
include/lang.h

@ -47,8 +47,8 @@
#include <num.h>
/// The instructions for bytecode.
typedef enum BcInst {
typedef enum BcInst
{
#if BC_ENABLED
/// Postfix increment and decrement. Prefix are translated into
@ -334,10 +334,10 @@ _Static_assert(BC_INST_INVALID <= UCHAR_MAX,
#endif // BC_C11
/// Used by maps to identify where items are in the array.
typedef struct BcId {
typedef struct BcId
{
/// The name of the item.
char *name;
char* name;
/// The index into the array where the item is.
size_t idx;
@ -345,8 +345,8 @@ typedef struct BcId {
} BcId;
/// The location of a var, array, or array element.
typedef struct BcLoc {
typedef struct BcLoc
{
/// The index of the var or array.
size_t loc;
@ -356,10 +356,10 @@ typedef struct BcLoc {
} BcLoc;
/// An entry for a constant.
typedef struct BcConst {
typedef struct BcConst
{
/// The original string as parsed from the source code.
char *val;
char* val;
/// The last base that the constant was parsed in.
BcBigDig base;
@ -372,8 +372,8 @@ typedef struct BcConst {
/// A function. This is also used in dc, not just bc. The reason is that strings
/// are executed in dc, and they are converted to functions in order to be
/// executed.
typedef struct BcFunc {
typedef struct BcFunc
{
/// The bytecode instructions.
BcVec code;
@ -399,7 +399,7 @@ typedef struct BcFunc {
BcVec consts;
/// The function's name.
const char *name;
const char* name;
#if BC_ENABLED
/// True if the function is a void function.
@ -409,8 +409,8 @@ typedef struct BcFunc {
} BcFunc;
/// Types of results that can be pushed onto the results stack.
typedef enum BcResultType {
typedef enum BcResultType
{
/// Result is a variable.
BC_RESULT_VAR,
@ -464,8 +464,8 @@ typedef enum BcResultType {
} BcResultType;
/// A union to store data for various result types.
typedef union BcResultData {
typedef union BcResultData
{
/// A number. Strings are stored here too; they are numbers with
/// cap == 0 && num == NULL. The string's index into the strings vector is
/// stored in the scale field. But this is only used for strings stored in
@ -482,8 +482,8 @@ typedef union BcResultData {
} BcResultData;
/// A tagged union for results.
typedef struct BcResult {
typedef struct BcResult
{
/// The tag. The type of the result.
BcResultType t;
@ -494,8 +494,8 @@ typedef struct BcResult {
/// An instruction pointer. This is how bc knows where in the bytecode vector,
/// and which function, the current execution is.
typedef struct BcInstPtr {
typedef struct BcInstPtr
{
/// The index of the currently executing function in the fns vector.
size_t func;
@ -510,8 +510,8 @@ typedef struct BcInstPtr {
} BcInstPtr;
/// Types of identifiers.
typedef enum BcType {
typedef enum BcType
{
/// Variable.
BC_TYPE_VAR,
@ -529,8 +529,8 @@ typedef enum BcType {
#if BC_ENABLED
/// An auto variable in bc.
typedef struct BcAuto {
typedef struct BcAuto
{
/// The index of the variable in the vars or arrs vectors.
size_t idx;
@ -549,7 +549,8 @@ struct BcProgram;
* @param name The name of the function. The string is assumed to be owned by
* some other entity.
*/
void bc_func_init(BcFunc *f, const char* name);
void
bc_func_init(BcFunc* f, const char* name);
/**
* Inserts an auto into the function.
@ -560,15 +561,17 @@ void bc_func_init(BcFunc *f, const char* name);
* @param line The line in the source code where the insert happened. This is
* solely for error reporting.
*/
void bc_func_insert(BcFunc *f, struct BcProgram* p, char* name,
BcType type, size_t line);
void
bc_func_insert(BcFunc* f, struct BcProgram* p, char* name, BcType type,
size_t line);
/**
* Resets a function in preparation for it to be reused. This can happen in bc
* because it is a dynamic language and functions can be redefined.
* @param f The functio to reset.
*/
void bc_func_reset(BcFunc *f);
void
bc_func_reset(BcFunc* f);
#ifndef NDEBUG
/**
@ -577,7 +580,8 @@ void bc_func_reset(BcFunc *f);
* check for memory leaks.
* @param func The function to free as a void pointer.
*/
void bc_func_free(void *func);
void
bc_func_free(void* func);
#endif // NDEBUG
/**
@ -591,7 +595,8 @@ void bc_func_free(void *func);
* @param nums True if the array should be for numbers, false if it should be
* for vectors.
*/
void bc_array_init(BcVec *a, bool nums);
void
bc_array_init(BcVec* a, bool nums);
/**
* Copies an array to another array. This is used to do pass arrays to functions
@ -600,19 +605,22 @@ void bc_array_init(BcVec *a, bool nums);
* @param d The destination array.
* @param s The source array.
*/
void bc_array_copy(BcVec *d, const BcVec *s);
void
bc_array_copy(BcVec* d, const BcVec* s);
/**
* Frees a string stored in a function. This is a destructor.
* @param string The string to free as a void pointer.
*/
void bc_string_free(void *string);
void
bc_string_free(void* string);
/**
* Frees a constant stored in a function. This is a destructor.
* @param constant The constant to free as a void pointer.
*/
void bc_const_free(void *constant);
void
bc_const_free(void* constant);
/**
* Clears a result. It sets the type to BC_RESULT_TEMP and clears the union by
@ -620,7 +628,8 @@ void bc_const_free(void *constant);
* uninitialized data.
* @param r The result to clear.
*/
void bc_result_clear(BcResult *r);
void
bc_result_clear(BcResult* r);
/**
* Copies a result into another. This is done for things like duplicating the
@ -629,13 +638,15 @@ void bc_result_clear(BcResult *r);
* @param d The destination result.
* @param src The source result.
*/
void bc_result_copy(BcResult *d, BcResult *src);
void
bc_result_copy(BcResult* d, BcResult* src);
/**
* Frees a result. This is a destructor.
* @param result The result to free as a void pointer.
*/
void bc_result_free(void *result);
void
bc_result_free(void* result);
/**
* Expands an array to @a len. This can happen because in bc, you do not have to
@ -646,7 +657,8 @@ void bc_result_free(void *result);
* @param a The array to expand.
* @param len The length to expand to.
*/
void bc_array_expand(BcVec *a, size_t len);
void
bc_array_expand(BcVec* a, size_t len);
/**
* Compare two BcId's and return the result. Since they are just comparing the
@ -656,7 +668,8 @@ void bc_array_expand(BcVec *a, size_t len);
* @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);
int
bc_id_cmp(const BcId* e1, const BcId* e2);
#if BC_ENABLED

49
include/lex.h

@ -84,8 +84,8 @@
((c) == '.' && !(pt) && !(int_only)))
/// An enum of lex token types.
typedef enum BcLexType {
typedef enum BcLexType
{
/// End of file.
BC_LEX_EOF,
@ -462,10 +462,10 @@ struct BcLex;
typedef void (*BcLexNext)(struct BcLex* l);
/// The lexer.
typedef struct BcLex {
typedef struct BcLex
{
/// A pointer to the text to lex.
const char *buf;
const char* buf;
/// The current index into buf.
size_t i;