Browse Source

Add leading zeroes to bcl

I did this because there were compile errors with the library using
BC_Z, and I thought to myself that it would be a good feature to add and
would take little code.

Signed-off-by: Gavin Howard <gavin@yzena.com>
line_libs
Gavin Howard 11 months ago
parent
commit
4dd84d12b4
Signed by: gavin
GPG Key ID: C08038BDF280D33E
  1. 2
      include/bcl.h
  2. 7
      include/vm.h
  3. 8
      src/library.c
  4. 74
      tests/bcl.c

2
include/bcl.h

@ -169,6 +169,8 @@ 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);

7
include/vm.h

@ -259,6 +259,10 @@
/// A convenience macro for checking if the prompt is enabled.
#define BC_PROMPT (BC_P)
#else // !BC_ENABLE_LIBRARY
#define BC_Z (vm.leading_zeroes)
#endif // !BC_ENABLE_LIBRARY
/**
@ -412,6 +416,9 @@ typedef struct BcVm {
/// Whether or not bcl should abort on fatal errors.
bool abrt;
/// Whether or not to print leading zeros.
bool leading_zeroes;
/// The number of "references," or times that the library was initialized.
unsigned int refs;

8
src/library.c

@ -185,6 +185,14 @@ void bcl_setAbortOnFatalError(bool abrt) {
vm.abrt = abrt;
}
bool bcl_leadingZeroes(void) {
return vm.leading_zeroes;
}
void bcl_setLeadingZeroes(bool leadingZeroes) {
vm.leading_zeroes = leadingZeroes;
}
BclContext bcl_ctxt_create(void) {
BclContext ctxt = NULL;

74
tests/bcl.c

@ -234,6 +234,80 @@ int main(void) {
// Clean up.
bcl_num_free(n);
// Test leading zeroes.
if (bcl_leadingZeroes())
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
n = bcl_parse("0.01");
err(bcl_err(n));
n2 = bcl_parse("-0.01");
err(bcl_err(n2));
n3 = bcl_parse("1.01");
err(bcl_err(n3));
n4 = bcl_parse("-1.01");
err(bcl_err(n4));
res = bcl_string(bcl_dup(n));
if (strcmp(res, ".01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(bcl_dup(n2));
if (strcmp(res, "-.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(bcl_dup(n3));
if (strcmp(res, "1.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(bcl_dup(n4));
if (strcmp(res, "-1.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
bcl_setLeadingZeroes(true);
if (!bcl_leadingZeroes())
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(bcl_dup(n));
if (strcmp(res, "0.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(bcl_dup(n2));
if (strcmp(res, "-0.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(bcl_dup(n3));
if (strcmp(res, "1.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(bcl_dup(n4));
if (strcmp(res, "-1.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
bcl_setLeadingZeroes(false);
if (bcl_leadingZeroes())
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(n);
if (strcmp(res, ".01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(n2);
if (strcmp(res, "-.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(n3);
if (strcmp(res, "1.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
res = bcl_string(n4);
if (strcmp(res, "-1.01"))
err(BCL_ERROR_FATAL_UNKNOWN_ERR);
bcl_ctxt_freeNums(ctxt);
bcl_gc();

Loading…
Cancel
Save