Index: contrib/bc/LICENSE.md =================================================================== --- contrib/bc/LICENSE.md +++ contrib/bc/LICENSE.md @@ -0,0 +1,59 @@ +# License + +Copyright (c) 2018-2019 Gavin D. Howard + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +## History + +The files `src/history.c` and `include/history.h` are under the following +copyrights and license: + +Copyright (c) 2010-2014, Salvatore Sanfilippo
+Copyright (c) 2010-2013, Pieter Noordhuis
+Copyright (c) 2018 rain-1
+Copyright (c) 2018-2019, Gavin D. Howard + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Index: contrib/bc/Makefile.in =================================================================== --- contrib/bc/Makefile.in +++ contrib/bc/Makefile.in @@ -0,0 +1,341 @@ +# +# Copyright (c) 2018-2019 Gavin D. Howard and contributors. +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# %%WARNING%% +# +.POSIX: + +SRC = %%SRC%% +OBJ = %%OBJ%% +GCDA = %%GCDA%% +GCNO = %%GCNO%% + +BC_SRC = %%BC_SRC%% +BC_OBJ = %%BC_OBJ%% +BC_GCDA = %%BC_GCDA%% +BC_GCNO = %%BC_GCNO%% + +DC_SRC = %%DC_SRC%% +DC_OBJ = %%DC_OBJ%% +DC_GCDA = %%DC_GCDA%% +DC_GCNO = %%DC_GCNO%% + +HISTORY_SRC = %%HISTORY_SRC%% +HISTORY_OBJ = %%HISTORY_OBJ%% +HISTORY_GCDA = %%HISTORY_GCDA%% +HISTORY_GCNO = %%HISTORY_GCNO%% + +BC_ENABLED_NAME = BC_ENABLED +BC_ENABLED = %%BC_ENABLED%% +DC_ENABLED_NAME = DC_ENABLED +DC_ENABLED = %%DC_ENABLED%% + +VERSION = 2.1.0 + +GEN_DIR = gen +GEN = %%GEN%% +GEN_EXEC = $(GEN_DIR)/$(GEN) +GEN_C = $(GEN_DIR)/$(GEN).c + +GEN_EMU = %%GEN_EMU%% + +BC_LIB = $(GEN_DIR)/lib.bc +BC_LIB_C = $(GEN_DIR)/lib.c +BC_LIB_O = %%BC_LIB_O%% +BC_LIB_GCDA = $(GEN_DIR)/lib.gcda +BC_LIB_GCNO = $(GEN_DIR)/lib.gcno + +BC_LIB2 = $(GEN_DIR)/lib2.bc +BC_LIB2_C = $(GEN_DIR)/lib2.c +BC_LIB2_O = %%BC_LIB2_O%% +BC_LIB2_GCDA = $(GEN_DIR)/lib2.gcda +BC_LIB2_GCNO = $(GEN_DIR)/lib2.gcno + +BC_HELP = $(GEN_DIR)/bc_help.txt +BC_HELP_C = $(GEN_DIR)/bc_help.c +BC_HELP_O = %%BC_HELP_O%% +BC_HELP_GCDA = $(GEN_DIR)/bc_help.gcda +BC_HELP_GCNO = $(GEN_DIR)/bc_help.gcno + +DC_HELP = $(GEN_DIR)/dc_help.txt +DC_HELP_C = $(GEN_DIR)/dc_help.c +DC_HELP_O = %%DC_HELP_O%% +DC_HELP_GCDA = $(GEN_DIR)/dc_help.gcda +DC_HELP_GCNO = $(GEN_DIR)/dc_help.gcno + +BIN = bin +LOCALES = locales +EXEC_SUFFIX = %%EXECSUFFIX%% +EXEC_PREFIX = %%EXECPREFIX%% + +BC = bc +DC = dc +BC_EXEC = $(BIN)/$(EXEC_PREFIX)$(BC) +DC_EXEC = $(BIN)/$(EXEC_PREFIX)$(DC) + +MANUALS = manuals +BC_MANPAGE_NAME = $(EXEC_PREFIX)$(BC)$(EXEC_SUFFIX).1 +BC_MANPAGE = $(MANUALS)/$(BC).1 +BC_RONN = $(BC_MANPAGE).ronn +DC_MANPAGE_NAME = $(EXEC_PREFIX)$(DC)$(EXEC_SUFFIX).1 +DC_MANPAGE = $(MANUALS)/$(DC).1 +DC_RONN = $(DC_MANPAGE).ronn + +MANPAGE_INSTALL_ARGS = -Dm644 + +%%DESTDIR%% +BINDIR = %%BINDIR%% +MAN1DIR = %%MAN1DIR%% +MAIN_EXEC = $(EXEC_PREFIX)$(%%MAIN_EXEC%%)$(EXEC_SUFFIX) +EXEC = $(%%EXEC%%) +NLSPATH = %%NLSPATH%% + +BC_ENABLE_SIGNALS = %%SIGNALS%% +BC_ENABLE_HISTORY = %%HISTORY%% +BC_ENABLE_EXTRA_MATH_NAME = BC_ENABLE_EXTRA_MATH +BC_ENABLE_EXTRA_MATH = %%EXTRA_MATH%% +BC_ENABLE_NLS = %%NLS%% +BC_LONG_BIT = %%LONG_BIT%% + +RM = rm +MKDIR = mkdir + +INSTALL = ./install.sh +SAFE_INSTALL = ./safe-install.sh +LINK = ./link.sh +KARATSUBA = ./karatsuba.py +LOCALE_INSTALL = ./locale_install.sh +LOCALE_UNINSTALL = ./locale_uninstall.sh + +VALGRIND_ARGS = --error-exitcode=100 --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all +RONN_ARGS = --roff --organization="Gavin D. Howard" --manual="General Commands Manual" + +BC_NUM_KARATSUBA_LEN = %%KARATSUBA_LEN%% + +CPPFLAGS1 = -D$(BC_ENABLED_NAME)=$(BC_ENABLED) -D$(DC_ENABLED_NAME)=$(DC_ENABLED) +CPPFLAGS2 = $(CPPFLAGS1) -I./include/ -DVERSION=$(VERSION) %%LONG_BIT_DEFINE%% +CPPFLAGS3 = $(CPPFLAGS2) -DEXECPREFIX=$(EXEC_PREFIX) -DMAINEXEC=$(MAIN_EXEC) +CPPFLAGS4 = $(CPPFLAGS3) -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 +CPPFLAGS5 = $(CPPFLAGS4) -DBC_NUM_KARATSUBA_LEN=$(BC_NUM_KARATSUBA_LEN) +CPPFLAGS6 = $(CPPFLAGS5) -DBC_ENABLE_NLS=$(BC_ENABLE_NLS) +CPPFLAGS7 = $(CPPFLAGS6) -D$(BC_ENABLE_EXTRA_MATH_NAME)=$(BC_ENABLE_EXTRA_MATH) +CPPFLAGS = $(CPPFLAGS7) -DBC_ENABLE_SIGNALS=$(BC_ENABLE_SIGNALS) -DBC_ENABLE_HISTORY=$(BC_ENABLE_HISTORY) +CFLAGS = $(CPPFLAGS) %%CPPFLAGS%% %%CFLAGS%% +LDFLAGS = %%LDFLAGS%% + +HOSTCFLAGS = %%HOSTCFLAGS%% + +CC = %%CC%% +HOSTCC = %%HOSTCC%% + +.c.o: + $(CC) $(CFLAGS) -o $@ -c $< + +all: make_bin $(DC_HELP_O) $(BC_HELP_O) $(BC_LIB_O) $(BC_LIB2_O) $(BC_LIB3_O) $(BC_OBJ) $(DC_OBJ) $(HISTORY_OBJ) $(OBJ) + $(CC) $(CFLAGS) $(OBJ) $(DC_OBJ) $(BC_OBJ) $(HISTORY_OBJ) $(BC_HELP_O) $(DC_HELP_O) \ + $(BC_LIB_O) $(BC_LIB2_O) $(BC_LIB3_O) $(LDFLAGS) -o $(EXEC) + %%LINK%% + +$(GEN_EXEC): + %%GEN_EXEC_TARGET%% + +$(BC_LIB_C): $(GEN_EXEC) $(BC_LIB) + $(GEN_EMU) $(GEN_EXEC) $(BC_LIB) $(BC_LIB_C) bc_lib bc.h bc_lib_name $(BC_ENABLED_NAME) 1 + +$(BC_LIB2_C): $(GEN_EXEC) $(BC_LIB2) + $(GEN_EMU) $(GEN_EXEC) $(BC_LIB2) $(BC_LIB2_C) bc_lib2 bc.h bc_lib2_name \ + "$(BC_ENABLED_NAME) && $(BC_ENABLE_EXTRA_MATH_NAME)" 1 + +$(BC_HELP_C): $(GEN_EXEC) $(BC_HELP) + $(GEN_EMU) $(GEN_EXEC) $(BC_HELP) $(BC_HELP_C) bc_help bc.h "" $(BC_ENABLED_NAME) + +$(DC_HELP_C): $(GEN_EXEC) $(DC_HELP) + $(GEN_EMU) $(GEN_EXEC) $(DC_HELP) $(DC_HELP_C) dc_help dc.h "" $(DC_ENABLED_NAME) + +make_bin: + $(MKDIR) -p $(BIN) + +help: + @printf 'available targets:\n' + @printf '\n' + @printf ' all (default) builds %%EXECUTABLES%%\n' + @printf ' check alias for `make test`\n' + @printf ' clean removes all build files\n' + @printf ' clean_config removes all build files as well as the generated Makefile\n' + @printf ' clean_tests removes all build files, the generated Makefile,\n + @printf ' and generated tests\n' + @printf ' install installs binaries to "%s%s"\n' "$(DESTDIR)" "$(BINDIR)" + @printf ' and (if enabled) manpages to "%s%s"\n' "$(DESTDIR)" "$(MAN1DIR)" + @printf ' karatsuba runs the karatsuba script (requires Python 3)\n' + @printf ' karatsuba_test runs the karatsuba script while running tests\n' + @printf ' (requires Python 3)\n' + @printf ' uninstall uninstalls binaries from "%s%s"\n' "$(DESTDIR)" "$(BINDIR)" + @printf ' and (if enabled) manpages from "%s%s"\n' "$(DESTDIR)" "$(MAN1DIR)" + @printf ' test runs the test suite\n' + @printf ' test_bc runs the bc test suite, if bc has been built\n' + @printf ' test_dc runs the dc test suite, if dc has been built\n' + @printf ' time_test runs the test suite, displaying times for some things\n' + @printf ' time_test_bc runs the bc test suite, displaying times for some things\n' + @printf ' time_test_dc runs the dc test suite, displaying times for some things\n' + @printf ' timeconst runs the test on the Linux timeconst.bc script,\n' + @printf ' if it exists and bc has been built\n' + @printf ' valgrind runs the test suite through valgrind\n' + @printf ' valgrind_bc runs the bc test suite, if bc has been built,\n' + @printf ' through valgrind\n' + @printf ' valgrind_dc runs the dc test suite, if dc has been built,\n' + @printf ' through valgrind\n' + +check: test + +test: test_bc timeconst test_dc + +test_bc: + %%BC_TEST%% + +test_dc: + %%DC_TEST%% + +time_test: time_test_bc timeconst time_test_dc + +time_test_bc: + %%BC_TIME_TEST%% + +time_test_dc: + %%DC_TIME_TEST%% + +timeconst: + %%TIMECONST%% + +valgrind: valgrind_bc valgrind_dc + +valgrind_bc: + %%VG_BC_TEST%% + +valgrind_dc: + %%VG_DC_TEST%% + +karatsuba: + %%KARATSUBA%% + +karatsuba_test: + %%KARATSUBA_TEST%% + +coverage:%%COVERAGE_PREREQS%% + %%COVERAGE%% + +version: + @printf '%s' "$(VERSION)" + +libcname: + @printf '%s' "$(BC_LIB_C)" + +extra_math: + @printf '%s' "$(BC_ENABLE_EXTRA_MATH)" + +manpages: + ronn $(RONN_ARGS) $(BC_RONN) + ronn $(RONN_ARGS) $(DC_RONN) + +clean_gen: + @$(RM) -f $(GEN_EXEC) + +clean:%%CLEAN_PREREQS%% + @printf 'Cleaning files...\n' + @$(RM) -f $(OBJ) + @$(RM) -f $(BC_OBJ) + @$(RM) -f $(DC_OBJ) + @$(RM) -f $(HISTORY_OBJ) + @$(RM) -f $(BC_EXEC) + @$(RM) -f $(DC_EXEC) + @$(RM) -fr $(BIN) + @$(RM) -f *.gcov + @$(RM) -f *.html + @$(RM) -f *.gcda *.gcno + @$(RM) -f *.profraw + @$(RM) -f $(LOCALES)/*.cat + @$(RM) -f $(BC_LIB_C) $(BC_LIB_O) + @$(RM) -f $(BC_LIB2_C) $(BC_LIB2_O) + @$(RM) -f $(BC_HELP_C) $(BC_HELP_O) + @$(RM) -f $(DC_HELP_C) $(DC_HELP_O) + +clean_config: clean + @printf 'Cleaning config...\n' + @$(RM) -f Makefile + +clean_tests: clean clean_config + @printf 'Cleaning test files...\n' + @$(RM) -f tests/bc/parse.txt tests/bc/parse_results.txt + @$(RM) -f tests/bc/print.txt tests/bc/print_results.txt + @$(RM) -f tests/bc/bessel.txt tests/bc/bessel_results.txt + @$(RM) -f tests/bc/scripts/bessel.txt + @$(RM) -f tests/bc/scripts/parse.txt + @$(RM) -f tests/bc/scripts/print.txt + @$(RM) -f tests/bc/scripts/add.txt + @$(RM) -f tests/bc/scripts/divide.txt + @$(RM) -f tests/bc/scripts/multiply.txt + @$(RM) -f tests/bc/scripts/subtract.txt + @$(RM) -f tests/dc/scripts/prime.txt tests/dc/scripts/stream.txt + @$(RM) -f .log_*.txt + @$(RM) -f .math.txt .results.txt .ops.txt + @$(RM) -f .test.txt + @$(RM) -f $(GCDA) $(GCNO) + @$(RM) -f $(BC_GCDA) $(BC_GCNO) + @$(RM) -f $(DC_GCDA) $(DC_GCNO) + @$(RM) -f $(HISTORY_GCDA) $(HISTORY_GCNO) + @$(RM) -f $(BC_LIB_GCDA) $(BC_LIB_GCNO) + @$(RM) -f $(BC_LIB2_GCDA) $(BC_LIB2_GCNO) + @$(RM) -f $(BC_HELP_GCDA) $(BC_HELP_GCNO) + @$(RM) -f $(DC_HELP_GCDA) $(DC_HELP_GCNO) + +install_locales: + $(LOCALE_INSTALL) $(NLSPATH) $(MAIN_EXEC) $(DESTDIR) + +install_bc_manpage: + $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BC_MANPAGE) $(DESTDIR)$(MAN1DIR)/$(BC_MANPAGE_NAME) + +install_dc_manpage: + $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(DC_MANPAGE) $(DESTDIR)$(MAN1DIR)/$(DC_MANPAGE_NAME) + +install:%%INSTALL_LOCALES_PREREQS%%%%INSTALL_PREREQS%% + $(INSTALL) $(DESTDIR)$(BINDIR) "$(EXEC_SUFFIX)" + +uninstall_locales: + $(LOCALE_UNINSTALL) $(NLSPATH) $(MAIN_EXEC) $(DESTDIR) + +uninstall_bc_manpage: + $(RM) -f $(DESTDIR)$(MAN1DIR)/$(BC_MANPAGE_NAME) + +uninstall_bc: + $(RM) -f $(DESTDIR)$(BINDIR)/$(EXEC_PREFIX)$(BC)$(EXEC_SUFFIX) + +uninstall_dc_manpage: + $(RM) -f $(DESTDIR)$(MAN1DIR)/$(DC_MANPAGE_NAME) + +uninstall_dc: + $(RM) -f $(DESTDIR)$(BINDIR)/$(EXEC_PREFIX)$(DC)$(EXEC_SUFFIX) + +uninstall:%%UNINSTALL_LOCALES_PREREQS%%%%UNINSTALL_MAN_PREREQS%%%%UNINSTALL_PREREQS%% Index: contrib/bc/NOTICE.md =================================================================== --- contrib/bc/NOTICE.md +++ contrib/bc/NOTICE.md @@ -0,0 +1,14 @@ +# Notice + +Copyright 2018-2019 Gavin D. Howard and contributors. + +## Contributors + +The contributors to `bc` (besides Gavin) are listed below in alphabetical order. + +* Laurent Bercot (skarnet) +* Stefan Eßer (stesser) +* Michael Forney (michaelforney) +* John Regan (jprjr) +* rofl0r +* Zach van Rijn (me@zv.io) Index: contrib/bc/README.md =================================================================== --- contrib/bc/README.md +++ contrib/bc/README.md @@ -0,0 +1,296 @@ +# `bc` + +[![Build Status][13]][14] +[![codecov][15]][16] +[![Coverity Scan Build Status][17]][18] + +This is an implementation of the [POSIX `bc` calculator][12] that implements +[GNU `bc`][1] extensions, as well as the period (`.`) extension for the BSD +flavor of `bc`. + +For more information, see this `bc`'s [full manual][2]. + +This `bc` also includes an implementation of `dc` in the same binary, accessible +via a symbolic link, which implements all FreeBSD and GNU extensions. (If a +standalone `dc` binary is desired, `bc` can be copied and renamed to `dc`.) The +`!` command is omitted; I believe this poses security concerns and that such +functionality is unnecessary. + +For more information, see the `dc`'s [full manual][3]. + +This `bc` is Free and Open Source Software (FOSS). It is offered under the BSD +2-clause License. Full license text may be found in the [`LICENSE.md`][4] file. + +## Prerequisites + +This `bc` only requires a C99-compatible compiler and a (mostly) POSIX +2008-compatible system with the XSI (X/Open System Interfaces) option group. + +Since POSIX 2008 with XSI requires the existence of a C99 compiler as `c99`, any +POSIX and XSI-compatible system will have everything needed. + +Systems that are known to work: + +* Linux +* FreeBSD +* OpenBSD +* NetBSD +* Mac OSX + +Please submit bug reports if this `bc` does not build out of the box on any +system besides Windows. If Windows binaries are needed, they can be found at +[xstatic][6]. + +## Build + +This `bc` should build unmodified on any POSIX-compliant system. + +For more complex build requirements than the ones below, see the +[build manual][5]. + +### Pre-built Binaries + +It is possible to download pre-compiled binaries for a wide list of platforms, +including Linux- and Windows-based systems, from [xstatic][6]. This link always +points to the latest release of `bc`. + +### Default + +For the default build with optimization, use the following commands in the root +directory: + +``` +./configure.sh -O3 +make +``` + +### One Calculator + +To only build `bc`, use the following commands: + +``` +./configure.sh --disable-dc +make +``` + +To only build `dc`, use the following commands: + +``` +./configure.sh --disable-bc +make +``` + +### Debug + +For debug builds, use the following commands in the root directory: + +``` +./configure.sh -g +make +``` + +### Install + +To install, use the following command: + +``` +make install +``` + +By default, `bc` and `dc` will be installed in `/usr/local`. For installing in +other locations, use the `PREFIX` environment variable when running +`configure.sh` or pass the `--prefix=` option to `configure.sh`. See the +[build manual][5], or run `./configure.sh --help`, for more details. + +### Package and Distro Maintainers + +#### Recommended Optimizations + +I wrote this `bc` with Separation of Concerns, which means that there are many +small functions that could be inlined. However, they are often called across +file boundaries, and the default optimizer can only look at the current file, +which means that they are not inlined. + +Thus, because of the way this `bc` is built, it will automatically be slower +than other `bc` implementations when running scripts with no math. (My `bc`'s +math is *much* faster, so any non-trivial script should run faster in my `bc`.) + +Some, or all, of the difference can be made up with the right optimizations. The +optimizations I recommend are: + +1. `-O3` +2. `-flto` (link-time optimization) +3. `-march=native` (optimize for the current CPU) + +in that order. + +Link-time optimization, in particular, speeds up the `bc` a lot. This is because +when link-time optimization is turned on, the optimizer can look across files +and inline *much* more heavily. + +For packages that are not built on the oldest supported hardware, +`-march=native` is not recommended because of the possibility of illegal +instructions. + +#### Using This `bc` as an Alternative + +If this `bc` is packaged as an alternative to an already existing `bc` package, +it is possible to rename it in the build to prevent name collision. To prepend +to the name, just run the following: + +``` +EXECPREFIX= ./configure.sh +``` + +To append to the name, just run the following: + +``` +EXECSUFFIX= ./configure.sh +``` + +If a package maintainer wishes to add both a prefix and a suffix, that is +allowed. + +**Note**: The suggested name (and package name) is `bc-gh`. + +#### Karatsuba Number + +Package and distro maintainers have one tool at their disposal to build this +`bc` in the optimal configuration: `karatsuba.py`. + +This script is not a compile-time or runtime prerequisite; it is for package and +distro maintainers to run once when a package is being created. It finds the +optimal Karatsuba number (see the [algorithms manual][7] for more information) +for the machine that it is running on. + +If desired, maintainers can also skip running this script because there is a +sane default for the Karatsuba number. + +## Status + +This `bc` is robust. + +It is well-tested, fuzzed, and fully standards-compliant (though not certified) +with POSIX `bc`. The math has been tested with 40+ million random problems, so +it is as correct as I can make it. + +This `bc` can be used as a drop-in replacement for any existing `bc`. This `bc` +is also compatible with MinGW toolchains, though history is not supported on +Windows. + +## Comparison to GNU `bc` + +This `bc` compares favorably to GNU `bc`. + +* It has more extensions, which make this `bc` more useful for scripting. +* This `bc` is a bit more POSIX compliant. +* It has a much less buggy parser. The GNU `bc` will give parse errors for what + is actually valid `bc` code, or should be. For example, putting an `else` on + a new line after a brace can cause GNU `bc` to give a parse error. +* This `bc` has fewer crashes. +* GNU `bc` calculates the wrong number of significant digits for `length(x)`. +* GNU `bc` will sometimes print numbers incorrectly. For example, when running + it on the file `tests/bc/power.txt` in this repo, GNU `bc` gets all the right + answers, but it fails to wrap the numbers at the proper place when outputting + to a file. +* This `bc` is faster. (See [Performance](#performance).) + +### Performance + +Because this `bc` packs more than `1` decimal digit per hardware integer, this +`bc` is faster than GNU `bc` and can be *much* faster. Full benchmarks can be +found at [manuals/benchmarks.md][19]. + +There is one instance where this `bc` is slower: if scripts are light on math. +This is because this `bc`'s intepreter is slightly slower than GNU `bc`, but +that is because it is more robust. See the [benchmarks][19]. + +## Algorithms + +To see what algorithms this `bc` uses, see the [algorithms manual][7]. + +## Locales + +Currently, this `bc` only has support for English (and US English), French and +German locales. Patches are welcome for translations; use the existing `*.msg` +files in `locales/` as a starting point. + +The message files provided assume that locales apply to all regions where a +language is used, but this might not be true for, e.g., `fr_CA` and `fr_CH`. +Any corrections or a confirmation that the current texts are acceptable for +those regions would be appreciated, too. + +## Other Projects + +Other projects based on this bc are: + +* [busybox `bc`][8]. The busybox maintainers have made their own changes, so any + bugs in the busybox `bc` should be reported to them. + +* [toybox `bc`][9]. The maintainer has also made his own changes, so bugs in the + toybox `bc` should be reported there. + +## Language + +This `bc` is written in pure ISO C99, using POSIX 2008 APIs. + +## Commit Messages + +This `bc` uses the commit message guidelines laid out in [this blog post][10]. + +## Semantic Versioning + +This `bc` uses [semantic versioning][11]. + +## Contents + +Items labeled with `(maintainer use only)` are not included in release source +tarballs. + +Files: + + .gitignore The git ignore file (maintainer use only). + .travis.yml The Travis CI file (maintainer use only). + codecov.yml The Codecov file (maintainer use only). + configure.sh The configure script. + functions.sh A script with functions used by other scripts. + install.sh Install script. + karatsuba.py Script to find the optimal Karatsuba number. + LICENSE.md A Markdown form of the BSD 2-clause License. + link.sh A script to link dc to bc. + locale_install.sh A script to install locales, if desired. + locale_uninstall.sh A script to uninstall locales. + Makefile.in The Makefile template. + NOTICE.md List of contributors and copyright owners. + RELEASE.md A checklist for making a release (maintainer use only). + release.sh A script to test for release (maintainer use only). + safe-install.sh Safe install script from musl libc. + +Folders: + + gen The bc math library, help texts, and code to generate C source. + include All header files. + locales Locale files, in .msg format. Patches welcome for translations. + manuals Manuals for both programs. + src All source code. + tests All tests. + +[1]: https://www.gnu.org/software/bc/ +[2]: ./manuals/bc.md +[3]: ./manuals/dc.md +[4]: ./LICENSE.md +[5]: ./manuals/build.md +[6]: https://pkg.musl.cc/bc/ +[7]: ./manuals/algorithms.md +[8]: https://git.busybox.net/busybox/tree/miscutils/bc.c +[9]: https://github.com/landley/toybox/blob/master/toys/pending/bc.c +[10]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html +[11]: http://semver.org/ +[12]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html +[13]: https://travis-ci.com/gavinhoward/bc.svg?branch=master +[14]: https://travis-ci.com/gavinhoward/bc +[15]: https://codecov.io/gh/gavinhoward/bc/branch/master/graph/badge.svg +[16]: https://codecov.io/gh/gavinhoward/bc +[17]: https://img.shields.io/coverity/scan/16609.svg +[18]: https://scan.coverity.com/projects/gavinhoward-bc +[19]: ./manuals/benchmarks.md Index: contrib/bc/RELEASE.md =================================================================== --- contrib/bc/RELEASE.md +++ contrib/bc/RELEASE.md @@ -0,0 +1,51 @@ +# Release Checklist + +This is the checklist for cutting a release. + +1. Update the README. +2. Update the manuals. +3. Test history manually. +4. Test with POSIX test suite. +5. Run the randmath.py script an excessive amount and add failing tests to + test suite. + * debug + * release + * minrelease +6. Fuzz with AFL. + * reldebug +7. Fix AFL crashes. +8. Find ASan crashes on AFL test cases. +9. Fix ASan crashes. +10. Build with xstatic. +11. Run and pass the `release.sh` script on my own machine. +12. Run and pass the `release.sh` script, without generated tests and + sanitizers, on FreeBSD. +13. Run and pass the `release.sh` script, without generated tests, sanitizers, + and 64-bit, on Thalheim's ARM server. +14. Run and pass the test suite on NetBSD. +15. Run and pass the test suite on OpenBSD. +16. Run Coverity Scan and eliminate warnings, if possible (both only). + * debug +17. Run `scan-build make`. +18. Repeat steps 3-14 again and repeat until nothing is found. +19. Change the version (remove "-dev") and commit. +20. Run `make clean_tests`. +21. Run the release script. +22. Upload the custom tarball to GitHub. +23. Add sha's to release notes. +24. Edit release notes for the changelog. +25. Increment to the next version (with "-dev"). +26. Notify the following: + * FreeBSD + * Adelie Linux + * Ataraxia Linux + * Sabotage + * xstatic +27. Submit new packages for the following: + * OpenBSD + * NetBSD + * Alpine Linux + * Void Linux + * Gentoo Linux + * Linux from Scratch + * Arch Linux Index: contrib/bc/gen/bc_help.txt =================================================================== --- contrib/bc/gen/bc_help.txt +++ contrib/bc/gen/bc_help.txt @@ -0,0 +1,115 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The bc help text. + * + */ + +usage: %s [options] [file...] + +bc is a command-line, arbitrary-precision calculator with a Turing-complete +language. For details, use `man %s`. + +This bc is compatible with both the GNU bc and the POSIX bc spec. See the GNU bc +manual (https://www.gnu.org/software/bc/manual/bc.html) and bc spec +(http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) +for details. + +This bc has three differences to the GNU bc: + + 1) Arrays can be passed to the builtin "length" function to get the number of + elements currently in the array. The following example prints "1": + + a[0] = 0 + length(a[]) + + 2) The precedence of the boolean "not" operator (!) is equal to that of the + unary minus (-), or negation, operator. This still allows POSIX-compliant + scripts to work while somewhat preserving expected behavior (versus C) and + making parsing easier. + 3) This bc has many more extensions than the GNU bc does. For details, see the + man page. + +Options: + + -e expr --expression=expr + + Run "expr" and quit. If multiple expressions or files (see below) are + given, they are all run before executing from stdin. + + -f file --file=file + + Run the bc code in "file" and exit. See above as well. + + -g --global-stacks + + Turn scale, ibase, and obase into stacks. This makes the value of each be + be restored on returning from functions. See the man page for more + details. + + -h --help + + Print this usage message and exit. + + -i --interactive + + Force interactive mode. + + -l --mathlib + + Use predefined math routines: + + s(expr) = sine of expr in radians + c(expr) = cosine of expr in radians + a(expr) = arctangent of expr, returning radians + l(expr) = natural log of expr + e(expr) = raises e to the power of expr + j(n, x) = Bessel function of integer order n of x + + -P --no-prompt + + Disable the prompt in interactive mode. + + -q --quiet + + Don't print version and copyright. + + -s --standard + + Error if any non-POSIX extensions are used. + + -w --warn + + Warn if any non-POSIX extensions are used. + + -v --version + + Print version information and copyright and exit. Index: contrib/bc/gen/dc_help.txt =================================================================== --- contrib/bc/gen/dc_help.txt +++ contrib/bc/gen/dc_help.txt @@ -0,0 +1,101 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The dc help text. + * + */ + +usage: %s [options] [file...] + +dc is a reverse-polish notation command-line calculator which supports unlimited +precision arithmetic. For details, use `man %s`. + +This dc is (mostly) compatible with the FreeBSD dc and the GNU dc. See the +FreeBSD man page (https://www.unix.com/man-page/FreeBSD/1/dc/) and the GNU dc +manual (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html) for +details. + +This dc has a few differences from the two above: + + 1) When printing a byte stream (command "P"), this bc follows what the FreeBSD + dc does. + 2) This dc implements the GNU extensions for divmod ("~") and modular + exponentiation ("|"). + 3) This dc implements all FreeBSD extensions, except for "J" and "M". + 4) This dc does not implement the run command ("!"), for security reasons. + 5) Like the FreeBSD dc, this dc supports extended registers. However, they are + implemented differently. When it encounters whitespace where a register + should be, it skips the whitespace. If the character following is not + a lowercase letter, an error is issued. Otherwise, the register name is + parsed by the following regex: + + [a-z][a-z0-9_]* + + This generally means that register names will be surrounded by whitespace. + + Examples: + + l idx s temp L index S temp2 < do_thing + + Also note that, unlike the FreeBSD dc, extended registers are not even + parsed unless the "-x" option is given. Instead, the space after a command + that requires a register name is taken as the register name. + +Options: + + -e expr --expression=expr + + Run "expr" and quit. If multiple expressions or files (see below) are + given, they are all run. After running, dc will exit. + + -f file --file=file + + Run the dc code in "file" and exit. See above. + + -h --help + + Print this usage message and exit. + + -i --interactive + + Put dc into interactive mode. See the man page for more details. + + -P --no-prompt + + Disable the prompt in interactive mode. + + -V --version + + Print version and copyright and exit. + + -x --extended-register + + Enable extended register mode. Index: contrib/bc/gen/lib.bc =================================================================== --- contrib/bc/gen/lib.bc +++ contrib/bc/gen/lib.bc @@ -0,0 +1,201 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The bc math library. + * + */ + +scale=20 +define e(x){ + auto b,s,n,r,d,i,p,f,v + b=ibase + ibase=A + if(x<0){ + n=1 + x=-x + } + s=scale + r=6+s+.44*x + scale=scale(x)+1 + while(x>1){ + d+=1 + x/=2 + scale+=1 + } + scale=r + r=x+1 + p=x + f=v=1 + for(i=2;v;++i){ + p*=x + f*=i + v=p/f + r+=v + } + while(d--)r*=r + scale=s + ibase=b + if(n)return(1/r) + return(r/1) +} +define l(x){ + auto b,s,r,p,a,q,i,v + if(x<=0)return((1-A^scale)/1) + b=ibase + ibase=A + s=scale + scale+=6 + p=2 + while(x>=2){ + p*=2 + x=sqrt(x) + } + while(x<=.5){ + p*=2 + x=sqrt(x) + } + r=a=(x-1)/(x+1) + q=a*a + v=1 + for(i=3;v;i+=2){ + a*=q + v=a/i + r+=v + } + r*=p + scale=s + ibase=b + return(r/1) +} +define s(x){ + auto b,s,r,a,q,i + if(x<0)return(-s(-x)) + b=ibase + ibase=A + s=scale + scale=1.1*s+2 + a=a(1) + scale=0 + q=(x/a+2)/4 + x-=4*q*a + if(q%2)x=-x + scale=s+2 + r=a=x + q=-x*x + for(i=3;a;i+=2){ + a*=q/(i*(i-1)) + r+=a + } + scale=s + ibase=b + return(r/1) +} +define c(x){ + auto b,s + b=ibase + ibase=A + s=scale + scale*=1.2 + x=s(2*a(1)+x) + scale=s + ibase=b + return(x/1) +} +define a(x){ + auto b,s,r,n,a,m,t,f,i,u + b=ibase + ibase=A + n=1 + if(x<0){ + n=-1 + x=-x + } + if(scale<65){ + if(x==1){ + r=.7853981633974483096156608458198757210492923498437764552437361480/n + ibase=b + return(r) + } + if(x==.2){ + r=.1973955598498807583700497651947902934475851037878521015176889402/n + ibase=b + return(r) + } + } + s=scale + if(x>.2){ + scale+=5 + a=a(.2) + } + scale=s+3 + while(x>.2){ + m+=1 + x=(x-.2)/(1+.2*x) + } + r=u=x + f=-x*x + t=1 + for(i=3;t;i+=2){ + u*=f + t=u/i + r+=t + } + scale=s + ibase=b + return((m*a+r)/n) +} +define j(n,x){ + auto b,s,o,a,i,v,f + b=ibase + ibase=A + s=scale + scale=0 + n/=1 + if(n<0){ + n=-n + o=n%2 + } + a=1 + for(i=2;i<=n;++i)a*=i + scale=1.5*s + a=(x^n)/2^n/a + r=v=1 + f=-x*x/4 + scale+=length(a)-scale(a) + for(i=1;v;++i){ + v=v*f/i/(n+i) + r+=v + } + scale=s + ibase=b + if(o)a=-a + return(a*r/1) +} Index: contrib/bc/gen/lib2.bc =================================================================== --- contrib/bc/gen/lib2.bc +++ contrib/bc/gen/lib2.bc @@ -0,0 +1,277 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The second bc math library. + * + */ + +define r(x,p){ + auto t,n + if(x==0)return x + p=abs(p)$ + n=(x<0) + x=abs(x) + t=x@p + if(p=5>>p+1)t+=1>>p + if(n)t=-t + return t +} +define ceil(x,p){ + auto t,n + if(x==0)return x + p=abs(p)$ + n=(x<0) + x=abs(x) + t=(x+(9>>p+1))@p + if(n)t=-t + return t +} +define f(n){ + auto r + n=abs(n)$ + for(r=1;n>1;--n)r*=n + return r +} +define perm(n,k){ + auto f,g,s + if(k>n)return 0 + n=abs(n)$ + k=abs(k)$ + f=f(n) + g=f(n-k) + s=scale + scale=0 + f/=g + scale=s + return f +} +define comb(n,r){ + auto s,f,g,h + if(r>n)return 0 + n=abs(n)$ + r=abs(r)$ + s=scale + scale=0 + f=f(n) + h=f(r) + g=f(n-r) + f/=h*g + scale=s + return f +} +define log(x,b){ + auto p,s + s=scale + if(scalescale)scale=scale(x) + scale*=2 + p=l(x)/l(b) + scale=s + return p +} +define l2(x){return log(x,2)} +define l10(x){return log(x,A)} +define pi(s){ + auto t,v + if(s==0)return 3 + s=abs(s)$ + t=scale + scale=s+1 + v=4*a(1) + scale=t + return v@s +} +define t(x){ + auto s,c,l + l=scale + scale+=2 + s=s(x) + c=c(x) + scale=l + return s/c +} +define a2(y,x){ + auto a,p + if(!x&&!y)y/x + if(x<=0){ + p=pi(scale+2) + if(y<0)p=-p + } + if(x==0)a=p/2 + else{ + scale+=2 + a=a(y/x)+p + scale-=2 + } + return a@scale +} +define sin(x){return s(x)} +define cos(x){return c(x)} +define atan(x){return a(x)} +define tan(x){return t(x)} +define atan2(y,x){return a2(y,x)} +define r2d(x){ + auto r,i,s + s=scale + scale+=5 + i=ibase + ibase=A + r=x*180/pi(scale) + ibase=i + scale=s + return r@s +} +define d2r(x){ + auto r,i,s + s=scale + scale+=5 + i=ibase + ibase=A + r=x*pi(scale)/180 + ibase=i + scale=s + return r@s +} +define void output(x,b){ + auto c + c=obase + obase=b + x + obase=c +} +define void hex(x){output(x,G)} +define void binary(x){output(x,2)} +define ubytes(x){ + auto p,b,i + b=ibase + ibase=A + x=abs(x)$ + i=2^8 + for(p=1;i-1p||(!z&&x==p))n*=2 + ibase=b + return n +} +define void output_byte(x,i){ + auto j,p,y,b + j=ibase + ibase=A + s=scale + scale=0 + x=abs(x)$ + b=x/(2^(i*8)) + b%=2^8 + y=log(256,obase)$ + if(b>1)p=log(b,obase)$+1 + else p=b + for(i=y-p;i>0;--i)print 0 + if(b)print b + scale=s + ibase=j +} +define void output_uint(x,n){ + auto i,b + b=ibase + ibase=A + for(i=n-1;i>=0;--i){ + output_byte(x,i) + if(i)print" " + else print"\n" + } + ibase=b +} +define void hex_uint(x,n){ + auto o + o=obase + obase=G + output_uint(x,n) + obase=o +} +define void binary_uint(x,n){ + auto o + o=obase + obase=2 + output_uint(x,n) + obase=o +} +define void uintn(x,n){ + if(scale(x)){ + print"Error: ",x," is not an integer.\n" + return + } + if(x<0){ + print"Error: ",x," is negative.\n" + return + } + if(x>=2^(n*8)){ + print"Error: ",x," cannot fit into ",n," unsigned byte(s).\n" + return + } + binary_uint(x,n) + hex_uint(x,n) +} +define void intn(x,n){ + auto t + if(scale(x)){ + print"Error: ",x," is not an integer.\n" + return + } + t=2^(n*8-1) + if(abs(x)>=t&&(x>0||x!=-t)){ + print "Error: ",x," cannot fit into ",n," signed byte(s).\n" + return + } + if(x<0)x=2^(n*8)-(-x) + binary_uint(x,n) + hex_uint(x,n) +} +define void uint8(x){uintn(x,1)} +define void int8(x){intn(x,1)} +define void uint16(x){uintn(x,2)} +define void int16(x){intn(x,2)} +define void uint32(x){uintn(x,4)} +define void int32(x){intn(x,4)} +define void uint64(x){uintn(x,8)} +define void int64(x){intn(x,8)} +define void uint(x){uintn(x,ubytes(x))} +define void int(x){intn(x,sbytes(x))} Index: contrib/bc/gen/strgen.sh =================================================================== --- contrib/bc/gen/strgen.sh +++ contrib/bc/gen/strgen.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +export LANG=C +export LC_CTYPE=C + +progname=${0##*/} + +if [ $# -lt 4 ]; then + echo "usage: $progname input output name header [label [define [remove_tabs]]]" + exit 1 +fi + +input="$1" +output="$2" +name="$3" +header="$4" +label="$5" +define="$6" +remove_tabs="$7" + +exec < "$input" +exec > "$output" + +if [ -n "$label" ]; then + nameline="const char *${label} = \"${input}\";" +fi + +if [ -n "$define" ]; then + condstart="#if ${define}" + condend="#endif" +fi + +if [ -n "$remove_tabs" ]; then + if [ "$remove_tabs" -ne 0 ]; then + remtabsexpr='s: ::g;' + fi +fi + +cat< + +$nameline + +const char ${name}[] = +$(sed -e "$remtabsexpr " -e '1,/^$/d; s:\\n:\\\\n:g; s:":\\":g; s:^:":; s:$:\\n":') +; +${condend} +EOF Index: contrib/bc/include/args.h =================================================================== --- contrib/bc/include/args.h +++ contrib/bc/include/args.h @@ -0,0 +1,46 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for processing command-line arguments. + * + */ + +#ifndef BC_ARGS_H +#define BC_ARGS_H + +#include +#include + +BcStatus bc_args(int argc, char *argv[]); + +extern const char* const bc_args_env_name; + +#endif // BC_ARGS_H Index: contrib/bc/include/bc.h =================================================================== --- contrib/bc/include/bc.h +++ contrib/bc/include/bc.h @@ -0,0 +1,168 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for bc. + * + */ + +#ifndef BC_BC_H +#define BC_BC_H + +#if BC_ENABLED + +#include +#include + +#include +#include +#include + +int bc_main(int argc, char **argv); + +extern const char bc_help[]; +extern const char bc_lib[]; +extern const char* bc_lib_name; +#if BC_ENABLE_EXTRA_MATH +extern const char bc_lib2[]; +extern const char* bc_lib2_name; +#endif // BC_ENABLE_EXTRA_MATH + +typedef struct BcLexKeyword { + uchar data; + const char name[9]; +} BcLexKeyword; + +#define BC_LEX_CHAR_MSB(bit) ((bit) << (CHAR_BIT - 1)) + +#define BC_LEX_KW_POSIX(kw) ((kw)->data & (BC_LEX_CHAR_MSB(1))) +#define BC_LEX_KW_LEN(kw) ((size_t) ((kw)->data & ~(BC_LEX_CHAR_MSB(1)))) + +#define BC_LEX_KW_ENTRY(a, b, c) \ + { .data = ((b) & ~(BC_LEX_CHAR_MSB(1))) | BC_LEX_CHAR_MSB(c),.name = a } + +extern const BcLexKeyword bc_lex_kws[]; +extern const size_t bc_lex_kws_len; + +BcStatus bc_lex_token(BcLex *l); + +#define BC_PARSE_TOP_FLAG_PTR(p) ((uint16_t*) bc_vec_top(&(p)->flags)) +#define BC_PARSE_TOP_FLAG(p) (*(BC_PARSE_TOP_FLAG_PTR(p))) + +#define BC_PARSE_FLAG_BRACE (UINTMAX_C(1)<<0) +#define BC_PARSE_BRACE(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_BRACE) + +#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) + +#define BC_PARSE_FLAG_FUNC (UINTMAX_C(1)<<2) +#define BC_PARSE_FUNC(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_FUNC) + +#define BC_PARSE_FLAG_BODY (UINTMAX_C(1)<<3) +#define BC_PARSE_BODY(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_BODY) + +#define BC_PARSE_FLAG_LOOP (UINTMAX_C(1)<<4) +#define BC_PARSE_LOOP(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_LOOP) + +#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) + +#define BC_PARSE_FLAG_IF (UINTMAX_C(1)<<6) +#define BC_PARSE_IF(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_IF) + +#define BC_PARSE_FLAG_ELSE (UINTMAX_C(1)<<7) +#define BC_PARSE_ELSE(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_ELSE) + +#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) + +#define BC_PARSE_NO_EXEC(p) ((p)->flags.len != 1 || BC_PARSE_TOP_FLAG(p) != 0) + +#define BC_PARSE_DELIMITER(t) \ + ((t) == BC_LEX_SCOLON || (t) == BC_LEX_NLINE || (t) == BC_LEX_EOF) + +#define BC_PARSE_BLOCK_STMT(f) \ + ((f) & (BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_LOOP_INNER)) + +#define BC_PARSE_OP(p, l) (((p) & ~(BC_LEX_CHAR_MSB(1))) | (BC_LEX_CHAR_MSB(l))) + +#define BC_PARSE_OP_DATA(t) bc_parse_ops[((t) - BC_LEX_OP_INC)] +#define BC_PARSE_OP_LEFT(op) (BC_PARSE_OP_DATA(op) & BC_LEX_CHAR_MSB(1)) +#define BC_PARSE_OP_PREC(op) (BC_PARSE_OP_DATA(op) & ~(BC_LEX_CHAR_MSB(1))) + +#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)) + +#define BC_PARSE_EXPR(i) \ + (bc_parse_exprs[(((i) & (uchar) ~(0x07)) >> 3)] & (1 << (7 - ((i) & 0x07)))) + +#define BC_PARSE_TOP_OP(p) (*((BcLexType*) bc_vec_top(&(p)->ops))) +#define BC_PARSE_LEAF(prev, bin_last, rparen) \ + (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev))) +#define BC_PARSE_INST_VAR(t) \ + ((t) >= BC_INST_VAR && (t) <= BC_INST_SCALE && (t) != BC_INST_ARRAY) + +#define BC_PARSE_PREV_PREFIX(p) \ + ((p) >= BC_INST_INC_PRE && (p) <= BC_INST_BOOL_NOT) +#define BC_PARSE_OP_PREFIX(t) ((t) == BC_LEX_OP_BOOL_NOT || (t) == BC_LEX_NEG) + +// We can calculate the conversion between tokens and exprs by subtracting the +// position of the first operator in the lex enum and adding the position of +// the first in the expr enum. Note: This only works for binary operators. +#define BC_PARSE_TOKEN_INST(t) ((uchar) ((t) - BC_LEX_NEG + BC_INST_NEG)) + +BcStatus bc_parse_expr(BcParse *p, uint8_t flags); + +BcStatus bc_parse_parse(BcParse *p); +BcStatus bc_parse_expr_status(BcParse *p, uint8_t flags, BcParseNext next); + +// This is necessary to clear up for if statements at the end of files. +void bc_parse_noElse(BcParse *p); + +#if BC_ENABLE_SIGNALS +extern const char bc_sig_msg[]; +#endif // BC_ENABLE_SIGNALS + +extern const char* const bc_parse_const1; +extern const uint8_t bc_parse_exprs[]; +extern const uchar bc_parse_ops[]; +extern const BcParseNext bc_parse_next_expr; +extern const BcParseNext bc_parse_next_param; +extern const BcParseNext bc_parse_next_print; +extern const BcParseNext bc_parse_next_rel; +extern const BcParseNext bc_parse_next_elem; +extern const BcParseNext bc_parse_next_for; +extern const BcParseNext bc_parse_next_read; + +#endif // BC_ENABLED + +#endif // BC_BC_H Index: contrib/bc/include/dc.h =================================================================== --- contrib/bc/include/dc.h +++ contrib/bc/include/dc.h @@ -0,0 +1,67 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for bc. + * + */ + +#ifndef BC_DC_H +#define BC_DC_H + +#if DC_ENABLED + +#include +#include +#include + +int dc_main(int argc, char **argv); + +extern const char dc_help[]; + +BcStatus dc_lex_token(BcLex *l); +bool dc_lex_negCommand(BcLex *l); + +#if BC_ENABLE_SIGNALS +extern const char dc_sig_msg[]; +#endif // BC_ENABLE_SIGNALS + +extern const uint8_t dc_lex_regs[]; +extern const size_t dc_lex_regs_len; + +extern const uint8_t dc_lex_tokens[]; +extern const uint8_t dc_parse_insts[]; + +BcStatus dc_parse_parse(BcParse *p); +BcStatus dc_parse_expr(BcParse *p, uint8_t flags); + +#endif // DC_ENABLED + +#endif // BC_DC_H Index: contrib/bc/include/history.h =================================================================== --- contrib/bc/include/history.h +++ contrib/bc/include/history.h @@ -0,0 +1,244 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Adapted from the following: + * + * linenoise.c -- guerrilla line editing library against the idea that a + * line editing lib needs to be 20,000 lines of C code. + * + * You can find the original source code at: + * http://github.com/antirez/linenoise + * + * You can find the fork that this code is based on at: + * https://github.com/rain-1/linenoise-mob + * + * ------------------------------------------------------------------------ + * + * This code is also under the following license: + * + * Copyright (c) 2010-2016, Salvatore Sanfilippo + * Copyright (c) 2010-2013, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for line history. + * + */ + +#ifndef BC_HISTORY_H +#define BC_HISTORY_H + +#ifndef BC_ENABLE_HISTORY +#define BC_ENABLE_HISTORY (1) +#endif // BC_ENABLE_HISTORY + +#if BC_ENABLE_HISTORY + +#ifdef _WIN32 +#error History is not supported on Windows. +#endif + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#define BC_HIST_DEF_COLS (80) +#define BC_HIST_MAX_LEN (128) +#define BC_HIST_MAX_LINE (4095) + +#define BC_HIST_BUF_LEN(h) ((h)->buf.len - 1) +#define BC_HIST_WRITE(s, n) (write(STDERR_FILENO, (s), (n)) != (ssize_t) (n)) +#define BC_HIST_READ(s, n) (read(STDIN_FILENO, (s), (n)) == -1) + +#define BC_HIST_NEXT (false) +#define BC_HIST_PREV (true) + +#if BC_DEBUG_CODE +#define lndebug(...) \ + do { \ + if (bc_history_debug_fp == NULL) { \ + bc_history_debug_fp = fopen("/tmp/lndebug.txt","a"); \ + fprintf(bc_history_debug_fp, \ + "[%zu %zu %zu] p: %d, rows: %d, " \ + "rpos: %d, max: %zu, oldmax: %d\n", \ + l->len, l->pos, l->oldcolpos, plen, rows, rpos, \ + l->maxrows, old_rows); \ + } \ + fprintf(bc_history_debug_fp, ", " __VA_ARGS__); \ + fflush(bc_history_debug_fp); \ + } while (0) +#else // BC_DEBUG_CODE +#define lndebug(fmt, ...) +#endif // BC_DEBUG_CODE + +typedef enum BcHistoryAction { + + BC_ACTION_NULL = 0, + BC_ACTION_CTRL_A = 1, + BC_ACTION_CTRL_B = 2, + BC_ACTION_CTRL_C = 3, + BC_ACTION_CTRL_D = 4, + BC_ACTION_CTRL_E = 5, + BC_ACTION_CTRL_F = 6, + BC_ACTION_CTRL_H = 8, + BC_ACTION_TAB = 9, + BC_ACTION_LINE_FEED = 10, + BC_ACTION_CTRL_K = 11, + BC_ACTION_CTRL_L = 12, + BC_ACTION_ENTER = 13, + BC_ACTION_CTRL_N = 14, + BC_ACTION_CTRL_P = 16, + BC_ACTION_CTRL_T = 20, + BC_ACTION_CTRL_U = 21, + BC_ACTION_CTRL_W = 23, + BC_ACTION_CTRL_Z = 26, + BC_ACTION_ESC = 27, + BC_ACTION_BACKSPACE = 127 + +} BcHistoryAction; + +/** + * This represents the state during line editing. We pass this state + * to functions implementing specific editing functionalities. + */ +typedef struct BcHistory { + + /// Edited line buffer. + BcVec buf; + + /// The history. + BcVec history; + + /// A temporary buffer for refresh. Using this + /// prevents an allocation on every refresh. + BcVec tmp; + + /// Prompt to display. + const char *prompt; + + /// Prompt length. + size_t plen; + + /// Prompt column length. + size_t pcol; + + /// Current cursor position. + size_t pos; + + /// Previous refresh cursor column position. + size_t oldcolpos; + + /// Number of columns in terminal. + size_t cols; + + /// The history index we are currently editing. + size_t idx; + + /// The original terminal state. + struct termios orig_termios; + + /// This is to check if stdin has more data. + fd_set rdset; + + /// This is to check if stdin has more data. + struct timespec ts; + + /// This is to check if stdin has more data. + sigset_t sigmask; + + /// This is to signal that there is more, so we don't process yet. + bool stdin_has_data; + + /// Whether we are in rawmode. + bool rawMode; + + /// Whether the terminal is bad. + bool badTerm; + +} BcHistory; + +BcStatus bc_history_line(BcHistory *h, BcVec *vec, const char *prompt); +void bc_history_add(BcHistory *h, char *line); + +void bc_history_init(BcHistory *h); +void bc_history_free(BcHistory *h); + +extern const char *bc_history_bad_terms[]; +extern const char bc_history_tab[]; +extern const size_t bc_history_tab_len; +extern const char bc_history_ctrlc[]; +extern const uint32_t bc_history_wchars[][2]; +extern const size_t bc_history_wchars_len; +extern const uint32_t bc_history_combo_chars[]; +extern const size_t bc_history_combo_chars_len; +#if BC_DEBUG_CODE +extern FILE *bc_history_debug_fp; +BcStatus bc_history_printKeyCodes(BcHistory* l); +#endif // BC_DEBUG_CODE + +#endif // BC_ENABLE_HISTORY + +#endif // BC_HISTORY_H Index: contrib/bc/include/lang.h =================================================================== --- contrib/bc/include/lang.h +++ contrib/bc/include/lang.h @@ -0,0 +1,304 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for program data. + * + */ + +#ifndef BC_LANG_H +#define BC_LANG_H + +#include + +#include +#include +#include + +#if BC_ENABLED +#define BC_INST_IS_ASSIGN(i) \ + ((i) == BC_INST_ASSIGN || (i) == BC_INST_ASSIGN_NO_VAL) +#define BC_INST_USE_VAL(i) ((i) <= BC_INST_ASSIGN) +#else // BC_ENABLED +#define BC_INST_IS_ASSIGN(i) ((i) == BC_INST_ASSIGN_NO_VAL) +#define BC_INST_USE_VAL(i) (false) +#endif // BC_ENABLED + +typedef enum BcInst { + +#if BC_ENABLED + BC_INST_INC_POST = 0, + BC_INST_DEC_POST, + BC_INST_INC_PRE, + BC_INST_DEC_PRE, +#endif // BC_ENABLED + + BC_INST_NEG, + BC_INST_BOOL_NOT, +#if BC_ENABLE_EXTRA_MATH + BC_INST_TRUNC, +#endif // BC_ENABLE_EXTRA_MATH + + BC_INST_POWER, + BC_INST_MULTIPLY, + BC_INST_DIVIDE, + BC_INST_MODULUS, + BC_INST_PLUS, + BC_INST_MINUS, + +#if BC_ENABLE_EXTRA_MATH + BC_INST_PLACES, + + BC_INST_LSHIFT, + BC_INST_RSHIFT, +#endif // BC_ENABLE_EXTRA_MATH + + BC_INST_REL_EQ, + BC_INST_REL_LE, + BC_INST_REL_GE, + BC_INST_REL_NE, + BC_INST_REL_LT, + BC_INST_REL_GT, + + BC_INST_BOOL_OR, + BC_INST_BOOL_AND, + +#if BC_ENABLED + BC_INST_ASSIGN_POWER, + BC_INST_ASSIGN_MULTIPLY, + BC_INST_ASSIGN_DIVIDE, + BC_INST_ASSIGN_MODULUS, + BC_INST_ASSIGN_PLUS, + BC_INST_ASSIGN_MINUS, +#if BC_ENABLE_EXTRA_MATH + BC_INST_ASSIGN_PLACES, + BC_INST_ASSIGN_LSHIFT, + BC_INST_ASSIGN_RSHIFT, +#endif // BC_ENABLE_EXTRA_MATH + BC_INST_ASSIGN, + + BC_INST_INC_NO_VAL, + BC_INST_DEC_NO_VAL, + + BC_INST_ASSIGN_POWER_NO_VAL, + BC_INST_ASSIGN_MULTIPLY_NO_VAL, + BC_INST_ASSIGN_DIVIDE_NO_VAL, + BC_INST_ASSIGN_MODULUS_NO_VAL, + BC_INST_ASSIGN_PLUS_NO_VAL, + BC_INST_ASSIGN_MINUS_NO_VAL, +#if BC_ENABLE_EXTRA_MATH + BC_INST_ASSIGN_PLACES_NO_VAL, + BC_INST_ASSIGN_LSHIFT_NO_VAL, + BC_INST_ASSIGN_RSHIFT_NO_VAL, +#endif // BC_ENABLE_EXTRA_MATH +#endif // BC_ENABLED + BC_INST_ASSIGN_NO_VAL, + + BC_INST_NUM, + BC_INST_VAR, + BC_INST_ARRAY_ELEM, + BC_INST_ARRAY, + + BC_INST_ONE, + +#if BC_ENABLED + BC_INST_LAST, +#endif // BC_ENABLED + BC_INST_IBASE, + BC_INST_OBASE, + BC_INST_SCALE, + BC_INST_LENGTH, + BC_INST_SCALE_FUNC, + BC_INST_SQRT, + BC_INST_ABS, + BC_INST_READ, + BC_INST_MAXIBASE, + BC_INST_MAXOBASE, + BC_INST_MAXSCALE, + + BC_INST_PRINT, + BC_INST_PRINT_POP, + BC_INST_STR, + BC_INST_PRINT_STR, + +#if BC_ENABLED + BC_INST_JUMP, + BC_INST_JUMP_ZERO, + + BC_INST_CALL, + + BC_INST_RET, + BC_INST_RET0, + BC_INST_RET_VOID, + + BC_INST_HALT, +#endif // BC_ENABLED + +#if DC_ENABLED + BC_INST_POP, + BC_INST_POP_EXEC, + BC_INST_MODEXP, + BC_INST_DIVMOD, + + BC_INST_EXECUTE, + BC_INST_EXEC_COND, + + BC_INST_ASCIIFY, + BC_INST_PRINT_STREAM, + + BC_INST_PRINT_STACK, + BC_INST_CLEAR_STACK, + BC_INST_STACK_LEN, + BC_INST_DUPLICATE, + BC_INST_SWAP, + + BC_INST_LOAD, + BC_INST_PUSH_VAR, + BC_INST_PUSH_TO_VAR, + + BC_INST_QUIT, + BC_INST_NQUIT, +#endif // DC_ENABLED + + BC_INST_INVALID = UCHAR_MAX, + +} BcInst; + +typedef struct BcId { + char *name; + size_t idx; +} BcId; + +typedef struct BcLoc { + size_t loc; + size_t idx; +} BcLoc; + +typedef struct BcConst { + char *val; + BcBigDig base; + BcNum num; +} BcConst; + +typedef struct BcFunc { + + BcVec code; +#if BC_ENABLED + BcVec labels; + BcVec autos; + size_t nparams; +#endif // BC_ENABLED + + BcVec strs; + BcVec consts; + + const char *name; +#if BC_ENABLED + bool voidfn; +#endif // BC_ENABLED + +} BcFunc; + +typedef enum BcResultType { + + BC_RESULT_VAR, + BC_RESULT_ARRAY_ELEM, + BC_RESULT_ARRAY, + + BC_RESULT_STR, + + BC_RESULT_CONSTANT, + BC_RESULT_TEMP, + + BC_RESULT_ONE, + +#if BC_ENABLED + BC_RESULT_LAST, + BC_RESULT_VOID, +#endif // BC_ENABLED + BC_RESULT_IBASE, + BC_RESULT_OBASE, + BC_RESULT_SCALE, + +} BcResultType; + +typedef union BcResultData { + BcNum n; + BcVec v; + BcLoc loc; +} BcResultData; + +typedef struct BcResult { + BcResultType t; + BcResultData d; +} BcResult; + +typedef struct BcInstPtr { + size_t func; + size_t idx; + size_t len; +} BcInstPtr; + +typedef enum BcType { + BC_TYPE_VAR, + BC_TYPE_ARRAY, +#if BC_ENABLED + BC_TYPE_REF, +#endif // BC_ENABLED +} BcType; + +struct BcProgram; + +void bc_func_init(BcFunc *f, const char* name); +BcStatus bc_func_insert(BcFunc *f, struct BcProgram* p, char* name, + BcType type, size_t line); +void bc_func_reset(BcFunc *f); +void bc_func_free(void *func); + +void bc_array_init(BcVec *a, bool nums); +void bc_array_copy(BcVec *d, const BcVec *s); + +void bc_string_free(void *string); +void bc_const_free(void *constant); +void bc_id_free(void *id); +void bc_result_copy(BcResult *d, BcResult *src); +void bc_result_free(void *result); + +void bc_array_expand(BcVec *a, size_t len); +int bc_id_cmp(const BcId *e1, const BcId *e2); + +#if BC_DEBUG_CODE +extern const char bc_inst_chars[]; +#endif // BC_DEBUG_CODE + +extern const char bc_func_main[]; +extern const char bc_func_read[]; + +#endif // BC_LANG_H Index: contrib/bc/include/lex.h =================================================================== --- contrib/bc/include/lex.h +++ contrib/bc/include/lex.h @@ -0,0 +1,217 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for bc's lexer. + * + */ + +#ifndef BC_LEX_H +#define BC_LEX_H + +#include +#include + +#include +#include +#include + +#define bc_lex_err(l, e) (bc_vm_error((e), (l)->line)) +#define bc_lex_verr(l, e, ...) (bc_vm_error((e), (l)->line, __VA_ARGS__)) + +#define BC_LEX_NEG_CHAR (BC_IS_BC ? '-' : '_') +#define BC_LEX_LAST_NUM_CHAR (BC_IS_BC ? 'Z' : 'F') +#define BC_LEX_NUM_CHAR(c, pt, int_only) \ + (isdigit(c) || ((c) >= 'A' && (c) <= BC_LEX_LAST_NUM_CHAR) || \ + ((c) == '.' && !(pt) && !(int_only))) + +// BC_LEX_NEG is not used in lexing; it is only for parsing. +typedef enum BcLexType { + + BC_LEX_EOF, + BC_LEX_INVALID, + +#if BC_ENABLED + BC_LEX_OP_INC, + BC_LEX_OP_DEC, +#endif // BC_ENABLED + + BC_LEX_NEG, + BC_LEX_OP_BOOL_NOT, +#if BC_ENABLE_EXTRA_MATH + BC_LEX_OP_TRUNC, +#endif // BC_ENABLE_EXTRA_MATH + + BC_LEX_OP_POWER, + BC_LEX_OP_MULTIPLY, + BC_LEX_OP_DIVIDE, + BC_LEX_OP_MODULUS, + BC_LEX_OP_PLUS, + BC_LEX_OP_MINUS, + +#if BC_ENABLE_EXTRA_MATH + BC_LEX_OP_PLACES, + + BC_LEX_OP_LSHIFT, + BC_LEX_OP_RSHIFT, +#endif // BC_ENABLE_EXTRA_MATH + + BC_LEX_OP_REL_EQ, + BC_LEX_OP_REL_LE, + BC_LEX_OP_REL_GE, + BC_LEX_OP_REL_NE, + BC_LEX_OP_REL_LT, + BC_LEX_OP_REL_GT, + + BC_LEX_OP_BOOL_OR, + BC_LEX_OP_BOOL_AND, + +#if BC_ENABLED + BC_LEX_OP_ASSIGN_POWER, + BC_LEX_OP_ASSIGN_MULTIPLY, + BC_LEX_OP_ASSIGN_DIVIDE, + BC_LEX_OP_ASSIGN_MODULUS, + BC_LEX_OP_ASSIGN_PLUS, + BC_LEX_OP_ASSIGN_MINUS, +#if BC_ENABLE_EXTRA_MATH + BC_LEX_OP_ASSIGN_PLACES, + BC_LEX_OP_ASSIGN_LSHIFT, + BC_LEX_OP_ASSIGN_RSHIFT, +#endif // BC_ENABLE_EXTRA_MATH +#endif // BC_ENABLED + BC_LEX_OP_ASSIGN, + + BC_LEX_NLINE, + BC_LEX_WHITESPACE, + + BC_LEX_LPAREN, + BC_LEX_RPAREN, + + BC_LEX_LBRACKET, + BC_LEX_COMMA, + BC_LEX_RBRACKET, + + BC_LEX_LBRACE, + BC_LEX_SCOLON, + BC_LEX_RBRACE, + + BC_LEX_STR, + BC_LEX_NAME, + BC_LEX_NUMBER, + +#if BC_ENABLED + BC_LEX_KW_AUTO, + BC_LEX_KW_BREAK, + BC_LEX_KW_CONTINUE, + BC_LEX_KW_DEFINE, + BC_LEX_KW_FOR, + BC_LEX_KW_IF, + BC_LEX_KW_LIMITS, + BC_LEX_KW_RETURN, + BC_LEX_KW_WHILE, + BC_LEX_KW_HALT, + BC_LEX_KW_LAST, +#endif // BC_ENABLED + BC_LEX_KW_IBASE, + BC_LEX_KW_OBASE, + BC_LEX_KW_SCALE, + BC_LEX_KW_LENGTH, + BC_LEX_KW_PRINT, + BC_LEX_KW_SQRT, + BC_LEX_KW_ABS, + BC_LEX_KW_QUIT, + BC_LEX_KW_READ, + BC_LEX_KW_MAXIBASE, + BC_LEX_KW_MAXOBASE, + BC_LEX_KW_MAXSCALE, + BC_LEX_KW_ELSE, + +#if DC_ENABLED + BC_LEX_EQ_NO_REG, + BC_LEX_OP_MODEXP, + BC_LEX_OP_DIVMOD, + + BC_LEX_COLON, + BC_LEX_EXECUTE, + BC_LEX_PRINT_STACK, + BC_LEX_CLEAR_STACK, + BC_LEX_STACK_LEVEL, + BC_LEX_DUPLICATE, + BC_LEX_SWAP, + BC_LEX_POP, + + BC_LEX_ASCIIFY, + BC_LEX_PRINT_STREAM, + + BC_LEX_STORE_IBASE, + BC_LEX_STORE_OBASE, + BC_LEX_STORE_SCALE, + BC_LEX_LOAD, + BC_LEX_LOAD_POP, + BC_LEX_STORE_PUSH, + BC_LEX_PRINT_POP, + BC_LEX_NQUIT, + BC_LEX_SCALE_FACTOR, +#endif // DC_ENABLED + +} BcLexType; + +struct BcLex; +typedef BcStatus (*BcLexNext)(struct BcLex*); + +typedef struct BcLex { + + const char *buf; + size_t i; + size_t line; + size_t len; + + BcLexType t; + BcLexType last; + BcVec str; + +} BcLex; + +void bc_lex_init(BcLex *l); +void bc_lex_free(BcLex *l); +void bc_lex_file(BcLex *l, const char *file); +BcStatus bc_lex_text(BcLex *l, const char *text); +BcStatus bc_lex_next(BcLex *l); + +void bc_lex_lineComment(BcLex *l); +BcStatus bc_lex_comment(BcLex *l); +void bc_lex_whitespace(BcLex *l); +BcStatus bc_lex_number(BcLex *l, char start); +void bc_lex_name(BcLex *l); +void bc_lex_commonTokens(BcLex *l, char c); + +BcStatus bc_lex_invalidChar(BcLex *l, char c); + +#endif // BC_LEX_H Index: contrib/bc/include/num.h =================================================================== --- contrib/bc/include/num.h +++ contrib/bc/include/num.h @@ -0,0 +1,221 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for the num type. + * + */ + +#ifndef BC_NUM_H +#define BC_NUM_H + +#include +#include +#include +#include +#include + +#include + +#include + +#ifndef BC_ENABLE_EXTRA_MATH +#define BC_ENABLE_EXTRA_MATH (1) +#endif // BC_ENABLE_EXTRA_MATH + +#define BC_BASE (10) + +typedef unsigned long ulong; + +// For some reason, LONG_BIT is not defined in some versions of gcc. +// I define it here to the minimum accepted value in the POSIX standard. +#ifndef LONG_BIT +#define LONG_BIT (32) +#endif // LONG_BIT + +#ifndef BC_LONG_BIT +#define BC_LONG_BIT LONG_BIT +#endif // BC_LONG_BIT + +#if BC_LONG_BIT > LONG_BIT +#error BC_LONG_BIT cannot be greater than LONG_BIT +#endif // BC_LONG_BIT > LONG_BIT + +#if BC_LONG_BIT >= 64 + +typedef int_least32_t BcDig; +typedef uint_fast64_t BcBigDig; + +#define BC_NUM_BIGDIG_MAX (UINT_FAST64_MAX) + +#define BC_BASE_DIGS (9) +#define BC_BASE_POW (1000000000) +#define BC_NUM_DEF_SIZE (2) + +#define BC_NUM_BIGDIG_C UINT64_C + +#elif BC_LONG_BIT >= 32 + +typedef int_least16_t BcDig; +typedef uint_fast32_t BcBigDig; + +#define BC_NUM_BIGDIG_MAX (UINT_FAST32_MAX) + +#define BC_BASE_DIGS (4) +#define BC_BASE_POW (10000) +#define BC_NUM_DEF_SIZE (4) + +#define BC_NUM_BIGDIG_C UINT32_C + +#else + +#error BC_LONG_BIT must be at least 32 + +#endif // BC_LONG_BIT >= 64 + +typedef struct BcNum { + BcDig *restrict num; + size_t rdx; + size_t scale; + size_t len; + size_t cap; + bool neg; +} BcNum; + +#define BC_NUM_MIN_BASE (BC_NUM_BIGDIG_C(2)) +#define BC_NUM_MAX_POSIX_IBASE (BC_NUM_BIGDIG_C(16)) +#define BC_NUM_MAX_IBASE (BC_NUM_BIGDIG_C(36)) +// This is the max base allowed by bc_num_parseChar(). +#define BC_NUM_MAX_LBASE (BC_NUM_BIGDIG_C('Z' + BC_BASE + 1)) +#define BC_NUM_PRINT_WIDTH (BC_NUM_BIGDIG_C(69)) + +#ifndef BC_NUM_KARATSUBA_LEN +#define BC_NUM_KARATSUBA_LEN (BC_NUM_BIGDIG_C(64)) +#elif BC_NUM_KARATSUBA_LEN < 16 +#error BC_NUM_KARATSUBA_LEN must be at least 16. +#endif // BC_NUM_KARATSUBA_LEN + +// A crude, but always big enough, calculation of +// the size required for ibase and obase BcNum's. +#define BC_NUM_BIGDIG_LOG10 ((CHAR_BIT * sizeof(BcBigDig) + 1) / 2 + 1) + +#define BC_NUM_NONZERO(n) ((n)->len) +#define BC_NUM_ZERO(n) (!BC_NUM_NONZERO(n)) +#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) + +#define BC_NUM_NUM_LETTER(c) ((c) - 'A' + BC_BASE) + +#define BC_NUM_KARATSUBA_ALLOCS (6) + +#define BC_NUM_CMP_SIGNAL_VAL (~((ssize_t) ((size_t) SSIZE_MAX))) +#define BC_NUM_CMP_SIGNAL(cmp) (cmp == BC_NUM_CMP_SIGNAL_VAL) + +#define BC_NUM_ROUND_POW(s) (bc_vm_growSize((s), BC_BASE_DIGS - 1)) +#define BC_NUM_RDX(s) (BC_NUM_ROUND_POW(s) / BC_BASE_DIGS) + +#define BC_NUM_SIZE(n) ((n) * sizeof(BcDig)) + +#if BC_DEBUG_CODE +#define BC_NUM_PRINT(x) fprintf(stderr, "%s = %lu\n", #x, (unsigned long)(x)) +#define DUMP_NUM bc_num_dump +#else // BC_DEBUG_CODE +#undef DUMP_NUM +#define DUMP_NUM(x,y) +#define BC_NUM_PRINT(x) +#endif // BC_DEBUG_CODE + +typedef BcStatus (*BcNumBinaryOp)(BcNum*, BcNum*, BcNum*, size_t); +typedef size_t (*BcNumBinaryOpReq)(BcNum*, BcNum*, size_t); +typedef void (*BcNumDigitOp)(size_t, size_t, bool); +typedef BcStatus (*BcNumShiftAddOp)(BcDig*, const BcDig*, size_t); + +void bc_num_init(BcNum *restrict n, size_t req); +void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap); +void bc_num_copy(BcNum *d, const BcNum *s); +void bc_num_createCopy(BcNum *d, const BcNum *s); +void bc_num_createFromBigdig(BcNum *n, BcBigDig val); +void bc_num_free(void *num); + +size_t bc_num_scale(const BcNum *restrict n); +size_t bc_num_len(const BcNum *restrict n); + +BcStatus bc_num_bigdig(const BcNum *restrict n, BcBigDig *result); +void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val); + +BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale); +BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale); +BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale); +BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale); +BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale); +BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale); +#if BC_ENABLE_EXTRA_MATH +BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale); +BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale); +BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale); +#endif // BC_ENABLE_EXTRA_MATH +BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale); +BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale); + +size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale); + +size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale); +size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale); +#if BC_ENABLE_EXTRA_MATH +size_t bc_num_placesReq(BcNum *a, BcNum *b, size_t scale); +#endif // BC_ENABLE_EXTRA_MATH + +void bc_num_truncate(BcNum *restrict n, size_t places); +ssize_t bc_num_cmp(const BcNum *a, const BcNum *b); + +#if DC_ENABLED +BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d); +#endif // DC_ENABLED + +void bc_num_one(BcNum *restrict n); +ssize_t bc_num_cmpZero(const BcNum *n); + +BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val, + BcBigDig base, bool letter); +BcStatus bc_num_print(BcNum *restrict n, BcBigDig base, bool newline); +#if DC_ENABLED +BcStatus bc_num_stream(BcNum *restrict n, BcBigDig base); +#endif // DC_ENABLED + +#if BC_DEBUG_CODE +void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline); +void bc_num_printDigs(const BcDig* n, size_t len, bool emptyline); +void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline); +void bc_num_dump(const char *varname, const BcNum *n); +#endif // BC_DEBUG_CODE + +extern const char bc_num_hex_digits[]; +extern const BcBigDig bc_num_pow10[BC_BASE_DIGS + 1]; + +#endif // BC_NUM_H Index: contrib/bc/include/parse.h =================================================================== --- contrib/bc/include/parse.h +++ contrib/bc/include/parse.h @@ -0,0 +1,109 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for bc's parser. + * + */ + +#ifndef BC_PARSE_H +#define BC_PARSE_H + +#include +#include +#include + +#include +#include +#include +#include + +#define BC_PARSE_REL (UINTMAX_C(1)<<0) +#define BC_PARSE_PRINT (UINTMAX_C(1)<<1) +#define BC_PARSE_NOCALL (UINTMAX_C(1)<<2) +#define BC_PARSE_NOREAD (UINTMAX_C(1)<<3) +#define BC_PARSE_ARRAY (UINTMAX_C(1)<<4) +#define BC_PARSE_NEEDVAL (UINTMAX_C(1)<<5) + +#define bc_parse_push(p, i) (bc_vec_pushByte(&(p)->func->code, (uchar) (i))) +#define bc_parse_string(p)(bc_parse_addId((p), (p)->l.str.v, BC_INST_STR)) +#define bc_parse_pushIndex(p, idx) (bc_vec_pushIndex(&(p)->func->code, (idx))) + +#define bc_parse_err(p, e) (bc_vm_error((e), (p)->l.line)) +#define bc_parse_verr(p, e, ...) (bc_vm_error((e), (p)->l.line, __VA_ARGS__)) + +typedef struct BcParseNext { + uchar len; + uchar tokens[4]; +} BcParseNext; + +#define BC_PARSE_NEXT_TOKENS(...) .tokens = { __VA_ARGS__ } +#define BC_PARSE_NEXT(a, ...) \ + { .len = (uchar) (a), BC_PARSE_NEXT_TOKENS(__VA_ARGS__) } + +struct BcParse; +struct BcProgram; + +typedef BcStatus (*BcParseParse)(struct BcParse*); +typedef BcStatus (*BcParseExpr)(struct BcParse*, uint8_t); + +typedef struct BcParse { + + BcLex l; + +#if BC_ENABLED + BcVec flags; + BcVec exits; + BcVec conds; + BcVec ops; + BcVec buf; +#endif // BC_ENABLED + + struct BcProgram *prog; + BcFunc *func; + size_t fidx; + + bool auto_part; + +} BcParse; + +void bc_parse_init(BcParse *p, struct BcProgram *prog, size_t func); +void bc_parse_free(BcParse *p); +BcStatus bc_parse_reset(BcParse *p, BcStatus s); + +void bc_parse_addId(BcParse *p, const char *string, uchar inst); +void bc_parse_number(BcParse *p); +void bc_parse_updateFunc(BcParse *p, size_t fidx); +void bc_parse_pushName(BcParse* p, char *name, bool var); +BcStatus bc_parse_text(BcParse *p, const char *text); + +extern const char bc_parse_one[]; + +#endif // BC_PARSE_H Index: contrib/bc/include/program.h =================================================================== --- contrib/bc/include/program.h +++ contrib/bc/include/program.h @@ -0,0 +1,162 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for bc programs. + * + */ + +#ifndef BC_PROGRAM_H +#define BC_PROGRAM_H + +#include + +#include +#include +#include +#include + +#define BC_PROG_GLOBALS_IBASE (0) +#define BC_PROG_GLOBALS_OBASE (1) +#define BC_PROG_GLOBALS_SCALE (2) +#define BC_PROG_GLOBALS_LEN (3) + +#define BC_PROG_ONE_CAP (1) + +typedef struct BcProgram { + + BcBigDig globals[BC_PROG_GLOBALS_LEN]; + BcVec globals_v[BC_PROG_GLOBALS_LEN]; + +#if DC_ENABLED + BcBigDig strm; + BcNum strmb; +#endif // DC_ENABLED + + BcVec results; + BcVec stack; + + BcVec fns; +#if BC_ENABLED + BcVec fn_map; +#endif // BC_ENABLED + + BcVec vars; + BcVec var_map; + + BcVec arrs; + BcVec arr_map; + + BcNum one; + +#if BC_ENABLED + BcNum last; +#endif // BC_ENABLED + +#if DC_ENABLED + // This uses BC_NUM_LONG_LOG10 because it is used in bc_num_ulong2num(), + // which attempts to realloc, unless it is big enough. This is big enough. + BcDig strmb_num[BC_NUM_BIGDIG_LOG10]; +#endif // DC_ENABLED + + BcDig one_num[BC_PROG_ONE_CAP]; + +} BcProgram; + +#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) (n))) + +#define BC_PROG_GLOBAL_PTR(v) (bc_vec_top(v)) +#define BC_PROG_GLOBAL(v) (*((BcBigDig*) BC_PROG_GLOBAL_PTR(v))) + +#define BC_PROG_IBASE(p) ((p)->globals[BC_PROG_GLOBALS_IBASE]) +#define BC_PROG_OBASE(p) ((p)->globals[BC_PROG_GLOBALS_OBASE]) +#define BC_PROG_SCALE(p) ((p)->globals[BC_PROG_GLOBALS_SCALE]) + +#define BC_PROG_MAIN (0) +#define BC_PROG_READ (1) + +#if DC_ENABLED +#define BC_PROG_REQ_FUNCS (2) +#if !BC_ENABLED +// For dc only, last is always true. +#define bc_program_copyToVar(p, name, t, last) \ + bc_program_copyToVar(p, name, t) +#endif // !BC_ENABLED +#else // DC_ENABLED +// For bc, 'pop' and 'copy' are always false. +#define bc_program_pushVar(p, code, bgn, pop, copy) \ + bc_program_pushVar(p, code, bgn) +#ifdef NDEBUG +#define BC_PROG_NO_STACK_CHECK +#endif // NDEBUG +#endif // DC_ENABLED + +#define BC_PROG_STR(n) ((n)->num == NULL && !(n)->cap) +#define BC_PROG_NUM(r, n) \ + ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) + +typedef void (*BcProgramUnary)(BcResult*, BcNum*); + +void bc_program_init(BcProgram *p); +void bc_program_free(BcProgram *p); + +#if BC_DEBUG_CODE +#if BC_ENABLED && DC_ENABLED +void bc_program_code(BcProgram *p); +void bc_program_printInst(BcProgram *p, const char *code, + size_t *restrict bgn); +#endif // BC_ENABLED && DC_ENABLED +#endif // BC_DEBUG_CODE + +size_t bc_program_search(BcProgram *p, char* id, bool var); +void bc_program_addFunc(BcProgram *p, BcFunc *f, const char* name); +size_t bc_program_insertFunc(BcProgram *p, char *name); +BcStatus bc_program_reset(BcProgram *p, BcStatus s); +BcStatus bc_program_exec(BcProgram *p); + +void bc_program_negate(BcResult *r, BcNum *n); +void bc_program_not(BcResult *r, BcNum *n); +#if BC_ENABLE_EXTRA_MATH +void bc_program_trunc(BcResult *r, BcNum *n); +#endif // BC_ENABLE_EXTRA_MATH + +extern const BcNumBinaryOp bc_program_ops[]; +extern const BcNumBinaryOpReq bc_program_opReqs[]; +extern const BcProgramUnary bc_program_unarys[]; +extern const char bc_program_exprs_name[]; +extern const char bc_program_stdin_name[]; +#if BC_ENABLE_SIGNALS +extern const char bc_program_ready_msg[]; +extern const size_t bc_program_ready_msg_len; +#endif // BC_ENABLE_SIGNALS +extern const char bc_program_esc_chars[]; +extern const char bc_program_esc_seqs[]; + +#endif // BC_PROGRAM_H Index: contrib/bc/include/read.h =================================================================== --- contrib/bc/include/read.h +++ contrib/bc/include/read.h @@ -0,0 +1,50 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code to handle special I/O for bc. + * + */ + +#ifndef BC_IO_H +#define BC_IO_H + +#include + +#include +#include + +#define BC_READ_BIN_CHAR(c) (((c) < ' ' && !isspace((c))) || ((uchar) c) > '~') + +BcStatus bc_read_line(BcVec* vec, const char *prompt); +BcStatus bc_read_file(const char *path, char **buf); +BcStatus bc_read_chars(BcVec *vec, const char *prompt); + +#endif // BC_IO_H Index: contrib/bc/include/status.h =================================================================== --- contrib/bc/include/status.h +++ contrib/bc/include/status.h @@ -0,0 +1,156 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * All bc status codes. + * + */ + +#ifndef BC_STATUS_H +#define BC_STATUS_H + +#ifndef BC_ENABLED +#define BC_ENABLED (1) +#endif // BC_ENABLED + +#ifndef DC_ENABLED +#define DC_ENABLED (1) +#endif // DC_ENABLED + +typedef enum BcStatus { + + BC_STATUS_SUCCESS = 0, + BC_STATUS_ERROR_MATH, + BC_STATUS_ERROR_PARSE, + BC_STATUS_ERROR_EXEC, + BC_STATUS_ERROR_FATAL, + BC_STATUS_EOF, + BC_STATUS_EMPTY_EXPR, + BC_STATUS_QUIT, + BC_STATUS_SIGNAL, + +} BcStatus; + +typedef enum BcError { + + BC_ERROR_MATH_NEGATIVE, + BC_ERROR_MATH_NON_INTEGER, + BC_ERROR_MATH_OVERFLOW, + BC_ERROR_MATH_DIVIDE_BY_ZERO, + + BC_ERROR_FATAL_ALLOC_ERR, + BC_ERROR_FATAL_IO_ERR, + BC_ERROR_FATAL_FILE_ERR, + BC_ERROR_FATAL_BIN_FILE, + BC_ERROR_FATAL_PATH_DIR, + BC_ERROR_FATAL_OPTION, + + BC_ERROR_EXEC_IBASE, + BC_ERROR_EXEC_OBASE, + BC_ERROR_EXEC_SCALE, + BC_ERROR_EXEC_READ_EXPR, + BC_ERROR_EXEC_REC_READ, + BC_ERROR_EXEC_TYPE, + + BC_ERROR_EXEC_STACK, + + BC_ERROR_EXEC_PARAMS, + BC_ERROR_EXEC_UNDEF_FUNC, + BC_ERROR_EXEC_VOID_VAL, + + BC_ERROR_PARSE_EOF, + BC_ERROR_PARSE_CHAR, + BC_ERROR_PARSE_STRING, + BC_ERROR_PARSE_COMMENT, + BC_ERROR_PARSE_TOKEN, +#if BC_ENABLED + BC_ERROR_PARSE_EXPR, + BC_ERROR_PARSE_EMPTY_EXPR, + BC_ERROR_PARSE_PRINT, + BC_ERROR_PARSE_FUNC, + BC_ERROR_PARSE_ASSIGN, + BC_ERROR_PARSE_NO_AUTO, + BC_ERROR_PARSE_DUP_LOCAL, + BC_ERROR_PARSE_BLOCK, + BC_ERROR_PARSE_RET_VOID, + BC_ERROR_PARSE_REF_VAR, + + BC_ERROR_POSIX_NAME_LEN, + BC_ERROR_POSIX_COMMENT, + BC_ERROR_POSIX_KW, + BC_ERROR_POSIX_DOT, + BC_ERROR_POSIX_RET, + BC_ERROR_POSIX_BOOL, + BC_ERROR_POSIX_REL_POS, + BC_ERROR_POSIX_MULTIREL, + BC_ERROR_POSIX_FOR, + BC_ERROR_POSIX_EXP_NUM, + BC_ERROR_POSIX_REF, + BC_ERROR_POSIX_VOID, + BC_ERROR_POSIX_BRACE, +#endif // BC_ENABLED + + BC_ERROR_NELEMS, + +#if BC_ENABLED + BC_ERROR_POSIX_START = BC_ERROR_POSIX_NAME_LEN, + BC_ERROR_POSIX_END = BC_ERROR_POSIX_BRACE, +#endif // BC_ENABLED + +} BcError; + +#define BC_ERR_IDX_MATH (0) +#define BC_ERR_IDX_PARSE (1) +#define BC_ERR_IDX_EXEC (2) +#define BC_ERR_IDX_FATAL (3) +#define BC_ERR_IDX_NELEMS (4) + +#if BC_ENABLED +#define BC_ERR_IDX_WARN (BC_ERR_IDX_NELEMS) +#endif // BC_ENABLED + +#define BC_UNUSED(e) ((void) (e)) + +#ifndef BC_LIKELY +#define BC_LIKELY(e) (e) +#endif // BC_LIKELY + +#ifndef BC_UNLIKELY +#define BC_UNLIKELY(e) (e) +#endif // BC_UNLIKELY + +#define BC_ERR(e) BC_UNLIKELY(e) +#define BC_NO_ERR(s) BC_LIKELY(s) + +#ifndef BC_DEBUG_CODE +#define BC_DEBUG_CODE (0) +#endif // BC_DEBUG_CODE + +#endif // BC_STATUS_H Index: contrib/bc/include/vector.h =================================================================== --- contrib/bc/include/vector.h +++ contrib/bc/include/vector.h @@ -0,0 +1,99 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for bc vectors (resizable arrays). + * + */ + +#ifndef BC_VECTOR_H +#define BC_VECTOR_H + +#include +#include +#include + +#include + +#define BC_VEC_INVALID_IDX (SIZE_MAX) +#define BC_VEC_START_CAP (UINTMAX_C(1)<<5) + +typedef unsigned char uchar; + +typedef void (*BcVecFree)(void*); + +// Forward declaration. +struct BcId; + +typedef struct BcVec { + char *v; + size_t len; + size_t cap; + size_t size; + BcVecFree dtor; +} BcVec; + +void bc_vec_init(BcVec *restrict v, size_t esize, BcVecFree dtor); +void bc_vec_expand(BcVec *restrict v, size_t req); + +void bc_vec_npop(BcVec *restrict v, size_t n); + +void bc_vec_push(BcVec *restrict v, const void *data); +void bc_vec_npush(BcVec *restrict v, size_t n, const void *data); +void bc_vec_pushByte(BcVec *restrict v, uchar data); +void bc_vec_pushIndex(BcVec *restrict v, size_t idx); +void bc_vec_string(BcVec *restrict v, size_t len, const char *restrict str); +void bc_vec_concat(BcVec *restrict v, const char *restrict str); +void bc_vec_empty(BcVec *restrict v); + +#if BC_ENABLE_HISTORY +void bc_vec_popAt(BcVec *restrict v, size_t idx); +void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data); +#endif // BC_ENABLE_HISTORY + +void* bc_vec_item(const BcVec *restrict v, size_t idx); +void* bc_vec_item_rev(const BcVec *restrict v, size_t idx); + +void bc_vec_free(void *vec); + +bool bc_map_insert(BcVec *restrict v, const struct BcId *restrict ptr, + size_t *restrict i); +size_t bc_map_index(const BcVec *restrict v, const struct BcId *restrict ptr); + +#define bc_vec_pop(v) (bc_vec_npop((v), 1)) +#define bc_vec_top(v) (bc_vec_item_rev((v), 0)) + +#ifndef NDEBUG +#define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), bc_id_free)) +#else // NDEBUG +#define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), NULL)) +#endif // NDEBUG + +#endif // BC_VECTOR_H Index: contrib/bc/include/vm.h =================================================================== --- contrib/bc/include/vm.h +++ contrib/bc/include/vm.h @@ -0,0 +1,245 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Definitions for bc's VM. + * + */ + +#ifndef BC_VM_H +#define BC_VM_H + +#include +#include + +#include + +#if BC_ENABLE_NLS + +# ifdef _WIN32 +# error NLS is not supported on Windows. +# endif // _WIN32 + +#include + +#endif // BC_ENABLE_NLS + +#include +#include +#include +#include +#include + +#if !BC_ENABLED && !DC_ENABLED +#error Must define BC_ENABLED, DC_ENABLED, or both +#endif + +// CHAR_BIT must be at least 6. +#if CHAR_BIT < 6 +#error CHAR_BIT must be at least 6. +#endif + +#ifndef BC_ENABLE_NLS +#define BC_ENABLE_NLS (0) +#endif // BC_ENABLE_NLS + +#ifndef BC_ENABLE_SIGNALS +#define BC_ENABLE_SIGNALS (1) +#endif // BC_ENABLE_SIGNALS + +#ifndef MAINEXEC +#define MAINEXEC bc +#endif + +#ifndef EXECPREFIX +#define EXECPREFIX +#endif + +#define GEN_STR(V) #V +#define GEN_STR2(V) GEN_STR(V) + +#define BC_VERSION GEN_STR2(VERSION) +#define BC_EXECPREFIX GEN_STR2(EXECPREFIX) +#define BC_MAINEXEC GEN_STR2(MAINEXEC) + +// Windows has deprecated isatty(). +#ifdef _WIN32 +#define isatty _isatty +#endif // _WIN32 + +#define DC_FLAG_X (UINTMAX_C(1)<<0) +#define BC_FLAG_W (UINTMAX_C(1)<<1) +#define BC_FLAG_S (UINTMAX_C(1)<<2) +#define BC_FLAG_Q (UINTMAX_C(1)<<3) +#define BC_FLAG_L (UINTMAX_C(1)<<4) +#define BC_FLAG_I (UINTMAX_C(1)<<5) +#define BC_FLAG_G (UINTMAX_C(1)<<6) +#define BC_FLAG_P (UINTMAX_C(1)<<7) +#define BC_FLAG_TTYIN (UINTMAX_C(1)<<8) +#define BC_TTYIN (vm->flags & BC_FLAG_TTYIN) +#define BC_TTY (vm->tty) + +#define BC_S (BC_ENABLED && (vm->flags & BC_FLAG_S)) +#define BC_W (BC_ENABLED && (vm->flags & BC_FLAG_W)) +#define BC_L (BC_ENABLED && (vm->flags & BC_FLAG_L)) +#define BC_I (vm->flags & BC_FLAG_I) +#define BC_G (BC_ENABLED && (vm->flags & BC_FLAG_G)) +#define DC_X (DC_ENABLED && (vm->flags & DC_FLAG_X)) +#define BC_P (vm->flags & BC_FLAG_P) + +#define BC_USE_PROMPT (!BC_P && BC_TTY && !BC_IS_POSIX) + +#define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) +#define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) + +#define BC_MAX_OBASE ((ulong) (BC_BASE_POW)) +#define BC_MAX_DIM ((ulong) (SIZE_MAX - 1)) +#define BC_MAX_SCALE ((ulong) (BC_NUM_BIGDIG_MAX - 1)) +#define BC_MAX_STRING ((ulong) (BC_NUM_BIGDIG_MAX - 1)) +#define BC_MAX_NAME BC_MAX_STRING +#define BC_MAX_NUM BC_MAX_SCALE +#define BC_MAX_EXP ((ulong) (BC_NUM_BIGDIG_MAX)) +#define BC_MAX_VARS ((ulong) (SIZE_MAX - 1)) + +#define BC_IS_BC (BC_ENABLED && (!DC_ENABLED || vm->name[0] != 'd')) +#define BC_IS_POSIX (BC_S || BC_W) + +#if BC_ENABLE_SIGNALS + +#define BC_SIG BC_UNLIKELY(vm->sig != vm->sig_chk) +#define BC_NO_SIG BC_LIKELY(vm->sig == vm->sig_chk) + +#define BC_SIGTERM_VAL (SIG_ATOMIC_MAX) +#define BC_SIGTERM (vm->sig == BC_SIGTERM_VAL) +#define BC_SIGINT (vm->sig && vm->sig != BC_SIGTERM_VAL) + +#else // BC_ENABLE_SIGNALS +#define BC_SIG (0) +#define BC_NO_SIG (1) +#endif // BC_ENABLE_SIGNALS + +#define bc_vm_err(e) (bc_vm_error((e), 0)) +#define bc_vm_verr(e, ...) (bc_vm_error((e), 0, __VA_ARGS__)) + +#define BC_IO_ERR(e, f) (BC_ERR((e) < 0 || ferror(f))) +#define BC_STATUS_IS_ERROR(s) \ + ((s) >= BC_STATUS_ERROR_MATH && (s) <= BC_STATUS_ERROR_FATAL) +#define BC_ERROR_SIGNAL_ONLY(s) (BC_ENABLE_SIGNALS && BC_ERR(s)) + +#define BC_VM_INVALID_CATALOG ((nl_catd) -1) + +typedef struct BcVm { + + BcParse prs; + BcProgram prog; + + size_t nchars; + + const char* file; + +#if BC_ENABLE_SIGNALS + const char *sigmsg; + volatile sig_atomic_t sig; + sig_atomic_t sig_chk; + uchar siglen; +#endif // BC_ENABLE_SIGNALS + + uint16_t flags; + uchar read_ret; + bool tty; + + uint16_t line_len; + + BcBigDig maxes[BC_PROG_GLOBALS_LEN]; + + BcVec files; + BcVec exprs; + + const char *name; + const char *help; + +#if BC_ENABLE_HISTORY + BcHistory history; +#endif // BC_ENABLE_HISTORY + + BcLexNext next; + BcParseParse parse; + BcParseExpr expr; + + const char *func_header; + + const char *err_ids[BC_ERR_IDX_NELEMS + BC_ENABLED]; + const char *err_msgs[BC_ERROR_NELEMS]; + + const char *locale; + + BcBigDig last_base; + BcBigDig last_pow; + BcBigDig last_exp; + BcBigDig last_rem; + +#if BC_ENABLE_NLS + nl_catd catalog; +#endif // BC_ENABLE_NLS + +} BcVm; + +#if BC_ENABLED +BcStatus bc_vm_posixError(BcError e, size_t line, ...); +#endif // BC_ENABLED + +void bc_vm_info(const char* const help); +BcStatus bc_vm_boot(int argc, char *argv[], const char *env_len, + const char* const env_args, const char* env_exp_quit); +void bc_vm_shutdown(void); + +size_t bc_vm_printf(const char *fmt, ...); +void bc_vm_puts(const char *str, FILE *restrict f); +void bc_vm_putchar(int c); +void bc_vm_fflush(FILE *restrict f); + +size_t bc_vm_arraySize(size_t n, size_t size); +size_t bc_vm_growSize(size_t a, size_t b); +void* bc_vm_malloc(size_t n); +void* bc_vm_realloc(void *ptr, size_t n); +char* bc_vm_strdup(const char *str); + +BcStatus bc_vm_error(BcError e, size_t line, ...); + +extern const char bc_copyright[]; +extern const char* const bc_err_line; +extern const char* const bc_err_func_header; +extern const char *bc_errs[]; +extern const uchar bc_err_ids[]; +extern const char* const bc_err_msgs[]; + +extern BcVm *vm; + +#endif // BC_VM_H Index: contrib/bc/locales/de_DE.ISO8859-1.msg =================================================================== --- contrib/bc/locales/de_DE.ISO8859-1.msg +++ contrib/bc/locales/de_DE.ISO8859-1.msg @@ -0,0 +1,78 @@ +$quote " + +$ Headers for printing errors/warnings. +$set 1 + +1 "Funktion:" + +$ Error types. +$set 2 + +1 "Rechenfehler:" +2 "Analysefehler:" +3 "Laufzeitfehler:" +4 "Fataler Fehler:" +5 "Warnung:" + +$ Math errors. +$set 3 + +1 "negative Zahl" +2 "Nicht-Ganzzahl-Wert" +3 "Überlauf: Zahl passt nicht in Register" +4 "Division durch 0" + +$ Parse errors. +$set 4 + +1 "Ende der Datei" +2 "ungültiges Zeichen: '%c'" +3 "Zeichenketten-Ende konnte nicht gefunden werden" +4 "Kommentar-Ende konnte nicht gefunden werden" +5 "ungültiges Token" +6 "ungültiger Ausdruck" +7 "leerer Ausdruck" +8 "Ungültige Druckanweisung" +9 "Ungültige Funktionsdefinition" +10 "Ungültige Zuweisung: Die linke Seite muss \"scale\", \"ibase\", \"obase\", \"last\", \"var\" oder \"array element\" sein" +11 "keine automatische Variable gefunden" +12 "Funktionsparameter oder Variable \"%s%s\" existiert bereits" +13 "Blockende konnte nicht gefunden werden" +14 "eine \"void-Funktion\" kann keinen Wert zurückgeben: %s()" +15 "Variable kann keine Referenz sein: %s" +16 "POSIX erlaubt keine Namen mit mehr als 1 Zeichen Länge: %s" +17 "POSIX erlaubt keine '#'-Skriptkommentare" +18 "POSIX erlaubt das Schlüsselwort \"%s\" nicht" +19 "POSIX erlaubt keinen Punkt ('.') als Abkürzung für das letzte Ergebnis" +20 "POSIX benötigt Klammern um Rückgabeausdrücke" +21 "POSIX erlaubt den Operator \"%s\" nicht" +22 "POSIX erlaubt keine Vergleichsoperatoren außerhalb von if-Anweisungen oder Schleifen" +23 "POSIX benötigt 0 oder 1 Vergleichsoperatoren pro Bedingung" +24 "POSIX erlaubt keinen leeren Ausdruck in einer for-Schleife" +25 "POSIX erlaubt keine exponentielle Notation" +26 "POSIX erlaubt keine Feld-Referenzen als Funktionsparameter" +27 "POSIX erfordert, dass die linke Klammer auf der gleichen Linie wie der Funktionskopf steht" + +$ Runtime errors. +$set 5 + +1 "ungültige \"ibase\": muss im Intervall [%lu, %lu] liegen" +2 "ungültige \"obase\": muss im Intervall [%lu, %lu] liegen" +3 "ungültige \"scale\": muss im Intervall [%lu, %lu] liegen" +4 "ungültiger read()-Ausdruck" +5 "rekursiver read()-Aufruf" +6 "Variable oder Feld-Element hat den falschen Typ" +7 "Stapel hat zu wenig Elemente" +8 "falsche Anzahl der Parameter: benötigt %zu, hat %zu" +9 "undefinierte Funktion: %s()" +10 "kann keinen ungültigen Wert in einem Ausdruck verwenden" + +$ Fatal errors. +$set 6 + +1 "Speicherzuweisung fehlgeschlagen" +2 "Ein-Ausgabe-Fehler" +3 "konnte die Datei nicht öffnen: %s" +4 "Datei ist nicht ASCII: %s" +5 "Pfad ist ein Verzeichnis: %s" +6 "ungültige Befehlszeilenoption: '%c' (\"%s\")" Index: contrib/bc/locales/de_DE.UTF-8.msg =================================================================== --- contrib/bc/locales/de_DE.UTF-8.msg +++ contrib/bc/locales/de_DE.UTF-8.msg @@ -0,0 +1,78 @@ +$quote " + +$ Headers for printing errors/warnings. +$set 1 + +1 "Funktion:" + +$ Error types. +$set 2 + +1 "Rechenfehler:" +2 "Analysefehler:" +3 "Laufzeitfehler:" +4 "Fataler Fehler:" +5 "Warnung:" + +$ Math errors. +$set 3 + +1 "negative Zahl" +2 "Nicht-Ganzzahl-Wert" +3 "Überlauf: Zahl passt nicht in Register" +4 "Division durch 0" + +$ Parse errors. +$set 4 + +1 "Ende der Datei" +2 "ungültiges Zeichen: '%c'" +3 "Zeichenketten-Ende konnte nicht gefunden werden" +4 "Kommentar-Ende konnte nicht gefunden werden" +5 "ungültiges Token" +6 "ungültiger Ausdruck" +7 "leerer Ausdruck" +8 "Ungültige Druckanweisung" +9 "Ungültige Funktionsdefinition" +10 "Ungültige Zuweisung: Die linke Seite muss \"scale\", \"ibase\", \"obase\", \"last\", \"var\" oder \"array element\" sein" +11 "keine automatische Variable gefunden" +12 "Funktionsparameter oder Variable \"%s%s\" existiert bereits" +13 "Blockende konnte nicht gefunden werden" +14 "eine \"void-Funktion\" kann keinen Wert zurückgeben: %s()" +15 "Variable kann keine Referenz sein: %s" +16 "POSIX erlaubt keine Namen mit mehr als 1 Zeichen Länge: %s" +17 "POSIX erlaubt keine '#'-Skriptkommentare" +18 "POSIX erlaubt das Schlüsselwort \"%s\" nicht" +19 "POSIX erlaubt keinen Punkt ('.') als Abkürzung für das letzte Ergebnis" +20 "POSIX benötigt Klammern um Rückgabeausdrücke" +21 "POSIX erlaubt den Operator \"%s\" nicht" +22 "POSIX erlaubt keine Vergleichsoperatoren außerhalb von if-Anweisungen oder Schleifen" +23 "POSIX benötigt 0 oder 1 Vergleichsoperatoren pro Bedingung" +24 "POSIX erlaubt keinen leeren Ausdruck in einer for-Schleife" +25 "POSIX erlaubt keine exponentielle Notation" +26 "POSIX erlaubt keine Feld-Referenzen als Funktionsparameter" +27 "POSIX erfordert, dass die linke Klammer auf der gleichen Linie wie der Funktionskopf steht" + +$ Runtime errors. +$set 5 + +1 "ungültige \"ibase\": muss im Intervall [%lu, %lu] liegen" +2 "ungültige \"obase\": muss im Intervall [%lu, %lu] liegen" +3 "ungültige \"scale\"; muss im Intervall [%lu, %lu] liegen" +4 "ungültiger read()-Ausdruck" +5 "rekursiver read()-Aufruf" +6 "Variable oder Feld-Element hat den falschen Typ" +7 "Stapel hat zu wenig Elemente" +8 "falsche Anzahl der Parameter: benötigt %zu, hat %zu" +9 "undefinierte Funktion: %s()" +10 "kann keinen ungültigen Wert in einem Ausdruck verwenden" + +$ Fatal errors. +$set 6 + +1 "Speicherzuweisung fehlgeschlagen" +2 "Ein-Ausgabe-Fehler" +3 "konnte die Datei nicht öffnen: %s" +4 "Datei ist nicht ASCII: %s" +5 "Pfad ist ein Verzeichnis: %s" +6 "ungültige Befehlszeilenoption: '%c' (\"%s\")" Index: contrib/bc/locales/en_US.UTF-8.msg =================================================================== --- contrib/bc/locales/en_US.UTF-8.msg +++ contrib/bc/locales/en_US.UTF-8.msg @@ -0,0 +1 @@ +link en_US.msg \ No newline at end of file Index: contrib/bc/locales/en_US.msg =================================================================== --- contrib/bc/locales/en_US.msg +++ contrib/bc/locales/en_US.msg @@ -0,0 +1,106 @@ +$ $ +$ Copyright (c) 2018-2019 Gavin D. Howard and contributors. +$ $ +$ All rights reserved. +$ $ +$ Redistribution and use in source and binary forms, with or without +$ modification, are permitted provided that the following conditions are met: +$ $ +$ * Redistributions of source code must retain the above copyright notice, this +$ list of conditions and the following disclaimer. +$ $ +$ * Redistributions in binary form must reproduce the above copyright notice, +$ this list of conditions and the following disclaimer in the documentation +$ and/or other materials provided with the distribution. +$ $ +$ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +$ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +$ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +$ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +$ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +$ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +$ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +$ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +$ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +$ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +$ POSSIBILITY OF SUCH DAMAGE. +$ $ + +$quote " + +$ Miscellaneous messages. +$set 1 + +1 "Function:" + +$ Error types. +$set 2 + +1 "Math error:" +2 "Parse error:" +3 "Runtime error:" +4 "Fatal error:" +5 "Warning:" + +$ Math errors. +$set 3 + +1 "negative number" +2 "non-integer number" +3 "overflow: number does not fit into a hardware number" +4 "divide by 0" + +$ Parse errors. +$set 4 + +1 "end of file" +2 "invalid character '%c'" +3 "string end could not be found" +4 "comment end could not be found" +5 "invalid token" +6 "invalid expression" +7 "empty expression" +8 "invalid print statement" +9 "invalid function definition" +10 "invalid assignment: left side must be scale, ibase, obase, last, var, or array element" +11 "no auto variable found" +12 "function parameter or auto \"%s%s\" already exists" +13 "block end could not be found" +14 "cannot return a value from void function: %s()" +15 "var cannot be a reference: %s" +16 "POSIX does not allow names longer than 1 character: %s" +17 "POSIX does not allow '#' script comments" +18 "POSIX does not allow the following keyword: %s" +19 "POSIX does not allow a period ('.') as a shortcut for the last result" +20 "POSIX requires parentheses around return expressions" +21 "POSIX does not allow the following operator: %s" +22 "POSIX does not allow comparison operators outside if statements or loops" +23 "POSIX requires 0 or 1 comparison operators per condition" +24 "POSIX requires all 3 parts of a for loop to be non-empty" +25 "POSIX does not allow exponential notation" +26 "POSIX does not allow array references as function parameters" +27 "POSIX requires the left brace be on the same line as the function header" + +$ Runtime errors. +$set 5 + +1 "invalid ibase: must be [%lu, %lu]" +2 "invalid obase: must be [%lu, %lu]" +3 "invalid scale: must be [%lu, %lu]" +4 "invalid read() expression" +5 "recursive read() call" +6 "variable or array element is the wrong type" +7 "stack has too few elements" +8 "wrong number of parameters; need %zu, have %zu" +9 "undefined function: %s()" +10 "cannot use a void value in an expression" + +$ Fatal errors. +$set 6 + +1 "memory allocation failed" +2 "I/O error" +3 "could not open file: %s" +4 "file is not ASCII: %s" +5 "path is a directory: %s" +6 "invalid command-line option: '%c' (\"%s\")" Index: contrib/bc/locales/fr_FR.ISO8859-1.msg =================================================================== --- contrib/bc/locales/fr_FR.ISO8859-1.msg +++ contrib/bc/locales/fr_FR.ISO8859-1.msg @@ -0,0 +1,106 @@ +$ $ +$ Copyright (c) 2018-2019 Gavin D. Howard and contributors. +$ $ +$ All rights reserved. +$ $ +$ Redistribution and use in source and binary forms, with or without +$ modification, are permitted provided that the following conditions are met: +$ $ +$ * Redistributions of source code must retain the above copyright notice, this +$ list of conditions and the following disclaimer. +$ $ +$ * Redistributions in binary form must reproduce the above copyright notice, +$ this list of conditions and the following disclaimer in the documentation +$ and/or other materials provided with the distribution. +$ $ +$ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +$ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +$ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +$ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +$ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +$ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +$ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +$ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +$ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +$ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +$ POSSIBILITY OF SUCH DAMAGE. +$ $ + +$quote " + +$ Miscellaneous messages. +$set 1 + +1 "Fonction :" + +$ Error types. +$set 2 + +1 "Erreur de calcul :" +2 "Erreur d'analyse syntaxique :" +3 "Erreur d'exécution :" +4 "Erreur fatale :" +5 "Avertissement :" + +$ Math errors. +$set 3 + +1 "nombre strictement négatif" +2 "nombre non entier" +3 "dépassement : le nombre ne tient pas dans un type traité par le processeur" +4 "division par 0" + +$ Parse errors. +$set 4 + +1 "fin de fichier" +2 "caractère invalide '%c'" +3 "fin de chaîne non trouvée" +4 "fin de commentaire non trouvée" +5 "symbole invalide" +6 "expression invalide" +7 "expression vide" +8 "instruction d'écriture invalide" +9 "définition de fonction invalide" +10 "affectation invalide : la partie gauche doit être 'scale', 'ibase', 'obase', 'last', une variable ou une case de tableau" +11 "aucune variable auto trouvée" +12 "Le paramètre de fonction ou variable auto \"%s%s\" existe déjà" +13 "fin de bloc non trouvée" +14 "une fonction 'void' ne peut pas retourner de valeur : %s()" +15 "Une variable ne peut pas être une référence : %s" +16 "POSIX interdit les noms de plus d'un caractère : %s" +17 "POSIX interdit les commentaires dans les scripts (pas de '#')" +18 "POSIX interdit le mot-clé '%s'" +19 "POSIX interdit l'utilisation du point ('.') comme raccourci pour le dernier résultat" +20 "POSIX impose des parenthèses autour des expressions de retour" +21 "POSIX interdit l'opérateur '%s'" +22 "POSIX interdit les opérateurs de comparaison en dehors des expressions 'if' ou des boucles" +23 "POSIX impose 0 ou 1 opérateur de comparaison par condition" +24 "POSIX interdit une expression vide dans une boucle 'for'" +25 "POSIX interdit la notation exponentielle" +26 "POSIX interdit les références à un tableau dans les paramètres d'une fonction" +27 "POSIX impose que l'en-tête de la fonction et le '{' soient sur la même ligne" + +$ Runtime errors. +$set 5 + +1 "ibase invalide : doit être [%lu, %lu]" +2 "obase invalide : doit être [%lu, %lu]" +3 "scale invalide : doit être [%lu, %lu]" +4 "expression read() invalide" +5 "appel read() récursif" +6 "mauvais type de variable ou d'élément de tableau" +7 "pile sous-remplie" +8 "nombre incorrect de paramètres - attendus : %zu, obtenus : %zu" +9 "fonction non définie : %s()" +10 "une valeur 'void' est inutilisable dans une expression" + +$ Fatal errors. +$set 6 + +1 "échec d'allocation mémoire" +2 "erreur d'entrée-sortie" +3 "impossible d'ouvrir le fichier : %s" +4 "fichier non ASCII : %s" +5 "le chemin est un répertoire : %s" +6 "option de ligne de commande invalide : '%c' (\"%s\")" Index: contrib/bc/locales/fr_FR.UTF-8.msg =================================================================== --- contrib/bc/locales/fr_FR.UTF-8.msg +++ contrib/bc/locales/fr_FR.UTF-8.msg @@ -0,0 +1,106 @@ +$ $ +$ Copyright (c) 2018-2019 Gavin D. Howard and contributors. +$ $ +$ All rights reserved. +$ $ +$ Redistribution and use in source and binary forms, with or without +$ modification, are permitted provided that the following conditions are met: +$ $ +$ * Redistributions of source code must retain the above copyright notice, this +$ list of conditions and the following disclaimer. +$ $ +$ * Redistributions in binary form must reproduce the above copyright notice, +$ this list of conditions and the following disclaimer in the documentation +$ and/or other materials provided with the distribution. +$ $ +$ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +$ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +$ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +$ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +$ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +$ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +$ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +$ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +$ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +$ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +$ POSSIBILITY OF SUCH DAMAGE. +$ $ + +$quote " + +$ Miscellaneous messages. +$set 1 + +1 "Fonction :" + +$ Error types. +$set 2 + +1 "Erreur de calcul :" +2 "Erreur d'analyse syntaxique :" +3 "Erreur d'exécution :" +4 "Erreur fatale :" +5 "Avertissement :" + +$ Math errors. +$set 3 + +1 "nombre strictement négatif" +2 "nombre non entier" +3 "dépassement : le nombre ne tient pas dans un type traité par le processeur" +4 "division par 0" + +$ Parse errors. +$set 4 + +1 "fin de fichier" +2 "caractère invalide '%c'" +3 "fin de chaîne non trouvée" +4 "fin de commentaire non trouvée" +5 "symbole invalide" +6 "expression invalide" +7 "expression vide" +8 "instruction d'écriture invalide" +9 "définition de fonction invalide" +10 "affectation invalide : la partie gauche doit être 'scale', 'ibase', 'obase', 'last', une variable ou une case de tableau" +11 "aucune variable auto trouvée" +12 "Le paramètre de fonction ou variable auto \"%s%s\" existe déjà" +13 "fin de bloc non trouvée" +14 "une fonction 'void' ne peut pas retourner de valeur : %s()" +15 "Une variable ne peut pas être une référence : %s" +16 "POSIX interdit les noms de plus d'un caractère : %s" +17 "POSIX interdit les commentaires dans les scripts (pas de '#')" +18 "POSIX interdit le mot-clé '%s'" +19 "POSIX interdit l'utilisation du point ('.') comme raccourci pour le dernier résultat" +20 "POSIX impose des parenthèses autour des expressions de retour" +21 "POSIX interdit l'opérateur '%s'" +22 "POSIX interdit les opérateurs de comparaison en dehors des expressions 'if' ou des boucles" +23 "POSIX impose 0 ou 1 opérateur de comparaison par condition" +24 "POSIX interdit une expression vide dans une boucle 'for'" +25 "POSIX interdit la notation exponentielle" +26 "POSIX interdit les références à un tableau dans les paramètres d'une fonction" +27 "POSIX impose que l'en-tête de la fonction et le '{' soient sur la même ligne" + +$ Runtime errors. +$set 5 + +1 "ibase invalide : doit être [%lu, %lu]" +2 "obase invalide : doit être [%lu, %lu]" +3 "scale invalide : doit être [%lu, %lu]" +4 "expression read() invalide" +5 "appel read() récursif" +6 "mauvais type de variable ou d'élément de tableau" +7 "pile sous-remplie" +8 "nombre incorrect de paramètres - attendus : %zu, obtenus : %zu" +9 "fonction non définie : %s()" +10 "une valeur 'void' est inutilisable dans une expression" + +$ Fatal errors. +$set 6 + +1 "échec d'allocation mémoire" +2 "erreur d'entrée-sortie" +3 "impossible d'ouvrir le fichier : %s" +4 "fichier non ASCII : %s" +5 "le chemin est un répertoire : %s" +6 "option de ligne de commande invalide : '%c' (\"%s\")" Index: contrib/bc/manuals/algorithms.md =================================================================== --- contrib/bc/manuals/algorithms.md +++ contrib/bc/manuals/algorithms.md @@ -0,0 +1,189 @@ +# Algorithms + +This `bc` uses the math algorithms below: + +### Addition + +This `bc` uses brute force addition, which is linear (`O(n)`) in the number of +digits. + +### Subtraction + +This `bc` uses brute force subtraction, which is linear (`O(n)`) in the number +of digits. + +### Multiplication + +This `bc` uses two algorithms: [Karatsuba][1] and brute force. + +Karatsuba is used for "large" numbers. ("Large" numbers are defined as any +number with `BC_NUM_KARATSUBA_LEN` digits or larger. `BC_NUM_KARATSUBA_LEN` has +a sane default, but may be configured by the user.) Karatsuba, as implemented in +this `bc`, is superlinear but subpolynomial (bounded by `O(n^log_2(3))`). + +Brute force multiplication is used below `BC_NUM_KARATSUBA_LEN` digits. It is +polynomial (`O(n^2)`), but since Karatsuba requires both more intermediate +values (which translate to memory allocations) and a few more additions, there +is a "break even" point in the number of digits where brute force multiplication +is faster than Karatsuba. There is a script (`$ROOT/karatsuba.py`) that will +find the break even point on a particular machine. + +***WARNING: The Karatsuba script requires Python 3.*** + +### Division + +This `bc` uses Algorithm D ([long division][2]). Long division is polynomial +(`O(n^2)`), but unlike Karatsuba, any division "divide and conquer" algorithm +reaches its "break even" point with significantly larger numbers. "Fast" +algorithms become less attractive with division as this operation typically +reduces the problem size. + +While the implementation of long division may appear to use the subtractive +chunking method, it only uses subtraction to find a quotient digit. It avoids +unnecessary work by aligning digits prior to performing subtraction and finding +a starting guess for the quotient. + +Subtraction was used instead of multiplication for two reasons: + +1. Division and subtraction can share code (one of the less important goals of + this `bc` is small code). +2. It minimizes algorithmic complexity. + +Using multiplication would make division have the even worse algorithmic +complexity of `O(n^(2*log_2(3)))` (best case) and `O(n^3)` (worst case). + +### Power + +This `bc` implements [Exponentiation by Squaring][3], which (via Karatsuba) has +a complexity of `O((n*log(n))^log_2(3))` which is favorable to the +`O((n*log(n))^2)` without Karatsuba. + +### Square Root + +This `bc` implements the fast algorithm [Newton's Method][4] (also known as the +Newton-Raphson Method, or the [Babylonian Method][5]) to perform the square root +operation. Its complexity is `O(log(n)*n^2)` as it requires one division per +iteration. + +### Sine and Cosine (`bc` Only) + +This `bc` uses the series + +``` +x - x^3/3! + x^5/5! - x^7/7! + ... +``` + +to calculate `sin(x)` and `cos(x)`. It also uses the relation + +``` +cos(x) = sin(x + pi/2) +``` + +to calculate `cos(x)`. It has a complexity of `O(n^3)`. + +**Note**: this series has a tendency to *occasionally* produce an error of 1 +[ULP][6]. (It is an unfortunate side effect of the algorithm, and there isn't +any way around it; [this article][7] explains why calculating sine and cosine, +and the other transcendental functions below, within less than 1 ULP is nearly +impossible and unnecessary.) Therefore, I recommend that users do their +calculations with the precision (`scale`) set to at least 1 greater than is +needed. + +### Exponentiation (`bc` Only) + +This `bc` uses the series + +``` +1 + x + x^2/2! + x^3/3! + ... +``` + +to calculate `e^x`. Since this only works when `x` is small, it uses + +``` +e^x = (e^(x/2))^2 +``` + +to reduce `x`. It has a complexity of `O(n^3)`. + +**Note**: this series can also produce errors of 1 ULP, so I recommend users do +their calculations with the precision (`scale`) set to at least 1 greater than +is needed. + +### Natural Logarithm (`bc` Only) + +This `bc` uses the series + +``` +a + a^3/3 + a^5/5 + ... +``` + +(where `a` is equal to `(x - 1)/(x + 1)`) to calculate `ln(x)` when `x` is small +and uses the relation + +``` +ln(x^2) = 2 * ln(x) +``` + +to sufficiently reduce `x`. It has a complexity of `O(n^3)`. + +**Note**: this series can also produce errors of 1 ULP, so I recommend users do +their calculations with the precision (`scale`) set to at least 1 greater than +is needed. + +### Arctangent (`bc` Only) + +This `bc` uses the series + +``` +x - x^3/3 + x^5/5 - x^7/7 + ... +``` + +to calculate `atan(x)` for small `x` and the relation + +``` +atan(x) = atan(c) + atan((x - c)/(1 + x * c)) +``` + +to reduce `x` to small enough. It has a complexity of `O(n^3)`. + +**Note**: this series can also produce errors of 1 ULP, so I recommend users do +their calculations with the precision (`scale`) set to at least 1 greater than +is needed. + +### Bessel (`bc` Only) + +This `bc` uses the series + +``` +x^n/(2^n * n!) * (1 - x^2 * 2 * 1! * (n + 1)) + x^4/(2^4 * 2! * (n + 1) * (n + 2)) - ... +``` + +to calculate the bessel function (integer order only). + +It also uses the relation + +``` +j(-n,x) = (-1)^n * j(n,x) +``` + +to calculate the bessel when `x < 0`, It has a complexity of `O(n^3)`. + +**Note**: this series can also produce errors of 1 ULP, so I recommend users do +their calculations with the precision (`scale`) set to at least 1 greater than +is needed. + +### Modular Exponentiation (`dc` Only) + +This `dc` uses the [Memory-efficient method][8] to compute modular +exponentiation. The complexity is `O(e*n^2)`, which may initially seem +inefficient, but `n` is kept small by maintaining small numbers. In practice, it +is extremely fast. + +[1]: https://en.wikipedia.org/wiki/Karatsuba_algorithm +[2]: https://en.wikipedia.org/wiki/Long_division +[3]: https://en.wikipedia.org/wiki/Exponentiation_by_squaring +[4]: https://en.wikipedia.org/wiki/Newton%27s_method#Square_root_of_a_number +[5]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method +[6]: https://en.wikipedia.org/wiki/Unit_in_the_last_place +[7]: https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT +[8]: https://en.wikipedia.org/wiki/Modular_exponentiation#Memory-efficient_method Index: contrib/bc/manuals/bc.1 =================================================================== --- contrib/bc/manuals/bc.1 +++ contrib/bc/manuals/bc.1 @@ -0,0 +1,1423 @@ +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "BC" "1" "June 2019" "Gavin D. Howard" "General Commands Manual" +. +.SH "NAME" +\fBbc\fR \- arbitrary\-precision arithmetic language and calculator +. +.SH "SYNOPSIS" +\fBbc\fR [\fB\-ghilPqsvVw\fR] [\fB\-\-global\-stacks\fR] [\fB\-\-help\fR] [\fB\-\-interactive\fR] [\fB\-\-mathlib\fR] [\fB\-\-no\-prompt\fR] [\fB\-\-quiet\fR] [\fB\-\-standard\fR] [\fB\-\-warn\fR] [\fB\-\-version\fR] [\fB\-e\fR \fIexpr\fR] [\fB\-\-expression=\fR\fIexpr\fR\.\.\.] [\fB\-f\fR \fIfile\fR\.\.\.] [\fB\-file=\fR\fIfile\fR\.\.\.] [\fIfile\fR\.\.\.] +. +.SH "DESCRIPTION" +bc(1) is an interactive processor for a language first standardized in 1991 by POSIX\. (The current standard is here \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR\.) The language provides unlimited precision decimal arithmetic and is somewhat C\-like, but there are differences\. Such differences will be noted in this document\. +. +.P +After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \fBstdin\fR\. +. +.P +With all build options, except for extra math, enabled this bc(1) is a drop\-in replacement for \fB\fIany\fR\fR bc(1), including (and especially) the GNU bc(1)\. It is also a drop\-in replacement for any bc(1) if extra math is enabled, but it will have extra features not found in other bc(1) implementations\. +. +.SH "OPTIONS" +The following are the options that bc(1) accepts\. +. +.TP +\fB\-g\fR, \fB\-\-global\-stacks\fR +Turns the globals \fBibase\fR, \fBobase\fR, and \fBscale\fR into stacks\. +. +.IP +This has the effect that a copy of the current value of all three are pushed onto a stack for every function call, as well as popped when every function returns\. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions\. Thus, \fBoutput(x,b)\fR (in the \fIextended library\fR) could have been written like this: +. +.IP +\fBdefine void output(x, b) { obase=b; x }\fR +. +.IP +instead of like this: +. +.IP +\fBdefine void output(x, b) { auto c; c=obase; obase=b; x; obase=c }\fR +. +.IP +This makes writing functions much easier\. +. +.IP +However, since using this flag means that functions cannot set \fBibase\fR, \fBobase\fR, or \fBscale\fR globally, functions that are made to do so cannot work anymore\. There are two possible use cases for that, and each has a solution\. +. +.IP +First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases\. Examples: +. +.IP +\fBalias d2o="bc \-e ibase=A \-e obase=8"; alias h2b="bc \-e ibase=G \-e obase=2"\fR +. +.IP +Second, if a function\'s purpose is to set \fBibase\fR, \fBobase\fR, or \fBscale\fR globally for any other purpose, it could be split into one to three functions (based on how many globals it sets) and each of those functions could return the desired value for a global\. +. +.IP +If this behavior is desired for every run of bc(1), then users could make sure to define \fBBC_ENV_ARGS\fR and include this option (see the ENVIRONMENT VARIABLES section for more details)\. +. +.IP +If \fB\-s\fR, \fB\-w\fR, or any equivalents are used, this option is ignored\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-h\fR, \fB\-\-help\fR +Prints a usage message and quits\. +. +.TP +\fB\-i\fR, \fB\-\-interactive\fR +Forces interactive mode\. +. +.IP +Per the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR, bc(1) has an interactive mode and a non\-interactive mode\. The interactive mode is turned on automatically when both \fBstdin\fR and \fBstdout\fR are hooked to a terminal, but this flag can turn it on in other cases\. In interactive mode, bc(1) attempts to recover from errors (see the RESET section), and in normal execution, flushes \fBstdout\fR as soon as execution is done for the current input\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-l\fR, \fB\-\-mathlib\fR +Sets \fBscale\fR (see the Scale section) to \fB20\fR and loads the included math library before running any code, including any expressions or files specified on the command line\. +. +.IP +To learn what is in the library, see the LIBRARY section\. +. +.TP +\fB\-P\fR, \fB\-\-no\-prompt\fR +Disables the prompt in interactive mode\. This is mostly for those users that do not want a prompt or are not used to having them in \fBbc\fR\. Most of those users would want to put this option in \fBBC_ENV_ARGS\fR\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Do not print copyright header\. bc(1) will also suppress the header in non\-interactive mode\. +. +.IP +This is mostly for compatibility with the GNU bc(1) \fIhttps://www\.gnu\.org/software/bc/\fR\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-s\fR, \fB\-\-standard\fR +Process exactly the language defined by the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR and error if any extensions are used\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-v\fR, \fB\-V\fR, \fB\-\-version\fR +Print the version information (copyright header) and exit\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-w\fR, \fB\-\-warn\fR +Like \fB\-s\fR and \fB\-\-standard\fR, except that warnings (and not errors) are given for non\-standard extensions\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-e\fR \fIexpr\fR, \fB\-\-expression\fR=\fIexpr\fR +Evaluates \fBexpr\fR\. If multiple expressions are given, they are evaluated in order\. If files are given as well (see below), the expressions and files are evaluated in the order given\. This means that if a file is given before an expression, the file is read in and evaluated first\. +. +.IP +In other bc(1) implementations, this option causes the program to execute the expressions and then exit\. This bc(1) does not, unless the \fBBC_EXPR_EXIT\fR is defined (see the ENVIRONMENT VARIABLES section)\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-f\fR \fIfile\fR, \fB\-\-file\fR=\fIfile\fR +Reads in \fBfile\fR and evaluates it\. If expressions are also given (see above), the expressions are evaluated in the order given\. +. +.IP +In other bc(1) implementations, this option causes the program to execute the files and then exit\. This bc(1) does not, unless the \fBBC_EXPR_EXIT\fR is defined (see the ENVIRONMENT VARIABLES section)\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.SH "STDOUT" +Any non\-error output is written to \fBstdout\fR\. +. +.P +\fBNote\fR: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the EXIT STATUS section) if it cannot write to \fBstdout\fR, so if \fBstdout\fR is closed, as in \fBbc >&\-\fR, it will quit with an error\. This is done so that bc(1) can report problems when \fBstdout\fR is redirected to a file\. +. +.P +If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \fBstdout\fR to \fB/dev/null\fR\. +. +.SH "STDERR" +Any error output is written to \fBstderr\fR\. +. +.P +\fBNote\fR: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the EXIT STATUS section) if it cannot write to \fBstderr\fR, so if \fBstderr\fR is closed, as in \fBbc 2>&\-\fR, it will quit with an error\. This is done so that bc(1) can report problems when \fBstderr\fR is redirected to a file\. +. +.P +If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \fBstderr\fR to \fB/dev/null\fR\. +. +.SH "SYNTAX" +The syntax for bc(1) programs is mostly C\-like, with some differences\. This bc(1) follows the POSIX standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR, which is a much more thorough resource for the language this bc(1) accepts\. This section is meant to be a summary and a listing of all the extensions to the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR\. +. +.P +In the sections below, \fBE\fR means expression, \fBS\fR means statement, and \fBI\fR means identifier\. +. +.P +Identifiers (\fBI\fR) start with a lowercase letter and can be followed by any number (up to \fBBC_NAME_MAX\-1\fR) of lowercase letters (\fBa\-z\fR), digits (\fB0\-9\fR), and underscores (\fB_\fR)\. The regex is \fB[a\-z][a\-z0\-9_]*\fR Identifiers with more than one character (letter) are a \fBnon\-portable extension\fR\. +. +.P +\fBibase\fR is a global variable determining how to interpret constant numbers\. It is the "input" base, or the number base used for interpreting input numbers\. \fBibase\fR is initially \fB10\fR\. If the \fB\-s\fR (\fB\-\-standard\fR) and \fB\-w\fR (\fB\-\-warn\fR) flags were not given on the command line, the max allowable value for \fBibase\fR is \fB36\fR\. Otherwise, it is \fB16\fR\. The min allowable value for \fBibase\fR is \fB2\fR\. The max allowable value for \fBibase\fR can be queried in bc(1) programs with the \fBmaxibase()\fR built in function\. +. +.P +\fBobase\fR is a global variable determining how to output results\. It is the "output" base, or the number base used for outputting numbers\. \fBobase\fR is initially \fB10\fR\. The max allowable value for \fBobase\fR is \fBBC_BASE_MAX\fR\. The min allowable value for \fBobase\fR is \fB2\fR, unless bc(1) was built with the extra math option\. If it was, then the min allowable value is \fB0\fR\. In this case, if \fBobase\fR is \fB0\fR, values are output in scientific notation, and if \fBobase\fR is \fB1\fR, values are output in engineering notation\. (Outputting in scientific or engineering notation are \fBnon\-portable extensions\fR\.) The max allowable value for \fBobase\fR can be queried in bc(1) programs with the \fBmaxobase()\fR built in function\. +. +.P +The \fBscale\fR of an expression is the number of digits in the result of the expression right of the decimal point, and \fBscale\fR is a global variable that sets the precision of any operations, with exceptions\. \fBscale\fR is initially \fB0\fR\. \fBscale\fR cannot be negative\. The max allowable value for \fBscale\fR can be queried in bc(1) programs with the \fBmaxscale()\fR built in function\. +. +.P +bc(1) has both \fBglobal\fR variables and \fBlocal\fR variables\. All \fBlocal\fR variables are local to the function; they are parameters or are introduced in a function\'s \fBauto\fR list (see FUNCTIONS)\. If a variable is accessed which is not a parameter or in the \fBauto\fR list, it is assumed to be \fBglobal\fR\. If a parent function has a \fBlocal\fR variable version of a \fBglobal\fR variable that is accessed by a function that it calls, the value of that \fBglobal\fR variable in the child function is the value of the variable in the parent function, not the value of the actual \fBglobal\fR variable\. +. +.P +All of the above applies to arrays as well\. +. +.P +The value of a statement that is an expression (i\.e\., any of the \fINamed Expressions\fR or \fIOperands\fR) is printed unless the lowest precedence operator is an \fI\fBassignment\fR\fR operator \fB\fIand\fR\fR the expression is not surrounded by parentheses\. +. +.P +The value that is printed is also assigned to the special variable \fBlast\fR\. A single dot (\fB\.\fR) may also be used as a synonym for \fBlast\fR\. These are \fBnon\-portable extensions\fR\. +. +.P +Either semicolons or newlines may separate statements\. +. +.SS "Comments" +There are two kinds of comments: +. +.IP "1." 4 +Block comments are enclosed in \fB/*\fR and \fB*/\fR\. +. +.IP "2." 4 +Line comments go from \fB#\fR until, and not including, the next newline\. This is a \fBnon\-portable extension\fR\. +. +.IP "" 0 +. +.P + \fI\fR +. +.SS "Named Expressions" +. +.IP "1." 4 +Variables: \fBI\fR +. +.IP "2." 4 +Array Elements: \fBI[E]\fR +. +.IP "3." 4 +\fBibase\fR +. +.IP "4." 4 +\fBobase\fR +. +.IP "5." 4 +\fBscale\fR +. +.IP "6." 4 +\fBlast\fR or a single dot (\fB\.\fR) +. +.IP "" 0 +. +.P +Variables and arrays do not interfere; users can have arrays named the same as variables\. This also applies to functions (see the FUNCTIONS section), so a user can have a variable, array, and function that all have the same name\. +. +.P +Named expressions are required as the operand of \fI\fBincrement\fR/\fBdecrement\fR operators\fR and as the left side of \fI\fBassignment\fR operators\fR\. +. +.P + \fI\fR +. +.SS "Operands" +. +.IP "1." 4 +Numbers (see \fINumbers\fR below)\. +. +.IP "2." 4 +Array indices (\fBI[E]\fR)\. +. +.IP "3." 4 +\fB(E)\fR: The value of \fBE\fR (used to change precedence)\. +. +.IP "4." 4 +\fBsqrt(E)\fR: The square root of \fBE\fR\. +. +.IP "5." 4 +\fBlength(E)\fR: The number of significant decimal digits in \fBE\fR\. +. +.IP "6." 4 +\fBlength(I[])\fR: The number of elements in the array \fBI\fR\. This is a \fBnon\-portable extension\fR\. +. +.IP "7." 4 +\fBscale(E)\fR: \fBE\fR\'s \fBscale\fR\. +. +.IP "8." 4 +\fBabs(E)\fR: The absolute value of \fBE\fR\. This is a \fBnon\-portable extension\fR\. +. +.IP "9." 4 +\fBI()\fR, \fBI(E)\fR, \fBI(E, E)\fR, and so on, where \fBI\fR is an identifier for a non\-\fIvoid function\fR\. The \fBE\fR parameters may also be arrays and \fIarray references\fR\. +. +.IP "10." 4 +\fBread()\fR: Reads a line from \fBstdin\fR and uses that as an expression\. The result of that expression is the result of the \fBread()\fR operand\. This is a \fBnon\-portable extension\fR\. +. +.IP "11." 4 +\fBmaxibase()\fR: The max allowable \fBibase\fR\. This is a \fBnon\-portable extension\fR\. +. +.IP "12." 4 +\fBmaxobase()\fR: The max allowable \fBobase\fR\. This is a \fBnon\-portable extension\fR\. +. +.IP "13." 4 +\fBmaxscale()\fR: The max allowable \fBscale\fR\. This is a \fBnon\-portable extension\fR\. +. +.IP "" 0 +. +.P + \fI\fR +. +.SS "Numbers" +Numbers are strings made up of digits, uppercase letters, and at most \fB1\fR period for a radix\. Numbers can have up to \fBBC_NUM_MAX\fR digits\. Uppercase letters equal \fB9\fR + their position in the alphabet (i\.e\., \fBA\fR equals \fB10\fR, or \fB9 + 1\fR)\. If a digit or letter makes no sense with the current value of \fBibase\fR, they are set to the value of the highest valid digit in \fBibase\fR\. +. +.P +Single\-character numbers (i\.e\., \fBA\fR) take the value that they would have if they were valid digits, regardless of the value of \fBibase\fR\. This means that \fBA\fR always equals decimal \fB10\fR and \fBZ\fR always equals decimal \fB35\fR\. +. +.P +In addition, if bc(1) was built with the extra math option, it accepts numbers in scientific notation\. For bc(1), an example is \fB1\.89237e9\fR, which is equal to \fB1892370000\fR\. Negative exponents are also allowed, so \fB4\.2890e\-3\fR is equal to \fB0\.0042890\fR\. +. +.P +Using scientific notation is an error or warning if the \fB\-s\fR or \fB\-w\fR, respectively, command\-line options (or equivalents) are given\. +. +.P +\fBWARNING\fR: Both the number and the exponent in scientific notation are interpreted according to the current \fBibase\fR, but the number is still multiplied by \fB10^exponent\fR regardless of the current \fBibase\fR\. For example, if \fBibase\fR is \fB16\fR and bc(1) is given the number string \fB"FFeA"\fR, the resulting decimal number will be \fB2550000000000\fR, and if bc(1) is given the number string \fB"10e\-4"\fR, the resulting decimal number will be \fB0\.0016\fR\. +. +.P +Accepting input as scientific notation is a \fBnon\-portable extension\fR\. +. +.SS "Operators" +The following arithmetic and logical operators can be used\. They are listed in order of decreasing precedence\. Operators in the same group have the same precedence\. +. +.TP +\fB++\fR \fB\-\-\fR +Type: Prefix and Postfix +. +.IP +Associativity: None +. +.IP +Description: \fBincrement\fR, \fBdecrement\fR +. +.TP +\fB\-\fR \fB!\fR +Type: Prefix +. +.IP +Associativity: None +. +.IP +Description: \fBnegation\fR, \fBboolean not\fR +. +.TP +\fB$\fR +Type: Postfix +. +.IP +Associativity: None +. +.IP +Description: \fBtruncation\fR +. +.TP +\fB@\fR +Type: Binary +. +.IP +Associativity: Right +. +.IP +Description: \fBset precision\fR +. +.TP +\fB^\fR +Type: Binary +. +.IP +Associativity: Right +. +.IP +Description: \fBpower\fR +. +.TP +\fB*\fR \fB/\fR \fB%\fR +Type: Binary +. +.IP +Associativity: Left +. +.IP +Description: \fBmultiply\fR, \fBdivide\fR, \fBmodulus\fR +. +.TP +\fB+\fR \fB\-\fR +Type: Binary +. +.IP +Associativity: Left +. +.IP +Description: \fBadd\fR, \fBsubtract\fR +. +.TP +\fB<<\fR \fB>>\fR +Type: Binary +. +.IP +Associativity: Left +. +.IP +Description: \fBshift left\fR, \fBshift right\fR +. +.TP +\fB=\fR \fB<<=\fR \fB>>=\fR \fB+=\fR \fB\-=\fR \fB*=\fR \fB/=\fR \fB%=\fR \fB^=\fR \fB@=\fR +Type: Binary +. +.IP +Associativity: Right +. +.IP +Description: \fBassignment\fR +. +.TP +\fB==\fR \fB<=\fR \fB>=\fR \fB!=\fR \fB<\fR \fB>\fR +Type: Binary +. +.IP +Associativity: Left +. +.IP +Description: \fBrelational\fR +. +.TP +\fB&&\fR +Type: Binary +. +.IP +Associativity: Left +. +.IP +Description: \fBboolean and\fR +. +.TP +\fB||\fR +Type: Binary +. +.IP +Associativity: Left +. +.IP +Description: \fBboolean or\fR +. +.P +The operators will be described in more detail below\. +. +.P + \fI\fR +. +.TP +\fB++\fR \fB\-\-\fR +The prefix and postfix \fBincrement\fR and \fBdecrement\fR operators behave exactly like they would in C\. They require a \fInamed expression\fR as an operand\. +. +.TP +\fB\-\fR +The \fBnegation\fR operator returns \fB0\fR if a user attempts to negate any expression with the value \fB0\fR\. Otherwise, a copy of the expression with its sign flipped is returned\. +. +.TP +\fB!\fR +The \fBboolean not\fR operator returns \fB1\fR if the expression is \fB0\fR, or \fB0\fR otherwise\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB$\fR +The \fBtruncation\fR operator returns a copy of the given expression with all of its \fBscale\fR removed\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.IP +This is only available if bc(1) has been compiled with the extra math option enabled\. +. +.TP +\fB@\fR +The \fBset precision\fR operator takes two expressions and returns a copy of the first with its \fBscale\fR equal to the value of the second expression\. That could either mean that the number is returned without change (if the first expression\'s \fBscale\fR matches the value of the second expression), extended (if it is less), or truncated (if it is more)\. +. +.IP +The second expression must be an integer (no \fBscale\fR) and non\-negative\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.IP +This is only available if bc(1) has been compiled with the extra math option enabled\. +. +.TP +\fB^\fR +The \fBpower\fR operator (not the \fBexclusive or\fR operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second\. +. +.IP +The second expression must be an integer (no \fBscale\fR)\. +. +.TP +\fB*\fR +The \fBmultiply\fR operator takes two expressions, multiplies them, and returns the product\. If \fBa\fR is the \fBscale\fR of the first expression and \fBb\fR is the \fBscale\fR of the second expression, the scale of the result is equal to \fBmin(a+b,max(scale,a,b))\fR where \fBmin\fR and \fBmax\fR return the obvious values\. +. +.TP +\fB/\fR +The \fBdivide\fR operator takes two expressions, divides them, and returns the quotient\. The scale of the result shall be the value of \fBscale\fR\. +. +.TP +\fB%\fR +The \fBmodulus\fR operator takes two expressions, \fBa\fR and \fBb\fR, and evaluates them by 1) Computing \fBa/b\fR to current \fBscale\fR and 2) Using the result of step 1 to calculate \fBa\-(a/b)*b\fR to scale \fBmax(scale+scale(b),scale(a))\fR\. +. +.TP +\fB+\fR +The \fBadd\fR operator takes two expressions, \fBa\fR and \fBb\fR, and returns the sum, with a \fBscale\fR equal to the max of the \fBscale\fRs of \fBa\fR and \fBb\fR\. +. +.TP +\fB\-\fR +The \fBsubtract\fR operator takes two expressions, \fBa\fR and \fBb\fR, and returns the difference, with a \fBscale\fR equal to the max of the \fBscale\fRs of \fBa\fR and \fBb\fR\. +. +.TP +\fB<<\fR +The \fBleft shift\fR operator takes two expressions, \fBa\fR and \fBb\fR, and returns a copy of the value of \fBa\fR with its decimal point moved \fBb\fR places to the right\. +. +.IP +The second expression must be an integer (no \fBscale\fR) and non\-negative\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.IP +This is only available if bc(1) has been compiled with the extra math option enabled\. +. +.TP +\fB>>\fR +The \fBright shift\fR operator takes two expressions, \fBa\fR and \fBb\fR, and returns a copy of the value of \fBa\fR with its decimal point moved \fBb\fR places to the left\. +. +.IP +The second expression must be an integer (no \fBscale\fR) and non\-negative\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.IP +This is only available if bc(1) has been compiled with the extra math option enabled\. +. +.P + \fI\fR +. +.TP +\fB=\fR \fB<<=\fR \fB>>=\fR \fB+=\fR \fB\-=\fR \fB*=\fR \fB/=\fR \fB%=\fR \fB^=\fR \fB@=\fR +The \fBassignment\fR operators take two expressions, \fBa\fR and \fBb\fR where \fBa\fR is a \fInamed expression\fR\. +. +.IP +For \fB=\fR, \fBb\fR is copied and the result is assigned to \fBa\fR\. For all others, \fBa\fR and \fBb\fR are applied as operands to the corresponding arithmetic operator and the result is assigned to \fBa\fR\. +. +.IP +The \fBassignment\fR operators that correspond to operators that are extensions are themselves extensions\. +. +.IP +Also, those \fBassignment\fR operators that are extensions are only available if bc(1) has been compiled with the extra math option enabled\. +. +.TP +\fB==\fR \fB<=\fR \fB>=\fR \fB!=\fR \fB<\fR \fB>\fR +The \fBrelational\fR operators compare two expressions, \fBa\fR and \fBb\fR, and if the relation holds, according to C language semantics, the result is \fB1\fR\. Otherwise, it is \fB0\fR\. +. +.IP +Note that unlike in C, these operators have a lower precedence than the \fBassignment\fR operators, which means that \fBa=b>c\fR is interpreted as \fB(a=b)>c\fR\. +. +.IP +Also, unlike the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR requires, these operators can appear anywhere any other expressions can be used\. This allowance is a \fBnon\-portable extension\fR\. +. +.TP +\fB&&\fR +The \fBboolean and\fR operator takes two expressions and returns \fB1\fR if both expressions are non\-zero, \fB0\fR otherwise\. +. +.IP +This is \fB\fInot\fR\fR a short\-circuit operator\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB||\fR +The \fBboolean or\fR operator takes two expressions and returns \fB1\fR if one of the expressions is non\-zero, \fB0\fR otherwise\. +. +.IP +This is \fB\fInot\fR\fR a short\-circuit operator\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.SS "Statements" +The following items are statements: +. +.IP "1." 4 +\fBE\fR +. +.IP "2." 4 +\fB{\fR \fBS\fR \fB;\fR \.\.\. \fB;\fR \fBS\fR \fB}\fR +. +.IP "3." 4 +\fBif\fR \fB(\fR \fBE\fR \fB)\fR \fBS\fR +. +.IP "4." 4 +\fBif\fR \fB(\fR \fBE\fR \fB)\fR \fBS\fR \fBelse\fR \fBS\fR +. +.IP "5." 4 +\fBwhile\fR \fB(\fR \fBE\fR \fB)\fR \fBS\fR +. +.IP "6." 4 +\fBfor\fR \fB(\fR \fBE\fR \fB;\fR \fBE\fR \fB;\fR \fBE\fR \fB)\fR \fBS\fR +. +.IP "7." 4 +An empty statement +. +.IP "8." 4 +\fBbreak\fR +. +.IP "9." 4 +\fBcontinue\fR +. +.IP "10." 4 +\fBquit\fR +. +.IP "11." 4 +\fBhalt\fR +. +.IP "12." 4 +\fBlimits\fR +. +.IP "13." 4 +A string of characters, enclosed in double quotes +. +.IP "14." 4 +\fBprint\fR \fBE\fR \fB,\fR \.\.\. \fB,\fR \fBE\fR +. +.IP "15." 4 +\fBI()\fR, \fBI(E)\fR, \fBI(E, E)\fR, and so on, where \fBI\fR is an identifier for a \fIvoid function\fR\. The \fBE\fR parameters may also be arrays and \fIarray references\fR\. +. +.IP "" 0 +. +.P +Numbers 4, 9, 11, 12, 14, and 15 are \fBnon\-portable extensions\fR\. +. +.P +Also, as a \fBnon\-portable extension\fR, any or all of the expressions in the header of a for loop may be omitted\. If the condition (second expression) is omitted, it is assumed to be a constant \fB1\fR\. +. +.P +The \fBbreak\fR statement causes a loop to stop iterating and resume execution immediately following a loop\. This is only allowed in loops\. +. +.P +The \fBcontinue\fR statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition\. This is only allowed in loops\. +. +.P +The \fBif\fR \fBelse\fR statement does the same thing as in C\. +. +.P +The \fBquit\fR statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile\-time command)\. +. +.P +The \fBhalt\fR statement causes bc(1) to quit, if it is executed\. (Unlike \fBquit\fR if it is on a branch of an \fBif\fR statement that is not executed, bc(1) does not quit\.) +. +.P +The \fBlimits\fR statement prints the limits that this bc(1) is subject to\. This is like the \fBquit\fR statement in that it is a compile\-time command\. +. +.P +An expression by itself is evaluated and printed, followed by a newline\. If bc(1) has been built with the extra math option enabled, both scientific notation and engineering notation are available for printing the results of expressions\. Scientific notation is activated by assigning \fB0\fR to \fBobase\fR (in any other context, an \fBobase\fR of \fB0\fR is invalid), and engineering notation is activated by assigning \fB1\fR to \fBobase\fR (which is also invalid in any other context)\. To deactivate them, just assign a different value to \fBobase\fR\. +. +.P +Scientific notation and engineering notation are disabled if bc(1) is run with either the \fB\-s\fR or \fB\-w\fR command\-line options (or equivalents)\. +. +.P +Printing numbers in scientific notation and/or engineering notation is a \fBnon\-portable extension\fR\. +. +.SS "Print Statement" +The "expressions" in a \fBprint\fR statement may also be strings\. If they are, there are backslash escape sequences that are interpreted specially\. What those sequences are, and what they cause to be printed, are shown below: +. +.TP +\fB\ea\fR +\fB\ea\fR +. +.TP +\fB\eb\fR +\fB\eb\fR +. +.TP +\fB\e\e\fR +\fB\e\fR +. +.TP +\fB\ee\fR +\fB\e\fR +. +.TP +\fB\ef\fR +\fB\ef\fR +. +.TP +\fB\en\fR +\fB\en\fR +. +.TP +\fB\eq\fR +\fB"\fR +. +.TP +\fB\er\fR +\fB\er\fR +. +.TP +\fB\et\fR +\fB\et\fR +. +.P +Any other character following a backslash causes the backslash and character to be printed as\-is\. +. +.P +Any non\-string expression in a print statement shall be assigned to \fBlast\fR, like any other expression that is printed\. +. +.SS "Order of Evaluation" +All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations\. This means, for example, that in the expression \fBi = 0; a[i++] = i++\fR, the first (or 0th) element of \fBa\fR is set to \fB1\fR, and \fBi\fR is equal to \fB2\fR at the end of the expression\. +. +.P +This includes function arguments\. Thus, this means that in the expression \fBi = 0; x(i++, i++)\fR, the first argument passed to \fBx()\fR is \fB0\fR, and the second argument is \fB1\fR, while \fBi\fR is equal to \fB2\fR before the function starts executing\. +. +.SH "FUNCTIONS" +Function definitions are as follows: +. +.IP "" 4 +. +.nf + +define I(I,\.\.\.,I){ + auto I,\.\.\.,I + S;\.\.\.;S + return(E) +} +. +.fi +. +.IP "" 0 +. +.P +Any \fBI\fR in the parameter list or \fBauto\fR list may be replaced with \fBI[]\fR to make a parameter or \fBauto\fR var an array\. +. +.P +As a \fBnon\-portable extension\fR, the opening brace of a \fBdefine\fR statement may appear on the next line\. +. +.P +The return statement may also be in the following forms: +. +.IP "1." 4 +\fBreturn\fR +. +.IP "2." 4 +\fBreturn\fR \fB(\fR \fB)\fR +. +.IP "3." 4 +\fBreturn\fR \fBE\fR +. +.IP "" 0 +. +.P +The first two, or not specifying a \fBreturn\fR statement, is equivalent to \fBreturn (0)\fR, unless the function is a \fIvoid function\fR\. +. +.P + \fI\fR +. +.SS "Void Functions" +Functions can also be void functions, defined as follows: +. +.IP "" 4 +. +.nf + +define void I(I,\.\.\.,I){ + auto I,\.\.\.,I + S;\.\.\.;S + return(E) +} +. +.fi +. +.IP "" 0 +. +.P +They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement\. +. +.P +Void functions can only use the first two \fBreturn\fR statements listed above\. +. +.P +The word \fBvoid\fR is not treated as a keyword; it is still possible to have variables, arrays, and functions named \fBvoid\fR\. +. +.P +This is a \fBnon\-portable extension\fR\. +. +.P + \fI\fR +. +.SS "Array References" +For any array in the parameter list, if the array is declared in the form +. +.IP "" 4 +. +.nf + +*I[] +. +.fi +. +.IP "" 0 +. +.P +it is a \fBreference\fR\. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in\. +. +.P +Other than this, all function arguments are passed by value\. +. +.P +This is a \fBnon\-portable extension\fR\. +. +.SH "LIBRARY" +All of the functions below, including the functions in the \fIextended library\fR if bc(1) has been compiled with the extra math option enabled, are available when the \fB\-l\fR or \fB\-\-mathlib\fR command\-line flags are given\. +. +.P + \fI\fR +. +.SS "Standard Library" +The standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR defines the following functions for the math library: +. +.TP +\fBs(x)\fR +Returns the sine of \fBx\fR, which is assumed to be in radians\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBc(x)\fR +Returns the cosine of \fBx\fR, which is assumed to be in radians\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBa(x)\fR +Returns the arctangent of \fBx\fR, in radians\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBl(x)\fR +Returns the natural logarithm of \fBx\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBe(x)\fR +Returns the mathematical constant \fBe\fR raised to the power of \fBx\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBj(x, n)\fR +Returns the bessel integer order \fBn\fR (truncated) of \fBx\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.P + \fI\fR +. +.SS "Extended Library" +In addition to the \fIstandard library\fR, if bc(1) has been built with the extra math option, the following functions are available when either the \fB\-l\fR or \fB\-\-mathlib\fR options are given\. +. +.P +However, the extended library is \fB\fInot\fR\fR loaded when the \fB\-s\fR/\fB\-\-standard\fR or \fB\-w\fR/\fB\-\-warn\fR options are given since they are not part of the library defined by the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR\. +. +.P +The extended library is a \fBnon\-portable extension\fR\. +. +.TP +\fBr(x, p)\fR +Returns \fBx\fR rounded to \fBp\fR decimal places according to the rounding mode round half away from \fB0\fR \fIhttps://en\.wikipedia\.org/wiki/Rounding#Round_half_away_from_zero\fR\. +. +.TP +\fBceil(x, p)\fR +Returns \fBx\fR rounded to \fBp\fR decimal places according to the rounding mode round away from \fB0\fR \fIhttps://en\.wikipedia\.org/wiki/Rounding#Rounding_away_from_zero\fR\. +. +.TP +\fBf(x)\fR +Returns the factorial of the truncated absolute value of \fBx\fR\. +. +.TP +\fBperm(n, k)\fR +Returns the permutation of the truncated absolute value of \fBn\fR of the truncated absolute value of \fBk\fR, if \fBk <= n\fR\. If not, it returns \fB0\fR\. +. +.TP +\fBcomb(n, k)\fR +Returns the combination of the truncated absolute value of \fBn\fR of the truncated absolute value of \fBk\fR, if \fBk <= n\fR\. If not, it returns \fB0\fR\. +. +.TP +\fBl2(x)\fR +Returns the logarithm base \fB2\fR of \fBx\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBl10(x)\fR +Returns the logarithm base \fB10\fR of \fBx\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBlog(x, b)\fR +Returns the logarithm base \fBb\fR of \fBx\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBpi(p)\fR +Returns \fBpi\fR to \fBp\fR decimal places\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBt(x)\fR +Returns the tangent of \fBx\fR, which is assumed to be in radians\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBa2(y, x)\fR +Returns the arctangent of \fBy/x\fR, in radians\. If both \fBy\fR and \fBx\fR are equal to \fB0\fR, it raises an error and causes bc(1) to reset (see the RESET section)\. Otherwise, if \fBx\fR is greater than \fB0\fR, it returns \fBa(y/x)\fR\. If \fBx\fR is less than \fB0\fR, and \fBy\fR is greater than or equal to \fB0\fR, it returns \fBa(y/x) + pi\fR\. If \fBx\fR is less than \fB0\fR, and \fBy\fR is less than \fB0\fR, it returns \fBa(y/x) \- pi\fR\. If \fBx\fR is equal to \fB0\fR, and \fBy\fR is greater than \fB0\fR, it returns \fBpi/2\fR\. If \fBx\fR is equal to \fB0\fR, and \fBy\fR is less than \fB0\fR, it returns \fB\-pi/2\fR\. +. +.IP +This function is the same as the \fBatan2()\fR function in many programming languages\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBsin(x)\fR +Returns the sine of \fBx\fR, which is assumed to be in radians\. +. +.IP +This is an alias of \fBs(x)\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBcos(x)\fR +Returns the cosine of \fBx\fR, which is assumed to be in radians\. +. +.IP +This is an alias of \fBc(x)\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBtan(x)\fR +Returns the tangent of \fBx\fR, which is assumed to be in radians\. +. +.IP +This is an alias of \fBt(x)\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBatan(x)\fR +Returns the arctangent of \fBx\fR, in radians\. +. +.IP +This is an alias of \fBa(x)\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBatan2(y, x)\fR +Returns the arctangent of \fBy/x\fR, in radians\. If both \fBy\fR and \fBx\fR are equal to \fB0\fR, it raises an error and causes bc(1) to reset (see the RESET section)\. Otherwise, if \fBx\fR is greater than \fB0\fR, it returns \fBa(y/x)\fR\. If \fBx\fR is less than \fB0\fR, and \fBy\fR is greater than or equal to \fB0\fR, it returns \fBa(y/x) + pi\fR\. If \fBx\fR is less than \fB0\fR, and \fBy\fR is less than \fB0\fR, it returns \fBa(y/x) \- pi\fR\. If \fBx\fR is equal to \fB0\fR, and \fBy\fR is greater than \fB0\fR, it returns \fBpi/2\fR\. If \fBx\fR is equal to \fB0\fR, and \fBy\fR is less than \fB0\fR, it returns \fB\-pi/2\fR\. +. +.IP +This function is the same as the \fBatan2()\fR function in many programming languages\. +. +.IP +This is an alias of \fBa2(y, x)\fR\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBr2d(x)\fR +Converts \fBx\fR from radians to degrees and returns the result\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBd2r(x)\fR +Converts \fBx\fR from degrees to radians and returns the result\. +. +.IP +This is a \fItranscendental function\fR\. +. +.TP +\fBubytes(x)\fR +Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of \fBx\fR\. +. +.TP +\fBsbytes(x)\fR +Returns the numbers of signed, two\'s\-complement integer bytes required to hold the truncated value of \fBx\fR\. +. +.TP +\fBhex(x)\fR +Outputs the hexadecimal (base \fB16\fR) representation of \fBx\fR\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBbinary(x)\fR +Outputs the binary (base \fB2\fR) representation of \fBx\fR\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBoutput(x, b)\fR +Outputs the base \fBb\fR representation of \fBx\fR\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBuint(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as an unsigned integer in as few power of two bytes as possible\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBint(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as a signed, two\'s\-complement integer in as few power of two bytes as possible\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBuintn(x, n)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as an unsigned integer in \fBn\fR bytes\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer, is negative, or cannot fit into \fBn\fR bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBintn(x, n)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as a signed, two\'s\-complement integer in \fBn\fR bytes\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer or cannot fit into \fBn\fR bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBuint8(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as an unsigned integer in \fB1\fR byte\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer, is negative, or cannot fit into \fB1\fR byte, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBint8(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as a signed, two\'s\-complement integer in \fB1\fR byte\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer or cannot fit into \fB1\fR byte, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBuint16(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as an unsigned integer in \fB2\fR bytes\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer, is negative, or cannot fit into \fB2\fR bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBint16(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as a signed, two\'s\-complement integer in \fB2\fR bytes\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer or cannot fit into \fB2\fR bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBuint32(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as an unsigned integer in \fB4\fR bytes\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer, is negative, or cannot fit into \fB4\fR bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBint32(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as a signed, two\'s\-complement integer in \fB4\fR bytes\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer or cannot fit into \fB4\fR bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBuint64(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as an unsigned integer in \fB8\fR bytes\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer, is negative, or cannot fit into \fB8\fR bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBint64(x)\fR +Outputs the representation, in binary and hexadecimal, of \fBx\fR as a signed, two\'s\-complement integer in \fB8\fR bytes\. Both outputs are split into bytes separated by spaces\. +. +.IP +If \fBx\fR is not an integer or cannot fit into \fB8\fR bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section)\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBhex_uint(x, n)\fR +Outputs the representation of the truncated absolute value of \fBx\fR as an unsigned integer in hexadecimal using \fBn\fR bytes\. Not all of the value will be output if \fBn\fR is too small\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBbinary_uint(x, n)\fR +Outputs the representation of the truncated absolute value of \fBx\fR as an unsigned integer in binary using \fBn\fR bytes\. Not all of the value will be output if \fBn\fR is too small\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBoutput_uint(x, n)\fR +Outputs the representation of the truncated absolute value of \fBx\fR as an unsigned integer in the current \fI\fBobase\fR\fR using \fBn\fR bytes\. Not all of the value will be output if \fBn\fR is too small\. +. +.IP +This is a \fIvoid function\fR\. +. +.TP +\fBoutput_byte(x, i)\fR +Outputs byte \fBi\fR of the truncated absolute value of \fBx\fR, where \fB0\fR is the least significant byte and \fBnumber_of_bytes \- 1\fR is the most significant byte\. +. +.IP +This is a \fIvoid function\fR\. +. +.P + \fI\fR +. +.SS "Transcendental Functions" +All transcendental functions can return slightly inaccurate results (up to 1 ULP \fIhttps://en\.wikipedia\.org/wiki/Unit_in_the_last_place\fR)\. This is unavoidable, and this article \fIhttps://people\.eecs\.berkeley\.edu/~wkahan/LOG10HAF\.TXT\fR explains why it is impossible and unnecessary to calculate exact results for the transcendental functions\. +. +.P +Because of the possible inaccuracy, I recommend that users call those functions with the precision (\fBscale\fR) set to at least 1 higher than is necessary\. If exact results are \fIabsolutely\fR required, users can double the precision (\fBscale\fR) and then truncate\. +. +.P +The transcendental functions in the standard math library are: +. +.IP "\(bu" 4 +\fBs(x)\fR +. +.IP "\(bu" 4 +\fBc(x)\fR +. +.IP "\(bu" 4 +\fBa(x)\fR +. +.IP "\(bu" 4 +\fBl(x)\fR +. +.IP "\(bu" 4 +\fBe(x)\fR +. +.IP "\(bu" 4 +\fBj(x, n)\fR +. +.IP "" 0 +. +.P +The transcendental functions in the extended math library are: +. +.IP "\(bu" 4 +\fBl2(x)\fR +. +.IP "\(bu" 4 +\fBl10(x)\fR +. +.IP "\(bu" 4 +\fBlog(x, b)\fR +. +.IP "\(bu" 4 +\fBpi(p)\fR +. +.IP "\(bu" 4 +\fBt(x)\fR +. +.IP "\(bu" 4 +\fBa2(y, x)\fR +. +.IP "\(bu" 4 +\fBsin(x)\fR +. +.IP "\(bu" 4 +\fBcos(x)\fR +. +.IP "\(bu" 4 +\fBtan(x)\fR +. +.IP "\(bu" 4 +\fBatan(x)\fR +. +.IP "\(bu" 4 +\fBatan2(y, x)\fR +. +.IP "\(bu" 4 +\fBr2d(x)\fR +. +.IP "\(bu" 4 +\fBd2r(x)\fR +. +.IP "" 0 +. +.SH "RESET" +When bc(1) encounters an error or a signal that it has a non\-default handler for, it resets\. This means that several things happen\. +. +.P +First, any functions that are executing are stopped and popped off the stack\. The behavior is not unlike that of exceptions in programming languages\. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped\. +. +.P +Thus, when bc(1) resets, it skips any remaining code waiting to be executed\. Then, if it is interactive mode, and the error was not a fatal error (see the EXIT STATUS section), it asks for more input; otherwise, it exits with the appropriate return code\. +. +.P +Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error\. +. +.SH "PERFORMANCE" +Most bc(1) implementations use \fBchar\fR types to calculate the value of \fB1\fR decimal digit at a time, but that can be slow\. This bc(1) does something different\. +. +.P +It uses large integers to calculate more than \fB1\fR decimal digit at a time\. If built in a environment where \fBBC_LONG_BIT\fR (see the LIMITS section) is \fB64\fR, then each integer has \fB9\fR decimal digits\. If built in an environment where \fBBC_LONG_BIT\fR is \fB32\fR then each integer has \fB4\fR decimal digits\. This value (the number of decimal digits per large integer) is called \fBBC_BASE_DIGS\fR\. +. +.P +In addition, this bc(1) uses an even larger integer for overflow checking\. This integer type depends on the value of \fBBC_LONG_BIT\fR, but is always at least twice as large as the integer type used to store digits\. +. +.SH "LIMITS" +The following are the limits on bc(1): +. +.TP +\fBBC_LONG_BIT\fR +The number of bits in the \fBlong\fR type in the environment where bc(1) was built\. This determines how many decimal digits can be stored in a single large integer (see the PERFORMANCE section)\. +. +.TP +\fBBC_BASE_DIGS\fR +The number of decimal digits per large integer (see the PERFORMANCE section)\. Depends on \fBBC_LONG_BIT\fR\. +. +.TP +\fBBC_BASE_POW\fR +The max decimal number that each large integer can store (see \fBBC_BASE_DIGS\fR) plus \fB1\fR\. Depends on \fBBC_BASE_DIGS\fR\. +. +.TP +\fBBC_OVERFLOW_MAX\fR +The max number that the overflow type (see the PERFORMANCE section) can hold\. Depends on \fBBC_LONG_BIT\fR\. +. +.TP +\fBBC_BASE_MAX\fR +The maximum output base\. Set at \fBBC_BASE_POW\fR\. +. +.TP +\fBBC_DIM_MAX\fR +The maximum size of arrays\. Set at \fBSIZE_MAX\-1\fR\. +. +.TP +\fBBC_SCALE_MAX\fR +The maximum \fBscale\fR\. Set at \fBBC_OVERFLOW_MAX\-1\fR\. +. +.TP +\fBBC_STRING_MAX\fR +The maximum length of strings\. Set at \fBBC_OVERFLOW_MAX\-1\fR\. +. +.TP +\fBBC_NAME_MAX\fR +The maximum length of identifiers\. Set at \fBBC_OVERFLOW_MAX\-1\fR\. +. +.TP +\fBBC_NUM_MAX\fR +The maximum length of a number (in decimal digits), which includes digits after the decimal point\. Set at \fBBC_OVERFLOW_MAX\-1\fR\. +. +.TP +Exponent +The maximum allowable exponent (positive or negative)\. Set at \fBBC_OVERFLOW_MAX\fR\. +. +.TP +Number of vars +The maximum number of vars/arrays\. Set at \fBSIZE_MAX\-1\fR\. +. +.P +Actual values can be queried with the \fBlimits\fR statement\. +. +.P +These limits are meant to be effectively non\-existent; the limits are so large (at least on 64\-bit machines) that there should not be any point at which they become a problem\. In fact, memory should be exhausted before these limits should be hit\. +. +.SH "ENVIRONMENT VARIABLES" +bc(1) recognizes the following environment variables: +. +.TP +\fBPOSIXLY_CORRECT\fR +If this variable exists (no matter the contents), bc(1) behaves as if the \fB\-s\fR option was given\. +. +.TP +\fBBC_ENV_ARGS\fR +This is another way to give command\-line arguments to bc(1)\. They should be in the same format as all other command\-line arguments\. These are always processed first, so any files given in \fBBC_ENV_ARGS\fR will be processed before files given on the command\-line\. This gives the user the ability to set up "standard" options and files to be used at every invocation\. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs\. +. +.TP +\fBBC_LINE_LENGTH\fR +If this environment variable exists and contains an integer that is greater than \fB1\fR and is less than \fBUINT16_MAX\fR (\fB2^16\-1\fR), bc(1) will output lines to that length, including the backslash (\fB\e\fR)\. The default line length is \fB70\fR\. +. +.TP +\fBBC_EXPR_EXIT\fR +If this variable exists (no matter the contents), bc(1) will exit immediately after executing expressions and files given by the \fB\-e\fR and/or \fB\-f\fR command\-line options (and any equivalents)\. +. +.SH "EXIT STATUS" +bc(1) returns the following exit statuses: +. +.TP +\fB0\fR +No error\. +. +.TP +\fB1\fR +A math error occurred\. This follows standard practice of using \fB1\fR for expected errors, since math errors will happen in the process of normal execution\. +. +.IP +Math errors include divide by \fB0\fR, taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, and attempting to use a non\-integer where an integer is required\. +. +.IP +Converting to a hardware integer happens for the second operand of the power (\fB^\fR), places (\fB@\fR), left shift (\fB<<\fR), and right shift (\fB>>\fR) operators and their corresponding assignment operators\. +. +.TP +\fB2\fR +A parse error occurred\. +. +.IP +Parse errors include unexpected \fBEOF\fR, using an invalid character, failing to find the end of a string or comment, using a token where it\'s invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a \fInamed expression\fR, giving an invalid \fBauto\fR list, having a duplicate \fBauto\fR/function parameter, failing to find the end of a code block, attempting to return a value from a \fBvoid\fR function, attempting to use a variable as a reference, and using any extensions when the option \fB\-s\fR or any equivalents were given\. +. +.TP +\fB3\fR +A runtime error occurred\. +. +.IP +Runtime errors include assigning an invalid number to \fBibase\fR, \fBobase\fR, or \fBscale\fR; give a bad expression to a \fBread()\fR call, calling \fBread()\fR inside of a \fBread()\fR call, type errors, passing the wrong number of parameters to functions, attempting to call an undefined function, and attempting to use a \fBvoid\fR function call as a value in an expression\. +. +.TP +\fB4\fR +A fatal error occurred\. +. +.IP +Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command\-line options\. +. +.P +The exit status \fB4\fR is special; when a fatal error occurs, bc(1) always exits and returns \fB4\fR, no matter what mode bc(1) is in\. +. +.P +The other statuses will only be returned when bc(1) is not in interactive mode, since bc(1) resets its state (see the RESET section) and accepts more input when one of those errors occurs in interactive mode\. This is also the case when interactive mode is forced by the \fB\-i\fR option\. +. +.P +These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using \fB\-i\fR\. +. +.SH "SIGNAL HANDLING" +If bc(1) has been compiled with the signal handling, sending a \fBSIGINT\fR will cause bc(1) to stop execution of the current input and reset (see the RESET section), asking for more input\. +. +.P +Otherwise, \fBSIGTERM\fR and \fBSIGQUIT\fR cause bc(1) to clean up and exit, and it uses the default handler for all other signals\. +. +.P +If bc(1) has not been compiled with signal handling, it uses the default signal handlers for all signals\. +. +.SH "COMMAND LINE HISTORY" +bc(1) supports interactive command\-line editing, if compiled with the history option enabled\. If \fBstdin\fR is hooked to a terminal, it is enabled\. Previous lines can be recalled and edited with the arrow keys\. +. +.SH "LOCALES" +This bc(1) ships with support for adding error messages for different locales\. +. +.SH "SEE ALSO" +dc(1) +. +.SH "STANDARDS" +bc(1) is compliant with the IEEE Std 1003\.1\-2017 (“POSIX\.1\-2017”) \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR specification\. The flags \fB\-efghiqsvVw\fR, all long options, and the extensions noted above are extensions to that specification\. +. +.P +Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\fB\.\fR) as a radix point, regardless of the value of \fBLC_NUMERIC\fR\. +. +.P +Because this bc(1) ships with support for adding error messages for different locales, so it supports \fBLC_MESSAGES\fR\. +. +.SH "AUTHOR" +This bc(1) was made from scratch by Gavin D\. Howard\. +. +.SH "BUGS" +None are known\. Report bugs at https://github\.com/gavinhoward/bc\. Index: contrib/bc/manuals/bc.1.ronn =================================================================== --- contrib/bc/manuals/bc.1.ronn +++ contrib/bc/manuals/bc.1.ronn @@ -0,0 +1,1361 @@ +bc(1) -- arbitrary-precision arithmetic language and calculator +=============================================================== + +SYNOPSIS +-------- + +`bc` [`-ghilPqsvVw`] [`--global-stacks`] [`--help`] [`--interactive`] +[`--mathlib`] [`--no-prompt`] [`--quiet`] [`--standard`] [`--warn`] +[`--version`] [`-e` *expr*] [`--expression=`*expr*...] [`-f` *file*...] +[`-file=`*file*...] [*file*...] + +DESCRIPTION +----------- + +bc(1) is an interactive processor for a language first standardized in 1991 by +POSIX. (The current standard is [here][1].) The language provides unlimited +precision decimal arithmetic and is somewhat C-like, but there are differences. +Such differences will be noted in this document. + +After parsing and handling options, this bc(1) reads any files given on the +command line and executes them before reading from `stdin`. + +With all build options, except for extra math, enabled this bc(1) is a drop-in +replacement for ***any*** bc(1), including (and especially) the GNU bc(1). It is +also a drop-in replacement for any bc(1) if extra math is enabled, but it will +have extra features not found in other bc(1) implementations. + +OPTIONS +------- + +The following are the options that bc(1) accepts. + + * `-g`, `--global-stacks`: + Turns the globals `ibase`, `obase`, and `scale` into stacks. + + This has the effect that a copy of the current value of all three are pushed + onto a stack for every function call, as well as popped when every function + returns. This means that functions can assign to any and all of those + globals without worrying that the change will affect other functions. + Thus, `output(x,b)` (in the [extended library](#extended-library)) could + have been written like this: + + `define void output(x, b) { obase=b; x }` + + instead of like this: + + `define void output(x, b) { auto c; c=obase; obase=b; x; obase=c }` + + This makes writing functions much easier. + + However, since using this flag means that functions cannot set `ibase`, + `obase`, or `scale` globally, functions that are made to do so cannot work + anymore. There are two possible use cases for that, and each has a solution. + + First, if a function is called on startup to turn bc(1) into a number + converter, it is possible to replace that capability with various shell + aliases. Examples: + + `alias d2o="bc -e ibase=A -e obase=8"; alias h2b="bc -e ibase=G -e obase=2"` + + Second, if a function's purpose is to set `ibase`, `obase`, or `scale` + globally for any other purpose, it could be split into one to three + functions (based on how many globals it sets) and each of those functions + could return the desired value for a global. + + If this behavior is desired for every run of bc(1), then users could make + sure to define `BC_ENV_ARGS` and include this option (see the + ENVIRONMENT VARIABLES section for more details). + + If `-s`, `-w`, or any equivalents are used, this option is ignored. + + This is a **non-portable extension**. + + * `-h`, `--help`: + Prints a usage message and quits. + + * `-i`, `--interactive`: + Forces interactive mode. + + Per the [standard][1], bc(1) has an interactive mode and a non-interactive + mode. The interactive mode is turned on automatically when both `stdin` and + `stdout` are hooked to a terminal, but this flag can turn it on in other + cases. In interactive mode, bc(1) attempts to recover from errors (see the + RESET section), and in normal execution, flushes `stdout` as soon as + execution is done for the current input. + + This is a **non-portable extension**. + + * `-l`, `--mathlib`: + Sets `scale` (see the Scale section) to `20` and loads the included math + library before running any code, including any expressions or files + specified on the command line. + + To learn what is in the library, see the LIBRARY section. + + * `-P`, `--no-prompt`: + Disables the prompt in interactive mode. This is mostly for those users that + do not want a prompt or are not used to having them in `bc`. Most of those + users would want to put this option in `BC_ENV_ARGS`. + + This is a **non-portable extension**. + + * `-q`, `--quiet`: + Do not print copyright header. bc(1) will also suppress the header in + non-interactive mode. + + This is mostly for compatibility with the [GNU bc(1)][2]. + + This is a **non-portable extension**. + + * `-s`, `--standard`: + Process exactly the language defined by the [standard][1] and error if any + extensions are used. + + This is a **non-portable extension**. + + * `-v`, `-V`, `--version`: + Print the version information (copyright header) and exit. + + This is a **non-portable extension**. + + * `-w`, `--warn`: + Like `-s` and `--standard`, except that warnings (and not errors) are given + for non-standard extensions. + + This is a **non-portable extension**. + + * `-e` *expr*, `--expression`=*expr*: + Evaluates `expr`. If multiple expressions are given, they are evaluated in + order. If files are given as well (see below), the expressions and files are + evaluated in the order given. This means that if a file is given before an + expression, the file is read in and evaluated first. + + In other bc(1) implementations, this option causes the program to execute + the expressions and then exit. This bc(1) does not, unless the + `BC_EXPR_EXIT` is defined (see the ENVIRONMENT VARIABLES section). + + This is a **non-portable extension**. + + * `-f` *file*, `--file`=*file*: + Reads in `file` and evaluates it. If expressions are also given (see above), + the expressions are evaluated in the order given. + + In other bc(1) implementations, this option causes the program to execute + the files and then exit. This bc(1) does not, unless the + `BC_EXPR_EXIT` is defined (see the ENVIRONMENT VARIABLES section). + + This is a **non-portable extension**. + +STDOUT +------ + +Any non-error output is written to `stdout`. + +**Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal +error (see the EXIT STATUS section) if it cannot write to `stdout`, so if +`stdout` is closed, as in `bc >&-`, it will quit with an error. This is +done so that bc(1) can report problems when `stdout` is redirected to a file. + +If there are scripts that depend on the behavior of other bc(1) implementations, +it is recommended that those scripts be changed to redirect `stdout` to +`/dev/null`. + +STDERR +------ + +Any error output is written to `stderr`. + +**Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal +error (see the EXIT STATUS section) if it cannot write to `stderr`, so if +`stderr` is closed, as in `bc 2>&-`, it will quit with an error. This is +done so that bc(1) can report problems when `stderr` is redirected to a file. + +If there are scripts that depend on the behavior of other bc(1) implementations, +it is recommended that those scripts be changed to redirect `stderr` to +`/dev/null`. + +SYNTAX +------ + +The syntax for bc(1) programs is mostly C-like, with some differences. This +bc(1) follows the [POSIX standard][1], which is a much more thorough resource +for the language this bc(1) accepts. This section is meant to be a summary and a +listing of all the extensions to the [standard][1]. + +In the sections below, `E` means expression, `S` means statement, and `I` means +identifier. + +Identifiers (`I`) start with a lowercase letter and can be followed by any +number (up to `BC_NAME_MAX-1`) of lowercase letters (`a-z`), digits (`0-9`), and +underscores (`_`). The regex is `[a-z][a-z0-9_]*` Identifiers with more than one +character (letter) are a **non-portable extension**. + +`ibase` is a global variable determining how to interpret constant numbers. It +is the "input" base, or the number base used for interpreting input numbers. +`ibase` is initially `10`. If the `-s` (`--standard`) and `-w` (`--warn`) flags +were not given on the command line, the max allowable value for `ibase` is `36`. +Otherwise, it is `16`. The min allowable value for `ibase` is `2`. The max +allowable value for `ibase` can be queried in bc(1) programs with the +`maxibase()` built in function. + +`obase` is a global variable determining how to output results. It is the +"output" base, or the number base used for outputting numbers. `obase` is +initially `10`. The max allowable value for `obase` is `BC_BASE_MAX`. The min +allowable value for `obase` is `2`, unless bc(1) was built with the extra math +option. If it was, then the min allowable value is `0`. In this case, if `obase` +is `0`, values are output in scientific notation, and if `obase` is `1`, values +are output in engineering notation. (Outputting in scientific or engineering +notation are **non-portable extensions**.) The max allowable value for `obase` +can be queried in bc(1) programs with the `maxobase()` built in function. + +The **scale** of an expression is the number of digits in the result of the +expression right of the decimal point, and `scale` is a global variable that +sets the precision of any operations, with exceptions. `scale` is initially `0`. +`scale` cannot be negative. The max allowable value for `scale` can be queried +in bc(1) programs with the `maxscale()` built in function. + +bc(1) has both **global** variables and **local** variables. All **local** +variables are local to the function; they are parameters or are introduced in a +function's `auto` list (see FUNCTIONS). If a variable is accessed which is not a +parameter or in the `auto` list, it is assumed to be **global**. If a parent +function has a **local** variable version of a **global** variable that is +accessed by a function that it calls, the value of that **global** variable in +the child function is the value of the variable in the parent function, not the +value of the actual **global** variable. + +All of the above applies to arrays as well. + +The value of a statement that is an expression (i.e., any of the +[Named Expressions](#bc-named-expressions) or [Operands](#bc-operands)) is +printed unless the lowest precedence operator is an +[`assignment`](#bc-assignment) operator ***and*** the expression is not +surrounded by parentheses. + +The value that is printed is also assigned to the special variable `last`. A +single dot (`.`) may also be used as a synonym for `last`. These are +**non-portable extensions**. + +Either semicolons or newlines may separate statements. + +### Comments + +There are two kinds of comments: + +1. Block comments are enclosed in `/*` and `*/`. +2. Line comments go from `#` until, and not including, the next newline. This + is a **non-portable extension**. + + + +### Named Expressions + +1. Variables: `I` +2. Array Elements: `I[E]` +3. `ibase` +4. `obase` +5. `scale` +6. `last` or a single dot (`.`) + +Variables and arrays do not interfere; users can have arrays named the same as +variables. This also applies to functions (see the FUNCTIONS section), so a user +can have a variable, array, and function that all have the same name. + +Named expressions are required as the operand of +[`increment`/`decrement` operators](#bc-increment-decrement) and as the left +side of [`assignment` operators](#bc-assignment). + + + +### Operands + +1. Numbers (see [Numbers](#bc-numbers) below). +2. Array indices (`I[E]`). +3. `(E)`: The value of `E` (used to change precedence). +4. `sqrt(E)`: The square root of `E`. +5. `length(E)`: The number of significant decimal digits in `E`. +6. `length(I[])`: The number of elements in the array `I`. This is a + **non-portable extension**. +7. `scale(E)`: `E`'s **scale**. +8. `abs(E)`: The absolute value of `E`. This is a **non-portable extension**. +9. `I()`, `I(E)`, `I(E, E)`, and so on, where `I` is an identifier for a + non-[void function](#void-functions). The `E` parameters may also be arrays + and [array references](#array-references). +10. `read()`: Reads a line from `stdin` and uses that as an expression. The + result of that expression is the result of the `read()` operand. This is a + **non-portable extension**. +11. `maxibase()`: The max allowable `ibase`. This is a **non-portable + extension**. +12. `maxobase()`: The max allowable `obase`. This is a **non-portable + extension**. +13. `maxscale()`: The max allowable `scale`. This is a **non-portable + extension**. + + + +### Numbers + +Numbers are strings made up of digits, uppercase letters, and at most `1` period +for a radix. Numbers can have up to `BC_NUM_MAX` digits. Uppercase letters +equal `9` + their position in the alphabet (i.e., `A` equals `10`, or `9 + 1`). +If a digit or letter makes no sense with the current value of `ibase`, they are +set to the value of the highest valid digit in `ibase`. + +Single-character numbers (i.e., `A`) take the value that they would have if they +were valid digits, regardless of the value of `ibase`. This means that `A` +always equals decimal `10` and `Z` always equals decimal `35`. + +In addition, if bc(1) was built with the extra math option, it accepts numbers +in scientific notation. For bc(1), an example is `1.89237e9`, which is equal to +`1892370000`. Negative exponents are also allowed, so `4.2890e-3` is equal to +`0.0042890`. + +Using scientific notation is an error or warning if the `-s` or `-w`, +respectively, command-line options (or equivalents) are given. + +**WARNING**: Both the number and the exponent in scientific notation are +interpreted according to the current `ibase`, but the number is still multiplied +by `10^exponent` regardless of the current `ibase`. For example, if `ibase` is +`16` and bc(1) is given the number string `"FFeA"`, the resulting decimal number +will be `2550000000000`, and if bc(1) is given the number string `"10e-4"`, the +resulting decimal number will be `0.0016`. + +Accepting input as scientific notation is a **non-portable extension**. + +### Operators + +The following arithmetic and logical operators can be used. They are listed in +order of decreasing precedence. Operators in the same group have the same +precedence. + + * `++` `--`: + Type: Prefix and Postfix + + Associativity: None + + Description: `increment`, `decrement` + + * `-` `!`: + Type: Prefix + + Associativity: None + + Description: `negation`, `boolean not` + + * `$`: + Type: Postfix + + Associativity: None + + Description: `truncation` + + * `@`: + Type: Binary + + Associativity: Right + + Description: `set precision` + + * `^`: + Type: Binary + + Associativity: Right + + Description: `power` + + * `*` `/` `%`: + Type: Binary + + Associativity: Left + + Description: `multiply`, `divide`, `modulus` + + * `+` `-`: + Type: Binary + + Associativity: Left + + Description: `add`, `subtract` + + * `<<` `>>`: + Type: Binary + + Associativity: Left + + Description: `shift left`, `shift right` + + * `=` `<<=` `>>=` `+=` `-=` `*=` `/=` `%=` `^=` `@=`: + Type: Binary + + Associativity: Right + + Description: `assignment` + + * `==` `<=` `>=` `!=` `<` `>`: + Type: Binary + + Associativity: Left + + Description: `relational` + + * `&&`: + Type: Binary + + Associativity: Left + + Description: `boolean and` + + * `||`: + Type: Binary + + Associativity: Left + + Description: `boolean or` + +The operators will be described in more detail below. + + + + * `++` `--`: + The prefix and postfix `increment` and `decrement` operators behave exactly + like they would in C. They require a [named expression](#named-expressions) + as an operand. + + * `-`: + The `negation` operator returns `0` if a user attempts to negate any + expression with the value `0`. Otherwise, a copy of the expression with its + sign flipped is returned. + + * `!`: + The `boolean not` operator returns `1` if the expression is `0`, or `0` + otherwise. + + This is a **non-portable extension**. + + * `$`: + The `truncation` operator returns a copy of the given expression with all of + its **scale** removed. + + This is a **non-portable extension**. + + This is only available if bc(1) has been compiled with the extra math option + enabled. + + * `@`: + The `set precision` operator takes two expressions and returns a copy of the + first with its **scale** equal to the value of the second expression. That + could either mean that the number is returned without change (if the first + expression's **scale** matches the value of the second expression), extended + (if it is less), or truncated (if it is more). + + The second expression must be an integer (no **scale**) and non-negative. + + This is a **non-portable extension**. + + This is only available if bc(1) has been compiled with the extra math option + enabled. + + * `^`: + The `power` operator (not the `exclusive or` operator, as it would be in C) + takes two expressions and raises the first to the power of the value of the + second. + + The second expression must be an integer (no **scale**). + + * `*`: + The `multiply` operator takes two expressions, multiplies them, and returns + the product. If `a` is the **scale** of the first expression and `b` is the + **scale** of the second expression, the scale of the result is equal to + `min(a+b,max(scale,a,b))` where `min` and `max` return the obvious values. + + * `/`: + The `divide` operator takes two expressions, divides them, and returns the + quotient. The scale of the result shall be the value of `scale`. + + * `%`: + The `modulus` operator takes two expressions, `a` and `b`, and evaluates + them by 1) Computing `a/b` to current `scale` and 2) Using the result of + step 1 to calculate `a-(a/b)*b` to scale `max(scale+scale(b),scale(a))`. + + * `+`: + The `add` operator takes two expressions, `a` and `b`, and returns the sum, + with a **scale** equal to the max of the **scale**s of `a` and `b`. + + * `-`: + The `subtract` operator takes two expressions, `a` and `b`, and returns the + difference, with a **scale** equal to the max of the **scale**s of `a` and + `b`. + + * `<<`: + The `left shift` operator takes two expressions, `a` and `b`, and returns a + copy of the value of `a` with its decimal point moved `b` places to the + right. + + The second expression must be an integer (no **scale**) and non-negative. + + This is a **non-portable extension**. + + This is only available if bc(1) has been compiled with the extra math option + enabled. + + * `>>`: + The `right shift` operator takes two expressions, `a` and `b`, and returns a + copy of the value of `a` with its decimal point moved `b` places to the + left. + + The second expression must be an integer (no **scale**) and non-negative. + + This is a **non-portable extension**. + + This is only available if bc(1) has been compiled with the extra math option + enabled. + + + + * `=` `<<=` `>>=` `+=` `-=` `*=` `/=` `%=` `^=` `@=`: + The `assignment` operators take two expressions, `a` and `b` where `a` is a + [named expression](#bc-named-expressions). + + For `=`, `b` is copied and the result is assigned to `a`. For all others, + `a` and `b` are applied as operands to the corresponding arithmetic + operator and the result is assigned to `a`. + + The `assignment` operators that correspond to operators that are extensions + are themselves extensions. + + Also, those `assignment` operators that are extensions are only available if + bc(1) has been compiled with the extra math option enabled. + + * `==` `<=` `>=` `!=` `<` `>`: + The `relational` operators compare two expressions, `a` and `b`, and if the + relation holds, according to C language semantics, the result is `1`. + Otherwise, it is `0`. + + Note that unlike in C, these operators have a lower precedence than the + `assignment` operators, which means that `a=b>c` is interpreted as + `(a=b)>c`. + + Also, unlike the [standard][1] requires, these operators can appear anywhere + any other expressions can be used. This allowance is a + **non-portable extension**. + + * `&&`: + The `boolean and` operator takes two expressions and returns `1` if both + expressions are non-zero, `0` otherwise. + + This is ***not*** a short-circuit operator. + + This is a **non-portable extension**. + + * `||`: + The `boolean or` operator takes two expressions and returns `1` if one of + the expressions is non-zero, `0` otherwise. + + This is ***not*** a short-circuit operator. + + This is a **non-portable extension**. + +### Statements + +The following items are statements: + +1. `E` +2. `{` `S` `;` ... `;` `S` `}` +3. `if` `(` `E` `)` `S` +4. `if` `(` `E` `)` `S` `else` `S` +5. `while` `(` `E` `)` `S` +6. `for` `(` `E` `;` `E` `;` `E` `)` `S` +7. An empty statement +8. `break` +9. `continue` +10. `quit` +11. `halt` +12. `limits` +13. A string of characters, enclosed in double quotes +14. `print` `E` `,` ... `,` `E` +15. `I()`, `I(E)`, `I(E, E)`, and so on, where `I` is an identifier for a + [void function](#void-functions). The `E` parameters may also be arrays and + [array references](#array-references). + +Numbers 4, 9, 11, 12, 14, and 15 are **non-portable extensions**. + +Also, as a **non-portable extension**, any or all of the expressions in the +header of a for loop may be omitted. If the condition (second expression) is +omitted, it is assumed to be a constant `1`. + +The `break` statement causes a loop to stop iterating and resume execution +immediately following a loop. This is only allowed in loops. + +The `continue` statement causes a loop iteration to stop early and returns to +the start of the loop, including testing the loop condition. This is only +allowed in loops. + +The `if` `else` statement does the same thing as in C. + +The `quit` statement causes bc(1) to quit, even if it is on a branch that will +not be executed (it is a compile-time command). + +The `halt` statement causes bc(1) to quit, if it is executed. (Unlike `quit` if +it is on a branch of an `if` statement that is not executed, bc(1) does not +quit.) + +The `limits` statement prints the limits that this bc(1) is subject to. This is +like the `quit` statement in that it is a compile-time command. + +An expression by itself is evaluated and printed, followed by a newline. If +bc(1) has been built with the extra math option enabled, both scientific +notation and engineering notation are available for printing the results of +expressions. Scientific notation is activated by assigning `0` to `obase` (in +any other context, an `obase` of `0` is invalid), and engineering notation is +activated by assigning `1` to `obase` (which is also invalid in any other +context). To deactivate them, just assign a different value to `obase`. + +Scientific notation and engineering notation are disabled if bc(1) is run with +either the `-s` or `-w` command-line options (or equivalents). + +Printing numbers in scientific notation and/or engineering notation is a +**non-portable extension**. + +### Print Statement + +The "expressions" in a `print` statement may also be strings. If they are, there +are backslash escape sequences that are interpreted specially. What those +sequences are, and what they cause to be printed, are shown below: + + * `\a`: + `\a` + + * `\b`: + `\b` + + * `\\`: + `\` + + * `\e`: + `\` + + * `\f`: + `\f` + + * `\n`: + `\n` + + * `\q`: + `"` + + * `\r`: + `\r` + + * `\t`: + `\t` + +Any other character following a backslash causes the backslash and character to +be printed as-is. + +Any non-string expression in a print statement shall be assigned to `last`, like +any other expression that is printed. + +### Order of Evaluation + +All expressions in a statment are evaluated left to right, except as necessary +to maintain order of operations. This means, for example, that in the expression +`i = 0; a[i++] = i++`, the first (or 0th) element of `a` is set to `1`, and `i` +is equal to `2` at the end of the expression. + +This includes function arguments. Thus, this means that in the expression +`i = 0; x(i++, i++)`, the first argument passed to `x()` is `0`, and the second +argument is `1`, while `i` is equal to `2` before the function starts executing. + +FUNCTIONS +--------- + +Function definitions are as follows: + +``` +define I(I,...,I){ + auto I,...,I + S;...;S + return(E) +} +``` + +Any `I` in the parameter list or `auto` list may be replaced with `I[]` to make +a parameter or `auto` var an array. + +As a **non-portable extension**, the opening brace of a `define` statement may +appear on the next line. + +The return statement may also be in the following forms: + +1. `return` +2. `return` `(` `)` +3. `return` `E` + +The first two, or not specifying a `return` statement, is equivalent to +`return (0)`, unless the function is a [void function](#void-functions). + + + +### Void Functions + +Functions can also be void functions, defined as follows: + +``` +define void I(I,...,I){ + auto I,...,I + S;...;S + return(E) +} +``` + +They can only be used as standalone expressions, where such an expression would +be printed alone, except in a print statement. + +Void functions can only use the first two `return` statements listed above. + +The word `void` is not treated as a keyword; it is still possible to have +variables, arrays, and functions named `void`. + +This is a **non-portable extension**. + + + +### Array References + +For any array in the parameter list, if the array is declared in the form + +``` +*I[] +``` + +it is a **reference**. Any changes to the array in the function are reflected, +when the function returns, to the array that was passed in. + +Other than this, all function arguments are passed by value. + +This is a **non-portable extension**. + +LIBRARY +------- + +All of the functions below, including the functions in the +[extended library](#extended-library) if bc(1) has been compiled with the extra +math option enabled, are available when the `-l` or `--mathlib` command-line +flags are given. + + + +### Standard Library + +The [standard][1] defines the following functions for the math library: + + * `s(x)`: + Returns the sine of `x`, which is assumed to be in radians. + + This is a [transcendental function][5]. + + * `c(x)`: + Returns the cosine of `x`, which is assumed to be in radians. + + This is a [transcendental function][5]. + + * `a(x)`: + Returns the arctangent of `x`, in radians. + + This is a [transcendental function][5]. + + * `l(x)`: + Returns the natural logarithm of `x`. + + This is a [transcendental function][5]. + + * `e(x)`: + Returns the mathematical constant `e` raised to the power of `x`. + + This is a [transcendental function][5]. + + * `j(x, n)`: + Returns the bessel integer order `n` (truncated) of `x`. + + This is a [transcendental function][5]. + + + +### Extended Library + +In addition to the [standard library](#standard-library), if bc(1) has been +built with the extra math option, the following functions are available when +either the `-l` or `--mathlib` options are given. + +However, the extended library is ***not*** loaded when the `-s`/`--standard` or +`-w`/`--warn` options are given since they are not part of the library defined +by the [standard][1]. + +The extended library is a **non-portable extension**. + + * `r(x, p)`: + Returns `x` rounded to `p` decimal places according to the rounding mode + [round half away from `0`][3]. + + * `ceil(x, p)`: + Returns `x` rounded to `p` decimal places according to the rounding mode + [round away from `0`][7]. + + * `f(x)`: + Returns the factorial of the truncated absolute value of `x`. + + * `perm(n, k)`: + Returns the permutation of the truncated absolute value of `n` of the + truncated absolute value of `k`, if `k <= n`. If not, it returns `0`. + + * `comb(n, k)`: + Returns the combination of the truncated absolute value of `n` of the + truncated absolute value of `k`, if `k <= n`. If not, it returns `0`. + + * `l2(x)`: + Returns the logarithm base `2` of `x`. + + This is a [transcendental function][5]. + + * `l10(x)`: + Returns the logarithm base `10` of `x`. + + This is a [transcendental function][5]. + + * `log(x, b)`: + Returns the logarithm base `b` of `x`. + + This is a [transcendental function][5]. + + * `pi(p)`: + Returns `pi` to `p` decimal places. + + This is a [transcendental function][5]. + + * `t(x)`: + Returns the tangent of `x`, which is assumed to be in radians. + + This is a [transcendental function][5]. + + * `a2(y, x)`: + Returns the arctangent of `y/x`, in radians. If both `y` and `x` are equal + to `0`, it raises an error and causes bc(1) to reset (see the RESET + section). Otherwise, if `x` is greater than `0`, it returns `a(y/x)`. If `x` + is less than `0`, and `y` is greater than or equal to `0`, it returns + `a(y/x) + pi`. If `x` is less than `0`, and `y` is less than `0`, it returns + `a(y/x) - pi`. If `x` is equal to `0`, and `y` is greater than `0`, it + returns `pi/2`. If `x` is equal to `0`, and `y` is less than `0`, it returns + `-pi/2`. + + This function is the same as the `atan2()` function in many programming + languages. + + This is a [transcendental function][5]. + + * `sin(x)`: + Returns the sine of `x`, which is assumed to be in radians. + + This is an alias of `s(x)`. + + This is a [transcendental function][5]. + + * `cos(x)`: + Returns the cosine of `x`, which is assumed to be in radians. + + This is an alias of `c(x)`. + + This is a [transcendental function][5]. + + * `tan(x)`: + Returns the tangent of `x`, which is assumed to be in radians. + + This is an alias of `t(x)`. + + This is a [transcendental function][5]. + + * `atan(x)`: + Returns the arctangent of `x`, in radians. + + This is an alias of `a(x)`. + + This is a [transcendental function][5]. + + * `atan2(y, x)`: + Returns the arctangent of `y/x`, in radians. If both `y` and `x` are equal + to `0`, it raises an error and causes bc(1) to reset (see the RESET + section). Otherwise, if `x` is greater than `0`, it returns `a(y/x)`. If `x` + is less than `0`, and `y` is greater than or equal to `0`, it returns + `a(y/x) + pi`. If `x` is less than `0`, and `y` is less than `0`, it returns + `a(y/x) - pi`. If `x` is equal to `0`, and `y` is greater than `0`, it + returns `pi/2`. If `x` is equal to `0`, and `y` is less than `0`, it returns + `-pi/2`. + + This function is the same as the `atan2()` function in many programming + languages. + + This is an alias of `a2(y, x)`. + + This is a [transcendental function][5]. + + * `r2d(x)`: + Converts `x` from radians to degrees and returns the result. + + This is a [transcendental function][5]. + + * `d2r(x)`: + Converts `x` from degrees to radians and returns the result. + + This is a [transcendental function][5]. + + * `ubytes(x)`: + Returns the numbers of unsigned integer bytes required to hold the truncated + absolute value of `x`. + + * `sbytes(x)`: + Returns the numbers of signed, two's-complement integer bytes required to + hold the truncated value of `x`. + + * `hex(x)`: + Outputs the hexadecimal (base `16`) representation of `x`. + + This is a [void function](#void-functions). + + * `binary(x)`: + Outputs the binary (base `2`) representation of `x`. + + This is a [void function](#void-functions). + + * `output(x, b)`: + Outputs the base `b` representation of `x`. + + This is a [void function](#void-functions). + + * `uint(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as an unsigned + integer in as few power of two bytes as possible. Both outputs are split + into bytes separated by spaces. + + If `x` is not an integer or is negative, an error message is printed + instead, but bc(1) is not reset (see the RESET section). + + This is a [void function](#void-functions). + + * `int(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as a signed, + two's-complement integer in as few power of two bytes as possible. Both + outputs are split into bytes separated by spaces. + + If `x` is not an integer, an error message is printed instead, but bc(1) is + not reset (see the RESET section). + + This is a [void function](#void-functions). + + * `uintn(x, n)`: + Outputs the representation, in binary and hexadecimal, of `x` as an unsigned + integer in `n` bytes. Both outputs are split into bytes separated by spaces. + + If `x` is not an integer, is negative, or cannot fit into `n` bytes, an + error message is printed instead, but bc(1) is not reset (see the RESET + section). + + This is a [void function](#void-functions). + + * `intn(x, n)`: + Outputs the representation, in binary and hexadecimal, of `x` as a signed, + two's-complement integer in `n` bytes. Both outputs are split into bytes + separated by spaces. + + If `x` is not an integer or cannot fit into `n` bytes, an error message is + printed instead, but bc(1) is not reset (see the RESET section). + + This is a [void function](#void-functions). + + * `uint8(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as an unsigned + integer in `1` byte. Both outputs are split into bytes separated by spaces. + + If `x` is not an integer, is negative, or cannot fit into `1` byte, an error + message is printed instead, but bc(1) is not reset (see the RESET section). + + This is a [void function](#void-functions). + + * `int8(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as a signed, + two's-complement integer in `1` byte. Both outputs are split into bytes + separated by spaces. + + If `x` is not an integer or cannot fit into `1` byte, an error message is + printed instead, but bc(1) is not reset (see the RESET section). + + This is a [void function](#void-functions). + + * `uint16(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as an unsigned + integer in `2` bytes. Both outputs are split into bytes separated by spaces. + + If `x` is not an integer, is negative, or cannot fit into `2` bytes, an + error message is printed instead, but bc(1) is not reset (see the RESET + section). + + This is a [void function](#void-functions). + + * `int16(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as a signed, + two's-complement integer in `2` bytes. Both outputs are split into bytes + separated by spaces. + + If `x` is not an integer or cannot fit into `2` bytes, an error message is + printed instead, but bc(1) is not reset (see the RESET section). + + This is a [void function](#void-functions). + + * `uint32(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as an unsigned + integer in `4` bytes. Both outputs are split into bytes separated by spaces. + + If `x` is not an integer, is negative, or cannot fit into `4` bytes, an + error message is printed instead, but bc(1) is not reset (see the RESET + section). + + This is a [void function](#void-functions). + + * `int32(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as a signed, + two's-complement integer in `4` bytes. Both outputs are split into bytes + separated by spaces. + + If `x` is not an integer or cannot fit into `4` bytes, an error message is + printed instead, but bc(1) is not reset (see the RESET section). + + This is a [void function](#void-functions). + + * `uint64(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as an unsigned + integer in `8` bytes. Both outputs are split into bytes separated by spaces. + + If `x` is not an integer, is negative, or cannot fit into `8` bytes, an + error message is printed instead, but bc(1) is not reset (see the RESET + section). + + This is a [void function](#void-functions). + + * `int64(x)`: + Outputs the representation, in binary and hexadecimal, of `x` as a signed, + two's-complement integer in `8` bytes. Both outputs are split into bytes + separated by spaces. + + If `x` is not an integer or cannot fit into `8` bytes, an error message is + printed instead, but bc(1) is not reset (see the RESET section). + + This is a [void function](#void-functions). + + * `hex_uint(x, n)`: + Outputs the representation of the truncated absolute value of `x` as an + unsigned integer in hexadecimal using `n` bytes. Not all of the value will + be output if `n` is too small. + + This is a [void function](#void-functions). + + * `binary_uint(x, n)`: + Outputs the representation of the truncated absolute value of `x` as an + unsigned integer in binary using `n` bytes. Not all of the value will be + output if `n` is too small. + + This is a [void function](#void-functions). + + * `output_uint(x, n)`: + Outputs the representation of the truncated absolute value of `x` as an + unsigned integer in the current [`obase`](#obase) using `n` bytes. Not all + of the value will be output if `n` is too small. + + This is a [void function](#void-functions). + + * `output_byte(x, i)`: + Outputs byte `i` of the truncated absolute value of `x`, where `0` is the + least significant byte and `number_of_bytes - 1` is the most significant + byte. + + This is a [void function](#void-functions). + + + +### Transcendental Functions + +All transcendental functions can return slightly inaccurate results (up to 1 +[ULP][4]). This is unavoidable, and [this article][6] explains why it is +impossible and unnecessary to calculate exact results for the transcendental +functions. + +Because of the possible inaccuracy, I recommend that users call those functions +with the precision (`scale`) set to at least 1 higher than is necessary. If +exact results are *absolutely* required, users can double the precision +(`scale`) and then truncate. + +The transcendental functions in the standard math library are: + +* `s(x)` +* `c(x)` +* `a(x)` +* `l(x)` +* `e(x)` +* `j(x, n)` + +The transcendental functions in the extended math library are: + +* `l2(x)` +* `l10(x)` +* `log(x, b)` +* `pi(p)` +* `t(x)` +* `a2(y, x)` +* `sin(x)` +* `cos(x)` +* `tan(x)` +* `atan(x)` +* `atan2(y, x)` +* `r2d(x)` +* `d2r(x)` + +RESET +----- + +When bc(1) encounters an error or a signal that it has a non-default handler +for, it resets. This means that several things happen. + +First, any functions that are executing are stopped and popped off the stack. +The behavior is not unlike that of exceptions in programming languages. Then +the execution point is set so that any code waiting to execute (after all +functions returned) is skipped. + +Thus, when bc(1) resets, it skips any remaining code waiting to be executed. +Then, if it is interactive mode, and the error was not a fatal error (see the +EXIT STATUS section), it asks for more input; otherwise, it exits with the +appropriate return code. + +Note that this reset behavior is different from the GNU bc(1), which attempts to +start executing the statement right after the one that caused an error. + +PERFORMANCE +----------- + +Most bc(1) implementations use `char` types to calculate the value of `1` +decimal digit at a time, but that can be slow. This bc(1) does something +different. + +It uses large integers to calculate more than `1` decimal digit at a time. If +built in a environment where `BC_LONG_BIT` (see the LIMITS section) is `64`, +then each integer has `9` decimal digits. If built in an environment where +`BC_LONG_BIT` is `32` then each integer has `4` decimal digits. This value (the +number of decimal digits per large integer) is called `BC_BASE_DIGS`. + +In addition, this bc(1) uses an even larger integer for overflow checking. This +integer type depends on the value of `BC_LONG_BIT`, but is always at least twice +as large as the integer type used to store digits. + +LIMITS +------ + +The following are the limits on bc(1): + + * `BC_LONG_BIT`: + The number of bits in the `long` type in the environment where bc(1) was + built. This determines how many decimal digits can be stored in a single + large integer (see the PERFORMANCE section). + + * `BC_BASE_DIGS`: + The number of decimal digits per large integer (see the PERFORMANCE + section). Depends on `BC_LONG_BIT`. + + * `BC_BASE_POW`: + The max decimal number that each large integer can store (see + `BC_BASE_DIGS`) plus `1`. Depends on `BC_BASE_DIGS`. + + * `BC_OVERFLOW_MAX`: + The max number that the overflow type (see the PERFORMANCE section) can + hold. Depends on `BC_LONG_BIT`. + + * `BC_BASE_MAX`: + The maximum output base. Set at `BC_BASE_POW`. + + * `BC_DIM_MAX`: + The maximum size of arrays. Set at `SIZE_MAX-1`. + + * `BC_SCALE_MAX`: + The maximum `scale`. Set at `BC_OVERFLOW_MAX-1`. + + * `BC_STRING_MAX`: + The maximum length of strings. Set at `BC_OVERFLOW_MAX-1`. + + * `BC_NAME_MAX`: + The maximum length of identifiers. Set at `BC_OVERFLOW_MAX-1`. + + * `BC_NUM_MAX`: + The maximum length of a number (in decimal digits), which includes digits + after the decimal point. Set at `BC_OVERFLOW_MAX-1`. + + * Exponent: + The maximum allowable exponent (positive or negative). Set at + `BC_OVERFLOW_MAX`. + + * Number of vars: + The maximum number of vars/arrays. Set at `SIZE_MAX-1`. + +Actual values can be queried with the `limits` statement. + +These limits are meant to be effectively non-existent; the limits are so large +(at least on 64-bit machines) that there should not be any point at which they +become a problem. In fact, memory should be exhausted before these limits should +be hit. + +ENVIRONMENT VARIABLES +--------------------- + +bc(1) recognizes the following environment variables: + + * `POSIXLY_CORRECT`: + If this variable exists (no matter the contents), bc(1) behaves as if + the `-s` option was given. + + * `BC_ENV_ARGS`: + This is another way to give command-line arguments to bc(1). They should be + in the same format as all other command-line arguments. These are always + processed first, so any files given in `BC_ENV_ARGS` will be processed + before files given on the command-line. This gives the user the ability to + set up "standard" options and files to be used at every invocation. The most + useful thing for such files to contain would be useful functions that the + user might want every time bc(1) runs. + + * `BC_LINE_LENGTH`: + If this environment variable exists and contains an integer that is greater + than `1` and is less than `UINT16_MAX` (`2^16-1`), bc(1) will output lines + to that length, including the backslash (`\`). The default line length is + `70`. + + * `BC_EXPR_EXIT`: + If this variable exists (no matter the contents), bc(1) will exit + immediately after executing expressions and files given by the `-e` and/or + `-f` command-line options (and any equivalents). + +EXIT STATUS +----------- + +bc(1) returns the following exit statuses: + + * `0`: + No error. + + * `1`: + A math error occurred. This follows standard practice of using `1` for + expected errors, since math errors will happen in the process of normal + execution. + + Math errors include divide by `0`, taking the square root of a negative + number, attempting to convert a negative number to a hardware integer, + overflow when converting a number to a hardware integer, and attempting to + use a non-integer where an integer is required. + + Converting to a hardware integer happens for the second operand of the power + (`^`), places (`@`), left shift (`<<`), and right shift (`>>`) operators and + their corresponding assignment operators. + + * `2`: + A parse error occurred. + + Parse errors include unexpected `EOF`, using an invalid character, failing + to find the end of a string or comment, using a token where it's invalid, + giving an invalid expression, giving an invalid print statement, giving an + invalid function definition, attempting to assign to an expression that is + not a [named expression](#bc-named-expressions), giving an invalid `auto` + list, having a duplicate `auto`/function parameter, failing to find the end + of a code block, attempting to return a value from a `void` function, + attempting to use a variable as a reference, and using any extensions when + the option `-s` or any equivalents were given. + + * `3`: + A runtime error occurred. + + Runtime errors include assigning an invalid number to `ibase`, `obase`, or + `scale`; give a bad expression to a `read()` call, calling `read()` inside + of a `read()` call, type errors, passing the wrong number of parameters to + functions, attempting to call an undefined function, and attempting to use a + `void` function call as a value in an expression. + + * `4`: + A fatal error occurred. + + Fatal errors include memory allocation errors, I/O errors, failing to open + files, attempting to use files that do not have only ASCII characters (bc(1) + only accepts ASCII characters), attempting to open a directory as a file, + and giving invalid command-line options. + +The exit status `4` is special; when a fatal error occurs, bc(1) always exits +and returns `4`, no matter what mode bc(1) is in. + +The other statuses will only be returned when bc(1) is not in interactive mode, +since bc(1) resets its state (see the RESET section) and accepts more input when +one of those errors occurs in interactive mode. This is also the case when +interactive mode is forced by the `-i` option. + +These exit statuses allow bc(1) to be used in shell scripting with error +checking, and its normal behavior can be forced by using `-i`. + +SIGNAL HANDLING +--------------- + +If bc(1) has been compiled with the signal handling, sending a `SIGINT` will +cause bc(1) to stop execution of the current input and reset (see the RESET +section), asking for more input. + +Otherwise, `SIGTERM` and `SIGQUIT` cause bc(1) to clean up and exit, and it uses +the default handler for all other signals. + +If bc(1) has not been compiled with signal handling, it uses the default signal +handlers for all signals. + +COMMAND LINE HISTORY +-------------------- + +bc(1) supports interactive command-line editing, if compiled with the history +option enabled. If `stdin` is hooked to a terminal, it is enabled. Previous +lines can be recalled and edited with the arrow keys. + +LOCALES +------- + +This bc(1) ships with support for adding error messages for different locales. + +SEE ALSO +-------- + +dc(1) + +STANDARDS +--------- + +bc(1) is compliant with the [IEEE Std 1003.1-2017 (“POSIX.1-2017”)][1] +specification. The flags `-efghiqsvVw`, all long options, and the extensions +noted above are extensions to that specification. + +Note that the specification explicitly says that bc(1) only accepts numbers that +use a period (`.`) as a radix point, regardless of the value of `LC_NUMERIC`. + +Because this bc(1) ships with support for adding error messages for different +locales, so it supports `LC_MESSAGES`. + +AUTHOR +------ + +This bc(1) was made from scratch by Gavin D. Howard. + +BUGS +---- + +None are known. Report bugs at https://github.com/gavinhoward/bc. + +[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html +[2]: https://www.gnu.org/software/bc/ +[3]: https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero +[4]: https://en.wikipedia.org/wiki/Unit_in_the_last_place +[5]: #transcendental-functions +[6]: https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT +[7]: https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero Index: contrib/bc/manuals/bc.md =================================================================== --- contrib/bc/manuals/bc.md +++ contrib/bc/manuals/bc.md @@ -0,0 +1 @@ +link bc.1.ronn \ No newline at end of file Index: contrib/bc/manuals/benchmarks.md =================================================================== --- contrib/bc/manuals/benchmarks.md +++ contrib/bc/manuals/benchmarks.md @@ -0,0 +1,312 @@ +# Benchmarks + +These are the results of benchmarks comparing this `bc` (at version `2.0.3`) and +GNU `bc` (at version `1.07.1`). + +Note: all benchmarks were run four times, and the fastest run is the one shown. +Also, `[bc]` means whichever `bc` was being run, and the assumed working +directory is the root directory of this repository. Also, this `bc` was built at +`-O2`. + +### Addition + +The command used was: + +``` +tests/script.sh bc add.bc 0 1 1 [bc] +``` + +For GNU `bc`: + +``` +Running bc script: add.bc + +real 2.24 +user 1.13 +sys 1.11 +``` + +For this `bc`: + +``` +Running bc script: add.bc + +real 1.32 +user 1.30 +sys 0.02 +``` + +### Subtraction + +The command used was: + +``` +tests/script.sh bc subtract.bc 0 1 1 [bc] +``` + +For GNU `bc`: + +``` +Running bc script: subtract.bc + +real 2.28 +user 1.33 +sys 0.94 +``` + +For this `bc`: + +``` +Running bc script: subtract.bc + +real 1.37 +user 1.34 +sys 0.02 +``` + +### Multiplication + +The command used was: + +``` +tests/script.sh bc multiply.bc 0 1 1 [bc] +``` + +For GNU `bc`: + +``` +Running bc script: multiply.bc + +real 6.02 +user 4.06 +sys 1.92 +``` + +For this `bc`: + +``` +Running bc script: multiply.bc + +real 2.59 +user 2.53 +sys 0.05 +``` + +### Division + +The command used was: + +``` +tests/script.sh bc divide.bc 0 1 1 [bc] +``` + +For GNU `bc`: + +``` +Running bc script: divide.bc + +real 2.94 +user 1.85 +sys 1.09 +``` + +For this `bc`: + +``` +Running bc script: divide.bc + +real 1.91 +user 1.90 +sys 0.00 +``` + +### Power + +The command used was: + +``` +printf '1234567890^100000; halt\n' | time -p [bc] -lq > /dev/null +``` + +For GNU `bc`: + +``` +real 11.81 +user 11.80 +sys 0.00 +``` + +For this `bc`: + +``` +real 0.76 +user 0.75 +sys 0.00 +``` + +### Scripts + +[This file][1] was downloaded, saved at `../timeconst.bc` and the following +patch was applied: + +``` +--- tests/bc/scripts/timeconst.bc 2018-09-28 11:32:22.808669000 -0600 ++++ ../timeconst.bc 2019-06-07 07:26:36.359913078 -0600 +@@ -108,8 +108,10 @@ + + print "#endif /* KERNEL_TIMECONST_H */\n" + } +- halt + } + +-hz = read(); +-timeconst(hz) ++for (i = 0; i <= 10000; ++i) { ++ timeconst(i) ++} ++ ++halt +``` + +The command used was: + +``` +time -p [bc] ../timeconst.bc > /dev/null +``` + +For GNU `bc`: + +``` +real 3.03 +user 2.90 +sys 0.13 +``` + +For this `bc`: + +``` +real 2.64 +user 2.64 +sys 0.00 +``` + +Because this `bc` is faster when doing math, it might be a better comparison to +run a script that is not running any math. As such, I put the following into +`../test.bc`: + +``` +for (i = 0; i < 100000000; ++i) { + y = i +} + +i +y + +halt +``` + +The command used was: + +``` +time -p [bc] ../test.bc > /dev/null +``` + +For GNU `bc`: + +``` +real 13.46 +user 13.46 +sys 0.00 +``` + +For this `bc`: + +``` +real 24.72 +user 24.72 +sys 0.00 +``` + +However, when I put the following into `../test2.bc`: + +``` +i = 0 + +while (i < 100000000) { + ++i +} + +i + +halt +``` + +the results were surprising. + +The command used was: + +``` +time -p [bc] ../test2.bc > /dev/null +``` + +For GNU `bc`: + +``` +real 53.85 +user 39.50 +sys 14.34 +``` + +For this `bc`: + +``` +real 26.61 +user 26.58 +sys 0.03 +``` + +It seems that both `bc`'s run `for` loops faster than `while` loops. I don't +know why my `bc` does that because both loops are using the same code underneath +the hood. + +Note that, when running the benchmarks, the optimization used is not the one I +recommend, which is `-O3 -flto -march=native`. This `bc` separates its code into +modules that, when optimized at link time, removes a lot of the inefficiency +that comes from function overhead. This is most keenly felt with one function: +`bc_vec_item()`, which should just turn into one instruction (on `x86_64`) when +optimized at link time and inlined. There are other functions that matter as +well. + +When compiling this `bc` with the recommended optimizations, the results are as +follows. + +For the first script: + +``` +real 1.88 +user 1.88 +sys 0.00 +``` + +For the second script: + +``` +real 19.43 +user 19.43 +sys 0.00 +``` + +For the third script: + +``` +real 20.58 +user 20.53 +sys 0.03 +``` + +This is more competitive. + +In addition, when compiling with the above recommendation, this `bc` gets even +faster when doing math. + +[1]: https://github.com/torvalds/linux/blob/master/kernel/time/timeconst.bc Index: contrib/bc/manuals/build.md =================================================================== --- contrib/bc/manuals/build.md +++ contrib/bc/manuals/build.md @@ -0,0 +1,602 @@ +# Build + +This `bc` attempts to be as portable as possible. It can be built on any +POSIX-compliant system. + +To accomplish that, a POSIX-compatible, custom `configure.sh` script is used to +select build options, compiler, and compiler flags and generate a `Makefile`. + +The general form of configuring, building, and installing this `bc` is as +follows: + +``` +[ENVIRONMENT_VARIABLE=...] ./configure.sh [build_options...] +make +make install +``` + +To get all of the options, including any useful environment variables, use +either one of the following commands: + +``` +./configure.sh -h +./configure.sh --help +``` + +***WARNING***: even though `configure.sh` supports both option types, short and +long, it does not support handling both at the same time. Use only one type. + +To learn the available `make` targets run the following command after running +the `configure.sh` script: + +``` +make help +``` + +See [Build Environment Variables][4] for a more detailed description of all +accepted environment variables and [Build Options][5] for more detail about all +accepted build options. + + + +## Cross Compiling + +To cross-compile this `bc`, an appropriate compiler must be present and assigned +to the environment variable `HOSTCC` or `HOST_CC` (the two are equivalent, +though `HOSTCC` is prioritized). This is in order to bootstrap core file(s), if +the architectures are not compatible (i.e., unlike i686 on x86_64). Thus, the +approach is: + +``` +HOSTCC="/path/to/native/compiler" ./configure.sh +make +make install +``` + +`HOST_CC` will work in exactly the same way. + +`HOSTCFLAGS` and `HOST_CFLAGS` can be used to set compiler flags for `HOSTCC`. +(The two are equivalent, as `HOSTCC` and `HOST_CC` are.) `HOSTCFLAGS` is +prioritized over `HOST_CFLAGS`. If neither are present, `HOSTCC` (or `HOST_CC`) +uses `CFLAGS` (see [Build Environment Variables][4] for more details). + +It is expected that `CC` produces code for the target system and `HOSTCC` +produces code for the host system. See [Build Environment Variables][4] for more +details. + +If an emulator is necessary to run the bootstrap binaries, it can be set with +the environment variable `GEN_EMU`. + + + +## Build Environment Variables + +This `bc` supports `CC`, `HOSTCC`, `HOST_CC`, `CFLAGS`, `HOSTCFLAGS`, +`HOST_CFLAGS`, `CPPFLAGS`, `LDFLAGS`, `LDLIBS`, `PREFIX`, `DESTDIR`, `BINDIR`, +`DATAROOTDIR`, `DATADIR`, `MANDIR`, `MAN1DIR`, `LOCALEDIR` `EXECSUFFIX`, +`EXECPREFIX`, `LONG_BIT`, `GEN_HOST`, and `GEN_EMU` environment variables in +`configure.sh`. Any values of those variables given to `configure.sh` will be +put into the generated Makefile. + +More detail on what those environment variables do can be found in the following +sections. + +### `CC` + +C compiler for the target system. `CC` must be compatible with POSIX `c99` +behavior and options. + +Defaults to `c99`. + +### `HOSTCC` or `HOST_CC` + +C compiler for the host system, used only in [cross compiling][6]. + +Defaults to `$CC`. + +### `CFLAGS` + +Command-line flags that will be passed verbatim to `CC`. + +Defaults to empty. + +### `HOSTCFLAGS` or `HOST_CFLAGS` + +Command-line flags that will be passed verbatim to `HOSTCC` or `HOST_CC`. + +Defaults to `$CFLAGS`. + +### `CPPFLAGS` + +Command-line flags for the C preprocessor. These are also passed verbatim to +both compilers (`CC` and `HOSTCC`); they are supported just for legacy reasons. + +Defaults to empty. + +### `LDFLAGS` + +Command-line flags for the linker. These are also passed verbatim to both +compilers (`CC` and `HOSTCC`); they are supported just for legacy reasons. + +Defaults to empty. + +### `LDLIBS` + +Libraries to link to. These are also passed verbatim to both compilers (`CC` and +`HOSTCC`); they are supported just for legacy reasons and for cross compiling +with different C standard libraries (like [musl][3]). + +Defaults to empty. + +### `PREFIX` + +The prefix to install to. + +Can be overridden by passing the `--prefix` option to `configure.sh`. + +Defaults to `/usr/local`. + +### `DESTDIR` + +Path to prepend onto `PREFIX`. This is mostly for distro and package +maintainers. + +This can be passed either to `configure.sh` or `make install`. If it is passed +to both, the one given to `configure.sh` takes precedence. + +Defaults to empty. + +### `BINDIR` + +The directory to install binaries in. + +Can be overridden by passing the `--bindir` option to `configure.sh`. + +Defaults to `$PREFIX/bin`. + +### `DATAROOTDIR` + +The root directory to install data files in. + +Can be overridden by passing the `--datarootdir` option to `configure.sh`. + +Defaults to `$PREFIX/share`. + +### `DATADIR` + +The directory to install data files in. + +Can be overridden by passing the `--datadir` option to `configure.sh`. + +Defaults to `$DATAROOTDIR`. + +### `MANDIR` + +The directory to install manpages in. + +Can be overridden by passing the `--mandir` option to `configure.sh`. + +Defaults to `$DATADIR/man` + +### `MAN1DIR` + +The directory to install Section 1 manpages in. Because both `bc` and `dc` are +Section 1 commands, this is the only relevant section directory. + +Can be overridden by passing the `--man1dir` option to `configure.sh`. + +Defaults to `$MANDIR/man1`. + +### `LOCALEDIR` + +The directory to install locales in. + +Can be overridden by passing the `--localedir` option to `configure.sh`. + +Defaults to `$DATAROOTDIR/locale`. + +### `EXECSUFFIX` + +The suffix to append onto the executable names *when installing*. This is for +packagers and distro maintainers who want this `bc` as an option, but do not +want to replace the default `bc`. + +Defaults to empty. + +### `EXECPREFIX` + +The prefix to append onto the executable names *when building and installing*. +This is for packagers and distro maintainers who want this `bc` as an option, +but do not want to replace the default `bc`. + +Defaults to empty. + +### `LONG_BIT` + +The number of bits in a C `long` type. This is mostly for the embedded space. + +This `bc` uses `long`s internally for overflow checking. In C99, a `long` is +required to be 32 bits. For this reason, on 8-bit and 16-bit microcontrollers, +the generated code to do math with `long` types may be inefficient. + +For most normal desktop systems, setting this is unnecessary, except that 32-bit +platforms with 64-bit longs may want to set it to `32`. + +Defaults to the default value of `LONG_BIT` for the target platform. For +compliance with the `bc` spec, the minimum allowed value is `32`. + +It is an error if the specified value is greater than the default value of +`LONG_BIT` for the target platform. + +### `GEN_HOST` + +Whether to use `gen/strgen.c`, instead of `gen/strgen.sh`, to produce the C +files that contain the help texts as well as the math libraries. By default, +`gen/strgen.c` is used, compiled by `$HOSTCC` and run on the host machine. Using +`gen/strgen.sh` removes the need to compile and run an executable on the host +machine since `gen/strgen.sh` is a POSIX shell script. However, `gen/lib2.bc` is +perilously close to 4095 characters, the max supported length of a string +literal in C99 (and it could be added to in the future), and `gen/strgen.sh` +generates a string literal instead of an array, as `gen/strgen.c` does. For most +production-ready compilers, this limit probably is not enforced, but it could +be. Both options are still available for this reason. + +If you are sure your compiler does not have the limit and do not want to compile +and run a binary on the host machine, set this variable to "0". Any other value, +or a non-existent value, will cause the build system to compile and run +`gen/strgen.c`. + +Default is "". + +### `GEN_EMU` + +The emulator to run bootstrap binaries under. This is only if the binaries +produced by `HOSTCC` (or `HOST_CC`) need to be run under an emulator to work. + +Defaults to empty. + + + +## Build Options + +This `bc` comes with several build options, all of which are enabled by default. + +All options can be used with each other, with a few exceptions that will be +noted below. + +**NOTE**: All long options with mandatory argumenst accept either one of the +following forms: + +``` +--option arg +--option=arg +``` + +### `bc` Only + +To build `bc` only (no `dc`), use any one of the following commands for the +configure step: + +``` +./configure.sh -b +./configure.sh --bc-only +./configure.sh -D +./configure.sh --disable-dc +``` + +Those commands are all equivalent. + +***Warning***: It is an error to use those options if `bc` has also been +disabled (see below). + +### `dc` Only + +To build `dc` only (no `bc`), use either one of the following commands for the +configure step: + +``` +./configure.sh -d +./configure.sh --dc-only +./configure.sh -B +./configure.sh --disable-bc +``` + +Those commands are all equivalent. + +***Warning***: It is an error to use those options if `dc` has also been +disabled (see above). + + + +### Signal Handling + +To disable signal handling, pass either the `-S` flag or the +`--disable-signal-handling` option to `configure.sh`, as follows: + +``` +./configure.sh -S +./configure.sh --disable-signal-handling +``` + +Both commands are equivalent. + + + +### History + +To disable signal handling, pass either the `-H` flag or the `--disable-history` +option to `configure.sh`, as follows: + +``` +./configure.sh -H +./configure.sh --disable-history +``` + +Both commands are equivalent. + +History is automatically disabled when building for Windows or on another +platform that does not support the terminal handling that is required. + +***WARNING***: Of all of the code in the `bc`, this is the only code that is not +completely portable. If the `bc` does not work on your platform, your first step +should be to retry with history disabled. + +### NLS (Locale Support) + +To disable locale support (use only English), pass either the `-N` flag or the +`--disable-nls` option to `configure.sh`, as follows: + +``` +./configure.sh -N +./configure.sh --disable-nls +``` + +Both commands are equivalent. + +NLS (locale support) is automatically disabled when building for Windows or on +another platform that does not support the POSIX locale API or utilities. + +### Extra Math + +This `bc` has 7 extra operators: + +* `$` (truncation to integer) +* `@` (set precision) +* `@=` (set precision and assign) +* `<<` (shift number left, shifts radix right) +* `<<=` (shift number left and assign) +* `>>` (shift number right, shifts radix left) +* `>>=` (shift number right and assign) + +There is no assignment version of `$` because it is a unary operator. + +The assignment versions of the above operators are not available in `dc`, but +the others are, as the operators `$`, `@`, `H`, and `h`, respectively. + +Extra operators can be disabled by passing either the `-E` flag or the +`--disable-extra-math` option to `configure.sh`, as follows: + +``` +./configure.sh -E +./configure.sh --disable-extra-math +``` + +Both commands are equivalent. + +This `bc` also has a larger library that is only enabled if extra operators are. +More information about the functions can be found in the +[Extended Library](./bc.md#extended-library) section of the +[full manual](./bc.md). + +### Manpages + +To disable installing manpages, pass either the `-M` flag or the +`--disable-man-pages` option to `configure.sh` as follows: + +``` +./configure.sh -M +./configure.sh --disable-man-pages +``` + +Both commands are equivalent. + +### Karatsuba Length + +The Karatsuba length is the point at which `bc` and `dc` switch from Karatsuba +multiplication to brute force, `O(n^2)` multiplication. It can be set by passing +the `-k` flag or the `--karatsuba-len` option to `configure.sh` as follows: + +``` +./configure.sh -k64 +./configure.sh --karatsuba-len 64 +``` + +Both commands are equivalent. + +Default is `64`. + +***WARNING***: The Karatsuba Length must be a **integer** greater than or equal +to `16` (to prevent stack overflow). If it is not, `configure.sh` will give an +error. + +### Install Options + +The relevant `autotools`-style install options are supported in `configure.sh`: + +* `--prefix` +* `--bindir` +* `--datarootdir` +* `--datadir` +* `--mandir` +* `--man1dir` +* `--localedir` + +An example is: + +``` +./configure.sh --prefix=/usr --localedir /usr/share/nls +make +make install +``` + +They correspond to the environment variables `$PREFIX`, `$BINDIR`, +`$DATAROOTDIR`, `$DATADIR`, `$MANDIR`, `$MAN1DIR`, and `$LOCALEDIR`, +respectively. + +***WARNING***: If the option is given, the value of the corresponding +environment variable is overridden. + +***WARNING***: If the command-line options are used, the long form of all other +command-line options must be used. + +## Optimization + +The `configure.sh` script will accept an optimization level to pass to the +compiler. Because `bc` is orders of magnitude faster with optimization, I +***highly*** recommend package and distro maintainers pass the highest +optimization level available in `CC` to `configure.sh` with the `-O` flag or +`--opt` option, as follows: + +``` +./configure.sh -O3 +./configure.sh --opt 3 +``` + +Both commands are equivalent. + +The build and install can then be run as normal: + +``` +make +make install +``` + +As usual, `configure.sh` will also accept additional `CFLAGS` on the command +line, so for SSE4 architectures, the following can add a bit more speed: + +``` +CFLAGS="-march=native -msse4" ./configure.sh -O3 +make +make install +``` + +Building with link-time optimization (`-flto` in clang) can further increase the +performance. + +Manual stripping is not necessary; non-debug builds are automatically stripped +in the link stage. + +## Debug Builds + +Debug builds (which also disable optimization if no optimization level is given +and if no extra `CFLAGS` are given) can be enabled with either the `-g` flag or +the `--debug` option, as follows: + +``` +./configure.sh -g +./configure.sh --debug +``` + +Both commands are equivalent. + +The build and install can then be run as normal: + +``` +make +make install +``` + +## Binary Size + +When built with both calculators, all available features, and `-Os` using clang, +the executable is 113 kb (113,248 bytes) on x86_64. That isn't much for what is +contained in the binary, but if necessary, it can be reduced. + +The single largest user of space is the `bc` calculator. If just `dc` is needed, +the size can be reduced to 80 kb (80,432 bytes). + +The next largest user of space is history support. If that is not needed, size +can be reduced (for a build with both calculators) to 97 kb (97,760 bytes). + +There are several reasons that history is a bigger user of space than `dc` +itself: + +* `dc`'s lexer and parser are *tiny* compared to `bc`'s because `dc` code is + almost already in the form that it is executed in, while `bc` has to not only + adjust the form to be executable, it has to parse functions, loops, `if` + statements, and other extra features. +* `dc` does not have much extra code in the interpreter. +* History has a lot of const data for supporting `UTF-8` terminals. + +The next biggest user is `dc`, so if just `bc` is needed, the size can be +reduced to 101 kb (100,960 bytes) with history and 84 kb (84,472 bytes) without +history. + +The next biggest user is signal handling. Without it, the size (with both +calculators) is reduced to 109 kb (109,120 bytes) with history and 93 kb (93,632 +bytes) without history. + +The next largest user is extra math support. If this is not needed, the size +(with both calculators) can be reduced to 105 kb (105,048 bytes) with history +and signal handling, 89 kb (88,560 bytes) without history, 101 kb (100,920 +bytes) without signal handling, and 84 kb (84,432 bytes) without both. + +## Testing + +The default test suite can be run with the following command: + +``` +make test +``` + +To test `bc` only, run the following command: + +``` +make test_bc +``` + +To test `dc` only, run the following command: + +``` +make test_dc +``` + +This `bc`, if built, assumes a working, GNU-compatible `bc`, installed on the +system and in the `PATH`, to generate some tests, unless the `-G` flag or +`--disable-generated-tests` option is given to `configure.sh`, as follows: + +``` +./configure.sh -G +./configure.sh --disable-generated-tests +``` + +After running `configure.sh`, build and run tests as follows: + +``` +make +make test +``` + +This `dc` also assumes a working, GNU-compatible `dc`, installed on the system +and in the `PATH`, to generate some tests, unless on of the above options is +given to `configure.sh`. + +To generate test coverage, pass the `-c` flag or the `--coverage` option to +`configure.sh` as follows: + +``` +./configure.sh -c +./configure.sh --coverage +``` + +Both commands are equivalent. + +***WARNING***: Both `bc` and `dc` must be built for test coverage. Otherwise, +`configure.sh` will give an error. + +[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html +[2]: https://www.gnu.org/software/bc/ +[3]: https://www.musl-libc.org/ +[4]: #build-environment-variables +[5]: #build-options +[6]: #cross-compiling Index: contrib/bc/manuals/dc.1 =================================================================== --- contrib/bc/manuals/dc.1 +++ contrib/bc/manuals/dc.1 @@ -0,0 +1,760 @@ +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "DC" "1" "June 2019" "Gavin D. Howard" "General Commands Manual" +. +.SH "NAME" +\fBdc\fR \- arbitrary\-precision reverse\-Polish notation calculator +. +.SH "SYNOPSIS" +\fBdc\fR [\fB\-hiPvVx\fR] [\fB\-\-version\fR] [\fB\-\-help\fR] [\fB\-\-interactive\fR] [\fB\-\-no\-prompt\fR] [\fB\-\-extended\-register\fR] [\fB\-e\fR \fIexpr\fR] [\fB\-\-expression=\fR\fIexpr\fR\.\.\.] [\fB\-f\fR \fIfile\fR\.\.\.] [\fB\-file=\fR\fIfile\fR\.\.\.] [\fIfile\fR\.\.\.] +. +.SH "DESCRIPTION" +dc(1) is an arbitrary\-precision calculator\. It uses a stack (reverse Polish notation) to store numbers and results of computations\. Arithmetic operations pop arguments off of the stack and push the results\. +. +.P +If no files are given on the command\-line as extra arguments (i\.e\., not as \fB\-f\fR or \fB\-\-file\fR arguments), then dc(1) reads from \fBstdin\fR\. Otherwise, those files are processed, and dc(1) will then exit\. +. +.P +This is different from the dc(1) on OpenBSD and possibly other dc(1) implementations, where \fB\-e\fR (\fB\-\-expression\fR) and \fB\-f\fR (\fB\-\-file\fR) arguments cause dc(1) to execute them and exit\. The reason for this is that this dc(1) allows users to set arguments in the environment variable \fBDC_ENV_ARGS\fR (see the ENVIRONMENT VARIABLES section)\. Any expressions given on the command\-line should be used to set up a standard environment\. For example, if a user wants the \fBscale\fR always set to \fB10\fR, they can set \fBDC_ENV_ARGS\fR to "\-e 10k", and this dc(1) will always start with a \fBscale\fR of \fB10\fR\. +. +.P +If users want to have dc(1) exit after processing all input from \fB\-e\fR and \fB\-f\fR arguments (and their equivalents), then they can just simply add "\-e q" as the last command\-line argument\. +. +.SH "OPTIONS" +The following are the options that dc(1) accepts\. +. +.TP +\fB\-h\fR, \fB\-\-help\fR +Prints a usage message and quits\. +. +.TP +\fB\-v\fR, \fB\-V\fR, \fB\-\-version\fR +Print the version information (copyright header) and exit\. +. +.TP +\fB\-i\fR, \fB\-\-interactive\fR +Forces interactive mode\. +. +.IP +dc(1) has an interactive mode and a non\-interactive mode\. The interactive mode is turned on automatically when both \fBstdin\fR and \fBstdout\fR are hooked to a terminal, but this flag can turn it on in other cases\. In interactive mode, dc(1) attempts to recover from errors (see the RESET section), and in normal execution, flushes \fBstdout\fR as soon as execution is done for the current input\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-P\fR, \fB\-\-no\-prompt\fR +Disables the prompt in interactive mode\. This is mostly for those users that do not want a prompt or are not used to having them in \fBdc\fR\. Most of those users would want to put this option in \fBDC_ENV_ARGS\fR\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-x\fR \fB\-\-extended\-register\fR +Enables extended register mode\. See the REGISTERS section for more information\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-e\fR \fIexpr\fR, \fB\-\-expression\fR=\fIexpr\fR +Evaluates \fBexpr\fR\. If multiple expressions are given, they are evaluated in order\. If files are given as well (see below), the expressions and files are evaluated in the order given\. This means that if a file is given before an expression, the file is read in and evaluated first\. +. +.IP +In other dc(1) implementations, this option causes the program to execute the expressions and then exit\. This dc(1) does not, unless the \fBDC_EXPR_EXIT\fR is defined (see the ENVIRONMENT VARIABLES section)\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB\-f\fR \fIfile\fR, \fB\-\-file\fR=\fIfile\fR +Reads in \fBfile\fR and evaluates it\. If expressions are also given (see above), the expressions are evaluated in the order given\. +. +.IP +In other dc(1) implementations, this option causes the program to execute the files and then exit\. This dc(1) does not, unless the \fBDC_EXPR_EXIT\fR is defined (see the ENVIRONMENT VARIABLES section)\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.SH "STDOUT" +Any non\-error output is written to \fBstdout\fR\. +. +.P +\fBNote\fR: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the EXIT STATUS section) if it cannot write to \fBstdout\fR, so if \fBstdout\fR is closed, as in \fBbc >&\-\fR, it will quit with an error\. This is done so that dc(1) can report problems when \fBstdout\fR is redirected to a file\. +. +.P +If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \fBstdout\fR to \fB/dev/null\fR\. +. +.SH "STDERR" +Any error output is written to \fBstderr\fR\. +. +.P +\fBNote\fR: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the EXIT STATUS section) if it cannot write to \fBstderr\fR, so if \fBstderr\fR is closed, as in \fBbc 2>&\-\fR, it will quit with an error\. This is done so that dc(1) can report problems when \fBstderr\fR is redirected to a file\. +. +.P +If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \fBstderr\fR to \fB/dev/null\fR\. +. +.SH "SYNTAX" +\fBibase\fR is a register (see the REGISTERS section) determining how to interpret constant numbers\. It is the "input" base, or the number base used for interpreting input numbers\. \fBibase\fR is initially \fB10\fR\. The max allowable value for \fBibase\fR is \fB16\fR\. The min allowable value for \fBibase\fR is \fB2\fR\. The max allowable value for \fBibase\fR can be queried in dc(1) programs with the \fBT\fR command\. +. +.P +\fBobase\fR is a register (see the REGISTERS section) determining how to output results\. It is the "output" base, or the number base used for outputting numbers\. \fBobase\fR is initially \fB10\fR\. The max allowable value for \fBobase\fR is \fBDC_BASE_MAX\fR\. The min allowable value for \fBobase\fR is \fB2\fR unless dc(1) was built with the extra math option\. If it was, then the min allowable value is \fB0\fR\. In this case, if \fBobase\fR is \fB0\fR, values are output in scientific notation, and if \fBobase\fR is \fB1\fR, values are output in engineering notation\. (Outputting in scientific or engineering notation are \fBnon\-portable extensions\fR\.) The max allowable value for \fBobase\fR can be queried in dc(1) programs with the \fBU\fR command\. +. +.P +The \fBscale\fR of an expression is the number of digits in the result of the expression right of the decimal point, and \fBscale\fR is a register (see the REGISTERS section) that sets the precision of any operations (with exceptions)\. \fBscale\fR is initially \fB0\fR\. \fBscale\fR cannot be negative\. The max allowable value for \fBscale\fR can be queried in dc(1) programs with the \fBV\fR command\. +. +.P +Each item in the input source code, either a number (see the NUMBERS section) or a command (see the COMMANDS section), is processed and executed, in order\. Input is processed immediately when entered\. +. +.SS "Comments" +Comments go from \fB#\fR until, and not including, the next newline\. This is a \fBnon\-portable extension\fR\. +. +.SH "NUMBERS" +Numbers are strings made up of digits, uppercase letters up to \fBF\fR, and at most \fB1\fR period for a radix\. Numbers can have up to \fBDC_NUM_MAX\fR digits\. Uppercase letters equal \fB9\fR + their position in the alphabet (i\.e\., \fBA\fR equals \fB10\fR, or \fB9 + 1\fR)\. If a digit or letter makes no sense with the current value of \fBibase\fR, they are set to the value of the highest valid digit in \fBibase\fR\. +. +.P +Single\-character numbers (i\.e\., \fBA\fR) take the value that they would have if they were valid digits, regardless of the value of \fBibase\fR\. This means that \fBA\fR always equals decimal \fB10\fR and \fBF\fR always equals decimal \fB15\fR\. +. +.P +In addition, if dc(1) was built with the extra math option, it accepts numbers in scientific notation\. For dc(1), an example is \fB1\.89237e9\fR, which is equal to \fB1892370000\fR\. Negative exponents are also allowed, so \fB4\.2890e_3\fR is equal to \fB0\.0042890\fR\. +. +.P +\fBWARNING\fR: Both the number and the exponent in scientific notation are interpreted according to the current \fBibase\fR, but the number is still multiplied by \fB10^exponent\fR regardless of the current \fBibase\fR\. For example, if \fBibase\fR is \fB16\fR and dc(1) is given the number string \fB"FFeA"\fR, the resulting decimal number will be \fB2550000000000\fR, and if dc(1) is given the number string \fB"10e_4"\fR, the resulting decimal number will be \fB0\.0016\fR\. +. +.P +Accepting input as scientific notation is a \fBnon\-portable extension\fR\. +. +.SH "COMMANDS" +The valid commands are listed below\. +. +.SS "Printing" +These commands are used for printing\. +. +.P +Note that if dc(1) has been built with the extra math option enabled, both scientific notation and engineering notation are available for printing numbers\. Scientific notation is activated by assigning \fB0\fR to \fBobase\fR using \fB0o\fR (in any other context, an \fBobase\fR of \fB0\fR is invalid), and engineering notation is activated by assigning \fB1\fR to \fBobase\fR using \fB1o\fR (which is also invalid in any other context)\. To deactivate them, just assign a different value to \fBobase\fR\. +. +.P +Printing numbers in scientific notation and/or engineering notation is a \fBnon\-portable extension\fR\. +. +.TP +\fBp\fR +Prints the value on top of the stack, whether number or string, and prints a newline after\. +. +.IP +This does not alter the stack\. +. +.TP +\fBn\fR +Prints the value on top of the stack, whether number or string, and pops it off of the stack\. +. +.TP +\fBP\fR +Pops a value off the stack\. +. +.IP +If the value is a number, it is truncated and the result\'s absolute value is printed as though \fBobase\fR is \fBUCHAR_MAX + 1\fR and each digit is interpreted as an ASCII character, making it a byte stream\. +. +.IP +If the value is a string, it is printed without a trailing newline\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fBf\fR +Prints the entire contents of the stack, in order from newest to oldest, without altering anything\. +. +.IP +Users should use this command when they get lost\. +. +.SS "Arithmetic" +These are the commands used for arithmetic\. +. +.TP +\fB+\fR +The top two values are popped off the stack, added, and the result is pushed onto the stack\. The result\'s \fBscale\fR is equal to the max \fBscale\fR of both operands\. +. +.TP +\fB\-\fR +The top two values are popped off the stack, subtracted, and the result is pushed onto the stack\. The result\'s \fBscale\fR is equal to the max \fBscale\fR of both operands\. +. +.TP +\fB*\fR +The top two values are popped off the stack, multiplied, and the result is pushed onto the stack\. If \fBa\fR is the \fBscale\fR of the first expression and \fBb\fR is the \fBscale\fR of the second expression, the \fBscale\fR of the result is equal to \fBmin(a+b,max(scale,a,b))\fR where \fBmin\fR and \fBmax\fR return the obvious values\. +. +.TP +\fB/\fR +The top two values are popped off the stack, divided, and the result is pushed onto the stack\. The result\'s \fBscale\fR is equal to \fBscale\fR\. +. +.TP +\fB%\fR +The top two values are popped off the stack, remaindered, and the result is pushed onto the stack\. +. +.IP +Remaindering is equivalent to 1) Computing \fBa/b\fR to current \fBscale\fR, and 2) Using the result of step 1 to calculate \fBa\-(a/b)*b\fR to \fBscale\fR \fBmax(scale + scale(b), scale(a))\fR\. +. +.TP +\fB~\fR +The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack\. This is equivalent to \fBx y / x y %\fR except that \fBx\fR and \fBy\fR are only evaluated once\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB^\fR +The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack\. +. +.IP +The first value popped off the stack must be an integer\. +. +.TP +\fBv\fR +The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack\. The result\'s \fBscale\fR is equal to \fBscale\fR\. +. +.TP +\fB_\fR +If this command \fIimmediately\fR precedes a number (i\.e\., no spaces or other commands), then that number is input as a negative number\. +. +.IP +Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack\. This behavior without a number is a \fBnon\-portable extension\fR\. +. +.TP +\fBb\fR +The top value is popped off the stack and its absolute value is pushed onto the stack\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB|\fR +The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack\. +. +.IP +The first value popped is used as the reduction modulus and must be an integer and non\-zero\. The second value popped is used as the exponent and must be an integer and non\-negative\. The third value popped is the base and must be an integer\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB$\fR +The top value is popped off the stack and copied, and the copy is truncated and pushed onto the stack\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB@\fR +The top two values are popped off the stack, and the second\'s precision is set to the value of the first, whether by truncation or extension\. +. +.IP +The first value must be an integer and non\-negative\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fBH\fR +The top two values are popped off the stack, and the second is shifted left (radix shifted right) to the value of the first\. +. +.IP +The first value must be an integer and non\-negative\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fBh\fR +The top two values are popped off the stack, and the second is shifted right (radix shifted left) to the value of the first\. +. +.IP +The first value must be an integer and non\-negative\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fBG\fR +The top two values are popped off of the stack, they are compared, and a \fB1\fR is pushed if they are equal, or \fB0\fR otherwise\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fBN\fR +The top value is popped off of the stack, and if it a \fB0\fR, a \fB1\fR is pushed; otherwise, a \fB0\fR is pushed\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB(\fR +The top two values are popped off of the stack, they are compared, and a \fB1\fR is pushed if the first is less than the second, or \fB0\fR otherwise\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB{\fR +The top two values are popped off of the stack, they are compared, and a \fB1\fR is pushed if the first is less than or equal to the second, or \fB0\fR otherwise\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB)\fR +The top two values are popped off of the stack, they are compared, and a \fB1\fR is pushed if the first is greater than the second, or \fB0\fR otherwise\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB}\fR +The top two values are popped off of the stack, they are compared, and a \fB1\fR is pushed if the first is greater than or equal to the second, or \fB0\fR otherwise\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.SS "Stack Control" +These commands control the stack\. +. +.TP +\fBc\fR +Removes all items from ("clears") the stack\. +. +.TP +\fBd\fR +Copies the item on top of the stack ("duplicates") and pushes the copy onto the stack\. +. +.TP +\fBr\fR +Swaps ("reverses") the two top items on the stack\. +. +.TP +\fBR\fR +Pops ("removes") the top value from the stack\. +. +.SS "Register Control" +These commands control registers (see the REGISTERS section)\. +. +.TP +\fBs\fR\fIr\fR +Pops the value off the top of the stack and stores it into register \fBr\fR\. +. +.TP +\fBl\fR\fIr\fR +Copies the value in register \fBr\fR and pushes it onto the stack\. This does not alter the contents of \fBr\fR\. +. +.TP +\fBS\fR\fIr\fR +Pops the value off the top of the (main) stack and pushes it onto the stack of register \fBr\fR\. The previous value of the register becomes inaccessible\. +. +.TP +\fBL\fR\fIr\fR +Pops the value off the top of register \fBr\fR\'s stack and push it onto the main stack\. The previous value in register \fBr\fR\'s stack, if any, is now accessible via the \fBl\fR\fIr\fR command\. +. +.SS "Parameters" +These commands control the values of \fBibase\fR, \fBobase\fR, and \fBscale\fR (see the SYNTAX section)\. +. +.TP +\fBi\fR +Pops the value off of the top of the stack and uses it to set \fBibase\fR, which must be between \fB2\fR and \fB16\fR, inclusive\. +. +.IP +If the value on top of the stack has any \fBscale\fR, the \fBscale\fR is ignored\. +. +.TP +\fBo\fR +Pops the value off of the top of the stack and uses it to set \fBobase\fR, which must be between \fB2\fR and \fBDC_BASE_MAX\fR, inclusive (see bc(1))\. The value can be either \fB0\fR or \fB1\fR if dc(1) was built with the extra math option\. +. +.IP +If the value on top of the stack has any \fBscale\fR, the \fBscale\fR is ignored\. +. +.TP +\fBk\fR +Pops the value off of the top of the stack and uses it to set \fBscale\fR, which must be non\-negative\. +. +.IP +If the value on top of the stack has any \fBscale\fR, the \fBscale\fR is ignored\. +. +.TP +\fBI\fR +Pushes the current value of \fBibase\fR onto the main stack\. +. +.TP +\fBO\fR +Pushes the current value of \fBobase\fR onto the main stack\. +. +.TP +\fBK\fR +Pushes the current value of \fBscale\fR onto the main stack\. +. +.TP +\fBT\fR +Pushes the maximum allowable value of \fBibase\fR onto the main stack\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fBU\fR +Pushes the maximum allowable value of \fBobase\fR onto the main stack\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fBV\fR +Pushes the maximum allowable value of \fBscale\fR onto the main stack\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.SS "Strings" +The following commands control strings\. +. +.P +dc(1) can work with both numbers and strings, and registers (see the REGISTERS section) can hold both strings and numbers\. dc(1) always knows whether a register\'s contents are a string or a number\. +. +.P +While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings\. +. +.P +Strings can also be executed as macros\. For example, if the string \fB[1pR]\fR is executed as a macro, then the code \fB1pR\fR is executed, meaning that the \fB1\fR will be printed with a newline after and then popped from the stack\. +. +.TP +\fB[\fR\fIcharacters\fR\fB]\fR +Makes a string containing \fBcharacters\fR and pushes it onto the stack\. +. +.IP +If there are brackets (\fB[\fR and \fB]\fR) in the string, then they must be balanced\. Unbalanced brackets can be escaped using a backslash (\fB\e\fR) character\. +. +.IP +If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not\. +. +.TP +\fBa\fR +The value on top of the stack is popped\. +. +.IP +If it is a number, it is truncated and its absolute value is taken\. The result mod \fBUCHAR_MAX + 1\fR is calculated\. If that result is \fB0\fR, push an empty string; otherwise, push a one\-character string where the character is the result of the mod interpreted as an ASCII character\. +. +.IP +If it is a string, then a new string is made\. If the original string is empty, the new string is empty\. If it is not, then the first character of the original string is used to create the new string as a one\-character string\. The new string is then pushed onto the stack\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fBx\fR +Pops a value off of the top of the stack\. +. +.IP +If it is a number, it is pushed onto the stack\. +. +.IP +If it is a string, it is executed as a macro\. +. +.IP +This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below\. +. +.TP +\fB>\fR\fIr\fR +Pops two values off of the stack that must be numbers and compares them\. If the first value is greater than the second, then the contents of register \fBr\fR are executed\. +. +.IP +For example, \fB0 1>a\fR will execute the contents of register \fBa\fR, and \fB1 0>a\fR will not\. +. +.TP +\fB>\fR\fIr\fR\fBe\fR\fIs\fR +Like the above, but will execute register \fBs\fR if the comparison fails\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB!>\fR\fIr\fR +Pops two values off of the stack that must be numbers and compares them\. If the first value is not greater than the second (less than or equal to), then the contents of register \fBr\fR are executed\. +. +.TP +\fB!>\fR\fIr\fR\fBe\fR\fIs\fR +Like the above, but will execute register \fBs\fR if the comparison fails\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB<\fR\fIr\fR +Pops two values off of the stack that must be numbers and compares them\. If the first value is less than the second, then the contents of register \fBr\fR are executed\. +. +.TP +\fB<\fR\fIr\fR\fBe\fR\fIs\fR +Like the above, but will execute register \fBs\fR if the comparison fails\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB!<\fR\fIr\fR +Pops two values off of the stack that must be numbers and compares them\. If the first value is not less than the second (greater than or equal to), then the contents of register \fBr\fR are executed\. +. +.TP +\fB!<\fR\fIr\fR\fBe\fR\fIs\fR +Like the above, but will execute register \fBs\fR if the comparison fails\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB=\fR\fIr\fR +Pops two values off of the stack that must be numbers and compares them\. If the first value is equal to the second (greater than or equal to), then the contents of register \fBr\fR are executed\. +. +.TP +\fB=\fR\fIr\fR\fBe\fR\fIs\fR +Like the above, but will execute register \fBs\fR if the comparison fails\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB!=\fR\fIr\fR +Pops two values off of the stack that must be numbers and compares them\. If the first value is not equal to the second (greater than or equal to), then the contents of register \fBr\fR are executed\. +. +.TP +\fB!=\fR\fIr\fR\fBe\fR\fIs\fR +Like the above, but will execute register \fBs\fR if the comparison fails\. +. +.IP +This is a \fBnon\-portable extension\fR\. +. +.TP +\fB?\fR +Reads a line from the \fBstdin\fR and executes it\. This is to allow macros to request input from users\. +. +.TP +\fBq\fR +During execution of a macro, this exits that macro\'s execution and the execution of the macro that executed it\. If there are no macros, or only one macro executing, dc(1) exits\. +. +.TP +\fBQ\fR +Pops a value from the stack which must be non\-negative and is used the number of macro executions to pop off of the execution stack\. If the number of levels to pop is greater than the number of executing macros, dc(1) exits\. +. +.SS "Status" +These commands query status of the stack or its top value\. +. +.TP +\fBZ\fR +Pops a value off of the stack\. +. +.IP +If it is a number, calculates the number of significant decimal digits it has and pushes the result\. +. +.IP +If it is a string, pushes the number of characters the string has\. +. +.TP +\fBX\fR +Pops a value off of the stack\. +. +.IP +If it is a number, pushes the \fBscale\fR of the value onto the stack\. +. +.IP +If it is a string, pushes \fB0\fR\. +. +.TP +\fBz\fR +Pushes the current stack depth (before execution of this command)\. +. +.SS "Arrays" +These commands manipulate arrays\. +. +.TP +\fB:\fR\fIr\fR +Pops the top two values off of the stack\. The second value will be stored in the array \fBr\fR (see the REGISTERS section), indexed by the first value\. +. +.TP +\fB;\fR\fIr\fR +Pops the value on top of the stack and uses it as an index into the array \fBr\fR\. The selected value is then pushed onto the stack\. +. +.SH "REGISTERS" +Registers are names that can store strings, numbers, and arrays\. (Number/string registers do not interfere with array registers\.) +. +.P +Each register is also its own stack, so the current register value is the top of the register\'s stack\. All registers, when first referenced, have one value (\fB0\fR) in their stack\. +. +.P +In non\-extended register mode, a register name is just the single character that follows any command that needs a register name\. The only exception is a newline (\fB\'\en\'\fR); it is a parse error for a newline to be used as a register name\. +. +.SS "Extended Register Mode" +Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled\. +. +.P +If extended register mode is enabled (\fB\-x\fR or \fB\-\-extended\-register\fR command\-line arguments are given), then normal single character registers are used \fB\fIunless\fR\fR the character immediately following a command that needs a register name is a space (according to \fBisspace()\fR) and not a newline (\fB\'\en\'\fR)\. +. +.P +In that case, the register name is found according to the regex \fB[a\-z][a\-z0\-9_]*\fR (like bc(1)), and it is a parse error if the next non\-space characters do not match that regex\. +. +.SH "RESET" +When dc(1) encounters an error or a signal that it has a non\-default handler for, it resets\. This means that several things happen\. +. +.P +First, any macros that are executing are stopped and popped off the stack\. The behavior is not unlike that of exceptions in programming languages\. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped\. +. +.P +Thus, when dc(1) resets, it skips any remaining code waiting to be executed\. Then, if it is interactive mode, and the error was not a fatal error (see the EXIT STATUS section), it asks for more input; otherwise, it exits with the appropriate return code\. +. +.SH "PERFORMANCE" +Most dc(1) implementations use \fBchar\fR types to calculate the value of \fB1\fR decimal digit at a time, but that can be slow\. This dc(1) does something different\. +. +.P +It uses large integers to calculate more than \fB1\fR decimal digit at a time\. If built in a environment where \fBDC_LONG_BIT\fR (see the LIMITS section) is \fB64\fR, then each integer has \fB9\fR decimal digits\. If built in an environment where \fBDC_LONG_BIT\fR is \fB32\fR then each integer has \fB4\fR decimal digits\. This value (the number of decimal digits per large integer) is called \fBDC_BASE_DIGS\fR\. +. +.P +In addition, this dc(1) uses an even larger integer for overflow checking\. This integer type depends on the value of \fBDC_LONG_BIT\fR, but is always at least twice as large as the integer type used to store digits\. +. +.SH "LIMITS" +The following are the limits on dc(1): +. +.TP +\fBDC_LONG_BIT\fR +The number of bits in the \fBlong\fR type in the environment where dc(1) was built\. This determines how many decimal digits can be stored in a single large integer (see the PERFORMANCE section)\. +. +.TP +\fBDC_BASE_DIGS\fR +The number of decimal digits per large integer (see the PERFORMANCE section)\. Depends on \fBDC_LONG_BIT\fR\. +. +.TP +\fBDC_BASE_POW\fR +The max decimal number that each large integer can store (see \fBDC_BASE_DIGS\fR) plus \fB1\fR\. Depends on \fBDC_BASE_DIGS\fR\. +. +.TP +\fBDC_OVERFLOW_MAX\fR +The max number that the overflow type (see the PERFORMANCE section) can hold\. Depends on \fBDC_LONG_BIT\fR\. +. +.TP +\fBDC_BASE_DIGS\fR +The number of decimal digits per large integer (see the PERFORMANCE section)\. +. +.TP +\fBDC_BASE_MAX\fR +The maximum output base\. Set at \fBDC_BASE_POW\fR\. +. +.TP +\fBDC_DIM_MAX\fR +The maximum size of arrays\. Set at \fBSIZE_MAX\-1\fR\. +. +.TP +\fBDC_SCALE_MAX\fR +The maximum \fBscale\fR\. Set at \fBDC_OVERFLOW_MAX\-1\fR\. +. +.TP +\fBDC_STRING_MAX\fR +The maximum length of strings\. Set at \fBDC_OVERFLOW_MAX\-1\fR\. +. +.TP +\fBDC_NAME_MAX\fR +The maximum length of identifiers\. Set at \fBDC_OVERFLOW_MAX\-1\fR\. +. +.TP +\fBDC_NUM_MAX\fR +The maximum length of a number (in decimal digits), which includes digits after the decimal point\. Set at \fBDC_OVERFLOW_MAX\-1\fR\. +. +.TP +Exponent +The maximum allowable exponent (positive or negative)\. Set at \fBDC_OVERFLOW_MAX\fR\. +. +.TP +Number of vars +The maximum number of vars/arrays\. Set at \fBSIZE_MAX\-1\fR\. +. +.P +These limits are meant to be effectively non\-existent; the limits are so large (at least on 64\-bit machines) that there should not be any point at which they become a problem\. In fact, memory should be exhausted before these limits should be hit\. +. +.SH "ENVIRONMENT VARIABLES" +dc(1) recognizes the following environment variables: +. +.TP +\fBDC_ENV_ARGS\fR +This is another way to give command\-line arguments to dc(1)\. They should be in the same format as all other command\-line arguments\. These are always processed first, so any files given in \fBDC_ENV_ARGS\fR will be processed before files given on the command\-line\. This gives the user the ability to set up "standard" options and files to be used at every invocation\. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs\. Another use would be to use the \fB\-e\fR option to set \fBscale\fR to a value other than \fB0\fR\. +. +.TP +\fBDC_LINE_LENGTH\fR +If this environment variable exists and contains an integer that is greater than \fB1\fR and is less than \fBUINT16_MAX\fR (\fB2^16\-1\fR), dc(1) will output lines to that length, including the backslash newline combo\. The default line length is \fB70\fR\. +. +.TP +\fBDC_EXPR_EXIT\fR +If this variable exists (no matter the contents), dc(1) will exit immediately after executing expressions and files given by the \fB\-e\fR and/or \fB\-f\fR command\-line options (and any equivalents)\. +. +.SH "EXIT STATUS" +dc(1) returns the following exit statuses: +. +.TP +\fB0\fR +No error\. +. +.TP +\fB1\fR +A math error occurred\. This follows standard practice of using \fB1\fR for expected errors, since math errors will happen in the process of normal execution\. +. +.IP +Math errors include divide by \fB0\fR, taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, and attempting to use a non\-integer where an integer is required\. +. +.IP +Converting to a hardware integer happens for the second operand of the power (\fB^\fR), places (\fB@\fR), left shift (\fBH\fR), and right shift (\fBh\fR) operators\. +. +.TP +\fB2\fR +A parse error occurred\. +. +.IP +Parse errors include unexpected \fBEOF\fR, using an invalid character, failing to find the end of a string or comment, and using a token where it\'s invalid\. +. +.TP +\fB3\fR +A runtime error occurred\. +. +.IP +Runtime errors include assigning an invalid number to \fBibase\fR, \fBobase\fR, or \fBscale\fR; give a bad expression to a \fBread()\fR call, calling \fBread()\fR inside of a \fBread()\fR call, type errors, and attempting an operation when the stack has too few elements\. +. +.TP +\fB4\fR +A fatal error occurred\. +. +.IP +Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command\-line options\. +. +.P +The exit status \fB4\fR is special; when a fatal error occurs, dc(1) always exits and returns \fB4\fR, no matter what mode dc(1) is in\. +. +.P +The other statuses will only be returned when dc(1) is not in interactive mode, since dc(1) resets its state (see the RESET section) and accepts more input when one of those errors occurs in interactive mode\. This is also the case when interactive mode is forced by the \fB\-i\fR option\. +. +.P +These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using \fB\-i\fR\. +. +.SH "SIGNAL HANDLING" +If dc(1) has been compiled with the signal handling, sending a \fBSIGINT\fR will cause dc(1) to stop execution of the current input and reset (see the RESET section), asking for more input\. +. +.P +Otherwise, \fBSIGTERM\fR and \fBSIGQUIT\fR cause dc(1) to clean up and exit, and it uses the default handler for all other signals\. +. +.P +If dc(1) has not been compiled with signal handling, it uses the default signal handlers for all signals\. +. +.SH "COMMAND LINE HISTORY" +dc(1) supports interactive command\-line editing, if compiled with the history option enabled\. If \fBstdin\fR is hooked to a terminal, it is enabled\. Previous lines can be recalled and edited with the arrow keys\. +. +.SH "LOCALES" +This dc(1) ships with support for adding error messages for different locales\. +. +.SH "SEE ALSO" +bc(1) +. +.SH "STANDARDS" +The dc(1) utility operators are compliant with the operators in the bc(1) IEEE Std 1003\.1\-2017 (“POSIX\.1\-2017”) \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR specification\. +. +.SH "AUTHOR" +This dc(1) was made from scratch by Gavin D\. Howard\. +. +.SH "BUGS" +None are known\. Report bugs at https://github\.com/gavinhoward/bc\. Index: contrib/bc/manuals/dc.1.ronn =================================================================== --- contrib/bc/manuals/dc.1.ronn +++ contrib/bc/manuals/dc.1.ronn @@ -0,0 +1,870 @@ +dc(1) -- arbitrary-precision reverse-Polish notation calculator +=============================================================== + +SYNOPSIS +-------- + +`dc` [`-hiPvVx`] [`--version`] [`--help`] [`--interactive`] [`--no-prompt`] +[`--extended-register`] [`-e` *expr*] [`--expression=`*expr*...] +[`-f` *file*...] [`-file=`*file*...] [*file*...] + +DESCRIPTION +----------- + +dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish +notation) to store numbers and results of computations. Arithmetic operations +pop arguments off of the stack and push the results. + +If no files are given on the command-line as extra arguments (i.e., not as `-f` +or `--file` arguments), then dc(1) reads from `stdin`. Otherwise, those files +are processed, and dc(1) will then exit. + +This is different from the dc(1) on OpenBSD and possibly other dc(1) +implementations, where `-e` (`--expression`) and `-f` (`--file`) arguments cause +dc(1) to execute them and exit. The reason for this is that this dc(1) allows +users to set arguments in the environment variable `DC_ENV_ARGS` (see the +ENVIRONMENT VARIABLES section). Any expressions given on the command-line should +be used to set up a standard environment. For example, if a user wants the +`scale` always set to `10`, they can set `DC_ENV_ARGS` to "-e 10k", and this +dc(1) will always start with a `scale` of `10`. + +If users want to have dc(1) exit after processing all input from `-e` and `-f` +arguments (and their equivalents), then they can just simply add "-e q" as the +last command-line argument. + +OPTIONS +------- + +The following are the options that dc(1) accepts. + + * `-h`, `--help`: + Prints a usage message and quits. + + * `-v`, `-V`, `--version`: + Print the version information (copyright header) and exit. + + * `-i`, `--interactive`: + Forces interactive mode. + + dc(1) has an interactive mode and a non-interactive mode. The interactive + mode is turned on automatically when both `stdin` and `stdout` are hooked to + a terminal, but this flag can turn it on in other cases. In interactive + mode, dc(1) attempts to recover from errors (see the RESET section), and in + normal execution, flushes `stdout` as soon as execution is done for the + current input. + + This is a **non-portable extension**. + + * `-P`, `--no-prompt`: + Disables the prompt in interactive mode. This is mostly for those users that + do not want a prompt or are not used to having them in `dc`. Most of those + users would want to put this option in `DC_ENV_ARGS`. + + This is a **non-portable extension**. + + * `-x` `--extended-register`: + Enables extended register mode. See the REGISTERS section for more + information. + + This is a **non-portable extension**. + + * `-e` *expr*, `--expression`=*expr*: + Evaluates `expr`. If multiple expressions are given, they are evaluated in + order. If files are given as well (see below), the expressions and files are + evaluated in the order given. This means that if a file is given before an + expression, the file is read in and evaluated first. + + In other dc(1) implementations, this option causes the program to execute + the expressions and then exit. This dc(1) does not, unless the + `DC_EXPR_EXIT` is defined (see the ENVIRONMENT VARIABLES section). + + This is a **non-portable extension**. + + * `-f` *file*, `--file`=*file*: + Reads in `file` and evaluates it. If expressions are also given (see above), + the expressions are evaluated in the order given. + + In other dc(1) implementations, this option causes the program to execute + the files and then exit. This dc(1) does not, unless the + `DC_EXPR_EXIT` is defined (see the ENVIRONMENT VARIABLES section). + + This is a **non-portable extension**. + +STDOUT +------ + +Any non-error output is written to `stdout`. + +**Note**: Unlike other dc(1) implementations, this dc(1) will issue a fatal +error (see the EXIT STATUS section) if it cannot write to `stdout`, so if +`stdout` is closed, as in `bc >&-`, it will quit with an error. This is +done so that dc(1) can report problems when `stdout` is redirected to a file. + +If there are scripts that depend on the behavior of other dc(1) implementations, +it is recommended that those scripts be changed to redirect `stdout` to +`/dev/null`. + +STDERR +------ + +Any error output is written to `stderr`. + +**Note**: Unlike other dc(1) implementations, this dc(1) will issue a fatal +error (see the EXIT STATUS section) if it cannot write to `stderr`, so if +`stderr` is closed, as in `bc 2>&-`, it will quit with an error. This is +done so that dc(1) can report problems when `stderr` is redirected to a file. + +If there are scripts that depend on the behavior of other dc(1) implementations, +it is recommended that those scripts be changed to redirect `stderr` to +`/dev/null`. + +SYNTAX +------ + +`ibase` is a register (see the REGISTERS section) determining how to interpret +constant numbers. It is the "input" base, or the number base used for +interpreting input numbers. `ibase` is initially `10`. The max allowable value +for `ibase` is `16`. The min allowable value for `ibase` is `2`. The max +allowable value for `ibase` can be queried in dc(1) programs with the `T` +command. + +`obase` is a register (see the REGISTERS section) determining how to output +results. It is the "output" base, or the number base used for outputting +numbers. `obase` is initially `10`. The max allowable value for `obase` is +`DC_BASE_MAX`. The min allowable value for `obase` is `2` unless dc(1) was built +with the extra math option. If it was, then the min allowable value is `0`. In +this case, if `obase` is `0`, values are output in scientific notation, and if +`obase` is `1`, values are output in engineering notation. (Outputting in +scientific or engineering notation are **non-portable extensions**.) The max +allowable value for `obase` can be queried in dc(1) programs with the `U` +command. + +The **scale** of an expression is the number of digits in the result of the +expression right of the decimal point, and `scale` is a register (see the +REGISTERS section) that sets the precision of any operations (with exceptions). +`scale` is initially `0`. `scale` cannot be negative. The max allowable value +for `scale` can be queried in dc(1) programs with the `V` command. + +Each item in the input source code, either a number (see the NUMBERS section) or +a command (see the COMMANDS section), is processed and executed, in order. Input +is processed immediately when entered. + +### Comments + +Comments go from `#` until, and not including, the next newline. This is a +**non-portable extension**. + +NUMBERS +------- + +Numbers are strings made up of digits, uppercase letters up to `F`, and at most +`1` period for a radix. Numbers can have up to `DC_NUM_MAX` digits. Uppercase +letters equal `9` + their position in the alphabet (i.e., `A` equals `10`, or +`9 + 1`). If a digit or letter makes no sense with the current value of `ibase`, +they are set to the value of the highest valid digit in `ibase`. + +Single-character numbers (i.e., `A`) take the value that they would have if they +were valid digits, regardless of the value of `ibase`. This means that `A` +always equals decimal `10` and `F` always equals decimal `15`. + +In addition, if dc(1) was built with the extra math option, it accepts numbers +in scientific notation. For dc(1), an example is `1.89237e9`, which is equal to +`1892370000`. Negative exponents are also allowed, so `4.2890e_3` is equal to +`0.0042890`. + +**WARNING**: Both the number and the exponent in scientific notation are +interpreted according to the current `ibase`, but the number is still multiplied +by `10^exponent` regardless of the current `ibase`. For example, if `ibase` is +`16` and dc(1) is given the number string `"FFeA"`, the resulting decimal number +will be `2550000000000`, and if dc(1) is given the number string `"10e_4"`, the +resulting decimal number will be `0.0016`. + +Accepting input as scientific notation is a **non-portable extension**. + +COMMANDS +-------- + +The valid commands are listed below. + +### Printing + +These commands are used for printing. + +Note that if dc(1) has been built with the extra math option enabled, both +scientific notation and engineering notation are available for printing numbers. +Scientific notation is activated by assigning `0` to `obase` using `0o` (in any +other context, an `obase` of `0` is invalid), and engineering notation is +activated by assigning `1` to `obase` using `1o` (which is also invalid in any +other context). To deactivate them, just assign a different value to `obase`. + +Printing numbers in scientific notation and/or engineering notation is a +**non-portable extension**. + + * `p`: + Prints the value on top of the stack, whether number or string, and prints a + newline after. + + This does not alter the stack. + + * `n`: + Prints the value on top of the stack, whether number or string, and pops it + off of the stack. + + * `P`: + Pops a value off the stack. + + If the value is a number, it is truncated and the result's absolute value is + printed as though `obase` is `UCHAR_MAX + 1` and each digit is interpreted + as an ASCII character, making it a byte stream. + + If the value is a string, it is printed without a trailing newline. + + This is a **non-portable extension**. + + * `f`: + Prints the entire contents of the stack, in order from newest to oldest, + without altering anything. + + Users should use this command when they get lost. + +### Arithmetic + +These are the commands used for arithmetic. + + * `+`: + The top two values are popped off the stack, added, and the result is pushed + onto the stack. The result's **scale** is equal to the max **scale** of both + operands. + + * `-`: + The top two values are popped off the stack, subtracted, and the result is + pushed onto the stack. The result's **scale** is equal to the max **scale** + of both operands. + + * `*`: + The top two values are popped off the stack, multiplied, and the result is + pushed onto the stack. If `a` is the **scale** of the first expression and + `b` is the **scale** of the second expression, the **scale** of the result + is equal to `min(a+b,max(scale,a,b))` where `min` and `max` return the + obvious values. + + * `/`: + The top two values are popped off the stack, divided, and the result is + pushed onto the stack. The result's **scale** is equal to `scale`. + + * `%`: + The top two values are popped off the stack, remaindered, and the result is + pushed onto the stack. + + Remaindering is equivalent to 1) Computing `a/b` to current `scale`, and 2) + Using the result of step 1 to calculate `a-(a/b)*b` to **scale** + `max(scale + scale(b), scale(a))`. + + * `~`: + The top two values are popped off the stack, divided and remaindered, and + the results (divided first, remainder second) are pushed onto the stack. + This is equivalent to `x y / x y %` except that `x` and `y` are only + evaluated once. + + This is a **non-portable extension**. + + * `^`: + The top two values are popped off the stack, the second is raised to the + power of the first, and the result is pushed onto the stack. + + The first value popped off the stack must be an integer. + + * `v`: + The top value is popped off the stack, its square root is computed, and the + result is pushed onto the stack. The result's **scale** is equal to `scale`. + + * `_`: + If this command *immediately* precedes a number (i.e., no spaces or other + commands), then that number is input as a negative number. + + Otherwise, the top value on the stack is popped and copied, and the copy is + negated and pushed onto the stack. This behavior without a number is a + **non-portable extension**. + + * `b`: + The top value is popped off the stack and its absolute value is pushed onto + the stack. + + This is a **non-portable extension**. + + * `|`: + The top three values are popped off the stack, a modular exponentiation is + computed, and the result is pushed onto the stack. + + The first value popped is used as the reduction modulus and must be an + integer and non-zero. The second value popped is used as the exponent and + must be an integer and non-negative. The third value popped is the base and + must be an integer. + + This is a **non-portable extension**. + + * `$`: + The top value is popped off the stack and copied, and the copy is truncated + and pushed onto the stack. + + This is a **non-portable extension**. + + * `@`: + The top two values are popped off the stack, and the second's precision is + set to the value of the first, whether by truncation or extension. + + The first value must be an integer and non-negative. + + This is a **non-portable extension**. + + * `H`: + The top two values are popped off the stack, and the second is shifted left + (radix shifted right) to the value of the first. + + The first value must be an integer and non-negative. + + This is a **non-portable extension**. + + * `h`: + The top two values are popped off the stack, and the second is shifted right + (radix shifted left) to the value of the first. + + The first value must be an integer and non-negative. + + This is a **non-portable extension**. + + * `G`: + The top two values are popped off of the stack, they are compared, and a `1` + is pushed if they are equal, or `0` otherwise. + + This is a **non-portable extension**. + + * `N`: + The top value is popped off of the stack, and if it a `0`, a `1` is pushed; + otherwise, a `0` is pushed. + + This is a **non-portable extension**. + + * `(`: + The top two values are popped off of the stack, they are compared, and a `1` + is pushed if the first is less than the second, or `0` otherwise. + + This is a **non-portable extension**. + + * `{`: + The top two values are popped off of the stack, they are compared, and a `1` + is pushed if the first is less than or equal to the second, or `0` + otherwise. + + This is a **non-portable extension**. + + * `)`: + The top two values are popped off of the stack, they are compared, and a `1` + is pushed if the first is greater than the second, or `0` otherwise. + + This is a **non-portable extension**. + + * `}`: + The top two values are popped off of the stack, they are compared, and a `1` + is pushed if the first is greater than or equal to the second, or `0` + otherwise. + + This is a **non-portable extension**. + +### Stack Control + +These commands control the stack. + + * `c`: + Removes all items from ("clears") the stack. + + * `d`: + Copies the item on top of the stack ("duplicates") and pushes the copy onto + the stack. + + * `r`: + Swaps ("reverses") the two top items on the stack. + + * `R`: + Pops ("removes") the top value from the stack. + +### Register Control + +These commands control registers (see the REGISTERS section). + + * `s`*r*: + Pops the value off the top of the stack and stores it into register `r`. + + * `l`*r*: + Copies the value in register `r` and pushes it onto the stack. This does not + alter the contents of `r`. + + * `S`*r*: + Pops the value off the top of the (main) stack and pushes it onto the stack + of register `r`. The previous value of the register becomes inaccessible. + + * `L`*r*: + Pops the value off the top of register `r`'s stack and push it onto the main + stack. The previous value in register `r`'s stack, if any, is now accessible + via the `l`*r* command. + +### Parameters + +These commands control the values of `ibase`, `obase`, and `scale` (see the +SYNTAX section). + + * `i`: + Pops the value off of the top of the stack and uses it to set `ibase`, which + must be between `2` and `16`, inclusive. + + If the value on top of the stack has any **scale**, the **scale** is + ignored. + + * `o`: + Pops the value off of the top of the stack and uses it to set `obase`, which + must be between `2` and `DC_BASE_MAX`, inclusive (see bc(1)). The value can + be either `0` or `1` if dc(1) was built with the extra math option. + + If the value on top of the stack has any **scale**, the **scale** is + ignored. + + * `k`: + Pops the value off of the top of the stack and uses it to set `scale`, which + must be non-negative. + + If the value on top of the stack has any **scale**, the **scale** is + ignored. + + * `I`: + Pushes the current value of `ibase` onto the main stack. + + * `O`: + Pushes the current value of `obase` onto the main stack. + + * `K`: + Pushes the current value of `scale` onto the main stack. + + * `T`: + Pushes the maximum allowable value of `ibase` onto the main stack. + + This is a **non-portable extension**. + + * `U`: + Pushes the maximum allowable value of `obase` onto the main stack. + + This is a **non-portable extension**. + + * `V`: + Pushes the maximum allowable value of `scale` onto the main stack. + + This is a **non-portable extension**. + +### Strings + +The following commands control strings. + +dc(1) can work with both numbers and strings, and registers (see the REGISTERS +section) can hold both strings and numbers. dc(1) always knows whether a +register's contents are a string or a number. + +While arithmetic operations have to have numbers, and will print an error if +given a string, other commands accept strings. + +Strings can also be executed as macros. For example, if the string `[1pR]` is +executed as a macro, then the code `1pR` is executed, meaning that the `1` will +be printed with a newline after and then popped from the stack. + + * `[`*characters*`]`: + Makes a string containing `characters` and pushes it onto the stack. + + If there are brackets (`[` and `]`) in the string, then they must be + balanced. Unbalanced brackets can be escaped using a backslash (`\`) + character. + + If there is a backslash character in the string, the character after it + (even another backslash) is put into the string verbatim, but the (first) + backslash is not. + + * `a`: + The value on top of the stack is popped. + + If it is a number, it is truncated and its absolute value is taken. The + result mod `UCHAR_MAX + 1` is calculated. If that result is `0`, push an + empty string; otherwise, push a one-character string where the character is + the result of the mod interpreted as an ASCII character. + + If it is a string, then a new string is made. If the original string is + empty, the new string is empty. If it is not, then the first character of + the original string is used to create the new string as a one-character + string. The new string is then pushed onto the stack. + + This is a **non-portable extension**. + + * `x`: + Pops a value off of the top of the stack. + + If it is a number, it is pushed onto the stack. + + If it is a string, it is executed as a macro. + + This behavior is the norm whenever a macro is executed, whether by this + command or by the conditional execution commands below. + + * `>`*r*: + Pops two values off of the stack that must be numbers and compares them. If + the first value is greater than the second, then the contents of register + `r` are executed. + + For example, `0 1>a` will execute the contents of register `a`, and `1 0>a` + will not. + + * `>`*r*`e`*s*: + Like the above, but will execute register `s` if the comparison fails. + + This is a **non-portable extension**. + + * `!>`*r*: + Pops two values off of the stack that must be numbers and compares them. If + the first value is not greater than the second (less than or equal to), then + the contents of register `r` are executed. + + * `!>`*r*`e`*s*: + Like the above, but will execute register `s` if the comparison fails. + + This is a **non-portable extension**. + + * `<`*r*: + Pops two values off of the stack that must be numbers and compares them. If + the first value is less than the second, then the contents of register `r` + are executed. + + * `<`*r*`e`*s*: + Like the above, but will execute register `s` if the comparison fails. + + This is a **non-portable extension**. + + * `!<`*r*: + Pops two values off of the stack that must be numbers and compares them. If + the first value is not less than the second (greater than or equal to), then + the contents of register `r` are executed. + + * `!<`*r*`e`*s*: + Like the above, but will execute register `s` if the comparison fails. + + This is a **non-portable extension**. + + * `=`*r*: + Pops two values off of the stack that must be numbers and compares them. If + the first value is equal to the second (greater than or equal to), then the + contents of register `r` are executed. + + * `=`*r*`e`*s*: + Like the above, but will execute register `s` if the comparison fails. + + This is a **non-portable extension**. + + * `!=`*r*: + Pops two values off of the stack that must be numbers and compares them. If + the first value is not equal to the second (greater than or equal to), then + the contents of register `r` are executed. + + * `!=`*r*`e`*s*: + Like the above, but will execute register `s` if the comparison fails. + + This is a **non-portable extension**. + + * `?`: + Reads a line from the `stdin` and executes it. This is to allow macros to + request input from users. + + * `q`: + During execution of a macro, this exits that macro's execution and the + execution of the macro that executed it. If there are no macros, or only one + macro executing, dc(1) exits. + + * `Q`: + Pops a value from the stack which must be non-negative and is used the + number of macro executions to pop off of the execution stack. If the number + of levels to pop is greater than the number of executing macros, dc(1) + exits. + +### Status + +These commands query status of the stack or its top value. + + * `Z`: + Pops a value off of the stack. + + If it is a number, calculates the number of significant decimal digits it + has and pushes the result. + + If it is a string, pushes the number of characters the string has. + + * `X`: + Pops a value off of the stack. + + If it is a number, pushes the **scale** of the value onto the stack. + + If it is a string, pushes `0`. + + * `z`: + Pushes the current stack depth (before execution of this command). + +### Arrays + +These commands manipulate arrays. + + * `:`*r*: + Pops the top two values off of the stack. The second value will be stored in + the array `r` (see the REGISTERS section), indexed by the first value. + + * `;`*r*: + Pops the value on top of the stack and uses it as an index into the array + `r`. The selected value is then pushed onto the stack. + +REGISTERS +--------- + +Registers are names that can store strings, numbers, and arrays. (Number/string +registers do not interfere with array registers.) + +Each register is also its own stack, so the current register value is the top of +the register's stack. All registers, when first referenced, have one value (`0`) +in their stack. + +In non-extended register mode, a register name is just the single character that +follows any command that needs a register name. The only exception is a newline +(`'\n'`); it is a parse error for a newline to be used as a register name. + +### Extended Register Mode + +Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited +amounts of registers, if extended register mode is enabled. + +If extended register mode is enabled (`-x` or `--extended-register` command-line +arguments are given), then normal single character registers are used +***unless*** the character immediately following a command that needs a register +name is a space (according to `isspace()`) and not a newline (`'\n'`). + +In that case, the register name is found according to the regex +`[a-z][a-z0-9_]*` (like bc(1)), and it is a parse error if the next +non-space characters do not match that regex. + +RESET +----- + +When dc(1) encounters an error or a signal that it has a non-default handler +for, it resets. This means that several things happen. + +First, any macros that are executing are stopped and popped off the stack. +The behavior is not unlike that of exceptions in programming languages. Then +the execution point is set so that any code waiting to execute (after all +functions returned) is skipped. + +Thus, when dc(1) resets, it skips any remaining code waiting to be executed. +Then, if it is interactive mode, and the error was not a fatal error (see the +EXIT STATUS section), it asks for more input; otherwise, it exits with the +appropriate return code. + +PERFORMANCE +----------- + +Most dc(1) implementations use `char` types to calculate the value of `1` +decimal digit at a time, but that can be slow. This dc(1) does something +different. + +It uses large integers to calculate more than `1` decimal digit at a time. If +built in a environment where `DC_LONG_BIT` (see the LIMITS section) is `64`, +then each integer has `9` decimal digits. If built in an environment where +`DC_LONG_BIT` is `32` then each integer has `4` decimal digits. This value (the +number of decimal digits per large integer) is called `DC_BASE_DIGS`. + +In addition, this dc(1) uses an even larger integer for overflow checking. This +integer type depends on the value of `DC_LONG_BIT`, but is always at least twice +as large as the integer type used to store digits. + +LIMITS +------ + +The following are the limits on dc(1): + + * `DC_LONG_BIT`: + The number of bits in the `long` type in the environment where dc(1) was + built. This determines how many decimal digits can be stored in a single + large integer (see the PERFORMANCE section). + + * `DC_BASE_DIGS`: + The number of decimal digits per large integer (see the PERFORMANCE + section). Depends on `DC_LONG_BIT`. + + * `DC_BASE_POW`: + The max decimal number that each large integer can store (see + `DC_BASE_DIGS`) plus `1`. Depends on `DC_BASE_DIGS`. + + * `DC_OVERFLOW_MAX`: + The max number that the overflow type (see the PERFORMANCE section) can + hold. Depends on `DC_LONG_BIT`. + + * `DC_BASE_DIGS`: + The number of decimal digits per large integer (see the PERFORMANCE + section). + + * `DC_BASE_MAX`: + The maximum output base. Set at `DC_BASE_POW`. + + * `DC_DIM_MAX`: + The maximum size of arrays. Set at `SIZE_MAX-1`. + + * `DC_SCALE_MAX`: + The maximum `scale`. Set at `DC_OVERFLOW_MAX-1`. + + * `DC_STRING_MAX`: + The maximum length of strings. Set at `DC_OVERFLOW_MAX-1`. + + * `DC_NAME_MAX`: + The maximum length of identifiers. Set at `DC_OVERFLOW_MAX-1`. + + * `DC_NUM_MAX`: + The maximum length of a number (in decimal digits), which includes digits + after the decimal point. Set at `DC_OVERFLOW_MAX-1`. + + * Exponent: + The maximum allowable exponent (positive or negative). Set at + `DC_OVERFLOW_MAX`. + + * Number of vars: + The maximum number of vars/arrays. Set at `SIZE_MAX-1`. + +These limits are meant to be effectively non-existent; the limits are so large +(at least on 64-bit machines) that there should not be any point at which they +become a problem. In fact, memory should be exhausted before these limits should +be hit. + +ENVIRONMENT VARIABLES +--------------------- + +dc(1) recognizes the following environment variables: + + * `DC_ENV_ARGS`: + This is another way to give command-line arguments to dc(1). They should be + in the same format as all other command-line arguments. These are always + processed first, so any files given in `DC_ENV_ARGS` will be processed + before files given on the command-line. This gives the user the ability to + set up "standard" options and files to be used at every invocation. The most + useful thing for such files to contain would be useful functions that the + user might want every time dc(1) runs. Another use would be to use the `-e` + option to set `scale` to a value other than `0`. + + * `DC_LINE_LENGTH`: + If this environment variable exists and contains an integer that is greater + than `1` and is less than `UINT16_MAX` (`2^16-1`), dc(1) will output lines + to that length, including the backslash newline combo. The default line + length is `70`. + + * `DC_EXPR_EXIT`: + If this variable exists (no matter the contents), dc(1) will exit + immediately after executing expressions and files given by the `-e` and/or + `-f` command-line options (and any equivalents). + +EXIT STATUS +----------- + +dc(1) returns the following exit statuses: + + * `0`: + No error. + + * `1`: + A math error occurred. This follows standard practice of using `1` for + expected errors, since math errors will happen in the process of normal + execution. + + Math errors include divide by `0`, taking the square root of a negative + number, attempting to convert a negative number to a hardware integer, + overflow when converting a number to a hardware integer, and attempting to + use a non-integer where an integer is required. + + Converting to a hardware integer happens for the second operand of the power + (`^`), places (`@`), left shift (`H`), and right shift (`h`) operators. + + * `2`: + A parse error occurred. + + Parse errors include unexpected `EOF`, using an invalid character, failing + to find the end of a string or comment, and using a token where it's + invalid. + + * `3`: + A runtime error occurred. + + Runtime errors include assigning an invalid number to `ibase`, `obase`, or + `scale`; give a bad expression to a `read()` call, calling `read()` inside + of a `read()` call, type errors, and attempting an operation when the stack + has too few elements. + + * `4`: + A fatal error occurred. + + Fatal errors include memory allocation errors, I/O errors, failing to open + files, attempting to use files that do not have only ASCII characters (dc(1) + only accepts ASCII characters), attempting to open a directory as a file, + and giving invalid command-line options. + +The exit status `4` is special; when a fatal error occurs, dc(1) always exits +and returns `4`, no matter what mode dc(1) is in. + +The other statuses will only be returned when dc(1) is not in interactive mode, +since dc(1) resets its state (see the RESET section) and accepts more input when +one of those errors occurs in interactive mode. This is also the case when +interactive mode is forced by the `-i` option. + +These exit statuses allow dc(1) to be used in shell scripting with error +checking, and its normal behavior can be forced by using `-i`. + +SIGNAL HANDLING +--------------- + +If dc(1) has been compiled with the signal handling, sending a `SIGINT` will +cause dc(1) to stop execution of the current input and reset (see the RESET +section), asking for more input. + +Otherwise, `SIGTERM` and `SIGQUIT` cause dc(1) to clean up and exit, and it uses +the default handler for all other signals. + +If dc(1) has not been compiled with signal handling, it uses the default signal +handlers for all signals. + +COMMAND LINE HISTORY +-------------------- + +dc(1) supports interactive command-line editing, if compiled with the history +option enabled. If `stdin` is hooked to a terminal, it is enabled. Previous +lines can be recalled and edited with the arrow keys. + +LOCALES +------- + +This dc(1) ships with support for adding error messages for different locales. + +SEE ALSO +-------- + +bc(1) + +STANDARDS +--------- + +The dc(1) utility operators are compliant with the operators in the bc(1) +[IEEE Std 1003.1-2017 (“POSIX.1-2017”)][1] specification. + +AUTHOR +------ + +This dc(1) was made from scratch by Gavin D. Howard. + +BUGS +---- + +None are known. Report bugs at https://github.com/gavinhoward/bc. + +[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html Index: contrib/bc/manuals/dc.md =================================================================== --- contrib/bc/manuals/dc.md +++ contrib/bc/manuals/dc.md @@ -0,0 +1 @@ +link dc.1.ronn \ No newline at end of file Index: contrib/bc/src/args.c =================================================================== --- contrib/bc/src/args.c +++ contrib/bc/src/args.c @@ -0,0 +1,230 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code for processing command-line arguments. + * + */ + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +static const struct option bc_args_lopt[] = { + + { "expression", required_argument, NULL, 'e' }, + { "file", required_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "interactive", no_argument, NULL, 'i' }, + { "no-prompt", no_argument, NULL, 'P' }, +#if BC_ENABLED + { "global-stacks", no_argument, NULL, 'g' }, + { "mathlib", no_argument, NULL, 'l' }, + { "quiet", no_argument, NULL, 'q' }, + { "standard", no_argument, NULL, 's' }, + { "warn", no_argument, NULL, 'w' }, +#endif // BC_ENABLED + { "version", no_argument, NULL, 'v' }, +#if DC_ENABLED + { "extended-register", no_argument, NULL, 'x' }, +#endif // DC_ENABLED + { 0, 0, 0, 0 }, + +}; + +#if !BC_ENABLED +static const char* const bc_args_opt = "e:f:hiPvVx"; +#elif !DC_ENABLED +static const char* const bc_args_opt = "e:f:ghilPqsvVw"; +#else // BC_ENABLED && DC_ENABLED +static const char* const bc_args_opt = "e:f:ghilPqsvVwx"; +#endif // BC_ENABLED && DC_ENABLED + +static void bc_args_exprs(BcVec *exprs, const char *str) { + bc_vec_concat(exprs, str); + bc_vec_concat(exprs, "\n"); +} + +static BcStatus bc_args_file(BcVec *exprs, const char *file) { + + BcStatus s; + char *buf; + + vm->file = file; + + s = bc_read_file(file, &buf); + if (BC_ERR(s)) return s; + + bc_args_exprs(exprs, buf); + free(buf); + + return s; +} + +BcStatus bc_args(int argc, char *argv[]) { + + BcStatus s = BC_STATUS_SUCCESS; + int c, i, err = 0; + bool do_exit = false, version = false; + + i = optind = 0; + + while ((c = getopt_long(argc, argv, bc_args_opt, bc_args_lopt, &i)) != -1) { + + switch (c) { + + case 0: + { + // This is the case when a long option is found. + break; + } + + case 'e': + { + bc_args_exprs(&vm->exprs, optarg); + break; + } + + case 'f': + { + s = bc_args_file(&vm->exprs, optarg); + if (BC_ERR(s)) return s; + break; + } + + case 'h': + { + bc_vm_info(vm->help); + do_exit = true; + break; + } + + case 'i': + { + vm->flags |= BC_FLAG_I; + break; + } + + case 'P': + { + vm->flags |= BC_FLAG_P; + break; + } + +#if BC_ENABLED + case 'g': + { + if (BC_ERR(!BC_IS_BC)) err = c; + vm->flags |= BC_FLAG_G; + break; + } + + case 'l': + { + if (BC_ERR(!BC_IS_BC)) err = c; + vm->flags |= BC_FLAG_L; + break; + } + + case 'q': + { + if (BC_ERR(!BC_IS_BC)) err = c; + vm->flags |= BC_FLAG_Q; + break; + } + + case 's': + { + if (BC_ERR(!BC_IS_BC)) err = c; + vm->flags |= BC_FLAG_S; + break; + } + + case 'w': + { + if (BC_ERR(!BC_IS_BC)) err = c; + vm->flags |= BC_FLAG_W; + break; + } +#endif // BC_ENABLED + + case 'V': + case 'v': + { + do_exit = version = true; + break; + } + +#if DC_ENABLED + case 'x': + { + if (BC_ERR(BC_IS_BC)) err = c; + vm->flags |= DC_FLAG_X; + break; + } +#endif // DC_ENABLED + + // Getopt printed an error message, but we should exit. + case '?': + default: + { + return BC_STATUS_ERROR_FATAL; + } + } + + if (BC_ERR(err)) { + + for (i = 0; bc_args_lopt[i].name; ++i) { + if (bc_args_lopt[i].val == err) break; + } + + return bc_vm_verr(BC_ERROR_FATAL_OPTION, err, bc_args_lopt[i].name); + } + } + + if (version) bc_vm_info(NULL); + if (do_exit) exit((int) s); + if (vm->exprs.len > 1 || !BC_IS_BC) vm->flags |= BC_FLAG_Q; + if (argv[optind] && !strcmp(argv[optind], "--")) ++optind; + + for (i = optind; i < argc; ++i) bc_vec_push(&vm->files, argv + i); + + return s; +} Index: contrib/bc/src/bc/bc.c =================================================================== --- contrib/bc/src/bc/bc.c +++ contrib/bc/src/bc/bc.c @@ -0,0 +1,63 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The main procedure of bc. + * + */ + +#if BC_ENABLED + +#include + +#include +#include +#include + +int bc_main(int argc, char **argv) { + + BcStatus s; + + vm->read_ret = BC_INST_RET; + vm->help = bc_help; +#if BC_ENABLE_SIGNALS + vm->sigmsg = bc_sig_msg; + vm->siglen = (uchar) strlen(vm->sigmsg); +#endif // BC_ENABLE_SIGNALS + + vm->next = bc_lex_token; + vm->parse = bc_parse_parse; + vm->expr = bc_parse_expr; + + s = bc_vm_boot(argc, argv, "BC_LINE_LENGTH", "BC_ENV_ARGS", "BC_EXPR_EXIT"); + + return (int) s; +} +#endif // BC_ENABLED Index: contrib/bc/src/bc/lex.c =================================================================== --- contrib/bc/src/bc/lex.c +++ contrib/bc/src/bc/lex.c @@ -0,0 +1,428 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The lexer for bc. + * + */ + +#if BC_ENABLED + +#include +#include +#include + +#include +#include +#include + +static BcStatus bc_lex_identifier(BcLex *l) { + + BcStatus s = BC_STATUS_SUCCESS; + size_t i; + const char *buf = l->buf + l->i - 1; + + for (i = 0; i < bc_lex_kws_len; ++i) { + + const BcLexKeyword *kw = bc_lex_kws + i; + size_t n = BC_LEX_KW_LEN(kw); + + if (!strncmp(buf, kw->name, n) && !isalnum(buf[n]) && buf[n] != '_') { + + l->t = BC_LEX_KW_AUTO + (BcLexType) i; + + if (!BC_LEX_KW_POSIX(kw)) { + s = bc_lex_verr(l, BC_ERROR_POSIX_KW, kw->name); + if (BC_ERR(s)) return s; + } + + // We minus 1 because the index has already been incremented. + l->i += n - 1; + return BC_STATUS_SUCCESS; + } + } + + bc_lex_name(l); + + if (BC_ERR(l->str.len - 1 > 1)) + s = bc_lex_verr(l, BC_ERROR_POSIX_NAME_LEN, l->str.v); + + return s; +} + +static BcStatus bc_lex_string(BcLex *l) { + + size_t len, nlines = 0, i = l->i; + const char *buf = l->buf; + char c; + + l->t = BC_LEX_STR; + + for (; (c = buf[i]) && c != '"'; ++i) nlines += c == '\n'; + + if (BC_ERR(c == '\0')) { + l->i = i; + return bc_lex_err(l, BC_ERROR_PARSE_STRING); + } + + len = i - l->i; + bc_vec_string(&l->str, len, l->buf + l->i); + + l->i = i + 1; + l->line += nlines; + + return BC_STATUS_SUCCESS; +} + +static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) { + if (l->buf[l->i] == '=') { + l->i += 1; + l->t = with; + } + else l->t = without; +} + +BcStatus bc_lex_token(BcLex *l) { + + BcStatus s = BC_STATUS_SUCCESS; + char c = l->buf[l->i++], c2; + + // This is the workhorse of the lexer. + switch (c) { + + case '\0': + case '\n': + case '\t': + case '\v': + case '\f': + case '\r': + case ' ': + { + bc_lex_commonTokens(l, c); + break; + } + + case '!': + { + bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); + + if (l->t == BC_LEX_OP_BOOL_NOT) { + s = bc_lex_verr(l, BC_ERROR_POSIX_BOOL, "!"); + if (BC_ERR(s)) return s; + } + + break; + } + + case '"': + { + s = bc_lex_string(l); + break; + } + + case '#': + { + s = bc_lex_err(l, BC_ERROR_POSIX_COMMENT); + if (BC_ERR(s)) return s; + + bc_lex_lineComment(l); + + break; + } + + case '%': + { + bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); + break; + } + + case '&': + { + c2 = l->buf[l->i]; + if (BC_LIKELY(c2 == '&')) { + + s = bc_lex_verr(l, BC_ERROR_POSIX_BOOL, "&&"); + if (BC_ERR(s)) return s; + + l->i += 1; + l->t = BC_LEX_OP_BOOL_AND; + } + else s = bc_lex_invalidChar(l, c); + + break; + } +#if BC_ENABLE_EXTRA_MATH + case '$': + { + l->t = BC_LEX_OP_TRUNC; + break; + } + + case '@': + { + bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLACES, BC_LEX_OP_PLACES); + break; + } +#endif // BC_ENABLE_EXTRA_MATH + case '(': + case ')': + { + l->t = (BcLexType) (c - '(' + BC_LEX_LPAREN); + break; + } + + case '*': + { + bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); + break; + } + + case '+': + { + c2 = l->buf[l->i]; + if (c2 == '+') { + l->i += 1; + l->t = BC_LEX_OP_INC; + } + else bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); + break; + } + + case ',': + { + l->t = BC_LEX_COMMA; + break; + } + + case '-': + { + c2 = l->buf[l->i]; + if (c2 == '-') { + l->i += 1; + l->t = BC_LEX_OP_DEC; + } + else bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); + break; + } + + case '.': + { + c2 = l->buf[l->i]; + if (BC_LEX_NUM_CHAR(c2, true, false)) s = bc_lex_number(l, c); + else { + l->t = BC_LEX_KW_LAST; + s = bc_lex_err(l, BC_ERROR_POSIX_DOT); + } + break; + } + + case '/': + { + c2 = l->buf[l->i]; + if (c2 =='*') s = bc_lex_comment(l); + else bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); + break; + } + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + // Apparently, GNU bc (and maybe others) allows any uppercase letter as + // a number. When single digits, they act like the ones above. When + // multi-digit, any letter above the input base is automatically set to + // the biggest allowable digit in the input base. + case 'G': + case 'H': + case 'I': + case 'J': + case 'K': + case 'L': + case 'M': + case 'N': + case 'O': + case 'P': + case 'Q': + case 'R': + case 'S': + case 'T': + case 'U': + case 'V': + case 'W': + case 'X': + case 'Y': + case 'Z': + { + s = bc_lex_number(l, c); + break; + } + + case ';': + { + l->t = BC_LEX_SCOLON; + break; + } + + case '<': + { +#if BC_ENABLE_EXTRA_MATH + c2 = l->buf[l->i]; + + if (c2 == '<') { + l->i += 1; + bc_lex_assign(l, BC_LEX_OP_ASSIGN_LSHIFT, BC_LEX_OP_LSHIFT); + break; + } +#endif // BC_ENABLE_EXTRA_MATH + bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); + break; + } + + case '=': + { + bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); + break; + } + + case '>': + { +#if BC_ENABLE_EXTRA_MATH + c2 = l->buf[l->i]; + + if (c2 == '>') { + l->i += 1; + bc_lex_assign(l, BC_LEX_OP_ASSIGN_RSHIFT, BC_LEX_OP_RSHIFT); + break; + } +#endif // BC_ENABLE_EXTRA_MATH + bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); + break; + } + + case '[': + case ']': + { + l->t = (BcLexType) (c - '[' + BC_LEX_LBRACKET); + break; + } + + case '\\': + { + if (BC_LIKELY(l->buf[l->i] == '\n')) { + l->i += 1; + l->t = BC_LEX_WHITESPACE; + } + else s = bc_lex_invalidChar(l, c); + break; + } + + case '^': + { + bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); + break; + } + + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + case 'h': + case 'i': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'o': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'u': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + { + s = bc_lex_identifier(l); + break; + } + + case '{': + case '}': + { + l->t = (BcLexType) (c - '{' + BC_LEX_LBRACE); + break; + } + + case '|': + { + c2 = l->buf[l->i]; + + if (BC_LIKELY(c2 == '|')) { + + s = bc_lex_verr(l, BC_ERROR_POSIX_BOOL, "||"); + if (BC_ERR(s)) return s; + + l->i += 1; + l->t = BC_LEX_OP_BOOL_OR; + } + else s = bc_lex_invalidChar(l, c); + + break; + } + + default: + { + s = bc_lex_invalidChar(l, c); + break; + } + } + + return s; +} +#endif // BC_ENABLED Index: contrib/bc/src/bc/parse.c =================================================================== --- contrib/bc/src/bc/parse.c +++ contrib/bc/src/bc/parse.c @@ -0,0 +1,1577 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The parser for bc. + * + */ + +#if BC_ENABLED + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +static BcStatus bc_parse_else(BcParse *p); +static BcStatus bc_parse_stmt(BcParse *p); +static BcStatus bc_parse_expr_err(BcParse *p, uint8_t flags, BcParseNext next); + +static bool bc_parse_inst_isLeaf(BcInst t) { + return (t >= BC_INST_NUM && t <= BC_INST_MAXSCALE) || +#if BC_ENABLE_EXTRA_MATH + t == BC_INST_TRUNC || +#endif // BC_ENABLE_EXTRA_MATH + t == BC_INST_INC_POST || t == BC_INST_DEC_POST; +} + +static bool bc_parse_isDelimiter(BcParse *p) { + + BcLexType t = p->l.t; + bool good = false; + + if (BC_PARSE_DELIMITER(t)) return true; + + if (t == BC_LEX_KW_ELSE) { + + size_t i; + uint16_t *fptr = NULL, flags = BC_PARSE_FLAG_ELSE; + + for (i = 0; i < p->flags.len && BC_PARSE_BLOCK_STMT(flags); ++i) { + + fptr = bc_vec_item_rev(&p->flags, i); + flags = *fptr; + + if ((flags & BC_PARSE_FLAG_BRACE) && p->l.last != BC_LEX_RBRACE) + return false; + } + + good = ((flags & BC_PARSE_FLAG_IF) != 0); + } + else if (t == BC_LEX_RBRACE) { + + size_t i; + + for (i = 0; !good && i < p->flags.len; ++i) { + uint16_t *fptr = bc_vec_item_rev(&p->flags, i); + good = (((*fptr) & BC_PARSE_FLAG_BRACE) != 0); + } + } + + return good; +} + +static void bc_parse_setLabel(BcParse *p) { + + BcFunc *func = p->func; + BcInstPtr *ip = bc_vec_top(&p->exits); + size_t *label; + + assert(func == bc_vec_item(&p->prog->fns, p->fidx)); + + label = bc_vec_item(&func->labels, ip->idx); + *label = func->code.len; + + bc_vec_pop(&p->exits); +} + +static void bc_parse_createLabel(BcParse *p, size_t idx) { + bc_vec_push(&p->func->labels, &idx); +} + +static void bc_parse_createCondLabel(BcParse *p, size_t idx) { + bc_parse_createLabel(p, p->func->code.len); + bc_vec_push(&p->conds, &idx); +} + +static void bc_parse_createExitLabel(BcParse *p, size_t idx, bool loop) { + + BcInstPtr ip; + + assert(p->func == bc_vec_item(&p->prog->fns, p->fidx)); + + ip.func = loop; + ip.idx = idx; + ip.len = 0; + + bc_vec_push(&p->exits, &ip); + bc_parse_createLabel(p, SIZE_MAX); +} + +static void bc_parse_operator(BcParse *p, BcLexType type, + size_t start, size_t *nexprs) +{ + BcLexType t; + uchar l, r = BC_PARSE_OP_PREC(type); + uchar left = BC_PARSE_OP_LEFT(type); + + while (p->ops.len > start) { + + t = BC_PARSE_TOP_OP(p); + if (t == BC_LEX_LPAREN) break; + + l = BC_PARSE_OP_PREC(t); + if (l >= r && (l != r || !left)) break; + + bc_parse_push(p, BC_PARSE_TOKEN_INST(t)); + bc_vec_pop(&p->ops); + *nexprs -= !BC_PARSE_OP_PREFIX(t); + } + + bc_vec_push(&p->ops, &type); +} + +static BcStatus bc_parse_rightParen(BcParse *p, size_t *nexs) { + + BcLexType top; + + while ((top = BC_PARSE_TOP_OP(p)) != BC_LEX_LPAREN) { + bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); + bc_vec_pop(&p->ops); + *nexs -= !BC_PARSE_OP_PREFIX(top); + } + + bc_vec_pop(&p->ops); + + return bc_lex_next(&p->l); +} + +static BcStatus bc_parse_params(BcParse *p, uint8_t flags) { + + BcStatus s; + bool comma = false; + size_t nparams; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + for (nparams = 0; p->l.t != BC_LEX_RPAREN; ++nparams) { + + flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); + flags |= (BC_PARSE_ARRAY | BC_PARSE_NEEDVAL); + + s = bc_parse_expr_status(p, flags, bc_parse_next_param); + if (BC_ERR(s)) return s; + + comma = p->l.t == BC_LEX_COMMA; + if (comma) { + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + } + + if (BC_ERR(comma)) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + bc_parse_push(p, BC_INST_CALL); + bc_parse_pushIndex(p, nparams); + + return BC_STATUS_SUCCESS; +} + +static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags) { + + BcStatus s; + BcId id; + size_t idx; + + id.name = name; + + s = bc_parse_params(p, flags); + if (BC_ERR(s)) goto err; + + // We just assert this because bc_parse_params() should + // ensure that the next token is what it should be. + assert(p->l.t == BC_LEX_RPAREN); + + // We cannot use bc_program_insertFunc() here + // because it will overwrite an existing function. + idx = bc_map_index(&p->prog->fn_map, &id); + + if (idx == BC_VEC_INVALID_IDX) { + + idx = bc_program_insertFunc(p->prog, name); + assert(idx != BC_VEC_INVALID_IDX); + + // Make sure that this pointer was not invalidated. + p->func = bc_vec_item(&p->prog->fns, p->fidx); + } + else { + free(name); + idx = ((BcId*) bc_vec_item(&p->prog->fn_map, idx))->idx; + } + + bc_parse_pushIndex(p, idx); + + return bc_lex_next(&p->l); + +err: + free(name); + return s; +} + +static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags) { + + BcStatus s; + char *name; + + name = bc_vm_strdup(p->l.str.v); + s = bc_lex_next(&p->l); + if (BC_ERR(s)) goto err; + + if (p->l.t == BC_LEX_LBRACKET) { + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) goto err; + + if (p->l.t == BC_LEX_RBRACKET) { + + if (BC_ERR(!(flags & BC_PARSE_ARRAY))) { + s = bc_parse_err(p, BC_ERROR_PARSE_EXPR); + goto err; + } + + *type = BC_INST_ARRAY; + } + else { + + *type = BC_INST_ARRAY_ELEM; + + flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); + flags |= BC_PARSE_NEEDVAL; + + s = bc_parse_expr_status(p, flags, bc_parse_next_elem); + if (BC_ERR(s)) goto err; + + if (BC_ERR(p->l.t != BC_LEX_RBRACKET)) { + s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + goto err; + } + } + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) goto err; + + bc_parse_push(p, *type); + bc_parse_pushName(p, name, false); + } + else if (p->l.t == BC_LEX_LPAREN) { + + if (BC_ERR(flags & BC_PARSE_NOCALL)) { + s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + goto err; + } + + *type = BC_INST_CALL; + + // Return early because bc_parse_call() frees the name. + return bc_parse_call(p, name, flags); + } + else { + *type = BC_INST_VAR; + bc_parse_push(p, BC_INST_VAR); + bc_parse_pushName(p, name, true); + } + +err: + free(name); + return s; +} + +static BcStatus bc_parse_noArgBuiltin(BcParse *p, BcInst inst) { + + BcStatus s; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_LPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + if ((p->l.t != BC_LEX_RPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + bc_parse_push(p, inst); + + return bc_lex_next(&p->l); +} + +static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, + uint8_t flags, BcInst *prev) +{ + BcStatus s; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_LPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); + flags |= BC_PARSE_NEEDVAL; + if (type == BC_LEX_KW_LENGTH) flags |= BC_PARSE_ARRAY; + + s = bc_parse_expr_status(p, flags, bc_parse_next_rel); + if (BC_ERR(s)) return s; + + if (BC_ERR(p->l.t != BC_LEX_RPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + *prev = type - BC_LEX_KW_LENGTH + BC_INST_LENGTH; + bc_parse_push(p, *prev); + + return bc_lex_next(&p->l); +} + +static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) { + + BcStatus s; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (p->l.t != BC_LEX_LPAREN) { + *type = BC_INST_SCALE; + bc_parse_push(p, BC_INST_SCALE); + return BC_STATUS_SUCCESS; + } + + *type = BC_INST_SCALE_FUNC; + flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); + flags |= BC_PARSE_NEEDVAL; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + s = bc_parse_expr_status(p, flags, bc_parse_next_rel); + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_RPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + bc_parse_push(p, BC_INST_SCALE_FUNC); + + return bc_lex_next(&p->l); +} + +static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, + size_t *nexs, uint8_t flags) +{ + BcStatus s; + BcLexType type; + uchar inst; + BcInst etype = *prev; + BcLexType last = p->l.last; + + if (BC_ERR(last == BC_LEX_OP_INC || last == BC_LEX_OP_DEC || + last == BC_LEX_RPAREN)) + { + return s = bc_parse_err(p, BC_ERROR_PARSE_ASSIGN); + } + + if (BC_PARSE_INST_VAR(etype)) { + *prev = inst = BC_INST_INC_POST + (p->l.t != BC_LEX_OP_INC); + bc_parse_push(p, inst); + s = bc_lex_next(&p->l); + } + else { + + *prev = inst = BC_INST_INC_PRE + (p->l.t != BC_LEX_OP_INC); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + type = p->l.t; + + // Because we parse the next part of the expression + // right here, we need to increment this. + *nexs = *nexs + 1; + + if (type == BC_LEX_NAME) + s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL); + else if (type >= BC_LEX_KW_LAST && type <= BC_LEX_KW_OBASE) { + bc_parse_push(p, type - BC_LEX_KW_LAST + BC_INST_LAST); + s = bc_lex_next(&p->l); + } + else if (BC_LIKELY(type == BC_LEX_KW_SCALE)) { + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (BC_ERR(p->l.t == BC_LEX_LPAREN)) + s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + else bc_parse_push(p, BC_INST_SCALE); + } + else s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + if (BC_NO_ERR(!s)) bc_parse_push(p, inst); + } + + return s; +} + +static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, + bool rparen, bool binlast, size_t *nexprs) +{ + BcStatus s; + BcLexType type; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + type = BC_PARSE_LEAF(*prev, binlast, rparen) ? BC_LEX_OP_MINUS : BC_LEX_NEG; + *prev = BC_PARSE_TOKEN_INST(type); + + // We can just push onto the op stack because this is the largest + // precedence operator that gets pushed. Inc/dec does not. + if (type != BC_LEX_OP_MINUS) bc_vec_push(&p->ops, &type); + else bc_parse_operator(p, type, ops_bgn, nexprs); + + return s; +} + +static BcStatus bc_parse_str(BcParse *p, char inst) { + bc_parse_string(p); + bc_parse_push(p, inst); + return bc_lex_next(&p->l); +} + +static BcStatus bc_parse_print(BcParse *p) { + + BcStatus s; + BcLexType t; + bool comma = false; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + t = p->l.t; + + if (bc_parse_isDelimiter(p)) return bc_parse_err(p, BC_ERROR_PARSE_PRINT); + + do { + if (t == BC_LEX_STR) s = bc_parse_str(p, BC_INST_PRINT_POP); + else { + s = bc_parse_expr_status(p, BC_PARSE_NEEDVAL, bc_parse_next_print); + if (BC_NO_ERR(!s)) bc_parse_push(p, BC_INST_PRINT_POP); + } + + if (BC_ERR(s)) return s; + + comma = (p->l.t == BC_LEX_COMMA); + + if (comma) s = bc_lex_next(&p->l); + else { + if (!bc_parse_isDelimiter(p)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + else break; + } + + t = p->l.t; + } while (BC_NO_ERR(!s)); + + if (BC_ERR(s)) return s; + if (BC_ERR(comma)) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + return s; +} + +static BcStatus bc_parse_return(BcParse *p) { + + BcStatus s; + BcLexType t; + bool paren; + uchar inst = BC_INST_RET0; + + if (BC_ERR(!BC_PARSE_FUNC(p))) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + if (p->func->voidfn) inst = BC_INST_RET_VOID; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + t = p->l.t; + paren = t == BC_LEX_LPAREN; + + if (bc_parse_isDelimiter(p)) bc_parse_push(p, inst); + else { + + s = bc_parse_expr_err(p, BC_PARSE_NEEDVAL, bc_parse_next_expr); + if (BC_ERR(s && s != BC_STATUS_EMPTY_EXPR)) return s; + else if (s == BC_STATUS_EMPTY_EXPR) { + bc_parse_push(p, inst); + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + + if (!paren || p->l.last != BC_LEX_RPAREN) { + s = bc_parse_err(p, BC_ERROR_POSIX_RET); + if (BC_ERR(s)) return s; + } + else if (BC_ERR(p->func->voidfn)) + return bc_parse_verr(p, BC_ERROR_PARSE_RET_VOID, p->func->name); + + bc_parse_push(p, BC_INST_RET); + } + + return s; +} + +static BcStatus bc_parse_endBody(BcParse *p, bool brace) { + + BcStatus s = BC_STATUS_SUCCESS; + bool has_brace, new_else = false; + + if (BC_ERR(p->flags.len <= 1)) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + if (brace) { + + assert(p->l.t == BC_LEX_RBRACE); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + if (BC_ERR(!bc_parse_isDelimiter(p))) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + } + + has_brace = (BC_PARSE_BRACE(p) != 0); + + do { + size_t len = p->flags.len; + bool loop; + + if (has_brace && !brace) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + loop = (BC_PARSE_LOOP_INNER(p) != 0); + + if (loop || BC_PARSE_ELSE(p)) { + + if (loop) { + + size_t *label = bc_vec_top(&p->conds); + + bc_parse_push(p, BC_INST_JUMP); + bc_parse_pushIndex(p, *label); + + bc_vec_pop(&p->conds); + } + + bc_parse_setLabel(p); + bc_vec_pop(&p->flags); + } + else if (BC_PARSE_FUNC_INNER(p)) { + BcInst inst = (p->func->voidfn ? BC_INST_RET_VOID : BC_INST_RET0); + bc_parse_push(p, inst); + bc_parse_updateFunc(p, BC_PROG_MAIN); + bc_vec_pop(&p->flags); + } + else if (BC_PARSE_BRACE(p) && !BC_PARSE_IF(p)) bc_vec_pop(&p->flags); + + // This needs to be last to parse nested if's properly. + if (BC_PARSE_IF(p) && (len == p->flags.len || !BC_PARSE_BRACE(p))) { + + while (p->l.t == BC_LEX_NLINE) { + s = bc_lex_next(&p->l); + if (s) return s; + } + + bc_vec_pop(&p->flags); + + if (!BC_S) { + + *(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_IF_END; + new_else = (p->l.t == BC_LEX_KW_ELSE); + + if (new_else) s = bc_parse_else(p); + else if (!has_brace && (!BC_PARSE_IF_END(p) || brace)) + bc_parse_noElse(p); + } + else bc_parse_noElse(p); + } + + if (brace && has_brace) brace = false; + + } while (p->flags.len > 1 && !new_else && (!BC_PARSE_IF_END(p) || brace) && + !(has_brace = (BC_PARSE_BRACE(p) != 0))); + + if (BC_NO_ERR(!s) && BC_ERR(p->flags.len == 1 && brace)) + s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + else if (brace && BC_PARSE_BRACE(p)) { + + uint16_t flags = BC_PARSE_TOP_FLAG(p); + + if (!(flags & (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_LOOP_INNER)) && + !(flags & (BC_PARSE_FLAG_IF | BC_PARSE_FLAG_ELSE)) && + !(flags & (BC_PARSE_FLAG_IF_END))) + { + bc_vec_pop(&p->flags); + } + } + + return s; +} + +static void bc_parse_startBody(BcParse *p, uint16_t flags) { + assert(flags); + flags |= (BC_PARSE_TOP_FLAG(p) & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP)); + flags |= BC_PARSE_FLAG_BODY; + bc_vec_push(&p->flags, &flags); +} + +void bc_parse_noElse(BcParse *p) { + uint16_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); + *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END)); + bc_parse_setLabel(p); +} + +static BcStatus bc_parse_if(BcParse *p) { + + BcStatus s; + size_t idx; + uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_LPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + s = bc_parse_expr_status(p, flags, bc_parse_next_rel); + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_RPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + bc_parse_push(p, BC_INST_JUMP_ZERO); + + idx = p->func->labels.len; + + bc_parse_pushIndex(p, idx); + bc_parse_createExitLabel(p, idx, false); + bc_parse_startBody(p, BC_PARSE_FLAG_IF); + + return BC_STATUS_SUCCESS; +} + +static BcStatus bc_parse_else(BcParse *p) { + + size_t idx = p->func->labels.len; + + if (BC_ERR(!BC_PARSE_IF_END(p))) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + bc_parse_push(p, BC_INST_JUMP); + bc_parse_pushIndex(p, idx); + + bc_parse_noElse(p); + + bc_parse_createExitLabel(p, idx, false); + bc_parse_startBody(p, BC_PARSE_FLAG_ELSE); + + return bc_lex_next(&p->l); +} + +static BcStatus bc_parse_while(BcParse *p) { + + BcStatus s; + size_t idx; + uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_LPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + bc_parse_createCondLabel(p, p->func->labels.len); + idx = p->func->labels.len; + bc_parse_createExitLabel(p, idx, true); + + s = bc_parse_expr_status(p, flags, bc_parse_next_rel); + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_RPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + bc_parse_push(p, BC_INST_JUMP_ZERO); + bc_parse_pushIndex(p, idx); + bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); + + return s; +} + +static BcStatus bc_parse_for(BcParse *p) { + + BcStatus s; + size_t cond_idx, exit_idx, body_idx, update_idx; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_LPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (p->l.t != BC_LEX_SCOLON) + s = bc_parse_expr_status(p, 0, bc_parse_next_for); + else s = bc_parse_err(p, BC_ERROR_POSIX_FOR); + + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_SCOLON)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + cond_idx = p->func->labels.len; + update_idx = cond_idx + 1; + body_idx = update_idx + 1; + exit_idx = body_idx + 1; + + bc_parse_createLabel(p, p->func->code.len); + + if (p->l.t != BC_LEX_SCOLON) { + uint8_t flags = (BC_PARSE_REL | BC_PARSE_NEEDVAL); + s = bc_parse_expr_status(p, flags, bc_parse_next_for); + } + else { + + // Set this for the next call to bc_parse_number. + // This is safe to set because the current token + // is a semicolon, which has no string requirement. + bc_vec_string(&p->l.str, strlen(bc_parse_const1), bc_parse_const1); + bc_parse_number(p); + + s = bc_parse_err(p, BC_ERROR_POSIX_FOR); + } + + if (BC_ERR(s)) return s; + if (BC_ERR(p->l.t != BC_LEX_SCOLON)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + bc_parse_push(p, BC_INST_JUMP_ZERO); + bc_parse_pushIndex(p, exit_idx); + bc_parse_push(p, BC_INST_JUMP); + bc_parse_pushIndex(p, body_idx); + + bc_parse_createCondLabel(p, update_idx); + + if (p->l.t != BC_LEX_RPAREN) + s = bc_parse_expr_status(p, 0, bc_parse_next_rel); + else s = bc_parse_err(p, BC_ERROR_POSIX_FOR); + + if (BC_ERR(s)) return s; + + if (BC_ERR(p->l.t != BC_LEX_RPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + bc_parse_push(p, BC_INST_JUMP); + bc_parse_pushIndex(p, cond_idx); + bc_parse_createLabel(p, p->func->code.len); + + bc_parse_createExitLabel(p, exit_idx, true); + s = bc_lex_next(&p->l); + if (BC_NO_ERR(!s)) + bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); + + return s; +} + +static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type) { + + size_t i; + BcInstPtr *ip; + + if (BC_ERR(!BC_PARSE_LOOP(p))) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + if (type == BC_LEX_KW_BREAK) { + + if (BC_ERR(!p->exits.len)) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + i = p->exits.len - 1; + ip = bc_vec_item(&p->exits, i); + + while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--); + assert(ip && (i < p->exits.len || ip->func)); + i = ip->idx; + } + else i = *((size_t*) bc_vec_top(&p->conds)); + + bc_parse_push(p, BC_INST_JUMP); + bc_parse_pushIndex(p, i); + + return bc_lex_next(&p->l); +} + +static BcStatus bc_parse_func(BcParse *p) { + + BcStatus s; + bool comma = false, voidfn; + uint16_t flags; + size_t idx; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (BC_ERR(p->l.t != BC_LEX_NAME)) + return bc_parse_err(p, BC_ERROR_PARSE_FUNC); + + voidfn = (!BC_IS_POSIX && p->l.t == BC_LEX_NAME && + !strcmp(p->l.str.v, "void")); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + voidfn = (voidfn && p->l.t == BC_LEX_NAME); + + if (voidfn) { + s = bc_parse_err(p, BC_ERROR_POSIX_VOID); + if (BC_ERR(s)) return s; + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + + if (BC_ERR(p->l.t != BC_LEX_LPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_FUNC); + + assert(p->prog->fns.len == p->prog->fn_map.len); + + idx = bc_program_insertFunc(p->prog, bc_vm_strdup(p->l.str.v)); + assert(idx); + bc_parse_updateFunc(p, idx); + p->func->voidfn = voidfn; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + while (p->l.t != BC_LEX_RPAREN) { + + BcType t = BC_TYPE_VAR; + + if (p->l.t == BC_LEX_OP_MULTIPLY) { + t = BC_TYPE_REF; + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + s = bc_parse_err(p, BC_ERROR_POSIX_REF); + if (BC_ERR(s)) return s; + } + + if (BC_ERR(p->l.t != BC_LEX_NAME)) + return bc_parse_err(p, BC_ERROR_PARSE_FUNC); + + p->func->nparams += 1; + + bc_vec_string(&p->buf, p->l.str.len, p->l.str.v); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (p->l.t == BC_LEX_LBRACKET) { + + if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (BC_ERR(p->l.t != BC_LEX_RBRACKET)) + return bc_parse_err(p, BC_ERROR_PARSE_FUNC); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + else if (BC_ERR(t == BC_TYPE_REF)) + return bc_parse_verr(p, BC_ERROR_PARSE_REF_VAR, p->buf.v); + + comma = (p->l.t == BC_LEX_COMMA); + if (comma) { + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + + s = bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line); + if (BC_ERR(s)) return s; + } + + if (BC_ERR(comma)) return bc_parse_err(p, BC_ERROR_PARSE_FUNC); + + flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER; + bc_parse_startBody(p, flags); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (p->l.t != BC_LEX_LBRACE) s = bc_parse_err(p, BC_ERROR_POSIX_BRACE); + + return s; +} + +static BcStatus bc_parse_auto(BcParse *p) { + + BcStatus s; + bool comma, one; + + if (BC_ERR(!p->auto_part)) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + p->auto_part = comma = false; + one = p->l.t == BC_LEX_NAME; + + while (p->l.t == BC_LEX_NAME) { + + BcType t; + + bc_vec_string(&p->buf, p->l.str.len - 1, p->l.str.v); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (p->l.t == BC_LEX_LBRACKET) { + + t = BC_TYPE_ARRAY; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (BC_ERR(p->l.t != BC_LEX_RBRACKET)) + return bc_parse_err(p, BC_ERROR_PARSE_FUNC); + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + else t = BC_TYPE_VAR; + + comma = (p->l.t == BC_LEX_COMMA); + if (comma) { + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + + s = bc_func_insert(p->func, p->prog, p->buf.v, t, p->l.line); + if (BC_ERR(s)) return s; + } + + if (BC_ERR(comma)) return bc_parse_err(p, BC_ERROR_PARSE_FUNC); + if (BC_ERR(!one)) return bc_parse_err(p, BC_ERROR_PARSE_NO_AUTO); + if (BC_ERR(!bc_parse_isDelimiter(p))) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + return s; +} + +static BcStatus bc_parse_body(BcParse *p, bool brace) { + + BcStatus s = BC_STATUS_SUCCESS; + uint16_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); + + assert(p->flags.len >= 2); + + *flag_ptr &= ~(BC_PARSE_FLAG_BODY); + + if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) { + + if (BC_ERR(!brace)) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + p->auto_part = (p->l.t != BC_LEX_KW_AUTO); + + if (!p->auto_part) { + + // Make sure this is true to not get a parse error. + p->auto_part = true; + + s = bc_parse_auto(p); + if (BC_ERR(s)) return s; + } + + if (p->l.t == BC_LEX_NLINE) s = bc_lex_next(&p->l); + } + else { + + size_t len = p->flags.len; + + assert(*flag_ptr); + + s = bc_parse_stmt(p); + + if (BC_NO_ERR(!s) && !brace && !BC_PARSE_BODY(p) && len <= p->flags.len) + s = bc_parse_endBody(p, false); + } + + return s; +} + +static BcStatus bc_parse_stmt(BcParse *p) { + + BcStatus s = BC_STATUS_SUCCESS; + size_t len; + uint16_t flags; + BcLexType type = p->l.t; + + if (type == BC_LEX_NLINE) return bc_lex_next(&p->l); + if (type == BC_LEX_KW_AUTO) return bc_parse_auto(p); + + p->auto_part = false; + + if (type != BC_LEX_KW_ELSE) { + + if (BC_PARSE_IF_END(p)) { + bc_parse_noElse(p); + if (p->flags.len > 1 && !BC_PARSE_BRACE(p)) + s = bc_parse_endBody(p, false); + return s; + } + else if (type == BC_LEX_LBRACE) { + + if (!BC_PARSE_BODY(p)) { + bc_parse_startBody(p, BC_PARSE_FLAG_BRACE); + s = bc_lex_next(&p->l); + } + else { + *(BC_PARSE_TOP_FLAG_PTR(p)) |= BC_PARSE_FLAG_BRACE; + s = bc_lex_next(&p->l); + if (BC_NO_ERR(!s)) s = bc_parse_body(p, true); + } + + return s; + } + else if (BC_PARSE_BODY(p) && !BC_PARSE_BRACE(p)) + return bc_parse_body(p, false); + } + + len = p->flags.len; + flags = BC_PARSE_TOP_FLAG(p); + + switch (type) { + + case BC_LEX_OP_INC: + case BC_LEX_OP_DEC: + case BC_LEX_OP_MINUS: + case BC_LEX_OP_BOOL_NOT: + case BC_LEX_LPAREN: + case BC_LEX_NAME: + case BC_LEX_NUMBER: + case BC_LEX_KW_IBASE: + case BC_LEX_KW_LAST: + case BC_LEX_KW_LENGTH: + case BC_LEX_KW_OBASE: + case BC_LEX_KW_SCALE: + case BC_LEX_KW_SQRT: + case BC_LEX_KW_ABS: + case BC_LEX_KW_READ: + case BC_LEX_KW_MAXIBASE: + case BC_LEX_KW_MAXOBASE: + case BC_LEX_KW_MAXSCALE: + { + s = bc_parse_expr_status(p, BC_PARSE_PRINT, bc_parse_next_expr); + break; + } + + case BC_LEX_KW_ELSE: + { + s = bc_parse_else(p); + break; + } + + case BC_LEX_SCOLON: + { + // Do nothing. + break; + } + + case BC_LEX_RBRACE: + { + s = bc_parse_endBody(p, true); + break; + } + + case BC_LEX_STR: + { + s = bc_parse_str(p, BC_INST_PRINT_STR); + break; + } + + case BC_LEX_KW_BREAK: + case BC_LEX_KW_CONTINUE: + { + s = bc_parse_loopExit(p, p->l.t); + break; + } + + case BC_LEX_KW_FOR: + { + s = bc_parse_for(p); + break; + } + + case BC_LEX_KW_HALT: + { + bc_parse_push(p, BC_INST_HALT); + s = bc_lex_next(&p->l); + break; + } + + case BC_LEX_KW_IF: + { + s = bc_parse_if(p); + break; + } + + case BC_LEX_KW_LIMITS: + { + bc_vm_printf("BC_LONG_BIT = %lu\n", (ulong) BC_LONG_BIT); + bc_vm_printf("BC_BASE_DIGS = %lu\n", (ulong) BC_BASE_DIGS); + bc_vm_printf("BC_BASE_POW = %lu\n", (ulong) BC_BASE_POW); + bc_vm_printf("BC_OVERFLOW_MAX = %lu\n", (ulong) BC_NUM_BIGDIG_MAX); + bc_vm_printf("\n"); + bc_vm_printf("BC_BASE_MAX = %lu\n", BC_MAX_OBASE); + bc_vm_printf("BC_DIM_MAX = %lu\n", BC_MAX_DIM); + bc_vm_printf("BC_SCALE_MAX = %lu\n", BC_MAX_SCALE); + bc_vm_printf("BC_STRING_MAX = %lu\n", BC_MAX_STRING); + bc_vm_printf("BC_NAME_MAX = %lu\n", BC_MAX_NAME); + bc_vm_printf("BC_NUM_MAX = %lu\n", BC_MAX_NUM); + bc_vm_printf("MAX Exponent = %lu\n", BC_MAX_EXP); + bc_vm_printf("Number of vars = %lu\n", BC_MAX_VARS); + + s = bc_lex_next(&p->l); + + break; + } + + case BC_LEX_KW_PRINT: + { + s = bc_parse_print(p); + break; + } + + case BC_LEX_KW_QUIT: + { + // Quit is a compile-time command. We don't exit directly, + // so the vm can clean up. Limits do the same thing. + s = BC_STATUS_QUIT; + break; + } + + case BC_LEX_KW_RETURN: + { + s = bc_parse_return(p); + break; + } + + case BC_LEX_KW_WHILE: + { + s = bc_parse_while(p); + break; + } + + default: + { + s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + break; + } + } + + if (BC_NO_ERR(!s) && len == p->flags.len && flags == BC_PARSE_TOP_FLAG(p)) { + if (BC_ERR(!bc_parse_isDelimiter(p))) + s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + } + + // Make sure semicolons are eaten. + while (BC_NO_ERR(!s) && p->l.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l); + + return s; +} + +BcStatus bc_parse_parse(BcParse *p) { + + BcStatus s; + + assert(p); + + if (BC_ERR(p->l.t == BC_LEX_EOF)) s = bc_parse_err(p, BC_ERROR_PARSE_EOF); + else if (p->l.t == BC_LEX_KW_DEFINE) { + if (BC_ERR(BC_PARSE_NO_EXEC(p))) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + s = bc_parse_func(p); + } + else s = bc_parse_stmt(p); + + if (BC_ERR((s && s != BC_STATUS_QUIT)) || BC_SIG) s = bc_parse_reset(p, s); + + return s; +} + +static BcStatus bc_parse_expr_err(BcParse *p, uint8_t flags, BcParseNext next) { + + BcStatus s = BC_STATUS_SUCCESS; + BcInst prev = BC_INST_PRINT; + BcLexType top, t = p->l.t; + size_t nexprs = 0, ops_bgn = p->ops.len; + uint32_t i, nparens, nrelops; + bool pfirst, rprn, done, get_token, assign, bin_last, incdec; + + pfirst = (p->l.t == BC_LEX_LPAREN); + nparens = nrelops = 0; + rprn = done = get_token = assign = incdec = false; + bin_last = true; + + // We want to eat newlines if newlines are not a valid ending token. + // This is for spacing in things like for loop headers. + if (!(flags & BC_PARSE_NOREAD)) { + while (BC_NO_ERR(!s) && (t = p->l.t) == BC_LEX_NLINE) + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + + for (; BC_NO_SIG && BC_NO_ERR(!s) && !done && BC_PARSE_EXPR(t); t = p->l.t) + { + switch (t) { + + case BC_LEX_OP_INC: + case BC_LEX_OP_DEC: + { + if (BC_ERR(incdec)) + return bc_parse_err(p, BC_ERROR_PARSE_ASSIGN); + s = bc_parse_incdec(p, &prev, &nexprs, flags); + rprn = get_token = bin_last = false; + incdec = true; + break; + } + +#if BC_ENABLE_EXTRA_MATH + case BC_LEX_OP_TRUNC: + { + if (BC_ERR(!BC_PARSE_LEAF(prev, bin_last, rprn))) + return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + // I can just add the instruction because + // negative will already be taken care of. + bc_parse_push(p, BC_INST_TRUNC); + rprn = false; + get_token = true; + break; + } +#endif // BC_ENABLE_EXTRA_MATH + + case BC_LEX_OP_MINUS: + { + s = bc_parse_minus(p, &prev, ops_bgn, rprn, bin_last, &nexprs); + rprn = get_token = false; + bin_last = (prev == BC_INST_MINUS); + if (bin_last) incdec = false; + break; + } + + case BC_LEX_OP_ASSIGN_POWER: + case BC_LEX_OP_ASSIGN_MULTIPLY: + case BC_LEX_OP_ASSIGN_DIVIDE: + case BC_LEX_OP_ASSIGN_MODULUS: + case BC_LEX_OP_ASSIGN_PLUS: + case BC_LEX_OP_ASSIGN_MINUS: +#if BC_ENABLE_EXTRA_MATH + case BC_LEX_OP_ASSIGN_PLACES: + case BC_LEX_OP_ASSIGN_LSHIFT: + case BC_LEX_OP_ASSIGN_RSHIFT: +#endif // BC_ENABLE_EXTRA_MATH + case BC_LEX_OP_ASSIGN: + { + if (!BC_PARSE_INST_VAR(prev)) { + s = bc_parse_err(p, BC_ERROR_PARSE_ASSIGN); + break; + } + } + // Fallthrough. + case BC_LEX_OP_POWER: + case BC_LEX_OP_MULTIPLY: + case BC_LEX_OP_DIVIDE: + case BC_LEX_OP_MODULUS: + case BC_LEX_OP_PLUS: +#if BC_ENABLE_EXTRA_MATH + case BC_LEX_OP_PLACES: + case BC_LEX_OP_LSHIFT: + case BC_LEX_OP_RSHIFT: +#endif // BC_ENABLE_EXTRA_MATH + case BC_LEX_OP_REL_EQ: + case BC_LEX_OP_REL_LE: + case BC_LEX_OP_REL_GE: + case BC_LEX_OP_REL_NE: + case BC_LEX_OP_REL_LT: + case BC_LEX_OP_REL_GT: + case BC_LEX_OP_BOOL_NOT: + case BC_LEX_OP_BOOL_OR: + case BC_LEX_OP_BOOL_AND: + { + if (BC_PARSE_OP_PREFIX(t)) { + if (BC_ERR(!bin_last && !BC_PARSE_OP_PREFIX(p->l.last))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + } + else if (BC_ERR(BC_PARSE_PREV_PREFIX(prev) || bin_last)) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + nrelops += (t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT); + prev = BC_PARSE_TOKEN_INST(t); + bc_parse_operator(p, t, ops_bgn, &nexprs); + rprn = incdec = false; + get_token = true; + bin_last = !BC_PARSE_OP_PREFIX(t); + + break; + } + + case BC_LEX_LPAREN: + { + if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + nparens += 1; + rprn = incdec = false; + get_token = true; + bc_vec_push(&p->ops, &t); + + break; + } + + case BC_LEX_RPAREN: + { + // This needs to be a status. The error + // is handled in bc_parse_expr_status(). + if (BC_ERR(p->l.last == BC_LEX_LPAREN)) + return BC_STATUS_EMPTY_EXPR; + + if (BC_ERR(bin_last || BC_PARSE_PREV_PREFIX(prev))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + if (!nparens) { + s = BC_STATUS_SUCCESS; + done = true; + get_token = false; + break; + } + + nparens -= 1; + rprn = true; + get_token = bin_last = incdec = false; + + s = bc_parse_rightParen(p, &nexprs); + + break; + } + + case BC_LEX_NAME: + { + if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + get_token = bin_last = false; + s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); + rprn = (prev == BC_INST_CALL); + nexprs += 1; + + break; + } + + case BC_LEX_NUMBER: + { + if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + bc_parse_number(p); + nexprs += 1; + prev = BC_INST_NUM; + get_token = true; + rprn = bin_last = false; + + break; + } + + case BC_LEX_KW_IBASE: + case BC_LEX_KW_LAST: + case BC_LEX_KW_OBASE: + { + if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + prev = t - BC_LEX_KW_LAST + BC_INST_LAST; + bc_parse_push(p, prev); + + get_token = true; + rprn = bin_last = false; + nexprs += 1; + + break; + } + + case BC_LEX_KW_LENGTH: + case BC_LEX_KW_SQRT: + case BC_LEX_KW_ABS: + { + if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + s = bc_parse_builtin(p, t, flags, &prev); + rprn = get_token = bin_last = incdec = false; + nexprs += 1; + + break; + } + + case BC_LEX_KW_READ: + case BC_LEX_KW_MAXIBASE: + case BC_LEX_KW_MAXOBASE: + case BC_LEX_KW_MAXSCALE: + { + if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + else if (t == BC_LEX_KW_READ && BC_ERR(flags & BC_PARSE_NOREAD)) + return bc_parse_err(p, BC_ERROR_EXEC_REC_READ); + else { + prev = t - BC_LEX_KW_READ + BC_INST_READ; + s = bc_parse_noArgBuiltin(p, prev); + } + + rprn = get_token = bin_last = incdec = false; + nexprs += 1; + + break; + } + + case BC_LEX_KW_SCALE: + { + if (BC_ERR(BC_PARSE_LEAF(prev, bin_last, rprn))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + s = bc_parse_scale(p, &prev, flags); + rprn = get_token = bin_last = false; + nexprs += 1; + + break; + } + + default: + { +#ifndef NDEBUG + s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + break; +#endif // NDEBUG + } + } + + if (BC_NO_ERR(!s) && get_token) s = bc_lex_next(&p->l); + } + + if (BC_ERR(s)) return s; + if (BC_SIG) return BC_STATUS_SIGNAL; + + while (p->ops.len > ops_bgn) { + + top = BC_PARSE_TOP_OP(p); + assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; + + if (BC_ERR(top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); + + nexprs -= !BC_PARSE_OP_PREFIX(top); + bc_vec_pop(&p->ops); + } + + if (BC_ERR(nexprs != 1)) return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + for (i = 0; i < next.len && t != next.tokens[i]; ++i); + if (BC_ERR(i == next.len && !bc_parse_isDelimiter(p))) + return bc_parse_err(p, BC_ERROR_PARSE_EXPR); + + if (!(flags & BC_PARSE_REL) && nrelops) { + s = bc_parse_err(p, BC_ERROR_POSIX_REL_POS); + if (BC_ERR(s)) return s; + } + else if ((flags & BC_PARSE_REL) && nrelops > 1) { + s = bc_parse_err(p, BC_ERROR_POSIX_MULTIREL); + if (BC_ERR(s)) return s; + } + + if (!(flags & BC_PARSE_NEEDVAL) && !pfirst) { + + uchar inst = BC_INST_INVALID; + + if (assign) { + inst = *((uchar*) bc_vec_top(&p->func->code)); + inst += (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER); + } + else if (incdec && !(flags & BC_PARSE_PRINT)) { + inst = *((uchar*) bc_vec_top(&p->func->code)); + inst = BC_INST_INC_NO_VAL + (inst & 0x01); + } + + if (inst >= BC_INST_INC_NO_VAL && inst <= BC_INST_ASSIGN_NO_VAL) { + bc_vec_pop(&p->func->code); + bc_parse_push(p, inst); + } + } + + if ((flags & BC_PARSE_PRINT) && (pfirst || !assign)) + bc_parse_push(p, BC_INST_PRINT); + + // We want to eat newlines if newlines are not a valid ending token. + // This is for spacing in things like for loop headers. + for (incdec = true, i = 0; i < next.len && incdec; ++i) + incdec = (next.tokens[i] != BC_LEX_NLINE); + if (incdec) { + while (BC_NO_ERR(!s) && p->l.t == BC_LEX_NLINE) s = bc_lex_next(&p->l); + } + + return s; +} + +BcStatus bc_parse_expr_status(BcParse *p, uint8_t flags, BcParseNext next) { + + BcStatus s = bc_parse_expr_err(p, flags, next); + + if (BC_ERR(s == BC_STATUS_EMPTY_EXPR)) + s = bc_parse_err(p, BC_ERROR_PARSE_EMPTY_EXPR); + + return s; +} + +BcStatus bc_parse_expr(BcParse *p, uint8_t flags) { + assert(p); + return bc_parse_expr_status(p, flags, bc_parse_next_read); +} +#endif // BC_ENABLED Index: contrib/bc/src/data.c =================================================================== --- contrib/bc/src/data.c +++ contrib/bc/src/data.c @@ -0,0 +1,767 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Constant data for bc. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#if BC_ENABLE_SIGNALS +#if BC_ENABLED +const char bc_sig_msg[] = "\ninterrupt (type \"quit\" to exit)\n"; +#endif // BC_ENABLED +#if DC_ENABLED +const char dc_sig_msg[] = "\ninterrupt (type \"q\" to exit)\n"; +#endif // DC_ENABLED +#endif // BC_ENABLE_SIGNALS + +const char bc_copyright[] = + "Copyright (c) 2018-2019 Gavin D. Howard and contributors\n" + "Report bugs at: https://github.com/gavinhoward/bc\n\n" + "This is free software with ABSOLUTELY NO WARRANTY.\n"; + +const char* const bc_err_func_header = "Function:"; +const char* const bc_err_line = ":%zu"; + +const char *bc_errs[] = { + "Math error:", + "Parse error:", + "Runtime error:", + "Fatal error:", +#if BC_ENABLED + "Warning:", +#endif // BC_ENABLED +}; + +const uchar bc_err_ids[] = { + + BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, + + BC_ERR_IDX_FATAL, BC_ERR_IDX_FATAL, BC_ERR_IDX_FATAL, BC_ERR_IDX_FATAL, + BC_ERR_IDX_FATAL, BC_ERR_IDX_FATAL, + + BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, + BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, + BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, + + BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, + BC_ERR_IDX_PARSE, +#if BC_ENABLED + BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, + BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, + BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, + + BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, + BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, + BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, + BC_ERR_IDX_PARSE, +#endif // BC_ENABLED + +}; + +const char* const bc_err_msgs[] = { + + "negative number", + "non-integer number", + "overflow: number cannot fit", + "divide by 0", + + "memory allocation failed", + "I/O error", + "could not open file: %s", + "file is not ASCII: %s", + "path is a directory: %s", + "bad command-line option: '%c' (\"%s\")", + + "bad ibase: must be [%lu, %lu]", + "bad obase: must be [%lu, %lu]", + "bad scale: must be [%lu, %lu]", + "bad read() expression", + "read() call inside of a read() call", + "variable or array element is the wrong type", +#if DC_ENABLED + "stack has too few elements", +#else // DC_ENABLED + NULL, +#endif // DC_ENABLED +#if BC_ENABLED + "wrong number of parameters; need %zu, have %zu", + "undefined function: %s()", + "cannot use a void value in an expression", +#else + NULL, NULL, NULL, +#endif // BC_ENABLED + + "end of file", + "bad character '%c'", + "string end could not be found", + "comment end could not be found", + "bad token", +#if BC_ENABLED + "bad expression", + "empty expression", + "bad print statement", + "bad function definition", + "bad assignment: left side must be scale, ibase, " + "obase, last, var, or array element", + "no auto variable found", + "function parameter or auto \"%s%s\" already exists", + "block end could not be found", + "cannot return a value from void function: %s()", + "var cannot be a reference: %s", + + "POSIX does not allow names longer than 1 character: %s", + "POSIX does not allow '#' script comments", + "POSIX does not allow the following keyword: %s", + "POSIX does not allow a period ('.') as a shortcut for the last result", + "POSIX requires parentheses around return expressions", + "POSIX does not allow the following operator: %s", + "POSIX does not allow comparison operators outside if statements or loops", + "POSIX requires 0 or 1 comparison operators per condition", + "POSIX requires all 3 parts of a for loop to be non-empty", +#if BC_ENABLE_EXTRA_MATH + "POSIX does not allow exponential notation", +#else + NULL, +#endif // BC_ENABLE_EXTRA_MATH + "POSIX does not allow array references as function parameters", + "POSIX does not allow void functions", + "POSIX requires the left brace be on the same line as the function header", +#endif // BC_ENABLED + +}; + +#if BC_ENABLE_HISTORY +const char *bc_history_bad_terms[] = { "dumb", "cons25", "emacs", NULL }; + +const char bc_history_tab[] = " "; +const size_t bc_history_tab_len = sizeof(bc_history_tab) - 1; + +// These are listed in ascending order for efficiency. +const uint32_t bc_history_wchars[][2] = { + { 0x1100, 0x115F }, + { 0x231A, 0x231B }, + { 0x2329, 0x232A }, + { 0x23E9, 0x23EC }, + { 0x23F0, 0x23F0 }, + { 0x23F3, 0x23F3 }, + { 0x25FD, 0x25FE }, + { 0x2614, 0x2615 }, + { 0x2648, 0x2653 }, + { 0x267F, 0x267F }, + { 0x2693, 0x2693 }, + { 0x26A1, 0x26A1 }, + { 0x26AA, 0x26AB }, + { 0x26BD, 0x26BE }, + { 0x26C4, 0x26C5 }, + { 0x26CE, 0x26CE }, + { 0x26D4, 0x26D4 }, + { 0x26EA, 0x26EA }, + { 0x26F2, 0x26F3 }, + { 0x26F5, 0x26F5 }, + { 0x26FA, 0x26FA }, + { 0x26FD, 0x26FD }, + { 0x2705, 0x2705 }, + { 0x270A, 0x270B }, + { 0x2728, 0x2728 }, + { 0x274C, 0x274C }, + { 0x274E, 0x274E }, + { 0x2753, 0x2755 }, + { 0x2757, 0x2757 }, + { 0x2795, 0x2797 }, + { 0x27B0, 0x27B0 }, + { 0x27BF, 0x27BF }, + { 0x2B1B, 0x2B1C }, + { 0x2B50, 0x2B50 }, + { 0x2B55, 0x2B55 }, + { 0x2E80, 0x2E99 }, + { 0x2E9B, 0x2EF3 }, + { 0x2F00, 0x2FD5 }, + { 0x2FF0, 0x2FFB }, + { 0x3001, 0x303E }, + { 0x3041, 0x3096 }, + { 0x3099, 0x30FF }, + { 0x3105, 0x312D }, + { 0x3131, 0x318E }, + { 0x3190, 0x31BA }, + { 0x31C0, 0x31E3 }, + { 0x31F0, 0x321E }, + { 0x3220, 0x3247 }, + { 0x3250, 0x32FE }, + { 0x3300, 0x4DBF }, + { 0x4E00, 0xA48C }, + { 0xA490, 0xA4C6 }, + { 0xA960, 0xA97C }, + { 0xAC00, 0xD7A3 }, + { 0xF900, 0xFAFF }, + { 0xFE10, 0xFE19 }, + { 0xFE30, 0xFE52 }, + { 0xFE54, 0xFE66 }, + { 0xFE68, 0xFE6B }, + { 0x16FE0, 0x16FE0 }, + { 0x17000, 0x187EC }, + { 0x18800, 0x18AF2 }, + { 0x1B000, 0x1B001 }, + { 0x1F004, 0x1F004 }, + { 0x1F0CF, 0x1F0CF }, + { 0x1F18E, 0x1F18E }, + { 0x1F191, 0x1F19A }, + { 0x1F200, 0x1F202 }, + { 0x1F210, 0x1F23B }, + { 0x1F240, 0x1F248 }, + { 0x1F250, 0x1F251 }, + { 0x1F300, 0x1F320 }, + { 0x1F32D, 0x1F335 }, + { 0x1F337, 0x1F37C }, + { 0x1F37E, 0x1F393 }, + { 0x1F3A0, 0x1F3CA }, + { 0x1F3CF, 0x1F3D3 }, + { 0x1F3E0, 0x1F3F0 }, + { 0x1F3F4, 0x1F3F4 }, + { 0x1F3F8, 0x1F43E }, + { 0x1F440, 0x1F440 }, + { 0x1F442, 0x1F4FC }, + { 0x1F4FF, 0x1F53D }, + { 0x1F54B, 0x1F54E }, + { 0x1F550, 0x1F567 }, + { 0x1F57A, 0x1F57A }, + { 0x1F595, 0x1F596 }, + { 0x1F5A4, 0x1F5A4 }, + { 0x1F5FB, 0x1F64F }, + { 0x1F680, 0x1F6C5 }, + { 0x1F6CC, 0x1F6CC }, + { 0x1F6D0, 0x1F6D2 }, + { 0x1F6EB, 0x1F6EC }, + { 0x1F6F4, 0x1F6F6 }, + { 0x1F910, 0x1F91E }, + { 0x1F920, 0x1F927 }, + { 0x1F930, 0x1F930 }, + { 0x1F933, 0x1F93E }, + { 0x1F940, 0x1F94B }, + { 0x1F950, 0x1F95E }, + { 0x1F980, 0x1F991 }, + { 0x1F9C0, 0x1F9C0 }, + { 0x20000, 0x2FFFD }, + { 0x30000, 0x3FFFD }, +}; + +const size_t bc_history_wchars_len = + sizeof(bc_history_wchars) / sizeof(bc_history_wchars[0]); + +// These are listed in ascending order for efficiency. +const uint32_t bc_history_combo_chars[] = { + 0x0300,0x0301,0x0302,0x0303,0x0304,0x0305,0x0306,0x0307, + 0x0308,0x0309,0x030A,0x030B,0x030C,0x030D,0x030E,0x030F, + 0x0310,0x0311,0x0312,0x0313,0x0314,0x0315,0x0316,0x0317, + 0x0318,0x0319,0x031A,0x031B,0x031C,0x031D,0x031E,0x031F, + 0x0320,0x0321,0x0322,0x0323,0x0324,0x0325,0x0326,0x0327, + 0x0328,0x0329,0x032A,0x032B,0x032C,0x032D,0x032E,0x032F, + 0x0330,0x0331,0x0332,0x0333,0x0334,0x0335,0x0336,0x0337, + 0x0338,0x0339,0x033A,0x033B,0x033C,0x033D,0x033E,0x033F, + 0x0340,0x0341,0x0342,0x0343,0x0344,0x0345,0x0346,0x0347, + 0x0348,0x0349,0x034A,0x034B,0x034C,0x034D,0x034E,0x034F, + 0x0350,0x0351,0x0352,0x0353,0x0354,0x0355,0x0356,0x0357, + 0x0358,0x0359,0x035A,0x035B,0x035C,0x035D,0x035E,0x035F, + 0x0360,0x0361,0x0362,0x0363,0x0364,0x0365,0x0366,0x0367, + 0x0368,0x0369,0x036A,0x036B,0x036C,0x036D,0x036E,0x036F, + 0x0483,0x0484,0x0485,0x0486,0x0487,0x0591,0x0592,0x0593, + 0x0594,0x0595,0x0596,0x0597,0x0598,0x0599,0x059A,0x059B, + 0x059C,0x059D,0x059E,0x059F,0x05A0,0x05A1,0x05A2,0x05A3, + 0x05A4,0x05A5,0x05A6,0x05A7,0x05A8,0x05A9,0x05AA,0x05AB, + 0x05AC,0x05AD,0x05AE,0x05AF,0x05B0,0x05B1,0x05B2,0x05B3, + 0x05B4,0x05B5,0x05B6,0x05B7,0x05B8,0x05B9,0x05BA,0x05BB, + 0x05BC,0x05BD,0x05BF,0x05C1,0x05C2,0x05C4,0x05C5,0x05C7, + 0x0610,0x0611,0x0612,0x0613,0x0614,0x0615,0x0616,0x0617, + 0x0618,0x0619,0x061A,0x064B,0x064C,0x064D,0x064E,0x064F, + 0x0650,0x0651,0x0652,0x0653,0x0654,0x0655,0x0656,0x0657, + 0x0658,0x0659,0x065A,0x065B,0x065C,0x065D,0x065E,0x065F, + 0x0670,0x06D6,0x06D7,0x06D8,0x06D9,0x06DA,0x06DB,0x06DC, + 0x06DF,0x06E0,0x06E1,0x06E2,0x06E3,0x06E4,0x06E7,0x06E8, + 0x06EA,0x06EB,0x06EC,0x06ED,0x0711,0x0730,0x0731,0x0732, + 0x0733,0x0734,0x0735,0x0736,0x0737,0x0738,0x0739,0x073A, + 0x073B,0x073C,0x073D,0x073E,0x073F,0x0740,0x0741,0x0742, + 0x0743,0x0744,0x0745,0x0746,0x0747,0x0748,0x0749,0x074A, + 0x07A6,0x07A7,0x07A8,0x07A9,0x07AA,0x07AB,0x07AC,0x07AD, + 0x07AE,0x07AF,0x07B0,0x07EB,0x07EC,0x07ED,0x07EE,0x07EF, + 0x07F0,0x07F1,0x07F2,0x07F3,0x0816,0x0817,0x0818,0x0819, + 0x081B,0x081C,0x081D,0x081E,0x081F,0x0820,0x0821,0x0822, + 0x0823,0x0825,0x0826,0x0827,0x0829,0x082A,0x082B,0x082C, + 0x082D,0x0859,0x085A,0x085B,0x08D4,0x08D5,0x08D6,0x08D7, + 0x08D8,0x08D9,0x08DA,0x08DB,0x08DC,0x08DD,0x08DE,0x08DF, + 0x08E0,0x08E1,0x08E3,0x08E4,0x08E5,0x08E6,0x08E7,0x08E8, + 0x08E9,0x08EA,0x08EB,0x08EC,0x08ED,0x08EE,0x08EF,0x08F0, + 0x08F1,0x08F2,0x08F3,0x08F4,0x08F5,0x08F6,0x08F7,0x08F8, + 0x08F9,0x08FA,0x08FB,0x08FC,0x08FD,0x08FE,0x08FF,0x0900, + 0x0901,0x0902,0x093A,0x093C,0x0941,0x0942,0x0943,0x0944, + 0x0945,0x0946,0x0947,0x0948,0x094D,0x0951,0x0952,0x0953, + 0x0954,0x0955,0x0956,0x0957,0x0962,0x0963,0x0981,0x09BC, + 0x09C1,0x09C2,0x09C3,0x09C4,0x09CD,0x09E2,0x09E3,0x0A01, + 0x0A02,0x0A3C,0x0A41,0x0A42,0x0A47,0x0A48,0x0A4B,0x0A4C, + 0x0A4D,0x0A51,0x0A70,0x0A71,0x0A75,0x0A81,0x0A82,0x0ABC, + 0x0AC1,0x0AC2,0x0AC3,0x0AC4,0x0AC5,0x0AC7,0x0AC8,0x0ACD, + 0x0AE2,0x0AE3,0x0B01,0x0B3C,0x0B3F,0x0B41,0x0B42,0x0B43, + 0x0B44,0x0B4D,0x0B56,0x0B62,0x0B63,0x0B82,0x0BC0,0x0BCD, + 0x0C00,0x0C3E,0x0C3F,0x0C40,0x0C46,0x0C47,0x0C48,0x0C4A, + 0x0C4B,0x0C4C,0x0C4D,0x0C55,0x0C56,0x0C62,0x0C63,0x0C81, + 0x0CBC,0x0CBF,0x0CC6,0x0CCC,0x0CCD,0x0CE2,0x0CE3,0x0D01, + 0x0D41,0x0D42,0x0D43,0x0D44,0x0D4D,0x0D62,0x0D63,0x0DCA, + 0x0DD2,0x0DD3,0x0DD4,0x0DD6,0x0E31,0x0E34,0x0E35,0x0E36, + 0x0E37,0x0E38,0x0E39,0x0E3A,0x0E47,0x0E48,0x0E49,0x0E4A, + 0x0E4B,0x0E4C,0x0E4D,0x0E4E,0x0EB1,0x0EB4,0x0EB5,0x0EB6, + 0x0EB7,0x0EB8,0x0EB9,0x0EBB,0x0EBC,0x0EC8,0x0EC9,0x0ECA, + 0x0ECB,0x0ECC,0x0ECD,0x0F18,0x0F19,0x0F35,0x0F37,0x0F39, + 0x0F71,0x0F72,0x0F73,0x0F74,0x0F75,0x0F76,0x0F77,0x0F78, + 0x0F79,0x0F7A,0x0F7B,0x0F7C,0x0F7D,0x0F7E,0x0F80,0x0F81, + 0x0F82,0x0F83,0x0F84,0x0F86,0x0F87,0x0F8D,0x0F8E,0x0F8F, + 0x0F90,0x0F91,0x0F92,0x0F93,0x0F94,0x0F95,0x0F96,0x0F97, + 0x0F99,0x0F9A,0x0F9B,0x0F9C,0x0F9D,0x0F9E,0x0F9F,0x0FA0, + 0x0FA1,0x0FA2,0x0FA3,0x0FA4,0x0FA5,0x0FA6,0x0FA7,0x0FA8, + 0x0FA9,0x0FAA,0x0FAB,0x0FAC,0x0FAD,0x0FAE,0x0FAF,0x0FB0, + 0x0FB1,0x0FB2,0x0FB3,0x0FB4,0x0FB5,0x0FB6,0x0FB7,0x0FB8, + 0x0FB9,0x0FBA,0x0FBB,0x0FBC,0x0FC6,0x102D,0x102E,0x102F, + 0x1030,0x1032,0x1033,0x1034,0x1035,0x1036,0x1037,0x1039, + 0x103A,0x103D,0x103E,0x1058,0x1059,0x105E,0x105F,0x1060, + 0x1071,0x1072,0x1073,0x1074,0x1082,0x1085,0x1086,0x108D, + 0x109D,0x135D,0x135E,0x135F,0x1712,0x1713,0x1714,0x1732, + 0x1733,0x1734,0x1752,0x1753,0x1772,0x1773,0x17B4,0x17B5, + 0x17B7,0x17B8,0x17B9,0x17BA,0x17BB,0x17BC,0x17BD,0x17C6, + 0x17C9,0x17CA,0x17CB,0x17CC,0x17CD,0x17CE,0x17CF,0x17D0, + 0x17D1,0x17D2,0x17D3,0x17DD,0x180B,0x180C,0x180D,0x1885, + 0x1886,0x18A9,0x1920,0x1921,0x1922,0x1927,0x1928,0x1932, + 0x1939,0x193A,0x193B,0x1A17,0x1A18,0x1A1B,0x1A56,0x1A58, + 0x1A59,0x1A5A,0x1A5B,0x1A5C,0x1A5D,0x1A5E,0x1A60,0x1A62, + 0x1A65,0x1A66,0x1A67,0x1A68,0x1A69,0x1A6A,0x1A6B,0x1A6C, + 0x1A73,0x1A74,0x1A75,0x1A76,0x1A77,0x1A78,0x1A79,0x1A7A, + 0x1A7B,0x1A7C,0x1A7F,0x1AB0,0x1AB1,0x1AB2,0x1AB3,0x1AB4, + 0x1AB5,0x1AB6,0x1AB7,0x1AB8,0x1AB9,0x1ABA,0x1ABB,0x1ABC, + 0x1ABD,0x1B00,0x1B01,0x1B02,0x1B03,0x1B34,0x1B36,0x1B37, + 0x1B38,0x1B39,0x1B3A,0x1B3C,0x1B42,0x1B6B,0x1B6C,0x1B6D, + 0x1B6E,0x1B6F,0x1B70,0x1B71,0x1B72,0x1B73,0x1B80,0x1B81, + 0x1BA2,0x1BA3,0x1BA4,0x1BA5,0x1BA8,0x1BA9,0x1BAB,0x1BAC, + 0x1BAD,0x1BE6,0x1BE8,0x1BE9,0x1BED,0x1BEF,0x1BF0,0x1BF1, + 0x1C2C,0x1C2D,0x1C2E,0x1C2F,0x1C30,0x1C31,0x1C32,0x1C33, + 0x1C36,0x1C37,0x1CD0,0x1CD1,0x1CD2,0x1CD4,0x1CD5,0x1CD6, + 0x1CD7,0x1CD8,0x1CD9,0x1CDA,0x1CDB,0x1CDC,0x1CDD,0x1CDE, + 0x1CDF,0x1CE0,0x1CE2,0x1CE3,0x1CE4,0x1CE5,0x1CE6,0x1CE7, + 0x1CE8,0x1CED,0x1CF4,0x1CF8,0x1CF9,0x1DC0,0x1DC1,0x1DC2, + 0x1DC3,0x1DC4,0x1DC5,0x1DC6,0x1DC7,0x1DC8,0x1DC9,0x1DCA, + 0x1DCB,0x1DCC,0x1DCD,0x1DCE,0x1DCF,0x1DD0,0x1DD1,0x1DD2, + 0x1DD3,0x1DD4,0x1DD5,0x1DD6,0x1DD7,0x1DD8,0x1DD9,0x1DDA, + 0x1DDB,0x1DDC,0x1DDD,0x1DDE,0x1DDF,0x1DE0,0x1DE1,0x1DE2, + 0x1DE3,0x1DE4,0x1DE5,0x1DE6,0x1DE7,0x1DE8,0x1DE9,0x1DEA, + 0x1DEB,0x1DEC,0x1DED,0x1DEE,0x1DEF,0x1DF0,0x1DF1,0x1DF2, + 0x1DF3,0x1DF4,0x1DF5,0x1DFB,0x1DFC,0x1DFD,0x1DFE,0x1DFF, + 0x20D0,0x20D1,0x20D2,0x20D3,0x20D4,0x20D5,0x20D6,0x20D7, + 0x20D8,0x20D9,0x20DA,0x20DB,0x20DC,0x20E1,0x20E5,0x20E6, + 0x20E7,0x20E8,0x20E9,0x20EA,0x20EB,0x20EC,0x20ED,0x20EE, + 0x20EF,0x20F0,0x2CEF,0x2CF0,0x2CF1,0x2D7F,0x2DE0,0x2DE1, + 0x2DE2,0x2DE3,0x2DE4,0x2DE5,0x2DE6,0x2DE7,0x2DE8,0x2DE9, + 0x2DEA,0x2DEB,0x2DEC,0x2DED,0x2DEE,0x2DEF,0x2DF0,0x2DF1, + 0x2DF2,0x2DF3,0x2DF4,0x2DF5,0x2DF6,0x2DF7,0x2DF8,0x2DF9, + 0x2DFA,0x2DFB,0x2DFC,0x2DFD,0x2DFE,0x2DFF,0x302A,0x302B, + 0x302C,0x302D,0x3099,0x309A,0xA66F,0xA674,0xA675,0xA676, + 0xA677,0xA678,0xA679,0xA67A,0xA67B,0xA67C,0xA67D,0xA69E, + 0xA69F,0xA6F0,0xA6F1,0xA802,0xA806,0xA80B,0xA825,0xA826, + 0xA8C4,0xA8C5,0xA8E0,0xA8E1,0xA8E2,0xA8E3,0xA8E4,0xA8E5, + 0xA8E6,0xA8E7,0xA8E8,0xA8E9,0xA8EA,0xA8EB,0xA8EC,0xA8ED, + 0xA8EE,0xA8EF,0xA8F0,0xA8F1,0xA926,0xA927,0xA928,0xA929, + 0xA92A,0xA92B,0xA92C,0xA92D,0xA947,0xA948,0xA949,0xA94A, + 0xA94B,0xA94C,0xA94D,0xA94E,0xA94F,0xA950,0xA951,0xA980, + 0xA981,0xA982,0xA9B3,0xA9B6,0xA9B7,0xA9B8,0xA9B9,0xA9BC, + 0xA9E5,0xAA29,0xAA2A,0xAA2B,0xAA2C,0xAA2D,0xAA2E,0xAA31, + 0xAA32,0xAA35,0xAA36,0xAA43,0xAA4C,0xAA7C,0xAAB0,0xAAB2, + 0xAAB3,0xAAB4,0xAAB7,0xAAB8,0xAABE,0xAABF,0xAAC1,0xAAEC, + 0xAAED,0xAAF6,0xABE5,0xABE8,0xABED,0xFB1E,0xFE00,0xFE01, + 0xFE02,0xFE03,0xFE04,0xFE05,0xFE06,0xFE07,0xFE08,0xFE09, + 0xFE0A,0xFE0B,0xFE0C,0xFE0D,0xFE0E,0xFE0F,0xFE20,0xFE21, + 0xFE22,0xFE23,0xFE24,0xFE25,0xFE26,0xFE27,0xFE28,0xFE29, + 0xFE2A,0xFE2B,0xFE2C,0xFE2D,0xFE2E,0xFE2F, + 0x101FD,0x102E0,0x10376,0x10377,0x10378,0x10379,0x1037A,0x10A01, + 0x10A02,0x10A03,0x10A05,0x10A06,0x10A0C,0x10A0D,0x10A0E,0x10A0F, + 0x10A38,0x10A39,0x10A3A,0x10A3F,0x10AE5,0x10AE6,0x11001,0x11038, + 0x11039,0x1103A,0x1103B,0x1103C,0x1103D,0x1103E,0x1103F,0x11040, + 0x11041,0x11042,0x11043,0x11044,0x11045,0x11046,0x1107F,0x11080, + 0x11081,0x110B3,0x110B4,0x110B5,0x110B6,0x110B9,0x110BA,0x11100, + 0x11101,0x11102,0x11127,0x11128,0x11129,0x1112A,0x1112B,0x1112D, + 0x1112E,0x1112F,0x11130,0x11131,0x11132,0x11133,0x11134,0x11173, + 0x11180,0x11181,0x111B6,0x111B7,0x111B8,0x111B9,0x111BA,0x111BB, + 0x111BC,0x111BD,0x111BE,0x111CA,0x111CB,0x111CC,0x1122F,0x11230, + 0x11231,0x11234,0x11236,0x11237,0x1123E,0x112DF,0x112E3,0x112E4, + 0x112E5,0x112E6,0x112E7,0x112E8,0x112E9,0x112EA,0x11300,0x11301, + 0x1133C,0x11340,0x11366,0x11367,0x11368,0x11369,0x1136A,0x1136B, + 0x1136C,0x11370,0x11371,0x11372,0x11373,0x11374,0x11438,0x11439, + 0x1143A,0x1143B,0x1143C,0x1143D,0x1143E,0x1143F,0x11442,0x11443, + 0x11444,0x11446,0x114B3,0x114B4,0x114B5,0x114B6,0x114B7,0x114B8, + 0x114BA,0x114BF,0x114C0,0x114C2,0x114C3,0x115B2,0x115B3,0x115B4, + 0x115B5,0x115BC,0x115BD,0x115BF,0x115C0,0x115DC,0x115DD,0x11633, + 0x11634,0x11635,0x11636,0x11637,0x11638,0x11639,0x1163A,0x1163D, + 0x1163F,0x11640,0x116AB,0x116AD,0x116B0,0x116B1,0x116B2,0x116B3, + 0x116B4,0x116B5,0x116B7,0x1171D,0x1171E,0x1171F,0x11722,0x11723, + 0x11724,0x11725,0x11727,0x11728,0x11729,0x1172A,0x1172B,0x11C30, + 0x11C31,0x11C32,0x11C33,0x11C34,0x11C35,0x11C36,0x11C38,0x11C39, + 0x11C3A,0x11C3B,0x11C3C,0x11C3D,0x11C3F,0x11C92,0x11C93,0x11C94, + 0x11C95,0x11C96,0x11C97,0x11C98,0x11C99,0x11C9A,0x11C9B,0x11C9C, + 0x11C9D,0x11C9E,0x11C9F,0x11CA0,0x11CA1,0x11CA2,0x11CA3,0x11CA4, + 0x11CA5,0x11CA6,0x11CA7,0x11CAA,0x11CAB,0x11CAC,0x11CAD,0x11CAE, + 0x11CAF,0x11CB0,0x11CB2,0x11CB3,0x11CB5,0x11CB6,0x16AF0,0x16AF1, + 0x16AF2,0x16AF3,0x16AF4,0x16B30,0x16B31,0x16B32,0x16B33,0x16B34, + 0x16B35,0x16B36,0x16F8F,0x16F90,0x16F91,0x16F92,0x1BC9D,0x1BC9E, + 0x1D167,0x1D168,0x1D169,0x1D17B,0x1D17C,0x1D17D,0x1D17E,0x1D17F, + 0x1D180,0x1D181,0x1D182,0x1D185,0x1D186,0x1D187,0x1D188,0x1D189, + 0x1D18A,0x1D18B,0x1D1AA,0x1D1AB,0x1D1AC,0x1D1AD,0x1D242,0x1D243, + 0x1D244,0x1DA00,0x1DA01,0x1DA02,0x1DA03,0x1DA04,0x1DA05,0x1DA06, + 0x1DA07,0x1DA08,0x1DA09,0x1DA0A,0x1DA0B,0x1DA0C,0x1DA0D,0x1DA0E, + 0x1DA0F,0x1DA10,0x1DA11,0x1DA12,0x1DA13,0x1DA14,0x1DA15,0x1DA16, + 0x1DA17,0x1DA18,0x1DA19,0x1DA1A,0x1DA1B,0x1DA1C,0x1DA1D,0x1DA1E, + 0x1DA1F,0x1DA20,0x1DA21,0x1DA22,0x1DA23,0x1DA24,0x1DA25,0x1DA26, + 0x1DA27,0x1DA28,0x1DA29,0x1DA2A,0x1DA2B,0x1DA2C,0x1DA2D,0x1DA2E, + 0x1DA2F,0x1DA30,0x1DA31,0x1DA32,0x1DA33,0x1DA34,0x1DA35,0x1DA36, + 0x1DA3B,0x1DA3C,0x1DA3D,0x1DA3E,0x1DA3F,0x1DA40,0x1DA41,0x1DA42, + 0x1DA43,0x1DA44,0x1DA45,0x1DA46,0x1DA47,0x1DA48,0x1DA49,0x1DA4A, + 0x1DA4B,0x1DA4C,0x1DA4D,0x1DA4E,0x1DA4F,0x1DA50,0x1DA51,0x1DA52, + 0x1DA53,0x1DA54,0x1DA55,0x1DA56,0x1DA57,0x1DA58,0x1DA59,0x1DA5A, + 0x1DA5B,0x1DA5C,0x1DA5D,0x1DA5E,0x1DA5F,0x1DA60,0x1DA61,0x1DA62, + 0x1DA63,0x1DA64,0x1DA65,0x1DA66,0x1DA67,0x1DA68,0x1DA69,0x1DA6A, + 0x1DA6B,0x1DA6C,0x1DA75,0x1DA84,0x1DA9B,0x1DA9C,0x1DA9D,0x1DA9E, + 0x1DA9F,0x1DAA1,0x1DAA2,0x1DAA3,0x1DAA4,0x1DAA5,0x1DAA6,0x1DAA7, + 0x1DAA8,0x1DAA9,0x1DAAA,0x1DAAB,0x1DAAC,0x1DAAD,0x1DAAE,0x1DAAF, + 0x1E000,0x1E001,0x1E002,0x1E003,0x1E004,0x1E005,0x1E006,0x1E008, + 0x1E009,0x1E00A,0x1E00B,0x1E00C,0x1E00D,0x1E00E,0x1E00F,0x1E010, + 0x1E011,0x1E012,0x1E013,0x1E014,0x1E015,0x1E016,0x1E017,0x1E018, + 0x1E01B,0x1E01C,0x1E01D,0x1E01E,0x1E01F,0x1E020,0x1E021,0x1E023, + 0x1E024,0x1E026,0x1E027,0x1E028,0x1E029,0x1E02A,0x1E8D0,0x1E8D1, + 0x1E8D2,0x1E8D3,0x1E8D4,0x1E8D5,0x1E8D6,0x1E944,0x1E945,0x1E946, + 0x1E947,0x1E948,0x1E949,0x1E94A,0xE0100,0xE0101,0xE0102,0xE0103, + 0xE0104,0xE0105,0xE0106,0xE0107,0xE0108,0xE0109,0xE010A,0xE010B, + 0xE010C,0xE010D,0xE010E,0xE010F,0xE0110,0xE0111,0xE0112,0xE0113, + 0xE0114,0xE0115,0xE0116,0xE0117,0xE0118,0xE0119,0xE011A,0xE011B, + 0xE011C,0xE011D,0xE011E,0xE011F,0xE0120,0xE0121,0xE0122,0xE0123, + 0xE0124,0xE0125,0xE0126,0xE0127,0xE0128,0xE0129,0xE012A,0xE012B, + 0xE012C,0xE012D,0xE012E,0xE012F,0xE0130,0xE0131,0xE0132,0xE0133, + 0xE0134,0xE0135,0xE0136,0xE0137,0xE0138,0xE0139,0xE013A,0xE013B, + 0xE013C,0xE013D,0xE013E,0xE013F,0xE0140,0xE0141,0xE0142,0xE0143, + 0xE0144,0xE0145,0xE0146,0xE0147,0xE0148,0xE0149,0xE014A,0xE014B, + 0xE014C,0xE014D,0xE014E,0xE014F,0xE0150,0xE0151,0xE0152,0xE0153, + 0xE0154,0xE0155,0xE0156,0xE0157,0xE0158,0xE0159,0xE015A,0xE015B, + 0xE015C,0xE015D,0xE015E,0xE015F,0xE0160,0xE0161,0xE0162,0xE0163, + 0xE0164,0xE0165,0xE0166,0xE0167,0xE0168,0xE0169,0xE016A,0xE016B, + 0xE016C,0xE016D,0xE016E,0xE016F,0xE0170,0xE0171,0xE0172,0xE0173, + 0xE0174,0xE0175,0xE0176,0xE0177,0xE0178,0xE0179,0xE017A,0xE017B, + 0xE017C,0xE017D,0xE017E,0xE017F,0xE0180,0xE0181,0xE0182,0xE0183, + 0xE0184,0xE0185,0xE0186,0xE0187,0xE0188,0xE0189,0xE018A,0xE018B, + 0xE018C,0xE018D,0xE018E,0xE018F,0xE0190,0xE0191,0xE0192,0xE0193, + 0xE0194,0xE0195,0xE0196,0xE0197,0xE0198,0xE0199,0xE019A,0xE019B, + 0xE019C,0xE019D,0xE019E,0xE019F,0xE01A0,0xE01A1,0xE01A2,0xE01A3, + 0xE01A4,0xE01A5,0xE01A6,0xE01A7,0xE01A8,0xE01A9,0xE01AA,0xE01AB, + 0xE01AC,0xE01AD,0xE01AE,0xE01AF,0xE01B0,0xE01B1,0xE01B2,0xE01B3, + 0xE01B4,0xE01B5,0xE01B6,0xE01B7,0xE01B8,0xE01B9,0xE01BA,0xE01BB, + 0xE01BC,0xE01BD,0xE01BE,0xE01BF,0xE01C0,0xE01C1,0xE01C2,0xE01C3, + 0xE01C4,0xE01C5,0xE01C6,0xE01C7,0xE01C8,0xE01C9,0xE01CA,0xE01CB, + 0xE01CC,0xE01CD,0xE01CE,0xE01CF,0xE01D0,0xE01D1,0xE01D2,0xE01D3, + 0xE01D4,0xE01D5,0xE01D6,0xE01D7,0xE01D8,0xE01D9,0xE01DA,0xE01DB, + 0xE01DC,0xE01DD,0xE01DE,0xE01DF,0xE01E0,0xE01E1,0xE01E2,0xE01E3, + 0xE01E4,0xE01E5,0xE01E6,0xE01E7,0xE01E8,0xE01E9,0xE01EA,0xE01EB, + 0xE01EC,0xE01ED,0xE01EE,0xE01EF, +}; + +const size_t bc_history_combo_chars_len = + sizeof(bc_history_combo_chars) / sizeof(bc_history_combo_chars[0]); + +#if BC_DEBUG_CODE +FILE *bc_history_debug_fp = NULL; +#endif // BC_DEBUG_CODE +#endif // BC_ENABLE_HISTORY + +const char bc_func_main[] = "(main)"; +const char bc_func_read[] = "(read)"; + +#if BC_DEBUG_CODE +const char bc_inst_chars[] = + "edED_^*/%+-=;?~<>!|&`{}@[],NVMACaI.LlrOqpQsSJjPR$bHovxzatfcZdghkKuU"; +#endif // BC_DEBUG_CODE + +#if BC_ENABLED +const BcLexKeyword bc_lex_kws[] = { + BC_LEX_KW_ENTRY("auto", 4, true), + BC_LEX_KW_ENTRY("break", 5, true), + BC_LEX_KW_ENTRY("continue", 8, false), + BC_LEX_KW_ENTRY("define", 6, true), + BC_LEX_KW_ENTRY("for", 3, true), + BC_LEX_KW_ENTRY("if", 2, true), + BC_LEX_KW_ENTRY("limits", 6, false), + BC_LEX_KW_ENTRY("return", 6, true), + BC_LEX_KW_ENTRY("while", 5, true), + BC_LEX_KW_ENTRY("halt", 4, false), + BC_LEX_KW_ENTRY("last", 4, false), + BC_LEX_KW_ENTRY("ibase", 5, true), + BC_LEX_KW_ENTRY("obase", 5, true), + BC_LEX_KW_ENTRY("scale", 5, true), + BC_LEX_KW_ENTRY("length", 6, true), + BC_LEX_KW_ENTRY("print", 5, false), + BC_LEX_KW_ENTRY("sqrt", 4, true), + BC_LEX_KW_ENTRY("abs", 3, false), + BC_LEX_KW_ENTRY("quit", 4, true), + BC_LEX_KW_ENTRY("read", 4, false), + BC_LEX_KW_ENTRY("maxibase", 8, false), + BC_LEX_KW_ENTRY("maxobase", 8, false), + BC_LEX_KW_ENTRY("maxscale", 8, false), + BC_LEX_KW_ENTRY("else", 4, false), +}; + +const size_t bc_lex_kws_len = sizeof(bc_lex_kws) / sizeof(BcLexKeyword); + +const char* const bc_parse_const1 = "1"; + +// This is an array that corresponds to token types. An entry is +// true if the token is valid in an expression, false otherwise. +const uint8_t bc_parse_exprs[] = { + BC_PARSE_EXPR_ENTRY(false, false, true, true, true, true, true, true), + BC_PARSE_EXPR_ENTRY(true, true, true, true, true, true, true, true), + BC_PARSE_EXPR_ENTRY(true, true, true, true, true, true, true, true), +#if BC_ENABLE_EXTRA_MATH + BC_PARSE_EXPR_ENTRY(true, true, true, true, true, true, true, true), + BC_PARSE_EXPR_ENTRY(true, true, false, false, true, true, false, false), + BC_PARSE_EXPR_ENTRY(false, false, false, false, false, true, true, false), + BC_PARSE_EXPR_ENTRY(false, false, false, false, false, false, false, false), + BC_PARSE_EXPR_ENTRY(false, true, true, true, true, true, false, true), + BC_PARSE_EXPR_ENTRY(true, false, true, true, true, true, false, 0), +#else // BC_ENABLE_EXTRA_MATH + BC_PARSE_EXPR_ENTRY(true, true, true, false, false, true, true, false), + BC_PARSE_EXPR_ENTRY(false, false, false, false, false, false, true, true), + BC_PARSE_EXPR_ENTRY(false, false, false, false, false, false, false, false), + BC_PARSE_EXPR_ENTRY(false, false, true, true, true, true, true, false), + BC_PARSE_EXPR_ENTRY(true, true, false, true, true, true, true, false) +#endif // BC_ENABLE_EXTRA_MATH +}; + +// This is an array of data for operators that correspond to token types. +const uchar bc_parse_ops[] = { + BC_PARSE_OP(0, false), BC_PARSE_OP(0, false), + BC_PARSE_OP(1, false), BC_PARSE_OP(1, false), +#if BC_ENABLE_EXTRA_MATH + BC_PARSE_OP(2, false), +#endif // BC_ENABLE_EXTRA_MATH + BC_PARSE_OP(4, false), + BC_PARSE_OP(5, true), BC_PARSE_OP(5, true), BC_PARSE_OP(5, true), + BC_PARSE_OP(6, true), BC_PARSE_OP(6, true), +#if BC_ENABLE_EXTRA_MATH + BC_PARSE_OP(3, false), + BC_PARSE_OP(7, true), BC_PARSE_OP(7, true), +#endif // BC_ENABLE_EXTRA_MATH + BC_PARSE_OP(9, true), BC_PARSE_OP(9, true), BC_PARSE_OP(9, true), + BC_PARSE_OP(9, true), BC_PARSE_OP(9, true), BC_PARSE_OP(9, true), + BC_PARSE_OP(11, true), BC_PARSE_OP(10, true), + BC_PARSE_OP(8, false), BC_PARSE_OP(8, false), BC_PARSE_OP(8, false), + BC_PARSE_OP(8, false), BC_PARSE_OP(8, false), BC_PARSE_OP(8, false), +#if BC_ENABLE_EXTRA_MATH + BC_PARSE_OP(8, false), BC_PARSE_OP(8, false), BC_PARSE_OP(8, false), +#endif // BC_ENABLE_EXTRA_MATH + BC_PARSE_OP(8, false), +}; + +// These identify what tokens can come after expressions in certain cases. +const BcParseNext bc_parse_next_expr = + BC_PARSE_NEXT(4, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF); +const BcParseNext bc_parse_next_param = + BC_PARSE_NEXT(2, BC_LEX_RPAREN, BC_LEX_COMMA); +const BcParseNext bc_parse_next_print = + BC_PARSE_NEXT(4, BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF); +const BcParseNext bc_parse_next_rel = BC_PARSE_NEXT(1, BC_LEX_RPAREN); +const BcParseNext bc_parse_next_elem = BC_PARSE_NEXT(1, BC_LEX_RBRACKET); +const BcParseNext bc_parse_next_for = BC_PARSE_NEXT(1, BC_LEX_SCOLON); +const BcParseNext bc_parse_next_read = + BC_PARSE_NEXT(2, BC_LEX_NLINE, BC_LEX_EOF); +#endif // BC_ENABLED + +#if DC_ENABLED +const uint8_t dc_lex_regs[] = { + BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE, + BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON, + BC_LEX_KW_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN, + BC_LEX_STORE_PUSH, +}; + +const size_t dc_lex_regs_len = sizeof(dc_lex_regs) / sizeof(uint8_t); + +const uchar dc_lex_tokens[] = { +#if BC_ENABLE_EXTRA_MATH + BC_LEX_OP_TRUNC, +#else // BC_ENABLE_EXTRA_MATH + BC_LEX_INVALID, +#endif // BC_ENABLE_EXTRA_MATH + BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN, + BC_LEX_RPAREN, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID, + BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE, + BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, + BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, + BC_LEX_INVALID, BC_LEX_INVALID, + BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ, + BC_LEX_OP_REL_LT, BC_LEX_KW_READ, +#if BC_ENABLE_EXTRA_MATH + BC_LEX_OP_PLACES, +#else // BC_ENABLE_EXTRA_MATH + BC_LEX_INVALID, +#endif // BC_ENABLE_EXTRA_MATH + BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, + BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, +#if BC_ENABLE_EXTRA_MATH + BC_LEX_OP_LSHIFT, +#else // BC_ENABLE_EXTRA_MATH + BC_LEX_INVALID, +#endif // BC_ENABLE_EXTRA_MATH + BC_LEX_KW_IBASE, BC_LEX_INVALID, BC_LEX_KW_SCALE, BC_LEX_LOAD_POP, + BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KW_OBASE, BC_LEX_PRINT_STREAM, + BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_KW_MAXIBASE, + BC_LEX_KW_MAXOBASE, BC_LEX_KW_MAXSCALE, BC_LEX_INVALID, + BC_LEX_SCALE_FACTOR, + BC_LEX_INVALID, BC_LEX_KW_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, + BC_LEX_INVALID, BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID, + BC_LEX_ASCIIFY, BC_LEX_KW_ABS, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE, + BC_LEX_KW_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, +#if BC_ENABLE_EXTRA_MATH + BC_LEX_OP_RSHIFT, +#else // BC_ENABLE_EXTRA_MATH + BC_LEX_INVALID, +#endif // BC_ENABLE_EXTRA_MATH + BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD, + BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KW_PRINT, + BC_LEX_KW_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID, + BC_LEX_INVALID, BC_LEX_KW_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE, + BC_LEX_INVALID, BC_LEX_STACK_LEVEL, + BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_RBRACE, BC_LEX_OP_DIVMOD, + BC_LEX_INVALID +}; + +const uchar dc_parse_insts[] = { + BC_INST_INVALID, BC_INST_INVALID, +#if BC_ENABLED + BC_INST_INVALID, BC_INST_INVALID, +#endif // BC_ENABLED + BC_INST_INVALID, BC_INST_BOOL_NOT, +#if BC_ENABLE_EXTRA_MATH + BC_INST_TRUNC, +#endif // BC_ENABLE_EXTRA_MATH + BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, BC_INST_MODULUS, + BC_INST_PLUS, BC_INST_MINUS, +#if BC_ENABLE_EXTRA_MATH + BC_INST_PLACES, + BC_INST_LSHIFT, BC_INST_RSHIFT, +#endif // BC_ENABLE_EXTRA_MATH + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, + BC_INST_INVALID, BC_INST_INVALID, + BC_INST_INVALID, BC_INST_INVALID, +#if BC_ENABLED + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, + BC_INST_INVALID, BC_INST_INVALID, +#if BC_ENABLE_EXTRA_MATH + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, +#endif // BC_ENABLE_EXTRA_MATH +#endif // BC_ENABLED + BC_INST_INVALID, + BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_REL_LT, + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, + BC_INST_INVALID, BC_INST_REL_LE, + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, +#if BC_ENABLED + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, +#endif // BC_ENABLED + BC_INST_IBASE, BC_INST_OBASE, BC_INST_SCALE, BC_INST_LENGTH, BC_INST_PRINT, + BC_INST_SQRT, BC_INST_ABS, BC_INST_QUIT, BC_INST_INVALID, BC_INST_MAXIBASE, + BC_INST_MAXOBASE, BC_INST_MAXSCALE, BC_INST_INVALID, + BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, + BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, + BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, + BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID, + BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, + BC_INST_PRINT_POP, BC_INST_NQUIT, BC_INST_SCALE_FUNC, +}; +#endif // DC_ENABLED + +const char bc_parse_one[] = "1"; + +const char bc_num_hex_digits[] = "0123456789ABCDEF"; + +const BcBigDig bc_num_pow10[BC_BASE_DIGS + 1] = { + 1, + 10, + 100, + 1000, + 10000, +#if BC_BASE_DIGS > 4 + 100000, + 1000000, + 10000000, + 100000000, + 1000000000, +#endif // BC_BASE_DIGS > 4 +}; + +const BcNumBinaryOp bc_program_ops[] = { + bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub, +#if BC_ENABLE_EXTRA_MATH + bc_num_places, bc_num_lshift, bc_num_rshift, +#endif // BC_ENABLE_EXTRA_MATH +}; + +const BcNumBinaryOpReq bc_program_opReqs[] = { + bc_num_powReq, bc_num_mulReq, bc_num_mulReq, bc_num_mulReq, + bc_num_addReq, bc_num_addReq, +#if BC_ENABLE_EXTRA_MATH + bc_num_placesReq, bc_num_placesReq, bc_num_placesReq, +#endif // BC_ENABLE_EXTRA_MATH +}; + +const BcProgramUnary bc_program_unarys[] = { + bc_program_negate, bc_program_not, +#if BC_ENABLE_EXTRA_MATH + bc_program_trunc, +#endif // BC_ENABLE_EXTRA_MATH +}; + +const char bc_program_exprs_name[] = ""; + +const char bc_program_stdin_name[] = ""; +#if BC_ENABLE_SIGNALS +const char bc_program_ready_msg[] = "ready for more input\n"; +const size_t bc_program_ready_msg_len = sizeof(bc_program_ready_msg) - 1; +#endif // BC_ENABLE_SIGNALS +const char bc_program_esc_chars[] = "ab\\efnqrt"; +const char bc_program_esc_seqs[] = "\a\b\\\\\f\n\"\r\t"; Index: contrib/bc/src/dc/dc.c =================================================================== --- contrib/bc/src/dc/dc.c +++ contrib/bc/src/dc/dc.c @@ -0,0 +1,63 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The main procedure of dc. + * + */ + +#if DC_ENABLED + +#include + +#include +#include +#include + +int dc_main(int argc, char **argv) { + + BcStatus s; + + vm->read_ret = BC_INST_POP_EXEC; + vm->help = dc_help; +#if BC_ENABLE_SIGNALS + vm->sigmsg = dc_sig_msg; + vm->siglen = (uchar) strlen(vm->sigmsg); +#endif // BC_ENABLE_SIGNALS + + vm->next = dc_lex_token; + vm->parse = dc_parse_parse; + vm->expr = dc_parse_expr; + + s = bc_vm_boot(argc, argv, "DC_LINE_LENGTH", "DC_ENV_ARGS", "DC_EXPR_EXIT"); + + return (int) s; +} +#endif // DC_ENABLED Index: contrib/bc/src/dc/lex.c =================================================================== --- contrib/bc/src/dc/lex.c +++ contrib/bc/src/dc/lex.c @@ -0,0 +1,204 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The lexer for dc. + * + */ + +#if DC_ENABLED + +#include + +#include +#include +#include +#include + +bool dc_lex_negCommand(BcLex *l) { + char c = l->buf[l->i]; + return !BC_LEX_NUM_CHAR(c, false, false); +} + +static BcStatus dc_lex_register(BcLex *l) { + + if (DC_X && isspace(l->buf[l->i - 1])) { + + char c; + + bc_lex_whitespace(l); + c = l->buf[l->i]; + + if (!isalnum(c) && c != '_') + return bc_lex_verr(l, BC_ERROR_PARSE_CHAR, c); + + l->i += 1; + bc_lex_name(l); + } + else { + bc_vec_npop(&l->str, l->str.len); + bc_vec_pushByte(&l->str, (uchar) l->buf[l->i - 1]); + bc_vec_pushByte(&l->str, '\0'); + l->t = BC_LEX_NAME; + } + + return BC_STATUS_SUCCESS; +} + +static BcStatus dc_lex_string(BcLex *l) { + + size_t depth = 1, nls = 0, i = l->i; + char c; + + l->t = BC_LEX_STR; + bc_vec_npop(&l->str, l->str.len); + + for (; (c = l->buf[i]) && depth; ++i) { + + if (c == '\\') c = l->buf[++i]; + else { + depth += (c == '['); + depth -= (c == ']'); + } + + nls += (c == '\n'); + + if (depth) bc_vec_push(&l->str, &c); + } + + if (BC_ERR(c == '\0' && depth)) { + l->i = i; + return bc_lex_err(l, BC_ERROR_PARSE_STRING); + } + + bc_vec_pushByte(&l->str, '\0'); + + l->i = i; + l->line += nls; + + return BC_STATUS_SUCCESS; +} + +BcStatus dc_lex_token(BcLex *l) { + + BcStatus s = BC_STATUS_SUCCESS; + char c = l->buf[l->i++], c2; + size_t i; + + for (i = 0; i < dc_lex_regs_len; ++i) { + if (l->last == dc_lex_regs[i]) return dc_lex_register(l); + } + + if (c >= '$' && c <= '~' && + (l->t = dc_lex_tokens[(c - '$')]) != BC_LEX_INVALID) + { + return s; + } + + // This is the workhorse of the lexer. + switch (c) { + + case '\0': + case '\n': + case '\t': + case '\v': + case '\f': + case '\r': + case ' ': + { + bc_lex_commonTokens(l, c); + break; + } + + case '!': + { + c2 = l->buf[l->i]; + + if (c2 == '=') l->t = BC_LEX_OP_REL_NE; + else if (c2 == '<') l->t = BC_LEX_OP_REL_LE; + else if (c2 == '>') l->t = BC_LEX_OP_REL_GE; + else return bc_lex_invalidChar(l, c); + + l->i += 1; + break; + } + + case '#': + { + bc_lex_lineComment(l); + break; + } + + case '.': + { + c2 = l->buf[l->i]; + if (BC_LIKELY(BC_LEX_NUM_CHAR(c2, true, false))) + s = bc_lex_number(l, c); + else s = bc_lex_invalidChar(l, c); + break; + } + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + { + s = bc_lex_number(l, c); + break; + } + + case '[': + { + s = dc_lex_string(l); + break; + } + + default: + { + s = bc_lex_invalidChar(l, c); + break; + } + } + + return s; +} +#endif // DC_ENABLED Index: contrib/bc/src/dc/parse.c =================================================================== --- contrib/bc/src/dc/parse.c +++ contrib/bc/src/dc/parse.c @@ -0,0 +1,264 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The parser for dc. + * + */ + +#if DC_ENABLED + +#include +#include +#include + +#include +#include +#include +#include +#include + +static BcStatus dc_parse_register(BcParse *p, bool var) { + + BcStatus s; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + if (p->l.t != BC_LEX_NAME) return bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + + bc_parse_pushName(p, p->l.str.v, var); + + return s; +} + +static BcStatus dc_parse_string(BcParse *p) { + + BcFunc f; + + bc_program_addFunc(p->prog, &f, bc_func_main); + bc_parse_string(p); + + return bc_lex_next(&p->l); +} + +static BcStatus dc_parse_mem(BcParse *p, uchar inst, bool name, bool store) { + + BcStatus s; + + bc_parse_push(p, inst); + if (name) { + s = dc_parse_register(p, inst != BC_INST_ARRAY_ELEM); + if (BC_ERR(s)) return s; + } + + if (store) { + bc_parse_push(p, BC_INST_SWAP); + bc_parse_push(p, BC_INST_ASSIGN_NO_VAL); + } + + return bc_lex_next(&p->l); +} + +static BcStatus dc_parse_cond(BcParse *p, uchar inst) { + + BcStatus s; + + bc_parse_push(p, inst); + bc_parse_push(p, BC_INST_EXEC_COND); + + s = dc_parse_register(p, true); + if (BC_ERR(s)) return s; + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + + if (p->l.t == BC_LEX_KW_ELSE) { + s = dc_parse_register(p, true); + if (BC_ERR(s)) return s; + s = bc_lex_next(&p->l); + } + else bc_parse_pushIndex(p, SIZE_MAX); + + return s; +} + +static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags) { + + BcStatus s = BC_STATUS_SUCCESS; + uchar inst; + bool assign, get_token = false; + + switch (t) { + + case BC_LEX_OP_REL_EQ: + case BC_LEX_OP_REL_LE: + case BC_LEX_OP_REL_GE: + case BC_LEX_OP_REL_NE: + case BC_LEX_OP_REL_LT: + case BC_LEX_OP_REL_GT: + { + inst = (uchar) (t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); + s = dc_parse_cond(p, inst); + break; + } + + case BC_LEX_SCOLON: + case BC_LEX_COLON: + { + s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); + break; + } + + case BC_LEX_STR: + { + s = dc_parse_string(p); + break; + } + + case BC_LEX_NEG: + { + if (dc_lex_negCommand(&p->l)) { + bc_parse_push(p, BC_INST_NEG); + get_token = true; + break; + } + + s = bc_lex_next(&p->l); + if (BC_ERR(s)) return s; + } + // Fallthrough. + case BC_LEX_NUMBER: + { + bc_parse_number(p); + + if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); + get_token = true; + + break; + } + + case BC_LEX_KW_READ: + { + if (BC_ERR(flags & BC_PARSE_NOREAD)) + s = bc_parse_err(p, BC_ERROR_EXEC_REC_READ); + else bc_parse_push(p, BC_INST_READ); + get_token = true; + break; + } + + case BC_LEX_OP_ASSIGN: + case BC_LEX_STORE_PUSH: + { + assign = t == BC_LEX_OP_ASSIGN; + inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; + s = dc_parse_mem(p, inst, true, assign); + break; + } + + case BC_LEX_LOAD: + case BC_LEX_LOAD_POP: + { + inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; + s = dc_parse_mem(p, inst, true, false); + break; + } + + case BC_LEX_STORE_IBASE: + case BC_LEX_STORE_OBASE: + case BC_LEX_STORE_SCALE: + { + inst = (uchar) (t - BC_LEX_STORE_IBASE + BC_INST_IBASE); + s = dc_parse_mem(p, inst, false, true); + break; + } + + default: + { + s = bc_parse_err(p, BC_ERROR_PARSE_TOKEN); + get_token = true; + break; + } + } + + if (BC_NO_ERR(!s) && get_token) s = bc_lex_next(&p->l); + + return s; +} + +BcStatus dc_parse_expr(BcParse *p, uint8_t flags) { + + BcStatus s = BC_STATUS_SUCCESS; + BcInst inst; + BcLexType t; + bool have_expr = false, need_expr = (flags & BC_PARSE_NOREAD) != 0; + + while (BC_NO_SIG && BC_NO_ERR(!s) && (t = p->l.t) != BC_LEX_EOF) { + + if (t == BC_LEX_NLINE) { + s = bc_lex_next(&p->l); + continue; + } + + inst = dc_parse_insts[t]; + + if (inst != BC_INST_INVALID) { + bc_parse_push(p, inst); + s = bc_lex_next(&p->l); + } + else s = dc_parse_token(p, t, flags); + + have_expr = true; + } + + if (BC_NO_ERR(!s)) { + if (BC_SIG) s = BC_STATUS_SIGNAL; + else if (BC_ERR(need_expr && !have_expr)) + s = bc_vm_err(BC_ERROR_EXEC_READ_EXPR); + else if (p->l.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL)) + bc_parse_push(p, BC_INST_POP_EXEC); + } + + return s; +} + +BcStatus dc_parse_parse(BcParse *p) { + + BcStatus s; + + assert(p); + + if (BC_ERR(p->l.t == BC_LEX_EOF)) s = bc_parse_err(p, BC_ERROR_PARSE_EOF); + else s = dc_parse_expr(p, 0); + + if (BC_ERR(s) || BC_SIG) s = bc_parse_reset(p, s); + + return s; +} +#endif // DC_ENABLED Index: contrib/bc/src/history/history.c =================================================================== --- contrib/bc/src/history/history.c +++ contrib/bc/src/history/history.c @@ -0,0 +1,1374 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Adapted from the following: + * + * linenoise.c -- guerrilla line editing library against the idea that a + * line editing lib needs to be 20,000 lines of C code. + * + * You can find the original source code at: + * http://github.com/antirez/linenoise + * + * You can find the fork that this code is based on at: + * https://github.com/rain-1/linenoise-mob + * + * ------------------------------------------------------------------------ + * + * This code is also under the following license: + * + * Copyright (c) 2010-2016, Salvatore Sanfilippo + * Copyright (c) 2010-2013, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ------------------------------------------------------------------------ + * + * Does a number of crazy assumptions that happen to be true in 99.9999% of + * the 2010 UNIX computers around. + * + * References: + * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html + * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html + * + * Todo list: + * - Filter bogus Ctrl+ combinations. + * - Win32 support + * + * Bloat: + * - History search like Ctrl+r in readline? + * + * List of escape sequences used by this program, we do everything just + * with three sequences. In order to be so cheap we may have some + * flickering effect with some slow terminal, but the lesser sequences + * the more compatible. + * + * EL (Erase Line) + * Sequence: ESC [ n K + * Effect: if n is 0 or missing, clear from cursor to end of line + * Effect: if n is 1, clear from beginning of line to cursor + * Effect: if n is 2, clear entire line + * + * CUF (CUrsor Forward) + * Sequence: ESC [ n C + * Effect: moves cursor forward n chars + * + * CUB (CUrsor Backward) + * Sequence: ESC [ n D + * Effect: moves cursor backward n chars + * + * The following is used to get the terminal width if getting + * the width with the TIOCGWINSZ ioctl fails + * + * DSR (Device Status Report) + * Sequence: ESC [ 6 n + * Effect: reports the current cusor position as ESC [ n ; m R + * where n is the row and m is the column + * + * When multi line mode is enabled, we also use two additional escape + * sequences. However multi line editing is disabled by default. + * + * CUU (CUrsor Up) + * Sequence: ESC [ n A + * Effect: moves cursor up of n chars. + * + * CUD (CUrsor Down) + * Sequence: ESC [ n B + * Effect: moves cursor down of n chars. + * + * When bc_history_clearScreen() is called, two additional escape sequences + * are used in order to clear the screen and position the cursor at home + * position. + * + * CUP (CUrsor Position) + * Sequence: ESC [ H + * Effect: moves the cursor to upper left corner + * + * ED (Erase Display) + * Sequence: ESC [ 2 J + * Effect: clear the whole screen + * + * ***************************************************************************** + * + * Code for line history. + * + */ + +#if BC_ENABLE_HISTORY + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +/** + * Check if the code is a wide character. + */ +static bool bc_history_wchar(uint32_t cp) { + + size_t i; + + for (i = 0; i < bc_history_wchars_len; ++i) { + + // Ranges are listed in ascending order. Therefore, once the + // whole range is higher than the codepoint we're testing, the + // codepoint won't be found in any remaining range => bail early. + if (bc_history_wchars[i][0] > cp) return false; + + // Test this range. + if (bc_history_wchars[i][0] <= cp && cp <= bc_history_wchars[i][1]) + return true; + } + + return false; +} + +/** + * Check if the code is a combining character. + */ +static bool bc_history_comboChar(uint32_t cp) { + + size_t i; + + for (i = 0; i < bc_history_combo_chars_len; ++i) { + + // Combining chars are listed in ascending order, so once we pass + // the codepoint of interest, we know it's not a combining char. + if(bc_history_combo_chars[i] > cp) return false; + if (bc_history_combo_chars[i] == cp) return true; + } + + return false; +} + +/** + * Get length of previous UTF8 character. + */ +static size_t bc_history_prevCharLen(const char *buf, size_t pos) { + size_t end = pos; + for (pos -= 1; pos < end && (buf[pos] & 0xC0) == 0x80; --pos); + return end - (pos >= end ? 0 : pos); +} + +/** + * Convert UTF-8 to Unicode code point. + */ +static size_t bc_history_codePoint(const char *s, size_t len, uint32_t *cp) { + + if (len) { + + uchar byte = (uchar) s[0]; + + if ((byte & 0x80) == 0) { + *cp = byte; + return 1; + } + else if ((byte & 0xE0) == 0xC0) { + + if (len >= 2) { + *cp = (((uint32_t) (s[0] & 0x1F)) << 6) | + ((uint32_t) (s[1] & 0x3F)); + return 2; + } + } + else if ((byte & 0xF0) == 0xE0) { + + if (len >= 3) { + *cp = (((uint32_t) (s[0] & 0x0F)) << 12) | + (((uint32_t) (s[1] & 0x3F)) << 6) | + ((uint32_t) (s[2] & 0x3F)); + return 3; + } + } + else if ((byte & 0xF8) == 0xF0) { + + if (len >= 4) { + *cp = (((uint32_t) (s[0] & 0x07)) << 18) | + (((uint32_t) (s[1] & 0x3F)) << 12) | + (((uint32_t) (s[2] & 0x3F)) << 6) | + ((uint32_t) (s[3] & 0x3F)); + return 4; + } + } + else { + *cp = 0xFFFD; + return 1; + } + } + + *cp = 0; + + return 1; +} + +/** + * Get length of next grapheme. + */ +static size_t bc_history_nextLen(const char *buf, size_t buf_len, + size_t pos, size_t *col_len) +{ + uint32_t cp; + size_t beg = pos; + size_t len = bc_history_codePoint(buf + pos, buf_len - pos, &cp); + + if (bc_history_comboChar(cp)) { + // Currently unreachable? + return 0; + } + + if (col_len != NULL) *col_len = bc_history_wchar(cp) ? 2 : 1; + + pos += len; + + while (pos < buf_len) { + + len = bc_history_codePoint(buf + pos, buf_len - pos, &cp); + + if (!bc_history_comboChar(cp)) return pos - beg; + + pos += len; + } + + return pos - beg; +} + +/** + * Get length of previous grapheme. + */ +static size_t bc_history_prevLen(const char *buf, size_t pos, size_t *col_len) { + + size_t end = pos; + + while (pos > 0) { + + uint32_t cp; + size_t len = bc_history_prevCharLen(buf, pos); + + pos -= len; + bc_history_codePoint(buf + pos, len, &cp); + + if (!bc_history_comboChar(cp)) { + if (col_len != NULL) *col_len = bc_history_wchar(cp) ? 2 : 1; + return end - pos; + } + } + + // Currently unreachable? + return 0; +} + +/** + * Read a Unicode code point from a file. + */ +static BcStatus bc_history_readCode(char *buf, size_t buf_len, + uint32_t *cp, size_t *nread) +{ + BcStatus s = BC_STATUS_EOF; + ssize_t n; + + assert(buf_len >= 1); + + n = read(STDIN_FILENO, buf, 1); + if (BC_ERR(n <= 0)) goto err; + + uchar byte = (uchar) buf[0]; + + if ((byte & 0x80) != 0) { + + if ((byte & 0xE0) == 0xC0) { + assert(buf_len >= 2); + n = read(STDIN_FILENO, buf + 1, 1); + if (BC_ERR(n <= 0)) goto err; + } + else if ((byte & 0xF0) == 0xE0) { + assert(buf_len >= 3); + n = read(STDIN_FILENO, buf + 1, 2); + if (BC_ERR(n <= 0)) goto err; + } + else if ((byte & 0xF8) == 0xF0) { + assert(buf_len >= 3); + n = read(STDIN_FILENO, buf + 1, 3); + if (BC_ERR(n <= 0)) goto err; + } + else { + n = -1; + goto err; + } + } + + *nread = bc_history_codePoint(buf, buf_len, cp); + + return BC_STATUS_SUCCESS; + +err: + if (BC_ERR(n < 0)) s = bc_vm_err(BC_ERROR_FATAL_IO_ERR); + else *nread = (size_t) n; + return s; +} + +/** + * Get column length from begining of buffer to current byte position. + */ +static size_t bc_history_colPos(const char *buf, size_t buf_len, size_t pos) { + + size_t ret = 0, off = 0; + + while (off < pos) { + + size_t col_len, len; + + len = bc_history_nextLen(buf, buf_len, off, &col_len); + + off += len; + ret += col_len; + } + + return ret; +} + +/** + * Return true if the terminal name is in the list of terminals we know are + * not able to understand basic escape sequences. + */ +static bool bc_history_isBadTerm(void) { + + size_t i; + char *term = getenv("TERM"); + + if (term == NULL) return false; + + for (i = 0; bc_history_bad_terms[i]; ++i) { + if (!strcasecmp(term, bc_history_bad_terms[i])) return true; + } + + return false; +} + +/** + * Raw mode: 1960's black magic. + */ +static BcStatus bc_history_enableRaw(BcHistory *h) { + + struct termios raw; + + assert(BC_TTYIN); + + if (h->rawMode) return BC_STATUS_SUCCESS; + + if (BC_ERR(tcgetattr(STDIN_FILENO, &h->orig_termios) == -1)) + return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + // Modify the original mode. + raw = h->orig_termios; + + // Input modes: no break, no CR to NL, no parity check, no strip char, + // no start/stop output control. + raw.c_iflag &= (unsigned int) (~(BRKINT | ICRNL | INPCK | ISTRIP | IXON)); + + // Control modes - set 8 bit chars. + raw.c_cflag |= (CS8); + + // Local modes - choing off, canonical off, no extended functions, + // no signal chars (^Z,^C). + raw.c_lflag &= (unsigned int) (~(ECHO | ICANON | IEXTEN | ISIG)); + + // Control chars - set return condition: min number of bytes and timer. + // We want read to give every single byte, w/o timeout (1 byte, no timer). + raw.c_cc[VMIN] = 1; + raw.c_cc[VTIME] = 0; + + // Put terminal in raw mode after flushing. + if (BC_ERR(tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) < 0)) + return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + h->rawMode = true; + + return BC_STATUS_SUCCESS; +} + +static void bc_history_disableRaw(BcHistory *h) { + // Don't even check the return value as it's too late. + if (!h->rawMode) return; + if (BC_ERR(tcsetattr(STDIN_FILENO, TCSAFLUSH, &h->orig_termios) != -1)) + h->rawMode = false; +} + +/** + * Use the ESC [6n escape sequence to query the horizontal cursor position + * and return it. On error -1 is returned, on success the position of the + * cursor. + */ +static size_t bc_history_cursorPos(void) { + + char buf[64]; + size_t cols, rows, i; + + // Report cursor location. + if (BC_ERR(BC_HIST_WRITE("\x1b[6n", 4))) return SIZE_MAX; + + // Read the response: ESC [ rows ; cols R. + for (i = 0; i < sizeof(buf) - 1; ++i) { + if (read(STDIN_FILENO, buf + i, 1) != 1 || buf[i] == 'R') break; + } + + buf[i] = '\0'; + + // Parse it. + if (BC_ERR(buf[0] != BC_ACTION_ESC || buf[1] != '[')) return SIZE_MAX; + if (BC_ERR(sscanf(buf + 2, "%zu;%zu", &rows, &cols) != 2)) return SIZE_MAX; + + return cols <= UINT16_MAX ? cols : 0; +} + +/** + * Try to get the number of columns in the current terminal, or assume 80 + * if it fails. + */ +static size_t bc_history_columns(void) { + + struct winsize ws; + + if (BC_ERR(ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 || !ws.ws_col)) { + + // Calling ioctl() failed. Try to query the terminal itself. + size_t start, cols; + + // Get the initial position so we can restore it later. + start = bc_history_cursorPos(); + if (BC_ERR(start == SIZE_MAX)) return BC_HIST_DEF_COLS; + + // Go to right margin and get position. + if (BC_ERR(BC_HIST_WRITE("\x1b[999C", 6))) return BC_HIST_DEF_COLS; + cols = bc_history_cursorPos(); + if (BC_ERR(cols == SIZE_MAX)) return BC_HIST_DEF_COLS; + + // Restore position. + if (cols > start) { + + char seq[64]; + size_t len; + + snprintf(seq, 64, "\x1b[%zuD", cols - start); + len = strlen(seq); + + // If this fails, return a value that + // callers will translate into an error. + if (BC_ERR(BC_HIST_WRITE(seq, len))) return SIZE_MAX; + } + + return cols; + } + + return ws.ws_col; +} + +/** + * Check if text is an ANSI escape sequence. + */ +static bool bc_history_ansiEscape(const char *buf, size_t buf_len, size_t *len) +{ + if (buf_len > 2 && !memcmp("\033[", buf, 2)) { + + size_t off = 2; + + while (off < buf_len) { + + char c = buf[off++]; + + if ((c >= 'A' && c <= 'K' && c != 'I') || + c == 'S' || c == 'T' || c == 'f' || c == 'm') + { + *len = off; + return true; + } + } + } + + return false; +} + +/** + * Get column length of prompt text. + */ +static size_t bc_history_promptColLen(const char *prompt, size_t plen) { + + char buf[BC_HIST_MAX_LINE + 1]; + size_t buf_len = 0, off = 0; + + while (off < plen) { + + size_t len; + + if (bc_history_ansiEscape(prompt + off, plen - off, &len)) { + off += len; + continue; + } + + buf[buf_len++] = prompt[off++]; + } + + return bc_history_colPos(buf, buf_len, buf_len); +} + +/** + * Rewrites the currently edited line accordingly to the buffer content, + * cursor position, and number of columns of the terminal. + */ +static BcStatus bc_history_refresh(BcHistory *h) { + + char seq[64]; + char* buf = h->buf.v; + size_t colpos, len = BC_HIST_BUF_LEN(h), pos = h->pos; + + while(h->pcol + bc_history_colPos(buf, len, pos) >= h->cols) { + + size_t chlen = bc_history_nextLen(buf, len, 0, NULL); + + buf += chlen; + len -= chlen; + pos -= chlen; + } + + while (h->pcol + bc_history_colPos(buf, len, len) > h->cols) + len -= bc_history_prevLen(buf, len, NULL); + + bc_vec_npop(&h->tmp, h->tmp.len); + + // Cursor to left edge. + snprintf(seq, 64, "\r"); + bc_vec_string(&h->tmp, strlen(seq), seq); + + // Write the prompt and the current buffer content, if desired. + if (BC_USE_PROMPT) { + bc_vec_concat(&h->tmp, h->prompt); + bc_vec_concat(&h->tmp, buf); + } + + // Erase to right. + snprintf(seq, 64, "\x1b[0K"); + bc_vec_concat(&h->tmp, seq); + + // Move cursor to original position. + colpos = bc_history_colPos(buf, len, pos) + h->pcol; + snprintf(seq, 64, "\r\x1b[%zuC", colpos); + bc_vec_concat(&h->tmp, seq); + + if (h->tmp.len && BC_ERR(BC_HIST_WRITE(h->tmp.v, h->tmp.len - 1))) + return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + return BC_STATUS_SUCCESS; +} + +/** + * Insert the character 'c' at cursor current position. + */ +static BcStatus bc_history_edit_insert(BcHistory *h, const char *cbuf, + size_t clen) +{ + BcStatus s = BC_STATUS_SUCCESS; + + bc_vec_expand(&h->buf, bc_vm_growSize(h->buf.len, clen)); + + if (h->pos == BC_HIST_BUF_LEN(h)) { + + size_t colpos, len; + + memcpy(bc_vec_item(&h->buf, h->pos), cbuf, clen); + + h->pos += clen; + h->buf.len += clen - 1; + bc_vec_pushByte(&h->buf, '\0'); + + len = BC_HIST_BUF_LEN(h); + colpos = !bc_history_promptColLen(h->prompt, h->plen); + colpos += bc_history_colPos(h->buf.v, len, len); + + if (colpos < h->cols) { + // Avoid a full update of the line in the trivial case. + if (BC_ERR(BC_HIST_WRITE(cbuf, clen))) + s = bc_vm_err(BC_ERROR_FATAL_IO_ERR); + } + else s = bc_history_refresh(h); + } + else { + + size_t amt = BC_HIST_BUF_LEN(h) - h->pos; + + memmove(h->buf.v + h->pos + clen, h->buf.v + h->pos, amt); + memcpy(h->buf.v + h->pos, cbuf, clen); + + h->pos += clen; + h->buf.len += clen; + h->buf.v[BC_HIST_BUF_LEN(h)] = '\0'; + + s = bc_history_refresh(h); + } + + return s; +} + +/** + * Move cursor to the left. + */ +static BcStatus bc_history_edit_left(BcHistory *h) { + + if (h->pos <= 0) return BC_STATUS_SUCCESS; + + h->pos -= bc_history_prevLen(h->buf.v, h->pos, NULL); + + return bc_history_refresh(h); +} + +/** + * Move cursor on the right. +*/ +static BcStatus bc_history_edit_right(BcHistory *h) { + + if (h->pos == BC_HIST_BUF_LEN(h)) return BC_STATUS_SUCCESS; + + h->pos += bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL); + + return bc_history_refresh(h); +} + +/** + * Move cursor to the end of the current word. + */ +static BcStatus bc_history_edit_wordEnd(BcHistory *h) { + + size_t len = BC_HIST_BUF_LEN(h); + + if (!len || h->pos >= len) return BC_STATUS_SUCCESS; + + while (h->pos < len && isspace(h->buf.v[h->pos])) h->pos += 1; + while (h->pos < len && !isspace(h->buf.v[h->pos])) h->pos += 1; + + return bc_history_refresh(h); +} + +/** + * Move cursor to the start of the current word. + */ +static BcStatus bc_history_edit_wordStart(BcHistory *h) { + + size_t len = BC_HIST_BUF_LEN(h); + + if (!len) return BC_STATUS_SUCCESS; + + while (h->pos > 0 && isspace(h->buf.v[h->pos - 1])) h->pos -= 1; + while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1])) h->pos -= 1; + + return bc_history_refresh(h); +} + +/** + * Move cursor to the start of the line. + */ +static BcStatus bc_history_edit_home(BcHistory *h) { + + if (!h->pos) return BC_STATUS_SUCCESS; + + h->pos = 0; + + return bc_history_refresh(h); +} + +/** + * Move cursor to the end of the line. + */ +static BcStatus bc_history_edit_end(BcHistory *h) { + + if (h->pos == BC_HIST_BUF_LEN(h)) return BC_STATUS_SUCCESS; + + h->pos = BC_HIST_BUF_LEN(h); + + return bc_history_refresh(h); +} + +/** + * Substitute the currently edited line with the next or previous history + * entry as specified by 'dir' (direction). + */ +static BcStatus bc_history_edit_next(BcHistory *h, bool dir) { + + char *dup, *str; + + if (h->history.len <= 1) return BC_STATUS_SUCCESS; + + dup = bc_vm_strdup(h->buf.v); + + // Update the current history entry before + // overwriting it with the next one. + bc_vec_replaceAt(&h->history, h->history.len - 1 - h->idx, &dup); + + // Show the new entry. + h->idx += (dir == BC_HIST_PREV ? 1 : SIZE_MAX); + + if (h->idx == SIZE_MAX) { + h->idx = 0; + return BC_STATUS_SUCCESS; + } + else if (h->idx >= h->history.len) { + h->idx = h->history.len - 1; + return BC_STATUS_SUCCESS; + } + + str = *((char**) bc_vec_item(&h->history, h->history.len - 1 - h->idx)); + bc_vec_string(&h->buf, strlen(str), str); + assert(h->buf.len > 0); + + h->pos = BC_HIST_BUF_LEN(h); + + return bc_history_refresh(h); +} + +/** + * Delete the character at the right of the cursor without altering the cursor + * position. Basically this is what happens with the "Delete" keyboard key. + */ +static BcStatus bc_history_edit_delete(BcHistory *h) { + + size_t chlen, len = BC_HIST_BUF_LEN(h); + + if (!len || h->pos >= len) return BC_STATUS_SUCCESS; + + chlen = bc_history_nextLen(h->buf.v, len, h->pos, NULL); + + memmove(h->buf.v + h->pos, h->buf.v + h->pos + chlen, len - h->pos - chlen); + + h->buf.len -= chlen; + h->buf.v[BC_HIST_BUF_LEN(h)] = '\0'; + + return bc_history_refresh(h); +} + +static BcStatus bc_history_edit_backspace(BcHistory *h) { + + size_t chlen, len = BC_HIST_BUF_LEN(h); + + if (!h->pos || !len) return BC_STATUS_SUCCESS; + + chlen = bc_history_prevLen(h->buf.v, h->pos, NULL); + + memmove(h->buf.v + h->pos - chlen, h->buf.v + h->pos, len - h->pos); + + h->pos -= chlen; + h->buf.len -= chlen; + h->buf.v[BC_HIST_BUF_LEN(h)] = '\0'; + + return bc_history_refresh(h); +} + +/** + * Delete the previous word, maintaining the cursor at the start of the + * current word. + */ +static BcStatus bc_history_edit_deletePrevWord(BcHistory *h) { + + size_t diff, old_pos = h->pos; + + while (h->pos > 0 && h->buf.v[h->pos - 1] == ' ') --h->pos; + while (h->pos > 0 && h->buf.v[h->pos - 1] != ' ') --h->pos; + + diff = old_pos - h->pos; + memmove(h->buf.v + h->pos, h->buf.v + old_pos, + BC_HIST_BUF_LEN(h) - old_pos + 1); + h->buf.len -= diff; + + return bc_history_refresh(h); +} + +/** + * Delete the next word, maintaining the cursor at the same position. + */ +static BcStatus bc_history_edit_deleteNextWord(BcHistory *h) { + + size_t next_end = h->pos, len = BC_HIST_BUF_LEN(h); + + while (next_end < len && h->buf.v[next_end] == ' ') ++next_end; + while (next_end < len && h->buf.v[next_end] != ' ') ++next_end; + + memmove(h->buf.v + h->pos, h->buf.v + next_end, len - next_end); + + h->buf.len -= next_end - h->pos; + + return bc_history_refresh(h); +} + +static BcStatus bc_history_swap(BcHistory *h) { + + BcStatus s = BC_STATUS_SUCCESS; + size_t pcl, ncl; + char auxb[5]; + + pcl = bc_history_prevLen(h->buf.v, h->pos, NULL); + ncl = bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL); + + // To perform a swap we need: + // * nonzero char length to the left + // * not at the end of the line + if (pcl && h->pos != BC_HIST_BUF_LEN(h) && pcl < 5 && ncl < 5) { + + memcpy(auxb, h->buf.v + h->pos - pcl, pcl); + memcpy(h->buf.v + h->pos - pcl, h->buf.v + h->pos, ncl); + memcpy(h->buf.v + h->pos - pcl + ncl, auxb, pcl); + + h->pos += -pcl + ncl; + + s = bc_history_refresh(h); + } + + return s; +} + +/** + * Handle escape sequences. + */ +static BcStatus bc_history_escape(BcHistory *h) { + + BcStatus s = BC_STATUS_SUCCESS; + char c, seq[3]; + + if (BC_ERR(BC_HIST_READ(seq, 1))) return s; + + c = seq[0]; + + // ESC ? sequences. + if (c != '[' && c != 'O') { + if (c == 'f') s = bc_history_edit_wordEnd(h); + else if (c == 'b') s = bc_history_edit_wordStart(h); + else if (c == 'd') s = bc_history_edit_deleteNextWord(h); + } + else { + + if (BC_ERR(BC_HIST_READ(seq + 1, 1))) + return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + // ESC [ sequences. + if (c == '[') { + + c = seq[1]; + + if (c >= '0' && c <= '9') { + + // Extended escape, read additional byte. + if (BC_ERR(BC_HIST_READ(seq + 2, 1))) + return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + if (seq[2] == '~' && c == '3') s = bc_history_edit_delete(h); + else if(seq[2] == ';') { + + if (BC_ERR(BC_HIST_READ(seq, 2))) + return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + if (seq[0] != '5') return s; + else if (seq[1] == 'C') s = bc_history_edit_wordEnd(h); + else if (seq[1] == 'D') s = bc_history_edit_wordStart(h); + } + } + else { + + switch(c) { + + // Up. + case 'A': + { + s = bc_history_edit_next(h, BC_HIST_PREV); + break; + } + + // Down. + case 'B': + { + s = bc_history_edit_next(h, BC_HIST_NEXT); + break; + } + + // Right. + case 'C': + { + s = bc_history_edit_right(h); + break; + } + + // Left. + case 'D': + { + s = bc_history_edit_left(h); + break; + } + + // Home. + case 'H': + case '1': + { + s = bc_history_edit_home(h); + break; + } + + // End. + case 'F': + case '4': + { + s = bc_history_edit_end(h); + break; + } + + case 'd': + { + s = bc_history_edit_deleteNextWord(h); + break; + } + } + } + } + // ESC O sequences. + else if (c == 'O') { + if (seq[1] == 'H') s = bc_history_edit_home(h); + else if (seq[1] == 'F') s = bc_history_edit_end(h); + } + } + + return s; +} + +static BcStatus bc_history_reset(BcHistory *h) { + + h->oldcolpos = h->pos = h->idx = 0; + h->cols = bc_history_columns(); + + // Check for error from bc_history_columns. + if (BC_ERR(h->cols == SIZE_MAX)) return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + // The latest history entry is always our current buffer, that + // initially is just an empty string. + bc_history_add(h, bc_vm_strdup("")); + + // Buffer starts empty. + bc_vec_empty(&h->buf); + + return BC_STATUS_SUCCESS; +} + +static BcStatus bc_history_printCtrl(BcHistory *h, unsigned int c) { + + BcStatus s; + char str[3] = "^A"; + const char newline[2] = "\n"; + + str[1] = (char) (c + 'A' - BC_ACTION_CTRL_A); + + bc_vec_concat(&h->buf, str); + + s = bc_history_refresh(h); + if (BC_ERR(s)) return s; + + bc_vec_npop(&h->buf, sizeof(str)); + bc_vec_pushByte(&h->buf, '\0'); + + if (c != BC_ACTION_CTRL_C && c != BC_ACTION_CTRL_D) { + + if (BC_ERR(BC_HIST_WRITE(newline, sizeof(newline) - 1))) + return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + s = bc_history_refresh(h); + } + + return s; +} + +/** + * This function is the core of the line editing capability of bc history. + * It expects 'fd' to be already in "raw mode" so that every key pressed + * will be returned ASAP to read(). + */ +static BcStatus bc_history_edit(BcHistory *h, const char *prompt) { + + BcStatus s = bc_history_reset(h); + + if (BC_ERR(s)) return s; + + if (BC_USE_PROMPT) { + + h->prompt = prompt; + h->plen = strlen(prompt); + h->pcol = bc_history_promptColLen(prompt, h->plen); + + if (BC_ERR(BC_HIST_WRITE(prompt, h->plen))) + return bc_vm_err(BC_ERROR_FATAL_IO_ERR); + } + + while (BC_NO_ERR(!s)) { + + // Large enough for any encoding? + char cbuf[32]; + unsigned int c = 0; + size_t nread = 0; + + s = bc_history_readCode(cbuf, sizeof(cbuf), &c, &nread); + if (BC_ERR(s)) return s; + + switch (c) { + + case BC_ACTION_LINE_FEED: + case BC_ACTION_ENTER: + { + bc_vec_pop(&h->history); + return s; + } + + case BC_ACTION_TAB: + { + memcpy(cbuf, bc_history_tab, bc_history_tab_len + 1); + s = bc_history_edit_insert(h, cbuf, bc_history_tab_len); + break; + } + + case BC_ACTION_CTRL_C: + { + s = bc_history_printCtrl(h, c); + if (BC_ERR(s)) return s; + +#if BC_ENABLE_SIGNALS + s = bc_history_reset(h); + if (BC_ERR(s)) break; + + if (BC_ERR(BC_HIST_WRITE(vm->sigmsg, vm->siglen)) || + BC_ERR(BC_HIST_WRITE(bc_program_ready_msg, + bc_program_ready_msg_len))) + { + s = bc_vm_err(BC_ERROR_FATAL_IO_ERR); + } + else s = bc_history_refresh(h); + + break; +#else // BC_ENABLE_SIGNALS + if (BC_ERR(BC_HIST_BAD_WRITE("\n", 1))) + bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + // Make sure the terminal is back to normal before exiting. + bc_vm_shutdown(); + exit((((uchar) 1) << (CHAR_BIT - 1)) + SIGINT); +#endif // BC_ENABLE_SIGNALS + } + + case BC_ACTION_BACKSPACE: + case BC_ACTION_CTRL_H: + { + s = bc_history_edit_backspace(h); + break; + } + + // Act as end-of-file. + case BC_ACTION_CTRL_D: + { + s = BC_STATUS_EOF; + break; + } + + // Swaps current character with previous. + case BC_ACTION_CTRL_T: + { + s = bc_history_swap(h); + break; + } + + case BC_ACTION_CTRL_B: + { + s = bc_history_edit_left(h); + break; + } + + case BC_ACTION_CTRL_F: + { + s = bc_history_edit_right(h); + break; + } + + case BC_ACTION_CTRL_P: + { + s = bc_history_edit_next(h, BC_HIST_PREV); + break; + } + + case BC_ACTION_CTRL_N: + { + s = bc_history_edit_next(h, BC_HIST_NEXT); + break; + } + + case BC_ACTION_ESC: + { + s = bc_history_escape(h); + break; + } + + // Delete the whole line. + case BC_ACTION_CTRL_U: + { + bc_vec_string(&h->buf, 0, ""); + h->pos = 0; + + s = bc_history_refresh(h); + + break; + } + + // Delete from current to end of line. + case BC_ACTION_CTRL_K: + { + bc_vec_npop(&h->buf, h->buf.len - h->pos); + bc_vec_pushByte(&h->buf, '\0'); + s = bc_history_refresh(h); + break; + } + + // Go to the start of the line. + case BC_ACTION_CTRL_A: + { + s = bc_history_edit_home(h); + break; + } + + // Go to the end of the line. + case BC_ACTION_CTRL_E: + { + s = bc_history_edit_end(h); + break; + } + + // Clear screen. + case BC_ACTION_CTRL_L: + { + if (BC_ERR(BC_HIST_WRITE("\x1b[H\x1b[2J", 7))) + s = bc_vm_err(BC_ERROR_FATAL_IO_ERR); + if (BC_NO_ERR(!s)) s = bc_history_refresh(h); + break; + } + + // Delete previous word. + case BC_ACTION_CTRL_W: + { + s = bc_history_edit_deletePrevWord(h); + break; + } + + default: + { + if (c >= BC_ACTION_CTRL_A && c <= BC_ACTION_CTRL_Z) + s = bc_history_printCtrl(h, c); + else s = bc_history_edit_insert(h, cbuf, nread); + break; + } + } + } + + return s; +} + +static bool bc_history_stdinHasData(BcHistory *h) { + int n; + return pselect(1, &h->rdset, NULL, NULL, &h->ts, &h->sigmask) > 0 || + (ioctl(STDIN_FILENO, FIONREAD, &n) >= 0 && n > 0); +} + +/** + * This function calls the line editing function bc_history_edit() + * using the STDIN file descriptor set in raw mode. + */ +static BcStatus bc_history_raw(BcHistory *h, const char *prompt) { + + BcStatus s; + + s = bc_history_enableRaw(h); + if (BC_ERR(s)) return s; + + s = bc_history_edit(h, prompt); + + h->stdin_has_data = bc_history_stdinHasData(h); + if (!h->stdin_has_data) bc_history_disableRaw(h); + + if (BC_NO_ERR(!s) && BC_ERR(BC_HIST_WRITE("\n", 1))) + s = bc_vm_err(BC_ERROR_FATAL_IO_ERR); + + return s; +} + +BcStatus bc_history_line(BcHistory *h, BcVec *vec, const char *prompt) { + + BcStatus s; + char* line; + + if (BC_TTYIN && !vm->history.badTerm) { + + s = bc_history_raw(h, prompt); + if (BC_ERR(s && s != BC_STATUS_EOF)) return s; + + bc_vec_string(vec, BC_HIST_BUF_LEN(h), h->buf.v); + + line = bc_vm_strdup(h->buf.v); + bc_history_add(h, line); + + bc_vec_concat(vec, "\n"); + } + else s = bc_read_chars(vec, prompt); + + return s; +} + +void bc_history_add(BcHistory *h, char *line) { + + if (h->history.len) { + if (!strcmp(*((char**) bc_vec_item_rev(&h->history, 0)), line)) { + free(line); + return; + } + } + + if (h->history.len == BC_HIST_MAX_LEN) bc_vec_popAt(&h->history, 0); + + bc_vec_push(&h->history, &line); +} + +void bc_history_init(BcHistory *h) { + + bc_vec_init(&h->buf, sizeof(char), NULL); + bc_vec_init(&h->history, sizeof(char*), bc_string_free); + bc_vec_init(&h->tmp, sizeof(char), NULL); + + FD_ZERO(&h->rdset); + FD_SET(STDIN_FILENO, &h->rdset); + h->ts.tv_sec = 0; + h->ts.tv_nsec = 0; + + sigemptyset(&h->sigmask); + sigaddset(&h->sigmask, SIGINT); + + h->rawMode = h->stdin_has_data = false; + h->badTerm = bc_history_isBadTerm(); +} + +void bc_history_free(BcHistory *h) { + bc_history_disableRaw(h); +#ifndef NDEBUG + bc_vec_free(&h->buf); + bc_vec_free(&h->history); + bc_vec_free(&h->tmp); +#endif // NDEBUG +} + +/** + * This special mode is used by bc history in order to print scan codes + * on screen for debugging / development purposes. + */ +#if BC_DEBUG_CODE +BcStatus bc_history_printKeyCodes(BcHistory *h) { + + BcStatus s; + char quit[4]; + + bc_vm_printf("Linenoise key codes debugging mode.\n" + "Press keys to see scan codes. " + "Type 'quit' at any time to exit.\n"); + + s = bc_history_enableRaw(h); + if (BC_ERR(s)) return s; + memset(quit, ' ', 4); + + while(true) { + + char c; + ssize_t nread; + + nread = read(STDIN_FILENO, &c, 1); + if (nread <= 0) continue; + + // Shift string to left. + memmove(quit, quit + 1, sizeof(quit) - 1); + + // Insert current char on the right. + quit[sizeof(quit) - 1] = c; + if (!memcmp(quit, "quit", sizeof(quit))) break; + + printf("'%c' %02x (%d) (type quit to exit)\n", + isprint(c) ? c : '?', (int) c, (int) c); + + // Go left edge manually, we are in raw mode. + bc_vm_putchar('\r'); + bc_vm_fflush(stdout); + } + + bc_history_disableRaw(h); + + return s; +} +#endif // BC_DEBUG_CODE + +#endif // BC_ENABLE_HISTORY Index: contrib/bc/src/lang.c =================================================================== --- contrib/bc/src/lang.c +++ contrib/bc/src/lang.c @@ -0,0 +1,263 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code to manipulate data structures in programs. + * + */ + +#include +#include +#include + +#include +#include + +int bc_id_cmp(const BcId *e1, const BcId *e2) { + return strcmp(e1->name, e2->name); +} + +#ifndef NDEBUG +void bc_id_free(void *id) { + assert(id); + free(((BcId*) id)->name); +} +#endif // NDEBUG + +void bc_string_free(void *string) { + assert(string && *((char**) string)); + free(*((char**) string)); +} + +void bc_const_free(void *constant) { + BcConst *c = constant; + assert(c->val); + free(c->val); + bc_num_free(&c->num); +} + +#if BC_ENABLED +BcStatus bc_func_insert(BcFunc *f, BcProgram *p, char *name, + BcType type, size_t line) +{ + BcLoc a; + size_t i, idx; + + assert(f); + + idx = bc_program_search(p, name, type == BC_TYPE_VAR); + + for (i = 0; i < f->autos.len; ++i) { + BcLoc *id = bc_vec_item(&f->autos, i); + if (BC_ERR(idx == id->loc && type == (BcType) id->idx)) { + const char *array = type == BC_TYPE_ARRAY ? "[]" : ""; + return bc_vm_error(BC_ERROR_PARSE_DUP_LOCAL, line, name, array); + } + } + + a.loc = idx; + a.idx = type; + + bc_vec_push(&f->autos, &a); + + return BC_STATUS_SUCCESS; +} +#endif // BC_ENABLED + +void bc_func_init(BcFunc *f, const char *name) { + assert(f && name); + bc_vec_init(&f->code, sizeof(uchar), NULL); + bc_vec_init(&f->strs, sizeof(char*), bc_string_free); + bc_vec_init(&f->consts, sizeof(BcConst), bc_const_free); +#if BC_ENABLED + bc_vec_init(&f->autos, sizeof(BcLoc), NULL); + bc_vec_init(&f->labels, sizeof(size_t), NULL); + f->nparams = 0; + f->voidfn = false; +#endif // BC_ENABLED + f->name = name; +} + +void bc_func_reset(BcFunc *f) { + assert(f); + bc_vec_npop(&f->code, f->code.len); + bc_vec_npop(&f->strs, f->strs.len); + bc_vec_npop(&f->consts, f->consts.len); +#if BC_ENABLED + bc_vec_npop(&f->autos, f->autos.len); + bc_vec_npop(&f->labels, f->labels.len); + f->nparams = 0; + f->voidfn = false; +#endif // BC_ENABLED +} + +void bc_func_free(void *func) { + BcFunc *f = (BcFunc*) func; + assert(f); + bc_vec_free(&f->code); + bc_vec_free(&f->strs); + bc_vec_free(&f->consts); +#if BC_ENABLED + bc_vec_free(&f->autos); + bc_vec_free(&f->labels); +#endif // BC_ENABLED +} + +void bc_array_init(BcVec *a, bool nums) { + if (nums) bc_vec_init(a, sizeof(BcNum), bc_num_free); + else bc_vec_init(a, sizeof(BcVec), bc_vec_free); + bc_array_expand(a, 1); +} + +void bc_array_copy(BcVec *d, const BcVec *s) { + + size_t i; + + assert(d && s && d != s && d->size == s->size && d->dtor == s->dtor); + + bc_vec_npop(d, d->len); + bc_vec_expand(d, s->cap); + d->len = s->len; + + for (i = 0; i < s->len; ++i) { + BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i); + bc_num_createCopy(dnum, snum); + } +} + +void bc_array_expand(BcVec *a, size_t len) { + + assert(a); + + bc_vec_expand(a, len); + + if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { + BcNum n; + while (len > a->len) { + bc_num_init(&n, BC_NUM_DEF_SIZE); + bc_vec_push(a, &n); + } + } + else { + BcVec v; + assert(a->size == sizeof(BcVec) && a->dtor == bc_vec_free); + while (len > a->len) { + bc_array_init(&v, true); + bc_vec_push(a, &v); + } + } +} + +#if DC_ENABLED +void bc_result_copy(BcResult *d, BcResult *src) { + + assert(d && src); + + d->t = src->t; + + switch (d->t) { + + case BC_RESULT_TEMP: + case BC_RESULT_IBASE: + case BC_RESULT_SCALE: + case BC_RESULT_OBASE: + { + bc_num_createCopy(&d->d.n, &src->d.n); + break; + } + + case BC_RESULT_VAR: + case BC_RESULT_ARRAY: + case BC_RESULT_ARRAY_ELEM: + { + memcpy(&d->d.loc, &src->d.loc, sizeof(BcLoc)); + break; + } + + case BC_RESULT_CONSTANT: + case BC_RESULT_STR: + { + memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); + break; + } + + case BC_RESULT_ONE: + { + // Do nothing. + break; + } + +#if BC_ENABLED + case BC_RESULT_VOID: + case BC_RESULT_LAST: + { +#ifndef NDEBUG + assert(false); + break; +#endif // NDEBUG + } +#endif // BC_ENABLED + } +} +#endif // DC_ENABLED + +void bc_result_free(void *result) { + + BcResult *r = (BcResult*) result; + + assert(r); + + switch (r->t) { + + case BC_RESULT_TEMP: + case BC_RESULT_IBASE: + case BC_RESULT_SCALE: + case BC_RESULT_OBASE: + { + bc_num_free(&r->d.n); + break; + } + + case BC_RESULT_VAR: + case BC_RESULT_ARRAY: + case BC_RESULT_ARRAY_ELEM: + case BC_RESULT_STR: + case BC_RESULT_CONSTANT: + case BC_RESULT_ONE: +#if BC_ENABLED + case BC_RESULT_VOID: + case BC_RESULT_LAST: +#endif // BC_ENABLED + { + // Do nothing. + break; + } + } +} Index: contrib/bc/src/lex.c =================================================================== --- contrib/bc/src/lex.c +++ contrib/bc/src/lex.c @@ -0,0 +1,240 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Common code for the lexers. + * + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +BcStatus bc_lex_invalidChar(BcLex *l, char c) { + l->t = BC_LEX_INVALID; + return bc_lex_verr(l, BC_ERROR_PARSE_CHAR, c); +} + +void bc_lex_lineComment(BcLex *l) { + l->t = BC_LEX_WHITESPACE; + while (l->i < l->len && l->buf[l->i] != '\n') l->i += 1; +} + +BcStatus bc_lex_comment(BcLex *l) { + + size_t i, nlines = 0; + const char *buf = l->buf; + bool end = false; + char c; + + l->i += 1; + l->t = BC_LEX_WHITESPACE; + + for (i = l->i; !end; i += !end) { + + for (; (c = buf[i]) && c != '*'; ++i) nlines += (c == '\n'); + + if (BC_ERR(!c || buf[i + 1] == '\0')) { + l->i = i; + return bc_lex_err(l, BC_ERROR_PARSE_COMMENT); + } + + end = buf[i + 1] == '/'; + } + + l->i = i + 2; + l->line += nlines; + + return BC_STATUS_SUCCESS; +} + +void bc_lex_whitespace(BcLex *l) { + char c; + l->t = BC_LEX_WHITESPACE; + for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); +} + +void bc_lex_commonTokens(BcLex *l, char c) { + if (!c) l->t = BC_LEX_EOF; + else if (c == '\n') l->t = BC_LEX_NLINE; + else bc_lex_whitespace(l); +} + +static size_t bc_lex_num(BcLex *l, char start, bool int_only) { + + const char *buf = l->buf + l->i; + size_t i; + char c; + bool last_pt, pt = (start == '.'); + + for (i = 0; (c = buf[i]) && (BC_LEX_NUM_CHAR(c, pt, int_only) || + (c == '\\' && buf[i + 1] == '\n')); ++i) + { + if (c == '\\') { + + if (buf[i + 1] == '\n') { + + i += 2; + + // Make sure to eat whitespace at the beginning of the line. + while(isspace(buf[i]) && buf[i] != '\n') i += 1; + + c = buf[i]; + + if (!BC_LEX_NUM_CHAR(c, pt, int_only)) break; + } + else break; + } + + last_pt = (c == '.'); + if (pt && last_pt) break; + pt = pt || last_pt; + + bc_vec_push(&l->str, &c); + } + + return i; +} + +BcStatus bc_lex_number(BcLex *l, char start) { + + l->t = BC_LEX_NUMBER; + + bc_vec_npop(&l->str, l->str.len); + bc_vec_push(&l->str, &start); + + l->i += bc_lex_num(l, start, false); + +#if BC_ENABLE_EXTRA_MATH + { + char c = l->buf[l->i]; + + if (c == 'e') { + +#if BC_ENABLED + if (BC_IS_POSIX) { + BcStatus s = bc_lex_err(l, BC_ERROR_POSIX_EXP_NUM); + if (BC_ERR(s)) return s; + } +#endif // BC_ENABLED + + bc_vec_push(&l->str, &c); + l->i += 1; + c = l->buf[l->i]; + + if (c == BC_LEX_NEG_CHAR) { + bc_vec_push(&l->str, &c); + l->i += 1; + c = l->buf[l->i]; + } + + if (BC_ERR(!BC_LEX_NUM_CHAR(c, false, true))) + return bc_lex_verr(l, BC_ERROR_PARSE_CHAR, c); + + l->i += bc_lex_num(l, 0, true); + } + } +#endif // BC_ENABLE_EXTRA_MATH + + bc_vec_pushByte(&l->str, '\0'); + + return BC_STATUS_SUCCESS; +} + +void bc_lex_name(BcLex *l) { + + size_t i = 0; + const char *buf = l->buf + l->i - 1; + char c = buf[i]; + + l->t = BC_LEX_NAME; + + while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i]; + + bc_vec_string(&l->str, i, buf); + + // Increment the index. We minus 1 because it has already been incremented. + l->i += i - 1; +} + +void bc_lex_init(BcLex *l) { + assert(l); + bc_vec_init(&l->str, sizeof(char), NULL); +} + +void bc_lex_free(BcLex *l) { + assert(l); + bc_vec_free(&l->str); +} + +void bc_lex_file(BcLex *l, const char *file) { + assert(l && file); + l->line = 1; + vm->file = file; +} + +BcStatus bc_lex_next(BcLex *l) { + + BcStatus s; + + assert(l); + + l->last = l->t; + l->line += (l->i != 0 && l->buf[l->i - 1] == '\n'); + + if (BC_ERR(l->last == BC_LEX_EOF)) return bc_lex_err(l, BC_ERROR_PARSE_EOF); + + l->t = BC_LEX_EOF; + + if (l->i == l->len) return BC_STATUS_SUCCESS; + + // Loop until failure or we don't have whitespace. This + // is so the parser doesn't get inundated with whitespace. + do { + s = vm->next(l); + } while (BC_NO_ERR(!s) && l->t == BC_LEX_WHITESPACE); + + return s; +} + +BcStatus bc_lex_text(BcLex *l, const char *text) { + assert(l && text); + l->buf = text; + l->i = 0; + l->len = strlen(text); + l->t = l->last = BC_LEX_INVALID; + return bc_lex_next(l); +} Index: contrib/bc/src/main.c =================================================================== --- contrib/bc/src/main.c +++ contrib/bc/src/main.c @@ -0,0 +1,75 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * The entry point for bc. + * + */ + +#include +#include + +#include +#include + +#include +#include +#include +#include + +BcVm *vm; + +int main(int argc, char *argv[]) { + + int s; + char *name; + size_t len = strlen(BC_EXECPREFIX); + + vm = calloc(1, sizeof(BcVm)); + if (BC_ERR(!vm)) return (int) bc_vm_err(BC_ERROR_FATAL_ALLOC_ERR); + + vm->locale = setlocale(LC_ALL, ""); + + name = strrchr(argv[0], '/'); + vm->name = !name ? argv[0] : name + 1; + + if (strlen(vm->name) > len) vm->name += len; + +#if !DC_ENABLED + s = bc_main(argc, argv); +#elif !BC_ENABLED + s = dc_main(argc, argv); +#else + if (BC_IS_BC) s = bc_main(argc, argv); + else s = dc_main(argc, argv); +#endif + + return s; +} Index: contrib/bc/src/num.c =================================================================== --- contrib/bc/src/num.c +++ contrib/bc/src/num.c @@ -0,0 +1,2484 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code for the number type. + * + */ + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); + +static ssize_t bc_num_neg(size_t n, bool neg) { + return (((ssize_t) n) ^ -((ssize_t) neg)) + neg; +} + +ssize_t bc_num_cmpZero(const BcNum *n) { + return bc_num_neg((n)->len != 0, (n)->neg); +} + +static size_t bc_num_int(const BcNum *n) { + return n->len ? n->len - n->rdx : 0; +} + +static void bc_num_expand(BcNum *restrict n, size_t req) { + assert(n); + req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; + if (req > n->cap) { + n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req)); + n->cap = req; + } +} + +static void bc_num_setToZero(BcNum *restrict n, size_t scale) { + assert(n); + n->scale = scale; + n->len = n->rdx = 0; + n->neg = false; +} + +static void bc_num_zero(BcNum *restrict n) { + bc_num_setToZero(n, 0); +} + +void bc_num_one(BcNum *restrict n) { + bc_num_setToZero(n, 0); + n->len = 1; + n->num[0] = 1; +} + +static void bc_num_clean(BcNum *restrict n) { + while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len; + if (BC_NUM_ZERO(n)) n->neg = false; + else if (n->len < n->rdx) n->len = n->rdx; +} + +static size_t bc_num_log10(size_t i) { + size_t len; + for (len = 1; i; i /= BC_BASE, ++len); + assert(len - 1 <= BC_BASE_DIGS + 1); + return len - 1; +} + +static size_t bc_num_zeroDigits(const BcDig *n) { + return BC_BASE_DIGS - bc_num_log10((size_t) *n); +} + +static size_t bc_num_intDigits(const BcNum *n) { + size_t digits = bc_num_int(n) * BC_BASE_DIGS; + if (digits > 0) digits -= bc_num_zeroDigits(n->num + n->len - 1); + return digits; +} + +static size_t bc_num_nonzeroLen(const BcNum *restrict n) { + size_t i, len = n->len; + assert(len == n->rdx); + for (i = len - 1; i < len && !n->num[i]; --i); + assert(i + 1 > 0); + return i + 1; +} + +static BcBigDig bc_num_addDigit(BcDig *restrict num, BcBigDig d, BcBigDig c) { + d += c; + *num = (BcDig) (d % BC_BASE_POW); + assert(*num >= 0 && *num < BC_BASE_POW); + return d / BC_BASE_POW; +} + +static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b, + size_t len) +{ + size_t i; + BcBigDig carry = 0; + + for (i = 0; BC_NO_SIG && i < len; ++i) { + BcBigDig in = ((BcBigDig) a[i]) + ((BcBigDig) b[i]); + carry = bc_num_addDigit(a + i, in, carry); + } + + for (; BC_NO_SIG && carry; ++i) + carry = bc_num_addDigit(a + i, (BcBigDig) a[i], carry); + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b, + size_t len) +{ + size_t i; + BcBigDig acc; + bool carry = false; + + for (i = 0; i < len; ++i) { + + BcBigDig sub; + + acc = (BcBigDig) a[i]; + sub = ((BcBigDig) b[i]) + carry; + + carry = (acc < sub); + acc += carry ? BC_BASE_POW : 0; + + assert(acc - sub < BC_BASE_POW); + + a[i] = (BcDig) (acc - sub); + } + + while (carry) { + acc = (BcBigDig) a[i]; + carry = (acc == 0); + a[i] = (BcDig) (carry ? BC_BASE_POW - 1 : acc - 1); + i += 1; + } + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static BcStatus bc_num_mulArray(const BcNum *restrict a, BcBigDig b, + BcNum *restrict c) +{ + size_t i; + BcBigDig carry = 0; + + assert(b <= BC_BASE_POW); + + if (a->len + 1 > c->cap) bc_num_expand(c, a->len + 1); + + memset(c->num, 0, BC_NUM_SIZE(c->cap)); + + for (i = 0; BC_NO_SIG && i < a->len; ++i) { + BcBigDig in = ((BcBigDig) a->num[i]) * b + carry; + c->num[i] = in % BC_BASE_POW; + carry = in / BC_BASE_POW; + } + + if (BC_NO_SIG) { + assert(carry < BC_BASE_POW); + c->num[i] = (BcDig) carry; + c->len = a->len; + c->len += (carry != 0); + } + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static BcStatus bc_num_divArray(const BcNum *restrict a, BcBigDig b, + BcNum *restrict c, BcBigDig *rem) +{ + size_t i; + BcBigDig carry = 0; + + assert(c->cap >= a->len); + + for (i = a->len - 1; BC_NO_SIG && i < a->len; --i) { + BcBigDig in = ((BcBigDig) a->num[i]) + carry * BC_BASE_POW; + assert(in / b < BC_BASE_POW); + c->num[i] = (BcDig) (in / b); + carry = in % b; + } + + c->len = a->len; + bc_num_clean(c); + *rem = carry; + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b, + size_t len) +{ + size_t i; + BcDig c = 0; + for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i); + return BC_SIG ? BC_NUM_CMP_SIGNAL_VAL : bc_num_neg(i + 1, c < 0); +} + +ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) { + + size_t i, min, a_int, b_int, diff; + BcDig *max_num, *min_num; + bool a_max, neg = false; + ssize_t cmp; + + assert(a && b); + + if (a == b) return 0; + if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg); + if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a); + if (a->neg) { + if (b->neg) neg = true; + else return -1; + } + else if (b->neg) return 1; + + a_int = bc_num_int(a); + b_int = bc_num_int(b); + a_int -= b_int; + a_max = (a->rdx > b->rdx); + + if (a_int) return (ssize_t) a_int; + + if (a_max) { + min = b->rdx; + diff = a->rdx - b->rdx; + max_num = a->num + diff; + min_num = b->num; + } + else { + min = a->rdx; + diff = b->rdx - a->rdx; + max_num = b->num + diff; + min_num = a->num; + } + + cmp = bc_num_compare(max_num, min_num, b_int + min); + +#if BC_ENABLE_SIGNALS + if (BC_NUM_CMP_SIGNAL(cmp)) return cmp; +#endif // BC_ENABLE_SIGNALS + + if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg); + + for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) { + if (max_num[i]) return bc_num_neg(1, !a_max == !neg); + } + + return BC_SIG ? BC_NUM_CMP_SIGNAL_VAL : 0; +} + +void bc_num_truncate(BcNum *restrict n, size_t places) { + + size_t places_rdx; + + if (!places) return; + + places_rdx = n->rdx - BC_NUM_RDX(n->scale - places); + assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len)); + + n->scale -= places; + n->rdx -= places_rdx; + + if (BC_NUM_NONZERO(n)) { + + size_t pow; + + pow = n->scale % BC_BASE_DIGS; + pow = pow ? BC_BASE_DIGS - pow : 0; + pow = bc_num_pow10[pow]; + + n->len -= places_rdx; + memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len)); + + // Clear the lower part of the last digit. + if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow; + + bc_num_clean(n); + } +} + +static void bc_num_extend(BcNum *restrict n, size_t places) { + + size_t places_rdx; + + if (!places) return; + if (BC_NUM_ZERO(n)) { + n->scale += places; + return; + } + + places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx; + + if (places_rdx) { + bc_num_expand(n, bc_vm_growSize(n->len, places_rdx)); + memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len)); + memset(n->num, 0, BC_NUM_SIZE(places_rdx)); + } + + n->rdx += places_rdx; + n->scale += places; + n->len += places_rdx; + + assert(n->rdx == BC_NUM_RDX(n->scale)); +} + +static void bc_num_retireMul(BcNum *restrict n, size_t scale, + bool neg1, bool neg2) +{ + if (n->scale < scale) bc_num_extend(n, scale - n->scale); + else bc_num_truncate(n, n->scale - scale); + + bc_num_clean(n); + if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2); +} + +static void bc_num_split(const BcNum *restrict n, size_t idx, + BcNum *restrict a, BcNum *restrict b) +{ + if (idx < n->len) { + + b->len = n->len - idx; + a->len = idx; + a->scale = a->rdx = b->scale = b->rdx = 0; + + memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len)); + memcpy(a->num, n->num, BC_NUM_SIZE(idx)); + + bc_num_clean(b); + } + else bc_num_copy(a, n); + + bc_num_clean(a); +} + +static size_t bc_num_shiftZero(BcNum *restrict n) { + + size_t i; + + assert(!n->rdx || BC_NUM_ZERO(n)); + + for (i = 0; i < n->len && !n->num[i]; ++i); + + n->len -= i; + n->num += i; + + return i; +} + +static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) { + n->len += places_rdx; + n->num -= places_rdx; +} + +static BcStatus bc_num_shift(BcNum *restrict n, BcBigDig dig) { + + size_t i, len = n->len; + BcBigDig carry = 0, pow; + BcDig *ptr = n->num; + + assert(dig < BC_BASE_DIGS); + + pow = bc_num_pow10[dig]; + dig = bc_num_pow10[BC_BASE_DIGS - dig]; + + for (i = len - 1; BC_NO_SIG && i < len; --i) { + BcBigDig in, temp; + in = ((BcBigDig) ptr[i]); + temp = carry * dig; + carry = in % pow; + ptr[i] = ((BcDig) (in / pow)) + (BcDig) temp; + } + + assert(!carry); + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) { + + BcStatus s = BC_STATUS_SUCCESS; + BcBigDig dig; + size_t places_rdx; + bool shift; + + if (!places) return s; + if (places > n->scale) { + size_t size = bc_vm_growSize(BC_NUM_RDX(places - n->scale), n->len); + if (size > SIZE_MAX - 1) return bc_vm_err(BC_ERROR_MATH_OVERFLOW); + } + if (BC_NUM_ZERO(n)) { + if (n->scale >= places) n->scale -= places; + else n->scale = 0; + return s; + } + + dig = (BcBigDig) (places % BC_BASE_DIGS); + shift = (dig != 0); + places_rdx = BC_NUM_RDX(places); + + if (n->scale) { + + if (n->rdx >= places_rdx) { + + size_t mod = n->scale % BC_BASE_DIGS, revdig; + + mod = mod ? mod : BC_BASE_DIGS; + revdig = dig ? BC_BASE_DIGS - dig : 0; + + if (mod + revdig > BC_BASE_DIGS) places_rdx = 1; + else places_rdx = 0; + } + else places_rdx -= n->rdx; + } + + if (places_rdx) { + bc_num_expand(n, bc_vm_growSize(n->len, places_rdx)); + memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len)); + memset(n->num, 0, BC_NUM_SIZE(places_rdx)); + n->len += places_rdx; + } + + if (places > n->scale) n->scale = n->rdx = 0; + else { + n->scale -= places; + n->rdx = BC_NUM_RDX(n->scale); + } + + if (shift) s = bc_num_shift(n, BC_BASE_DIGS - dig); + + bc_num_clean(n); + + return BC_SIG && !s ? BC_STATUS_SIGNAL : s; +} + +static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) { + + BcStatus s = BC_STATUS_SUCCESS; + BcBigDig dig; + size_t places_rdx, scale, scale_mod, int_len, expand; + bool shift; + + if (!places) return s; + if (BC_NUM_ZERO(n)) { + n->scale += places; + bc_num_expand(n, BC_NUM_RDX(n->scale)); + return s; + } + + dig = (BcBigDig) (places % BC_BASE_DIGS); + shift = (dig != 0); + scale = n->scale; + scale_mod = scale % BC_BASE_DIGS; + scale_mod = scale_mod ? scale_mod : BC_BASE_DIGS; + int_len = bc_num_int(n); + places_rdx = BC_NUM_RDX(places); + + if (scale_mod + dig > BC_BASE_DIGS) { + expand = places_rdx - 1; + places_rdx = 1; + } + else { + expand = places_rdx; + places_rdx = 0; + } + + if (expand > int_len) expand -= int_len; + else expand = 0; + + bc_num_extend(n, places_rdx * BC_BASE_DIGS); + bc_num_expand(n, bc_vm_growSize(expand, n->len)); + memset(n->num + n->len, 0, BC_NUM_SIZE(expand)); + n->len += expand; + n->scale = n->rdx = 0; + + if (shift) s = bc_num_shift(n, dig); + + n->scale = scale + places; + n->rdx = BC_NUM_RDX(n->scale); + + bc_num_clean(n); + + assert(n->rdx <= n->len && n->len <= n->cap); + assert(n->rdx == BC_NUM_RDX(n->scale)); + + return BC_SIG && !s ? BC_STATUS_SIGNAL : s; +} + +static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) { + + BcNum one; + BcDig num[2]; + + assert(BC_NUM_NONZERO(a)); + + bc_num_setup(&one, num, sizeof(num) / sizeof(BcDig)); + bc_num_one(&one); + + return bc_num_div(&one, a, b, scale); +} + +#if BC_ENABLE_EXTRA_MATH +static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c, + BcBigDig *v) +{ + if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); + bc_num_copy(c, a); + return bc_num_bigdig(b, v); +} +#endif // BC_ENABLE_EXTRA_MATH + +static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { + + BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; + size_t i, max, min_rdx, min_int, diff, a_int, b_int; + BcBigDig carry; + + // Because this function doesn't need to use scale (per the bc spec), + // I am hijacking it to say whether it's doing an add or a subtract. + + if (BC_NUM_ZERO(a)) { + bc_num_copy(c, b); + if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg; + return BC_STATUS_SUCCESS; + } + if (BC_NUM_ZERO(b)) { + bc_num_copy(c, a); + return BC_STATUS_SUCCESS; + } + + c->neg = a->neg; + c->rdx = BC_MAX(a->rdx, b->rdx); + c->scale = BC_MAX(a->scale, b->scale); + min_rdx = BC_MIN(a->rdx, b->rdx); + + if (a->rdx > b->rdx) { + diff = a->rdx - b->rdx; + ptr = a->num; + ptr_a = a->num + diff; + ptr_b = b->num; + } + else { + diff = b->rdx - a->rdx; + ptr = b->num; + ptr_a = a->num; + ptr_b = b->num + diff; + } + + for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i]; + + c->len = diff; + ptr_c += diff; + a_int = bc_num_int(a); + b_int = bc_num_int(b); + + if (a_int > b_int) { + min_int = b_int; + max = a_int; + ptr = ptr_a; + } + else { + min_int = a_int; + max = b_int; + ptr = ptr_b; + } + + for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) { + BcBigDig in = ((BcBigDig) ptr_a[i]) + ((BcBigDig) ptr_b[i]); + carry = bc_num_addDigit(ptr_c + i, in, carry); + } + + for (; BC_NO_SIG && i < max + min_rdx; ++i) + carry = bc_num_addDigit(ptr_c + i, (BcBigDig) ptr[i], carry); + + c->len += i; + + if (carry) c->num[c->len++] = (BcDig) carry; + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { + + BcStatus s; + ssize_t cmp; + BcNum *minuend, *subtrahend; + size_t start; + bool aneg, bneg, neg; + + // Because this function doesn't need to use scale (per the bc spec), + // I am hijacking it to say whether it's doing an add or a subtract. + + if (BC_NUM_ZERO(a)) { + bc_num_copy(c, b); + if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg; + return BC_STATUS_SUCCESS; + } + if (BC_NUM_ZERO(b)) { + bc_num_copy(c, a); + return BC_STATUS_SUCCESS; + } + + aneg = a->neg; + bneg = b->neg; + a->neg = b->neg = false; + + cmp = bc_num_cmp(a, b); + + a->neg = aneg; + b->neg = bneg; + +#if BC_ENABLE_SIGNALS + if (BC_NUM_CMP_SIGNAL(cmp)) return BC_STATUS_SIGNAL; +#endif // BC_ENABLE_SIGNALS + + if (!cmp) { + bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); + return BC_STATUS_SUCCESS; + } + + if (cmp > 0) { + neg = a->neg; + minuend = a; + subtrahend = b; + } + else { + neg = b->neg; + if (sub) neg = !neg; + minuend = b; + subtrahend = a; + } + + bc_num_copy(c, minuend); + c->neg = neg; + + if (c->scale < subtrahend->scale) { + bc_num_extend(c, subtrahend->scale - c->scale); + start = 0; + } + else start = c->rdx - subtrahend->rdx; + + s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); + + bc_num_clean(c); + + return s; +} + +static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c) +{ + size_t i, alen = a->len, blen = b->len, clen; + BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c; + BcBigDig sum = 0, carry = 0; + + assert(sizeof(sum) >= sizeof(BcDig) * 2); + assert(!a->rdx && !b->rdx); + + clen = bc_vm_growSize(alen, blen); + bc_num_expand(c, bc_vm_growSize(clen, 1)); + + ptr_c = c->num; + memset(ptr_c, 0, BC_NUM_SIZE(c->cap)); + + for (i = 0; BC_NO_SIG && i < clen; ++i) { + + ssize_t sidx = (ssize_t) (i - blen + 1); + size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1); + + for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) { + + sum += ((BcBigDig) ptr_a[j]) * ((BcBigDig) ptr_b[k]); + + if (sum >= BC_BASE_POW) { + carry += sum / BC_BASE_POW; + sum %= BC_BASE_POW; + } + } + + ptr_c[i] = (BcDig) sum; + assert(ptr_c[i] < BC_BASE_POW); + sum = carry; + carry = 0; + } + + if (sum) { + assert(sum < BC_BASE_POW); + ptr_c[clen] = (BcDig) sum; + clen += 1; + } + + c->len = clen; + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a, + size_t shift, BcNumShiftAddOp op) +{ + assert(n->len >= shift + a->len); + assert(!n->rdx && !a->rdx); + return op(n->num + shift, a->num, a->len); +} + +static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) { + + BcStatus s; + size_t max, max2, total; + BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; + BcDig *digs, *dig_ptr; + BcNumShiftAddOp op; + bool aone = BC_NUM_ONE(a); + + assert(BC_NUM_ZERO(c)); + + // This is here because the function is recursive. + if (BC_SIG) return BC_STATUS_SIGNAL; + if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) return BC_STATUS_SUCCESS; + if (aone || BC_NUM_ONE(b)) { + bc_num_copy(c, aone ? b : a); + if ((aone && a->neg) || b->neg) c->neg = !c->neg; + return BC_STATUS_SUCCESS; + } + if (a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) + return bc_num_m_simp(a, b, c); + + max = BC_MAX(a->len, b->len); + max = BC_MAX(max, BC_NUM_DEF_SIZE); + max2 = (max + 1) / 2; + + total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max); + digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total)); + + bc_num_setup(&l1, dig_ptr, max); + dig_ptr += max; + bc_num_setup(&h1, dig_ptr, max); + dig_ptr += max; + bc_num_setup(&l2, dig_ptr, max); + dig_ptr += max; + bc_num_setup(&h2, dig_ptr, max); + dig_ptr += max; + bc_num_setup(&m1, dig_ptr, max); + dig_ptr += max; + bc_num_setup(&m2, dig_ptr, max); + max = bc_vm_growSize(max, 1); + bc_num_init(&z0, max); + bc_num_init(&z1, max); + bc_num_init(&z2, max); + max = bc_vm_growSize(max, max) + 1; + bc_num_init(&temp, max); + + bc_num_split(a, max2, &l1, &h1); + bc_num_clean(&l1); + bc_num_clean(&h1); + bc_num_split(b, max2, &l2, &h2); + bc_num_clean(&l2); + bc_num_clean(&h2); + + bc_num_expand(c, max); + c->len = max; + memset(c->num, 0, BC_NUM_SIZE(c->len)); + + s = bc_num_sub(&h1, &l1, &m1, 0); + if (BC_ERR(s)) goto err; + s = bc_num_sub(&l2, &h2, &m2, 0); + if (BC_ERR(s)) goto err; + + if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) { + + s = bc_num_m(&h1, &h2, &z2, 0); + if (BC_ERR(s)) goto err; + bc_num_clean(&z2); + + s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays); + if (BC_ERR(s)) goto err; + s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays); + if (BC_ERR(s)) goto err; + } + + if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) { + + s = bc_num_m(&l1, &l2, &z0, 0); + if (BC_ERR(s)) goto err; + bc_num_clean(&z0); + + s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays); + if (BC_ERR(s)) goto err; + s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays); + if (BC_ERR(s)) goto err; + } + + if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) { + + s = bc_num_m(&m1, &m2, &z1, 0); + if (BC_ERR(s)) goto err; + bc_num_clean(&z1); + + op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays; + s = bc_num_shiftAddSub(c, &z1, max2, op); + if (BC_ERR(s)) goto err; + } + +err: + free(digs); + bc_num_free(&temp); + bc_num_free(&z2); + bc_num_free(&z1); + bc_num_free(&z0); + return s; +} + +static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { + + BcStatus s; + BcNum cpa, cpb; + size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale; + + bc_num_setToZero(c, 0); + ascale = a->scale; + bscale = b->scale; + scale = BC_MAX(scale, ascale); + scale = BC_MAX(scale, bscale); + + rscale = ascale + bscale; + scale = BC_MIN(rscale, scale); + + if ((a->len == 1 || b->len == 1) && !a->rdx && !b->rdx) { + + BcNum *operand; + BcBigDig dig; + + if (a->len == 1) { + dig = (BcBigDig) a->num[0]; + operand = b; + } + else { + dig = (BcBigDig) b->num[0]; + operand = a; + } + + s = bc_num_mulArray(operand, dig, c); + if (BC_ERROR_SIGNAL_ONLY(s)) return s; + + if (BC_NUM_NONZERO(c)) c->neg = (a->neg != b->neg); + + return s; + } + + bc_num_init(&cpa, a->len + a->rdx); + bc_num_init(&cpb, b->len + b->rdx); + bc_num_copy(&cpa, a); + bc_num_copy(&cpb, b); + + cpa.neg = cpb.neg = false; + + ardx = cpa.rdx * BC_BASE_DIGS; + s = bc_num_shiftLeft(&cpa, ardx); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + bc_num_clean(&cpa); + azero = bc_num_shiftZero(&cpa); + + brdx = cpb.rdx * BC_BASE_DIGS; + s = bc_num_shiftLeft(&cpb, brdx); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + bzero = bc_num_shiftZero(&cpb); + bc_num_clean(&cpb); + + s = bc_num_k(&cpa, &cpb, c); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + zero = bc_vm_growSize(azero, bzero); + len = bc_vm_growSize(c->len, zero); + + bc_num_expand(c, len); + s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_DIGS); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + s = bc_num_shiftRight(c, ardx + brdx); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + bc_num_retireMul(c, scale, a->neg, b->neg); + +err: + bc_num_unshiftZero(&cpb, bzero); + bc_num_free(&cpb); + bc_num_unshiftZero(&cpa, azero); + bc_num_free(&cpa); + return s; +} + +static bool bc_num_nonZeroDig(BcDig *restrict a, size_t len) +{ + size_t i; + bool nonzero = false; + for (i = len - 1; !nonzero && i < len; --i) nonzero = (a[i] != 0); + return nonzero; +} + +static ssize_t bc_num_divCmp(const BcDig *a, const BcNum *b, size_t len) { + + ssize_t cmp; + + if (b->len > len && a[len]) cmp = bc_num_compare(a, b->num, len + 1); + else if (b->len <= len) { + if (a[len]) cmp = 1; + else cmp = bc_num_compare(a, b->num, len); + } + else cmp = -1; + + return cmp; +} + +static BcStatus bc_num_divExtend(BcNum *restrict a, BcNum *restrict b, + BcBigDig divisor) +{ + BcStatus s; + size_t pow; + + pow = BC_BASE_DIGS - bc_num_log10((size_t) divisor); + + s = bc_num_shiftLeft(a, pow); + if (BC_ERROR_SIGNAL_ONLY(s)) return s; + + return bc_num_shiftLeft(b, pow); +} + +static BcStatus bc_num_d_long(BcNum *restrict a, BcNum *restrict b, + BcNum *restrict c, size_t scale) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcBigDig divisor; + size_t len, end, i, rdx; + BcNum cpb; + bool nonzero = false; + + len = b->len; + end = a->len - len; + assert(len >= 1); + + bc_num_expand(c, a->len); + memset(c->num, 0, c->cap * sizeof(BcDig)); + + c->rdx = a->rdx; + c->scale = a->scale; + c->len = a->len; + + divisor = (BcBigDig) b->num[len - 1]; + + if (len > 1 && bc_num_nonZeroDig(b->num, len - 1)) { + + nonzero = (divisor > 1 << ((10 * BC_BASE_DIGS) / 6 + 1)); + + if (!nonzero) { + + s = bc_num_divExtend(a, b, divisor); + if (BC_ERROR_SIGNAL_ONLY(s)) return s; + + bc_num_expand(a, a->len + 1); + a->len += 1; + + len = b->len; + end = a->len - len; + divisor = (BcBigDig) b->num[len - 1]; + + nonzero = bc_num_nonZeroDig(b->num, len - 1); + } + } + + divisor += nonzero; + + bc_num_expand(c, a->len); + memset(c->num, 0, c->cap * sizeof(BcDig)); + + assert(c->scale >= scale); + rdx = c->rdx - BC_NUM_RDX(scale); + + bc_num_init(&cpb, len + 1); + + i = end - 1; + + for (; BC_NO_SIG && i < end && i >= rdx && BC_NUM_NONZERO(a); --i) { + + ssize_t cmp; + BcDig *n; + BcBigDig q, result; + + n = a->num + i; + result = q = 0; + + cmp = bc_num_divCmp(n, b, len); + +#if BC_ENABLE_SIGNALS + if (BC_NUM_CMP_SIGNAL(cmp)) goto err; +#endif // BC_ENABLE_SIGNALS + + while (cmp >= 0) { + + BcBigDig n1, dividend; + + n1 = (BcBigDig) n[len]; + dividend = n1 * BC_BASE_POW + (BcBigDig) n[len - 1]; + q = (dividend / divisor); + + if (q <= 1) { + q = 1; + s = bc_num_subArrays(n, b->num, len); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + else { + + assert(q <= BC_BASE_POW); + + s = bc_num_mulArray(b, (BcBigDig) q, &cpb); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + s = bc_num_subArrays(n, cpb.num, cpb.len); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + + result += q; + assert(result <= BC_BASE_POW); + + bc_num_clean(a); + + if (a->len > i) { + cmp = bc_num_divCmp(n, b, len); +#if BC_ENABLE_SIGNALS + if (BC_NUM_CMP_SIGNAL(cmp)) goto err; +#endif // BC_ENABLE_SIGNALS + } + else cmp = -1; + } + + assert(result < BC_BASE_POW); + + c->num[i] = (BcDig) result; + } + +err: + if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; + bc_num_free(&cpb); + return s; +} + +static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { + + BcStatus s = BC_STATUS_SUCCESS; + size_t len; + BcNum cpa, cpb; + + if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); + if (BC_NUM_ZERO(a)) { + bc_num_setToZero(c, scale); + return BC_STATUS_SUCCESS; + } + if (BC_NUM_ONE(b)) { + bc_num_copy(c, a); + bc_num_retireMul(c, scale, a->neg, b->neg); + return BC_STATUS_SUCCESS; + } + if (!a->rdx && !b->rdx && b->len == 1 && !scale) { + BcBigDig rem; + s = bc_num_divArray(a, (BcBigDig) b->num[0], c, &rem); + bc_num_retireMul(c, scale, a->neg, b->neg); + return s; + } + + len = bc_num_mulReq(a, b, scale); + bc_num_init(&cpa, len); + bc_num_copy(&cpa, a); + bc_num_createCopy(&cpb, b); + + len = b->len; + + if (len > cpa.len) { + bc_num_expand(&cpa, bc_vm_growSize(len, 2)); + bc_num_extend(&cpa, (len - cpa.len) * BC_BASE_DIGS); + } + + cpa.scale = cpa.rdx * BC_BASE_DIGS; + + bc_num_extend(&cpa, b->scale); + cpa.rdx -= BC_NUM_RDX(b->scale); + cpa.scale = cpa.rdx * BC_BASE_DIGS; + + if (scale > cpa.scale) { + bc_num_extend(&cpa, scale); + cpa.scale = cpa.rdx * BC_BASE_DIGS; + } + + if (cpa.cap == cpa.len) bc_num_expand(&cpa, bc_vm_growSize(cpa.len, 1)); + + // We want an extra zero in front to make things simpler. + cpa.num[cpa.len++] = 0; + + if (cpa.rdx == cpa.len) cpa.len = bc_num_nonzeroLen(&cpa); + if (cpb.rdx == cpb.len) cpb.len = bc_num_nonzeroLen(&cpb); + cpb.scale = cpb.rdx = 0; + + s = bc_num_d_long(&cpa, &cpb, c, scale); + + if (BC_NO_ERR(!s)) { + bc_num_retireMul(c, scale, a->neg, b->neg); + if (BC_SIG) s = BC_STATUS_SIGNAL; + } + + bc_num_free(&cpb); + bc_num_free(&cpa); + + return s; +} + +static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, + BcNum *restrict d, size_t scale, size_t ts) +{ + BcStatus s; + BcNum temp; + bool neg; + + if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); + if (BC_NUM_ZERO(a)) { + bc_num_setToZero(c, ts); + bc_num_setToZero(d, ts); + return BC_STATUS_SUCCESS; + } + + bc_num_init(&temp, d->cap); + s = bc_num_d(a, b, c, scale); + assert(!s || s == BC_STATUS_SIGNAL); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + if (scale) scale = ts + 1; + + s = bc_num_m(c, b, &temp, scale); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + s = bc_num_sub(a, &temp, d, scale); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale); + + neg = d->neg; + bc_num_retireMul(d, ts, a->neg, b->neg); + d->neg = BC_NUM_NONZERO(d) ? neg : false; + +err: + bc_num_free(&temp); + return s; +} + +static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) +{ + BcStatus s; + BcNum c1; + size_t ts; + + ts = bc_vm_growSize(scale, b->scale); + ts = BC_MAX(ts, a->scale); + + bc_num_init(&c1, bc_num_mulReq(a, b, ts)); + s = bc_num_r(a, b, &c1, c, scale, ts); + bc_num_free(&c1); + + return s; +} + +static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { + + BcStatus s = BC_STATUS_SUCCESS; + BcNum copy; + BcBigDig pow = 0; + size_t i, powrdx, resrdx; + bool neg, zero; + + if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); + + if (BC_NUM_ZERO(b)) { + bc_num_one(c); + return BC_STATUS_SUCCESS; + } + if (BC_NUM_ZERO(a)) { + if (b->neg) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); + bc_num_setToZero(c, scale); + return BC_STATUS_SUCCESS; + } + if (BC_NUM_ONE(b)) { + if (!b->neg) bc_num_copy(c, a); + else s = bc_num_inv(a, c, scale); + return s; + } + + neg = b->neg; + b->neg = false; + s = bc_num_bigdig(b, &pow); + b->neg = neg; + if (s) return s; + + bc_num_createCopy(©, a); + + if (!neg) { + size_t max = BC_MAX(scale, a->scale), scalepow = a->scale * pow; + scale = BC_MIN(scalepow, max); + } + + for (powrdx = a->scale; BC_NO_SIG && !(pow & 1); pow >>= 1) { + powrdx <<= 1; + s = bc_num_mul(©, ©, ©, powrdx); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + + if (BC_SIG) goto sig_err; + + bc_num_copy(c, ©); + resrdx = powrdx; + + while (BC_NO_SIG && (pow >>= 1)) { + + powrdx <<= 1; + s = bc_num_mul(©, ©, ©, powrdx); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + if (pow & 1) { + resrdx += powrdx; + s = bc_num_mul(c, ©, c, resrdx); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + } + + if (BC_SIG) goto sig_err; + if (neg) { + s = bc_num_inv(c, c, scale); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + + if (c->scale > scale) bc_num_truncate(c, c->scale - scale); + + // We can't use bc_num_clean() here. + for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; + if (zero) bc_num_setToZero(c, scale); + +sig_err: + if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; +err: + bc_num_free(©); + return s; +} + +#if BC_ENABLE_EXTRA_MATH +static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, + size_t scale) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcBigDig val = 0; + + BC_UNUSED(scale); + + s = bc_num_intop(a, b, c, &val); + if (BC_ERR(s)) return s; + + if (val < c->scale) bc_num_truncate(c, c->scale - val); + else if (val > c->scale) bc_num_extend(c, val - c->scale); + + return s; +} + +static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcBigDig val = 0; + + BC_UNUSED(scale); + + s = bc_num_intop(a, b, c, &val); + if (BC_ERR(s)) return s; + + return bc_num_shiftLeft(c, (size_t) val); +} + +static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, + size_t scale) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcBigDig val = 0; + + BC_UNUSED(scale); + + s = bc_num_intop(a, b, c, &val); + if (BC_ERR(s)) return s; + + if (BC_NUM_ZERO(c)) return s; + + return bc_num_shiftRight(c, (size_t) val); +} +#endif // BC_ENABLE_EXTRA_MATH + +static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, + BcNumBinaryOp op, size_t req) +{ + BcStatus s; + BcNum num2, *ptr_a, *ptr_b; + bool init = false; + + assert(a && b && c && op); + + if (c == a) { + ptr_a = &num2; + memcpy(ptr_a, c, sizeof(BcNum)); + init = true; + } + else ptr_a = a; + + if (c == b) { + ptr_b = &num2; + if (c != a) { + memcpy(ptr_b, c, sizeof(BcNum)); + init = true; + } + } + else ptr_b = b; + + if (init) bc_num_init(c, req); + else bc_num_expand(c, req); + + s = op(ptr_a, ptr_b, c, scale); + + assert(!c->neg || BC_NUM_NONZERO(c)); + assert(c->rdx <= c->len || !c->len || s); + + if (init) bc_num_free(&num2); + + return s; +} + +#ifndef NDEBUG +static bool bc_num_strValid(const char *val) { + + bool radix = false; + size_t i, len = strlen(val); + + if (!len) return true; + + for (i = 0; i < len; ++i) { + + BcDig c = val[i]; + + if (c == '.') { + + if (radix) return false; + + radix = true; + continue; + } + + if (!(isdigit(c) || isupper(c))) return false; + } + + return true; +} +#endif // NDEBUG + +static BcBigDig bc_num_parseChar(char c, size_t base_t) { + + if (isupper(c)) { + c = BC_NUM_NUM_LETTER(c); + c = ((size_t) c) >= base_t ? (char) base_t - 1 : c; + } + else c -= '0'; + + return (BcBigDig) (uchar) c; +} + +static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) { + + size_t len, i, j, idx, intchars; + const char *ptr; + char c; + BcDig bcdig; + bool zero = true; + + for (i = 0; val[i] == '0'; ++i); + + val += i; + assert(!val[0] || isalnum(val[0]) || val[0] == '.'); + + // All 0's. We can just return, since this + // procedure expects a virgin (already 0) BcNum. + if (!val[0]) return; + + for (i = 0; zero && val[i] != '\0'; ++i) + zero = (val[i] == '0' || val[i] == '.'); + + len = i + strlen(val + i); + ptr = strchr(val, '.'); + + if (ptr != NULL) { + // decimal point at index (ptr - val) + n->scale = (size_t) (len - (ptr - val + 1)); + n->rdx = BC_NUM_RDX(n->scale); + intchars = ptr - val; + } + else intchars = len; + + if (zero) return; + + n->len = BC_NUM_RDX(intchars) + n->rdx; + bc_num_expand(n, n->len); + + intchars %= BC_BASE_DIGS; + if (intchars == 0) intchars = BC_BASE_DIGS; + idx = n->len; + c = *val; + do { + idx--; + bcdig = 0; + if (c == '.') { + val++; + c = *val; + } + for (j = 0; j < intchars; j++) { + bcdig *= 10; + if (c != '\0') { + if (isupper(c)) c = '9'; + bcdig += (BcBigDig) c - '0'; + val++; + c = *val; + } + } + n->num[idx] = bcdig; + intchars = 9; + } while (idx != 0); +} + +static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val, + BcBigDig base) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcNum temp, mult1, mult2, result1, result2, *m1, *m2, *ptr; + char c = 0; + bool zero = true; + BcBigDig v; + size_t i, digs, len = strlen(val); + + for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0'); + if (zero) return BC_STATUS_SUCCESS; + + bc_num_init(&temp, BC_NUM_BIGDIG_LOG10); + bc_num_init(&mult1, BC_NUM_BIGDIG_LOG10); + + for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) { + + v = bc_num_parseChar(c, base); + + s = bc_num_mulArray(n, base, &mult1); + if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err; + bc_num_bigdig2num(&temp, v); + s = bc_num_add(&mult1, &temp, n, 0); + if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err; + } + + if (i == len && !(c = val[i])) goto int_err; + + assert(c == '.'); + bc_num_init(&mult2, BC_NUM_BIGDIG_LOG10); + bc_num_init(&result1, BC_NUM_DEF_SIZE); + bc_num_init(&result2, BC_NUM_DEF_SIZE); + bc_num_one(&mult1); + + m1 = &mult1; + m2 = &mult2; + + for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs) { + + v = bc_num_parseChar(c, base); + + s = bc_num_mulArray(&result1, base, &result2); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + bc_num_bigdig2num(&temp, v); + s = bc_num_add(&result2, &temp, &result1, 0); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + s = bc_num_mulArray(m1, base, m2); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + ptr = m1; + m1 = m2; + m2 = ptr; + } + + if (BC_SIG) { + s = BC_STATUS_SIGNAL; + goto err; + } + + // This one cannot be a divide by 0 because mult starts out at 1, then is + // multiplied by base, and base cannot be 0, so mult cannot be 0. + s = bc_num_div(&result1, m1, &result2, digs * 2); + assert(!s || s == BC_STATUS_SIGNAL); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + bc_num_truncate(&result2, digs); + s = bc_num_add(n, &result2, n, digs); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + if (BC_NUM_NONZERO(n)) { + if (n->scale < digs) bc_num_extend(n, digs - n->scale); + } + else bc_num_zero(n); + +err: + bc_num_free(&result2); + bc_num_free(&result1); + bc_num_free(&mult2); +int_err: + bc_num_free(&mult1); + bc_num_free(&temp); + return s; +} + +static void bc_num_printNewline(void) { + if (vm->nchars >= (size_t) (vm->line_len - 1)) { + bc_vm_putchar('\\'); + bc_vm_putchar('\n'); + } +} + +static void bc_num_putchar(int c) { + if (c != '\n') bc_num_printNewline(); + bc_vm_putchar(c); +} + +#if DC_ENABLED +static void bc_num_printChar(size_t n, size_t len, bool rdx) { + BC_UNUSED(rdx); + BC_UNUSED(len); + assert(len == 1); + bc_vm_putchar((uchar) n); +} +#endif // DC_ENABLED + +static void bc_num_printDigits(size_t n, size_t len, bool rdx) { + + size_t exp, pow; + + bc_num_putchar(rdx ? '.' : ' '); + + for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE); + + for (exp = 0; exp < len; pow /= BC_BASE, ++exp) { + size_t dig = n / pow; + n -= dig * pow; + bc_num_putchar(((uchar) dig) + '0'); + } +} + +static void bc_num_printHex(size_t n, size_t len, bool rdx) { + + BC_UNUSED(len); + + assert(len == 1); + + if (rdx) bc_num_putchar('.'); + + bc_num_putchar(bc_num_hex_digits[n]); +} + +static void bc_num_printDecimal(const BcNum *restrict n) { + + size_t i, j, rdx = n->rdx; + bool zero = true; + size_t buffer[BC_BASE_DIGS]; + + if (n->neg) bc_num_putchar('-'); + + for (i = n->len - 1; i < n->len; --i) { + + BcDig n9 = n->num[i]; + size_t temp; + bool irdx = (i == rdx - 1); + + zero = (zero & !irdx); + temp = n->scale % BC_BASE_DIGS; + temp = i || !temp ? 0 : BC_BASE_DIGS - temp; + + memset(buffer, 0, BC_BASE_DIGS * sizeof(size_t)); + + for (j = 0; n9 && j < BC_BASE_DIGS; ++j) { + buffer[j] = n9 % BC_BASE; + n9 /= BC_BASE; + } + + for (j = BC_BASE_DIGS - 1; j < BC_BASE_DIGS && j >= temp; --j) { + bool print_rdx = (irdx & (j == BC_BASE_DIGS - 1)); + zero = (zero && buffer[j] == 0); + if (!zero) bc_num_printHex(buffer[j], 1, print_rdx); + } + } +} + +#if BC_ENABLE_EXTRA_MATH +static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) { + + BcStatus s = BC_STATUS_SUCCESS; + bool neg = (n->len <= n->rdx); + BcNum temp, exp; + size_t places, mod; + BcDig digs[BC_NUM_BIGDIG_LOG10]; + + bc_num_createCopy(&temp, n); + + if (neg) { + + size_t i, idx = bc_num_nonzeroLen(n) - 1; + + places = 1; + + for (i = BC_BASE_DIGS - 1; i < BC_BASE_DIGS; --i) { + if (bc_num_pow10[i] > (BcBigDig) n->num[idx]) places += 1; + else break; + } + + places += (n->rdx - (idx + 1)) * BC_BASE_DIGS; + mod = places % 3; + + if (eng && mod != 0) places += 3 - mod; + s = bc_num_shiftLeft(&temp, places); + if (BC_ERROR_SIGNAL_ONLY(s)) goto exit; + } + else { + places = bc_num_intDigits(n) - 1; + mod = places % 3; + if (eng && mod != 0) places -= 3 - (3 - mod); + s = bc_num_shiftRight(&temp, places); + if (BC_ERROR_SIGNAL_ONLY(s)) goto exit; + } + + bc_num_printDecimal(&temp); + bc_num_putchar('e'); + + if (!places) { + bc_num_printHex(0, 1, false); + goto exit; + } + + if (neg) bc_num_putchar('-'); + + bc_num_setup(&exp, digs, BC_NUM_BIGDIG_LOG10); + bc_num_bigdig2num(&exp, (BcBigDig) places); + + bc_num_printDecimal(&exp); + +exit: + bc_num_free(&temp); + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} +#endif // BC_ENABLE_EXTRA_MATH + +static BcStatus bc_num_printFixup(BcNum *restrict n, BcBigDig rem, + BcBigDig pow, size_t idx) +{ + size_t i, len = n->len - idx; + BcBigDig acc; + BcDig *a = n->num + idx; + + if (len < 2) return BC_STATUS_SUCCESS; + + for (i = len - 1; BC_NO_SIG && i > 0; --i) { + + acc = ((BcBigDig) a[i]) * rem + ((BcBigDig) a[i - 1]); + a[i - 1] = (BcDig) (acc % pow); + acc /= pow; + acc += (BcBigDig) a[i]; + + if (acc >= BC_BASE_POW) { + + if (i == len - 1) { + len = bc_vm_growSize(len, 1); + bc_num_expand(n, bc_vm_growSize(len, idx)); + a = n->num + idx; + a[len - 1] = 0; + } + + a[i + 1] += acc / BC_BASE_POW; + acc %= BC_BASE_POW; + } + + assert(acc < BC_BASE_POW); + a[i] = (BcDig) acc; + } + + n->len = len + idx; + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static BcStatus bc_num_printPrepare(BcNum *restrict n, BcBigDig rem, + BcBigDig pow) +{ + BcStatus s = BC_STATUS_SUCCESS; + size_t i; + + for (i = 0; BC_NO_SIG && BC_NO_ERR(!s) && i < n->len; ++i) + s = bc_num_printFixup(n, rem, pow, i); + + if (BC_ERR(s)) return s; + + for (i = 0; BC_NO_SIG && i < n->len; ++i) { + + assert(pow == ((BcBigDig) ((BcDig) pow))); + + if (n->num[i] >= (BcDig) pow) { + + if (i + 1 == n->len) { + n->len = bc_vm_growSize(n->len, 1); + bc_num_expand(n, n->len); + n->num[i + 1] = 0; + } + + assert(pow < BC_BASE_POW); + n->num[i + 1] += n->num[i] / ((BcDig) pow); + n->num[i] %= (BcDig) pow; + } + } + + return BC_NO_ERR(!s) && BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +static BcStatus bc_num_printNum(BcNum *restrict n, BcBigDig base, + size_t len, BcNumDigitOp print) +{ + BcStatus s; + BcVec stack; + BcNum intp, fracp1, fracp2, digit, flen1, flen2, *n1, *n2, *temp; + BcBigDig dig = 0, *ptr, acc, exp; + size_t i, j; + bool radix; + BcDig digit_digs[BC_NUM_BIGDIG_LOG10 + 1]; + + assert(base > 1); + + if (BC_NUM_ZERO(n)) { + print(0, len, false); + return BC_STATUS_SUCCESS; + } + + // This function uses an algorithm that Stefan Esser came + // up with to print the integer part of a number. What it does is convert + // intp into a number of the specified base, but it does it directly, + // instead of just doing a series of divisions and printing the remainders + // in reverse order. + // + // Let me explain in a bit more detail: + // + // The algorithm takes the current least significant digit (after intp has + // been converted to an integer) and the next to least significant digit, + // and it converts the least significant digit into one of the specified + // base, putting any overflow into the next to least significant digit. It + // iterates through the whole number, from least significant to most + // significant, doing this conversion. At the end of that iteration, the + // least significant digit is converted, but the others are not, so it + // iterates again, starting at the next to least significant digit. It keeps + // doing that conversion, skipping one more digit than the last time, until + // all digits have been converted. Then it prints them in reverse order. + // + // That is the gist of the algorithm. It leaves out several things, such as + // the fact that digits are not always converted into the specified base, + // but into something close, basically a power of the specified base. In + // Stefan's words, "You could consider BcDigs to be of base 10^BC_BASE_DIGS + // in the normal case and obase^N for the largest value of N that satisfies + // obase^N <= 10^BC_BASE_DIGS. [This means that] the result is not in base + // "obase", but in base "obase^N", which happens to be printable as a number + // of base "obase" without consideration for neighbouring BcDigs." This fact + // is what necessitates the existence of the loop later in this function. + // + // The conversion happens in bc_num_printPrepare() where the outer loop + // happens and bc_num_printFixup() where the inner loop, or actual + // conversion, happens. + + bc_vec_init(&stack, sizeof(BcBigDig), NULL); + bc_num_init(&fracp1, n->rdx); + + bc_num_createCopy(&intp, n); + bc_num_truncate(&intp, intp.scale); + + s = bc_num_sub(n, &intp, &fracp1, 0); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + if (base != vm->last_base) { + + vm->last_pow = 1; + vm->last_exp = 0; + + while (vm->last_pow * base <= BC_BASE_POW) { + vm->last_pow *= base; + vm->last_exp += 1; + } + + vm->last_rem = BC_BASE_POW - vm->last_pow; + vm->last_base = base; + } + + exp = vm->last_exp; + + if (vm->last_rem != 0) { + s = bc_num_printPrepare(&intp, vm->last_rem, vm->last_pow); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + + for (i = 0; BC_NO_SIG && i < intp.len; ++i) { + + acc = (BcBigDig) intp.num[i]; + + for (j = 0; BC_NO_SIG && j < exp && (i < intp.len - 1 || acc != 0); ++j) + { + if (j != exp - 1) { + dig = acc % base; + acc /= base; + } + else { + dig = acc; + acc = 0; + } + + assert(dig < base); + + bc_vec_push(&stack, &dig); + } + + assert(acc == 0 || BC_SIG); + } + + if (BC_SIG) goto sig_err; + + for (i = 0; BC_NO_SIG && i < stack.len; ++i) { + ptr = bc_vec_item_rev(&stack, i); + assert(ptr); + print(*ptr, len, false); + } + + if (BC_SIG) goto sig_err; + if (!n->scale) goto err; + + bc_num_init(&fracp2, n->rdx); + bc_num_setup(&digit, digit_digs, sizeof(digit_digs) / sizeof(BcDig)); + bc_num_init(&flen1, BC_NUM_BIGDIG_LOG10 + 1); + bc_num_init(&flen2, BC_NUM_BIGDIG_LOG10 + 1); + bc_num_one(&flen1); + + radix = true; + n1 = &flen1; + n2 = &flen2; + + while (BC_NO_SIG && bc_num_intDigits(n1) < n->scale + 1) { + + bc_num_expand(&fracp2, fracp1.len + 1); + s = bc_num_mulArray(&fracp1, base, &fracp2); + if (BC_ERROR_SIGNAL_ONLY(s)) goto frac_err; + fracp2.scale = n->scale; + fracp2.rdx = BC_NUM_RDX(fracp2.scale); + + // Will never fail (except for signals) because fracp is + // guaranteed to be non-negative and small enough. + s = bc_num_bigdig(&fracp2, &dig); + if (BC_ERROR_SIGNAL_ONLY(s)) goto frac_err; + + bc_num_bigdig2num(&digit, dig); + s = bc_num_sub(&fracp2, &digit, &fracp1, 0); + if (BC_ERROR_SIGNAL_ONLY(s)) goto frac_err; + + print(dig, len, radix); + s = bc_num_mulArray(n1, base, n2); + if (BC_ERROR_SIGNAL_ONLY(s)) goto frac_err; + + radix = false; + temp = n1; + n1 = n2; + n2 = temp; + } + +frac_err: + bc_num_free(&flen2); + bc_num_free(&flen1); + bc_num_free(&fracp2); +sig_err: + if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; +err: + bc_num_free(&fracp1); + bc_num_free(&intp); + bc_vec_free(&stack); + return s; +} + +static BcStatus bc_num_printBase(BcNum *restrict n, BcBigDig base) { + + BcStatus s; + size_t width; + BcNumDigitOp print; + bool neg = n->neg; + + if (neg) bc_num_putchar('-'); + + n->neg = false; + + if (base <= BC_NUM_MAX_POSIX_IBASE) { + width = 1; + print = bc_num_printHex; + } + else { + width = bc_num_log10(base - 1); + print = bc_num_printDigits; + } + + s = bc_num_printNum(n, base, width, print); + n->neg = neg; + + return s; +} + +#if DC_ENABLED +BcStatus bc_num_stream(BcNum *restrict n, BcBigDig base) { + return bc_num_printNum(n, base, 1, bc_num_printChar); +} +#endif // DC_ENABLED + +void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) { + assert(n); + n->num = num; + n->cap = cap; + n->rdx = n->scale = n->len = 0; + n->neg = false; +} + +void bc_num_init(BcNum *restrict n, size_t req) { + assert(n); + req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; + bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req); +} + +void bc_num_free(void *num) { + assert(num); + free(((BcNum*) num)->num); +} + +void bc_num_copy(BcNum *d, const BcNum *s) { + assert(d && s); + if (d == s) return; + bc_num_expand(d, s->len); + d->len = s->len; + d->neg = s->neg; + d->rdx = s->rdx; + d->scale = s->scale; + memcpy(d->num, s->num, BC_NUM_SIZE(d->len)); +} + +void bc_num_createCopy(BcNum *d, const BcNum *s) { + bc_num_init(d, s->len); + bc_num_copy(d, s); +} + +void bc_num_createFromBigdig(BcNum *n, BcBigDig val) { + bc_num_init(n, (BC_NUM_BIGDIG_LOG10 - 1) / BC_BASE_DIGS + 1); + bc_num_bigdig2num(n, val); +} + +size_t bc_num_scale(const BcNum *restrict n) { + return n->scale; +} + +size_t bc_num_len(const BcNum *restrict n) { + + size_t len = n->len; + + if (BC_NUM_ZERO(n)) return 0; + if (n->rdx == len) { + + size_t zero, scale; + + len = bc_num_nonzeroLen(n); + + scale = n->scale % BC_BASE_DIGS; + scale = scale ? scale : BC_BASE_DIGS; + + zero = bc_num_zeroDigits(n->num + len - 1); + + len = len * BC_BASE_DIGS - zero - (BC_BASE_DIGS - scale); + } + else len = bc_num_intDigits(n) + n->scale; + + return len; +} + +BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val, + BcBigDig base, bool letter) +{ + BcStatus s = BC_STATUS_SUCCESS; + + assert(n && val && base); + assert(base >= BC_NUM_MIN_BASE && base <= vm->maxes[BC_PROG_GLOBALS_IBASE]); + assert(bc_num_strValid(val)); + + if (letter) { + BcBigDig dig = bc_num_parseChar(val[0], BC_NUM_MAX_LBASE); + bc_num_bigdig2num(n, dig); + } + else if (base == BC_BASE) bc_num_parseDecimal(n, val); + else s = bc_num_parseBase(n, val, base); + + return s; +} + +BcStatus bc_num_print(BcNum *restrict n, BcBigDig base, bool newline) { + + BcStatus s = BC_STATUS_SUCCESS; + + assert(n); + assert(BC_ENABLE_EXTRA_MATH || base >= BC_NUM_MIN_BASE); + + bc_num_printNewline(); + + if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false); + else if (base == BC_BASE) bc_num_printDecimal(n); +#if BC_ENABLE_EXTRA_MATH + else if (base == 0 || base == 1) + s = bc_num_printExponent(n, base != 0); +#endif // BC_ENABLE_EXTRA_MATH + else s = bc_num_printBase(n, base); + + if (BC_NO_ERR(!s) && newline) bc_num_putchar('\n'); + + return s; +} + +BcStatus bc_num_bigdig(const BcNum *restrict n, BcBigDig *result) { + + size_t i; + BcBigDig r; + + assert(n && result); + + if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); + + for (r = 0, i = n->len; i > n->rdx;) { + + BcBigDig prev = r * BC_BASE_POW; + + if (BC_ERR(prev / BC_BASE_POW != r)) + return bc_vm_err(BC_ERROR_MATH_OVERFLOW); + + r = prev + (BcBigDig) n->num[--i]; + + if (BC_ERR(r < prev)) return bc_vm_err(BC_ERROR_MATH_OVERFLOW); + } + + *result = r; + + return BC_STATUS_SUCCESS; +} + +void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val) { + + BcDig *ptr; + size_t i; + + assert(n); + + bc_num_zero(n); + + if (!val) return; + + bc_num_expand(n, BC_NUM_BIGDIG_LOG10); + + for (ptr = n->num, i = 0; val; ++i, val /= BC_BASE_POW) + ptr[i] = val % BC_BASE_POW; + + n->len = i; +} + +size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) { + + size_t aint, bint, ardx, brdx; + + BC_UNUSED(scale); + + ardx = a->rdx; + brdx = b->rdx; + aint = bc_num_int(a); + bint = bc_num_int(b); + ardx = BC_MAX(ardx, brdx); + aint = BC_MAX(aint, bint); + + return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1); +} + +size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) { + size_t max, rdx; + rdx = bc_vm_growSize(a->rdx, b->rdx); + max = BC_NUM_RDX(scale); + max = bc_vm_growSize(BC_MAX(max, rdx), 1); + rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max); + return rdx; +} + +size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) { + BC_UNUSED(scale); + return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1); +} + +#if BC_ENABLE_EXTRA_MATH +size_t bc_num_placesReq(BcNum *a, BcNum *b, size_t scale) { + BC_UNUSED(scale); + return a->len + b->len - a->rdx - b->rdx; +} +#endif // BC_ENABLE_EXTRA_MATH + +BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s; + BC_UNUSED(scale); + return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale)); +} + +BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a; + BC_UNUSED(scale); + return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale)); +} + +BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale)); +} + +BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale)); +} + +BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + size_t req = bc_num_mulReq(a, b, scale); + return bc_num_binary(a, b, c, scale, bc_num_rem, req); +} + +BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale)); +} + +#if BC_ENABLE_EXTRA_MATH +BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + size_t req = bc_num_placesReq(a, b, scale); + return bc_num_binary(a, b, c, scale, bc_num_place, req); +} + +BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + size_t req = bc_num_placesReq(a, b, scale); + return bc_num_binary(a, b, c, scale, bc_num_left, req); +} + +BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) { + size_t req = bc_num_placesReq(a, b, scale); + return bc_num_binary(a, b, c, scale, bc_num_right, req); +} +#endif // BC_ENABLE_EXTRA_MATH + +BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) { + + BcStatus s = BC_STATUS_SUCCESS; + BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; + size_t pow, len, rdx, req, digs, digs1, digs2, resscale, times = 0; + ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; + BcDig half_digs[1]; + + assert(a && b && a != b); + + if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); + + if (a->scale > scale) scale = a->scale; + len = bc_vm_growSize(bc_num_intDigits(a), 1); + rdx = BC_NUM_RDX(scale); + req = bc_vm_growSize(BC_MAX(rdx, a->rdx), len >> 1); + bc_num_init(b, bc_vm_growSize(req, 1)); + + if (BC_NUM_ZERO(a)) { + bc_num_setToZero(b, scale); + return BC_STATUS_SUCCESS; + } + if (BC_NUM_ONE(a)) { + bc_num_one(b); + bc_num_extend(b, scale); + return BC_STATUS_SUCCESS; + } + + rdx = BC_NUM_RDX(scale); + rdx = BC_MAX(rdx, a->rdx); + len = bc_vm_growSize(a->len, rdx); + + bc_num_init(&num1, len); + bc_num_init(&num2, len); + bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig)); + + bc_num_one(&half); + half.num[0] = BC_BASE_POW / 2; + half.len = 1; + half.rdx = 1; + half.scale = 1; + + bc_num_init(&f, len); + bc_num_init(&fprime, len); + + x0 = &num1; + x1 = &num2; + + bc_num_one(x0); + pow = bc_num_intDigits(a); + + if (pow) { + + if (pow & 1) x0->num[0] = 2; + else x0->num[0] = 6; + + pow -= 2 - (pow & 1); + s = bc_num_shiftLeft(x0, pow / 2); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + + x0->scale = x0->rdx = digs = digs1 = digs2 = 0; + resscale = (scale + BC_BASE_DIGS) * 2; + + len = BC_NUM_RDX(bc_num_intDigits(x0) + resscale - 1); + + while (BC_NO_SIG && (cmp || digs < len)) { + + assert(BC_NUM_NONZERO(x0)); + + s = bc_num_div(a, x0, &f, resscale); + assert(!s || s == BC_STATUS_SIGNAL); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + s = bc_num_add(x0, &f, &fprime, resscale); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + s = bc_num_mul(&fprime, &half, x1, resscale); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + cmp = bc_num_cmp(x1, x0); + +#if BC_ENABLE_SIGNALS + if (BC_NUM_CMP_SIGNAL(cmp)) { + s = BC_STATUS_SIGNAL; + break; + } +#endif // BC_ENABLE_SIGNALS + + digs = x1->len - (size_t) labs(cmp); + + if (cmp == cmp2 && digs == digs1) times += 1; + else times = 0; + + resscale += (times > 2); + + cmp2 = cmp1; + cmp1 = cmp; + digs1 = digs; + + temp = x0; + x0 = x1; + x1 = temp; + } + + if (BC_SIG) { + s = BC_STATUS_SIGNAL; + goto err; + } + + bc_num_copy(b, x0); + if (b->scale > scale) bc_num_truncate(b, b->scale - scale); + +err: + if (BC_ERR(s)) bc_num_free(b); + bc_num_free(&fprime); + bc_num_free(&f); + bc_num_free(&num2); + bc_num_free(&num1); + assert(!b->neg || BC_NUM_NONZERO(b)); + assert(b->rdx <= b->len || !b->len); + return s; +} + +BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) { + + BcStatus s; + BcNum num2, *ptr_a; + bool init = false; + size_t ts, len; + + ts = BC_MAX(scale + b->scale, a->scale); + len = bc_num_mulReq(a, b, ts); + + assert(c != d && a != d && b != d && b != c); + + if (c == a) { + memcpy(&num2, c, sizeof(BcNum)); + ptr_a = &num2; + bc_num_init(c, len); + init = true; + } + else { + ptr_a = a; + bc_num_expand(c, len); + } + + if (BC_NUM_NONZERO(a) && !a->rdx && !b->rdx && b->len == 1 && !scale) { + + BcBigDig rem; + + s = bc_num_divArray(ptr_a, (BcBigDig) b->num[0], c, &rem); + + assert(rem < BC_BASE_POW); + + d->num[0] = (BcDig) rem; + d->len = (rem != 0); + } + else s = bc_num_r(ptr_a, b, c, d, scale, ts); + + assert(!c->neg || BC_NUM_NONZERO(c)); + assert(c->rdx <= c->len || !c->len); + assert(!d->neg || BC_NUM_NONZERO(d)); + assert(d->rdx <= d->len || !d->len); + + if (init) bc_num_free(&num2); + + return s; +} + +#if DC_ENABLED +BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) { + + BcStatus s; + BcNum base, exp, two, temp; + BcDig two_digs[2]; + + assert(a && b && c && d && a != d && b != d && c != d); + + if (BC_ERR(BC_NUM_ZERO(c))) + return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); + if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); + if (BC_ERR(a->rdx || b->rdx || c->rdx)) + return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); + + bc_num_expand(d, c->len); + bc_num_init(&base, c->len); + bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig)); + bc_num_init(&temp, b->len + 1); + + bc_num_one(&two); + two.num[0] = 2; + bc_num_one(d); + + // We already checked for 0. + s = bc_num_rem(a, c, &base, 0); + assert(!s || s == BC_STATUS_SIGNAL); + if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err; + bc_num_createCopy(&exp, b); + + while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) { + + // Num two cannot be 0, so no errors. + s = bc_num_divmod(&exp, &two, &exp, &temp, 0); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + if (BC_NUM_ONE(&temp) && !temp.neg) { + + s = bc_num_mul(d, &base, &temp, 0); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + // We already checked for 0. + s = bc_num_rem(&temp, c, d, 0); + assert(!s || s == BC_STATUS_SIGNAL); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + + s = bc_num_mul(&base, &base, &temp, 0); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + + // We already checked for 0. + s = bc_num_rem(&temp, c, &base, 0); + assert(!s || s == BC_STATUS_SIGNAL); + if (BC_ERROR_SIGNAL_ONLY(s)) goto err; + } + + if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; + +err: + bc_num_free(&exp); +rem_err: + bc_num_free(&temp); + bc_num_free(&base); + assert(!d->neg || d->len); + return s; +} +#endif // DC_ENABLED + +#if BC_DEBUG_CODE +void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline) { + printf("%s: ", name); + bc_num_printDecimal(n); + printf("\n"); + if (emptyline) printf("\n"); + vm->nchars = 0; +} + +void bc_num_printDigs(const BcDig *n, size_t len, bool emptyline) { + + size_t i; + + for (i = len - 1; i < len; --i) printf(" %0*d", BC_BASE_DIGS, n[i]); + + printf("\n"); + if (emptyline) printf("\n"); + vm->nchars = 0; +} + +void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline) { + printf("%s len: %zu, rdx: %zu, scale: %zu\n", + name, n->len, n->rdx, n->scale); + bc_num_printDigs(n->num, n->len, emptyline); +} + +void bc_num_dump(const char *varname, const BcNum *n) { + + ulong i, scale = n->scale; + + fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 "); + + for (i = n->len - 1; i < n->len; --i) { + + if (i + 1 == n->rdx) fprintf(stderr, ". "); + + if (scale / BC_BASE_DIGS != n->rdx - i - 1) + fprintf(stderr, "%0*d ", BC_BASE_DIGS, n->num[i]); + else { + + int mod = scale % BC_BASE_DIGS; + int d = BC_BASE_DIGS - mod; + BcDig div; + + if (mod != 0) { + div = n->num[i] / ((BcDig) bc_num_pow10[(ulong) d]); + fprintf(stderr, "%0*d", (int) mod, div); + } + + div = n->num[i] % ((BcDig) bc_num_pow10[(ulong) d]); + fprintf(stderr, " ' %0*d ", d, div); + } + } + + fprintf(stderr, "(%zu | %zu.%zu / %zu) %p\n", + n->scale, n->len, n->rdx, n->cap, (void*) n->num); +} +#endif // BC_DEBUG_CODE Index: contrib/bc/src/parse.c =================================================================== --- contrib/bc/src/parse.c +++ contrib/bc/src/parse.c @@ -0,0 +1,182 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code common to the parsers. + * + */ + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +void bc_parse_updateFunc(BcParse *p, size_t fidx) { + p->fidx = fidx; + p->func = bc_vec_item(&p->prog->fns, fidx); +} + +void bc_parse_pushName(BcParse *p, char *name, bool var) { + size_t idx = bc_program_search(p->prog, name, var); + bc_parse_pushIndex(p, idx); +} + +void bc_parse_addId(BcParse *p, const char *string, uchar inst) { + + BcFunc *f = BC_IS_BC ? p->func : bc_vec_item(&p->prog->fns, BC_PROG_MAIN); + BcVec *v = inst == BC_INST_NUM ? &f->consts : &f->strs; + size_t idx = v->len; + + if (inst == BC_INST_NUM) { + + if (bc_parse_one[0] == string[0] && bc_parse_one[1] == string[1]) { + bc_parse_push(p, BC_INST_ONE); + return; + } + else { + + BcConst c; + char *str = bc_vm_strdup(string); + + c.val = str; + c.base = BC_NUM_BIGDIG_MAX; + + bc_num_init(&c.num, strlen(str)); + bc_vec_push(v, &c); + } + } + else { + char *str = bc_vm_strdup(string); + bc_vec_push(v, &str); + } + + bc_parse_updateFunc(p, p->fidx); + bc_parse_push(p, inst); + bc_parse_pushIndex(p, idx); +} + +void bc_parse_number(BcParse *p) { + +#if BC_ENABLE_EXTRA_MATH + char *exp = strchr(p->l.str.v, 'e'); + size_t idx = SIZE_MAX; + + if (exp) { + idx = ((size_t) (exp - p->l.str.v)); + *exp = 0; + } +#endif // BC_ENABLE_EXTRA_MATH + + bc_parse_addId(p, p->l.str.v, BC_INST_NUM); + +#if BC_ENABLE_EXTRA_MATH + if (exp) { + + bool neg; + + neg = (*((char*) bc_vec_item(&p->l.str, idx + 1)) == BC_LEX_NEG_CHAR); + + bc_parse_addId(p, bc_vec_item(&p->l.str, idx + 1 + neg), BC_INST_NUM); + bc_parse_push(p, BC_INST_LSHIFT + neg); + } +#endif // BC_ENABLE_EXTRA_MATH +} + +BcStatus bc_parse_text(BcParse *p, const char *text) { + // Make sure the pointer isn't invalidated. + p->func = bc_vec_item(&p->prog->fns, p->fidx); + return bc_lex_text(&p->l, text); +} + +BcStatus bc_parse_reset(BcParse *p, BcStatus s) { + + if (p->fidx != BC_PROG_MAIN) { + bc_func_reset(p->func); + bc_parse_updateFunc(p, BC_PROG_MAIN); + } + + p->l.i = p->l.len; + p->l.t = BC_LEX_EOF; + p->auto_part = false; + +#if BC_ENABLED + bc_vec_npop(&p->flags, p->flags.len - 1); + bc_vec_npop(&p->exits, p->exits.len); + bc_vec_npop(&p->conds, p->conds.len); + bc_vec_npop(&p->ops, p->ops.len); +#endif // BC_ENABLED + + return bc_program_reset(p->prog, s); +} + +void bc_parse_free(BcParse *p) { + assert(p); +#if BC_ENABLED + bc_vec_free(&p->flags); + bc_vec_free(&p->exits); + bc_vec_free(&p->conds); + bc_vec_free(&p->ops); + bc_vec_free(&p->buf); +#endif // BC_ENABLED + bc_lex_free(&p->l); +} + +void bc_parse_init(BcParse *p, BcProgram *prog, size_t func) { + +#if BC_ENABLED + uint16_t flag = 0; +#endif // BC_ENABLED + + assert(p && prog); + +#if BC_ENABLED + bc_vec_init(&p->flags, sizeof(uint16_t), NULL); + bc_vec_push(&p->flags, &flag); + bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL); + bc_vec_init(&p->conds, sizeof(size_t), NULL); + bc_vec_init(&p->ops, sizeof(BcLexType), NULL); + bc_vec_init(&p->buf, sizeof(char), NULL); +#endif // BC_ENABLED + + bc_lex_init(&p->l); + + p->prog = prog; + p->auto_part = false; + bc_parse_updateFunc(p, func); +} Index: contrib/bc/src/program.c =================================================================== --- contrib/bc/src/program.c +++ contrib/bc/src/program.c @@ -0,0 +1,2106 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code to execute bc programs. + * + */ + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#ifndef BC_PROG_NO_STACK_CHECK +static BcStatus bc_program_checkStack(BcVec *v, size_t n) { +#if DC_ENABLED + if (!BC_IS_BC && BC_ERR(!BC_PROG_STACK(v, n))) + return bc_vm_err(BC_ERROR_EXEC_STACK); +#endif // DC_ENABLED + assert(BC_PROG_STACK(v, n)); + return BC_STATUS_SUCCESS; +} +#endif // BC_PROG_NO_STACK_CHECK + +static BcStatus bc_program_type_num(BcResult *r, BcNum *n) { +#if BC_ENABLED + assert(r->t != BC_RESULT_VOID); +#endif // BC_ENABLED + if (BC_ERR(!BC_PROG_NUM(r, n))) return bc_vm_err(BC_ERROR_EXEC_TYPE); + return BC_STATUS_SUCCESS; +} + +static BcStatus bc_program_type_match(BcResult *r, BcType t) { +#if DC_ENABLED + assert(!BC_IS_BC || BC_NO_ERR(r->t != BC_RESULT_STR)); +#endif // DC_ENABLED + if (BC_ERR((r->t != BC_RESULT_ARRAY) != (!t))) + return bc_vm_err(BC_ERROR_EXEC_TYPE); + return BC_STATUS_SUCCESS; +} + +static BcFunc* bc_program_func(BcProgram *p) { + + size_t i; + + if (BC_IS_BC) { + BcInstPtr *ip = bc_vec_item_rev(&p->stack, 0); + i = ip->func; + } + else i = BC_PROG_MAIN; + + return bc_vec_item(&p->fns, i); +} + +static BcConst* bc_program_const(BcProgram *p, size_t idx) { + BcFunc *f = bc_program_func(p); + return bc_vec_item(&f->consts, idx); +} + +static char* bc_program_str(BcProgram *p, size_t idx) { + BcFunc *f = bc_program_func(p); + return *((char**) bc_vec_item(&f->strs, idx)); +} + +static size_t bc_program_index(const char *restrict code, size_t *restrict bgn) +{ + uchar amt = (uchar) code[(*bgn)++], i = 0; + size_t res = 0; + + for (; i < amt; ++i, ++(*bgn)) { + size_t temp = ((size_t) ((int) (uchar) code[*bgn]) & UCHAR_MAX); + res |= (temp << (i * CHAR_BIT)); + } + + return res; +} + +static void bc_program_prepGlobals(BcProgram *p) { + size_t i; + for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i) + bc_vec_push(p->globals_v + i, p->globals + i); +} + +#if BC_ENABLED +static BcVec* bc_program_dereference(BcProgram *p, BcVec *vec) { + + BcVec *v; + size_t vidx, nidx, i = 0; + + assert(vec->size == sizeof(uchar)); + + vidx = bc_program_index(vec->v, &i); + nidx = bc_program_index(vec->v, &i); + + v = bc_vec_item(&p->arrs, vidx); + v = bc_vec_item(v, nidx); + + assert(v->size != sizeof(uchar)); + + return v; +} +#endif // BC_ENABLED + +size_t bc_program_search(BcProgram *p, char *id, bool var) { + + BcId e, *ptr; + BcVec *v, *map; + size_t i; + BcResultData data; + bool new; + + v = var ? &p->vars : &p->arrs; + map = var ? &p->var_map : &p->arr_map; + + e.name = id; + e.idx = v->len; + new = bc_map_insert(map, &e, &i); + + if (new) { + bc_array_init(&data.v, var); + bc_vec_push(v, &data.v); + } + + ptr = bc_vec_item(map, i); + if (new) ptr->name = bc_vm_strdup(e.name); + + return ptr->idx; +} + +static BcVec* bc_program_vec(BcProgram *p, size_t idx, BcType type) { + BcVec *v = (type == BC_TYPE_VAR) ? &p->vars : &p->arrs; + return bc_vec_item(v, idx); +} + +static BcStatus bc_program_num(BcProgram *p, BcResult *r, BcNum **num) { + + BcStatus s = BC_STATUS_SUCCESS; + BcNum *n = &r->d.n; + + switch (r->t) { + + case BC_RESULT_CONSTANT: + { + BcConst *c = bc_program_const(p, r->d.loc.loc); + BcBigDig base = BC_PROG_IBASE(p); + + if (c->base != base) { + + s = bc_num_parse(&c->num, c->val, base, !c->val[1]); + assert(!s || s == BC_STATUS_SIGNAL); + +#if BC_ENABLE_SIGNALS + // bc_num_parse() should only do operations that can + // only fail when signals happen. Thus, if signals + // are not enabled, we don't need this check. + if (BC_ERROR_SIGNAL_ONLY(s)) return s; +#endif // BC_ENABLE_SIGNALS + + c->base = base; + } + + bc_num_createCopy(n, &c->num); + + r->t = BC_RESULT_TEMP; + break; + } + + case BC_RESULT_STR: + case BC_RESULT_TEMP: + case BC_RESULT_IBASE: + case BC_RESULT_SCALE: + case BC_RESULT_OBASE: + { + break; + } + + case BC_RESULT_VAR: + case BC_RESULT_ARRAY: + case BC_RESULT_ARRAY_ELEM: + { + BcVec *v; + BcType type = (r->t == BC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY; + + v = bc_program_vec(p, r->d.loc.loc, type); + + if (r->t == BC_RESULT_ARRAY_ELEM) { + + size_t idx = r->d.loc.idx; + + v = bc_vec_top(v); + +#if BC_ENABLED + if (v->size == sizeof(uchar)) v = bc_program_dereference(p, v); +#endif // BC_ENABLED + + assert(v->size == sizeof(BcNum)); + + if (v->len <= idx) bc_array_expand(v, bc_vm_growSize(idx, 1)); + n = bc_vec_item(v, idx); + } + else n = bc_vec_top(v); + + break; + } + + case BC_RESULT_ONE: + { + n = &p->one; + break; + } + +#if BC_ENABLED + case BC_RESULT_LAST: + { + n = &p->last; + break; + } + + case BC_RESULT_VOID: + { +#ifndef NDEBUG + assert(false); + break; +#endif // NDEBUG + } +#endif // BC_ENABLED + } + + *num = n; + + return s; +} + +static BcStatus bc_program_operand(BcProgram *p, BcResult **r, + BcNum **n, size_t idx) +{ +#ifndef BC_PROG_NO_STACK_CHECK + BcStatus s = bc_program_checkStack(&p->results, idx + 1); + + if (BC_ERR(s)) return s; +#endif // BC_PROG_NO_STACK_CHECK + + *r = bc_vec_item_rev(&p->results, idx); +#if BC_ENABLED + if (BC_ERR((*r)->t == BC_RESULT_VOID)) + return bc_vm_err(BC_ERROR_EXEC_VOID_VAL); +#endif // BC_ENABLED + + return bc_program_num(p, *r, n); +} + +static BcStatus bc_program_binPrep(BcProgram *p, BcResult **l, BcNum **ln, + BcResult **r, BcNum **rn) +{ + BcStatus s; + BcResultType lt; + + assert(p && l && ln && r && rn); + + s = bc_program_operand(p, l, ln, 1); + if (BC_ERR(s)) return s; + s = bc_program_operand(p, r, rn, 0); + if (BC_ERR(s)) return s; + + lt = (*l)->t; + +#if BC_ENABLED + assert(lt != BC_RESULT_VOID && (*r)->t != BC_RESULT_VOID); +#endif // BC_ENABLED + + // We run this again under these conditions in case any vector has been + // reallocated out from under the BcNums or arrays we had. + if (lt == (*r)->t && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) + s = bc_program_num(p, *l, ln); + + if (BC_NO_ERR(!s) && BC_ERR(lt == BC_RESULT_STR)) + return bc_vm_err(BC_ERROR_EXEC_TYPE); + + return s; +} + +static BcStatus bc_program_binOpPrep(BcProgram *p, BcResult **l, BcNum **ln, + BcResult **r, BcNum **rn) +{ + BcStatus s; + + s = bc_program_binPrep(p, l, ln, r, rn); + if (BC_ERR(s)) return s; + + s = bc_program_type_num(*l, *ln); + if (BC_ERR(s)) return s; + + return bc_program_type_num(*r, *rn); +} + +static BcStatus bc_program_assignPrep(BcProgram *p, BcResult **l, BcNum **ln, + BcResult **r, BcNum **rn) +{ + BcStatus s; + bool good = false; + BcResultType lt; + + s = bc_program_binPrep(p, l, ln, r, rn); + if (BC_ERR(s)) return s; + + lt = (*l)->t; + + if (BC_ERR(lt == BC_RESULT_CONSTANT || lt == BC_RESULT_TEMP || + lt == BC_RESULT_ARRAY || lt == BC_RESULT_ONE)) + { + return bc_vm_err(BC_ERROR_EXEC_TYPE); + } + +#if BC_ENABLED + assert(!BC_IS_BC || BC_NO_ERR(lt != BC_RESULT_ONE)); +#endif // BC_ENABLED + +#if DC_ENABLED + if(!BC_IS_BC) good = (((*r)->t == BC_RESULT_STR || BC_PROG_STR(*rn)) && + (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)); +#else + assert((*r)->t != BC_RESULT_STR); +#endif // DC_ENABLED + + if (!good) s = bc_program_type_num(*r, *rn); + + return s; +} + +static void bc_program_binOpRetire(BcProgram *p, BcResult *r) { + r->t = BC_RESULT_TEMP; + bc_vec_pop(&p->results); + bc_vec_pop(&p->results); + bc_vec_push(&p->results, r); +} + +static BcStatus bc_program_prep(BcProgram *p, BcResult **r, BcNum **n) { + + BcStatus s; + + assert(p && r && n); + + s = bc_program_operand(p, r, n, 0); + if (BC_ERR(s)) return s; + +#if DC_ENABLED + assert((*r)->t != BC_RESULT_VAR || !BC_PROG_STR(*n)); +#endif // DC_ENABLED + + return bc_program_type_num(*r, *n); +} + +static void bc_program_retire(BcProgram *p, BcResult *r, BcResultType t) { + r->t = t; + bc_vec_pop(&p->results); + bc_vec_push(&p->results, r); +} + +static BcStatus bc_program_op(BcProgram *p, uchar inst) { + + BcStatus s; + BcResult *opd1, *opd2, res; + BcNum *n1 = NULL, *n2 = NULL; + size_t idx = inst - BC_INST_POWER; + + s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2); + if (BC_ERR(s)) return s; + bc_num_init(&res.d.n, bc_program_opReqs[idx](n1, n2, BC_PROG_SCALE(p))); + + s = bc_program_ops[idx](n1, n2, &res.d.n, BC_PROG_SCALE(p)); + if (BC_ERR(s)) goto err; + bc_program_binOpRetire(p, &res); + + return s; + +err: + bc_num_free(&res.d.n); + return s; +} + +static BcStatus bc_program_read(BcProgram *p) { + + BcStatus s; + BcParse parse; + BcVec buf; + BcInstPtr ip; + size_t i; + const char* file; + BcFunc *f = bc_vec_item(&p->fns, BC_PROG_READ); + + for (i = 0; i < p->stack.len; ++i) { + BcInstPtr *ip_ptr = bc_vec_item(&p->stack, i); + if (ip_ptr->func == BC_PROG_READ) + return bc_vm_err(BC_ERROR_EXEC_REC_READ); + } + + file = vm->file; + bc_lex_file(&parse.l, bc_program_stdin_name); + bc_vec_npop(&f->code, f->code.len); + bc_vec_init(&buf, sizeof(char), NULL); + + s = bc_read_line(&buf, BC_IS_BC ? "read> " : "?> "); + if (BC_ERR(s)) { + if (s == BC_STATUS_EOF) s = bc_vm_err(BC_ERROR_EXEC_READ_EXPR); + goto io_err; + } + + bc_parse_init(&parse, p, BC_PROG_READ); + + s = bc_parse_text(&parse, buf.v); + if (BC_ERR(s)) goto exec_err; + s = vm->expr(&parse, BC_PARSE_NOREAD | BC_PARSE_NEEDVAL); + if (BC_ERR(s)) goto exec_err; + + if (BC_ERR(parse.l.t != BC_LEX_NLINE && parse.l.t != BC_LEX_EOF)) { + s = bc_vm_err(BC_ERROR_EXEC_READ_EXPR); + goto exec_err; + } + + if (BC_G) bc_program_prepGlobals(p); + + ip.func = BC_PROG_READ; + ip.idx = 0; + ip.len = p->results.len; + + // Update this pointer, just in case. + f = bc_vec_item(&p->fns, BC_PROG_READ); + + bc_vec_pushByte(&f->code, vm->read_ret); + bc_vec_push(&p->stack, &ip); + +exec_err: + bc_parse_free(&parse); +io_err: + bc_vec_free(&buf); + vm->file = file; + return s; +} + +static void bc_program_printChars(const char *str) { + const char *nl; + vm->nchars += bc_vm_printf("%s", str); + nl = strrchr(str, '\n'); + if (nl) vm->nchars = strlen(nl + 1); +} + +static void bc_program_printString(const char *restrict str) { + + size_t i, len = strlen(str); + +#if DC_ENABLED + if (!len && !BC_IS_BC) { + bc_vm_putchar('\0'); + return; + } +#endif // DC_ENABLED + + for (i = 0; i < len; ++i) { + + int c = str[i]; + + if (c == '\\' && i != len - 1) { + + const char *ptr; + + c = str[++i]; + ptr = strchr(bc_program_esc_chars, c); + + if (ptr) { + if (c == 'n') vm->nchars = SIZE_MAX; + c = bc_program_esc_seqs[(size_t) (ptr - bc_program_esc_chars)]; + } + else { + // Just print the backslash. The following + // character will be printed later. + bc_vm_putchar('\\'); + } + } + + bc_vm_putchar(c); + } +} + +static BcStatus bc_program_print(BcProgram *p, uchar inst, size_t idx) { + + BcStatus s = BC_STATUS_SUCCESS; + BcResult *r; + char *str; + BcNum *n = NULL; + bool pop = (inst != BC_INST_PRINT); + + assert(p); + +#ifndef BC_PROG_NO_STACK_CHECK + s = bc_program_checkStack(&p->results, idx + 1); + if (BC_ERR(s)) return s; +#endif // BC_PROG_NO_STACK_CHECK + + r = bc_vec_item_rev(&p->results, idx); + +#if BC_ENABLED + if (r->t == BC_RESULT_VOID) { + if (BC_ERR(pop)) return bc_vm_err(BC_ERROR_EXEC_VOID_VAL); + return s; + } +#endif // BC_ENABLED + + s = bc_program_num(p, r, &n); + if (BC_ERR(s)) return s; + + if (BC_PROG_NUM(r, n)) { + assert(inst != BC_INST_PRINT_STR); + s = bc_num_print(n, BC_PROG_OBASE(p), !pop); +#if BC_ENABLED + if (BC_NO_ERR(!s)) bc_num_copy(&p->last, n); +#endif // BC_ENABLED + } + else { + + size_t i = (r->t == BC_RESULT_STR) ? r->d.loc.loc : n->rdx; + + str = bc_program_str(p, i); + + if (inst == BC_INST_PRINT_STR) bc_program_printChars(str); + else { + bc_program_printString(str); + if (inst == BC_INST_PRINT) bc_vm_putchar('\n'); + } + } + + if (BC_NO_ERR(!s) && (BC_IS_BC || pop)) bc_vec_pop(&p->results); + + return s; +} + +void bc_program_negate(BcResult *r, BcNum *n) { + BcNum *rn = &r->d.n; + bc_num_copy(rn, n); + if (BC_NUM_NONZERO(rn)) rn->neg = !rn->neg; +} + +void bc_program_not(BcResult *r, BcNum *n) { + if (!bc_num_cmpZero(n)) bc_num_one(&r->d.n); +} + +#if BC_ENABLE_EXTRA_MATH +void bc_program_trunc(BcResult *r, BcNum *n) { + BcNum *rn = &r->d.n; + bc_num_copy(rn, n); + bc_num_truncate(rn, n->scale); +} +#endif // BC_ENABLE_EXTRA_MATH + +static BcStatus bc_program_unary(BcProgram *p, uchar inst) { + + BcStatus s; + BcResult res, *ptr; + BcNum *num = NULL; + + s = bc_program_prep(p, &ptr, &num); + if (BC_ERR(s)) return s; + + bc_num_init(&res.d.n, num->len); + bc_program_unarys[inst - BC_INST_NEG](&res, num); + bc_program_retire(p, &res, BC_RESULT_TEMP); + + return s; +} + +static BcStatus bc_program_logical(BcProgram *p, uchar inst) { + + BcStatus s; + BcResult *opd1, *opd2, res; + BcNum *n1 = NULL, *n2 = NULL; + bool cond = 0; + ssize_t cmp; + + s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2); + if (BC_ERR(s)) return s; + + if (inst == BC_INST_BOOL_AND) + cond = (bc_num_cmpZero(n1) && bc_num_cmpZero(n2)); + else if (inst == BC_INST_BOOL_OR) + cond = (bc_num_cmpZero(n1) || bc_num_cmpZero(n2)); + else { + + cmp = bc_num_cmp(n1, n2); + +#if BC_ENABLE_SIGNALS + if (BC_NUM_CMP_SIGNAL(cmp)) return BC_STATUS_SIGNAL; +#endif // BC_ENABLE_SIGNALS + + switch (inst) { + + case BC_INST_REL_EQ: + { + cond = (cmp == 0); + break; + } + + case BC_INST_REL_LE: + { + cond = (cmp <= 0); + break; + } + + case BC_INST_REL_GE: + { + cond = (cmp >= 0); + break; + } + + case BC_INST_REL_NE: + { + cond = (cmp != 0); + break; + } + + case BC_INST_REL_LT: + { + cond = (cmp < 0); + break; + } + + case BC_INST_REL_GT: + { + cond = (cmp > 0); + break; + } +#ifndef NDEBUG + default: + { + assert(false); + break; + } +#endif // NDEBUG + } + } + + bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); + if (cond) bc_num_one(&res.d.n); + + bc_program_binOpRetire(p, &res); + + return s; +} + +#if DC_ENABLED +static BcStatus bc_program_assignStr(BcProgram *p, BcResult *r, + BcVec *v, bool push) +{ + BcNum n2; + + memset(&n2, 0, sizeof(BcNum)); + n2.rdx = r->d.loc.loc; + + if (!push) { +#ifndef BC_PROG_NO_STACK_CHECK + BcStatus s = bc_program_checkStack(&p->results, 2); + if (BC_ERR(s)) return s; +#endif // BC_PROG_NO_STACK_CHECK + bc_vec_pop(v); + bc_vec_pop(&p->results); + } + + bc_vec_pop(&p->results); + bc_vec_push(v, &n2); + + return BC_STATUS_SUCCESS; +} +#endif // DC_ENABLED + +static BcStatus bc_program_copyToVar(BcProgram *p, size_t idx, + BcType t, bool last) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcResult *ptr, r; + BcVec *vec; + BcNum *n = NULL; + bool var = (t == BC_TYPE_VAR); + +#if BC_ENABLED + if (last) s = bc_program_operand(p, &ptr, &n, 0); + else { + + ptr = bc_vec_top(&p->results); + + // The only place that last could not be true is when doing parameters + // for a function that are either a variable or an array. Thus, we can + // assert that here instead of check. + assert(ptr->t == BC_RESULT_VAR || ptr->t == BC_RESULT_ARRAY); + + n = bc_vec_item_rev(bc_program_vec(p, ptr->d.loc.loc, t), 1); + } +#else // BC_ENABLED + s = bc_program_operand(p, &ptr, &n, 0); +#endif // BC_ENABLED + + if (BC_ERR(s)) return s; + + s = bc_program_type_match(ptr, t); + if (BC_ERR(s)) return s; + + vec = bc_program_vec(p, idx, t); + +#if DC_ENABLED + if (ptr->t == BC_RESULT_STR) { + if (BC_ERR(!var)) return bc_vm_err(BC_ERROR_EXEC_TYPE); + return bc_program_assignStr(p, ptr, vec, true); + } +#endif // DC_ENABLED + + // Do this once more to make sure that pointers were not invalidated. + vec = bc_program_vec(p, idx, t); + + if (var) bc_num_createCopy(&r.d.n, n); + else { + + BcVec *v = (BcVec*) n, *rv = &r.d.v; +#if BC_ENABLED + bool ref, ref_size; + + assert(v != NULL); + + ref = (v->size == sizeof(BcNum) && t != BC_TYPE_ARRAY); + ref_size = (v->size == sizeof(uchar)); + + if (ref || (ref_size && t == BC_TYPE_REF)) { + + bc_vec_init(rv, sizeof(uchar), NULL); + + if (ref) { + + v = bc_program_vec(p, ptr->d.loc.loc, BC_TYPE_REF); + + // Make sure the pointer was not invalidated. + vec = bc_program_vec(p, idx, t); + + bc_vec_pushIndex(rv, ptr->d.loc.loc); + bc_vec_pushIndex(rv, v->len - 1); + } + // If we get here, we are copying a ref to a ref. + else bc_vec_npush(rv, v->len * sizeof(uchar), v->v); + + // We need to return early. + bc_vec_push(vec, &r.d); + bc_vec_pop(&p->results); + + return s; + } + else if (ref_size && t != BC_TYPE_REF) v = bc_program_dereference(p, v); +#endif // BC_ENABLED + + bc_array_init(rv, true); + bc_array_copy(rv, v); + } + + bc_vec_push(vec, &r.d); + bc_vec_pop(&p->results); + + return s; +} + +static BcStatus bc_program_assign(BcProgram *p, uchar inst) { + + BcStatus s; + BcResult *left, *right, res; + BcNum *l = NULL, *r = NULL; + bool ob, sc, use_val = BC_INST_USE_VAL(inst); + + s = bc_program_assignPrep(p, &left, &l, &right, &r); + if (BC_ERR(s)) return s; + +#if DC_ENABLED + assert(left->t != BC_RESULT_STR); + + if (right->t == BC_RESULT_STR) { + + size_t idx = right->d.loc.loc; + + if (left->t == BC_RESULT_ARRAY_ELEM) { + bc_num_free(l); + memset(l, 0, sizeof(BcNum)); + l->rdx = idx; + } + else { + BcVec *v = bc_program_vec(p, left->d.loc.loc, BC_TYPE_VAR); + s = bc_program_assignStr(p, right, v, false); + } + + return s; + } +#endif // DC_ENABLED + + if (BC_INST_IS_ASSIGN(inst)) bc_num_copy(l, r); +#if BC_ENABLED + else { + + BcBigDig scale = BC_PROG_SCALE(p); + + if (!use_val) + inst -= (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER); + + s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, scale); + if (BC_ERR(s)) return s; + } +#endif // BC_ENABLED + + ob = (left->t == BC_RESULT_OBASE); + sc = (left->t == BC_RESULT_SCALE); + + if (ob || sc || left->t == BC_RESULT_IBASE) { + + BcVec *v; + BcBigDig *ptr, *ptr_t, val, max, min; + BcError e; + + s = bc_num_bigdig(l, &val); + if (BC_ERR(s)) return s; + e = left->t - BC_RESULT_IBASE + BC_ERROR_EXEC_IBASE; + + if (sc) { + min = 0; + max = vm->maxes[BC_PROG_GLOBALS_SCALE]; + v = p->globals_v + BC_PROG_GLOBALS_SCALE; + ptr_t = p->globals + BC_PROG_GLOBALS_SCALE; + } + else { + min = BC_NUM_MIN_BASE; + if (BC_ENABLE_EXTRA_MATH && ob && (!BC_IS_BC || !BC_IS_POSIX)) + min = 0; + max = vm->maxes[ob + BC_PROG_GLOBALS_IBASE]; + v = p->globals_v + BC_PROG_GLOBALS_IBASE + ob; + ptr_t = p->globals + BC_PROG_GLOBALS_IBASE + ob; + } + + if (BC_ERR(val > max || val < min)) return bc_vm_verr(e, min, max); + + ptr = bc_vec_top(v); + *ptr = val; + *ptr_t = val; + } + + if (use_val) { + bc_num_createCopy(&res.d.n, l); + bc_program_binOpRetire(p, &res); + } + else { + bc_vec_pop(&p->results); + bc_vec_pop(&p->results); + } + + return s; +} + +static BcStatus bc_program_pushVar(BcProgram *p, const char *restrict code, + size_t *restrict bgn, bool pop, bool copy) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcResult r; + size_t idx = bc_program_index(code, bgn); + + r.t = BC_RESULT_VAR; + r.d.loc.loc = idx; + +#if DC_ENABLED + if (pop || copy) { + + BcVec *v = bc_program_vec(p, idx, BC_TYPE_VAR); + BcNum *num = bc_vec_top(v); + + s = bc_program_checkStack(v, 2 - copy); + if (BC_ERR(s)) return s; + + if (!BC_PROG_STR(num)) { + r.t = BC_RESULT_TEMP; + bc_num_createCopy(&r.d.n, num); + } + else { + r.t = BC_RESULT_STR; + r.d.loc.loc = num->rdx; + } + + if (!copy) bc_vec_pop(v); + } +#endif // DC_ENABLED + + bc_vec_push(&p->results, &r); + + return s; +} + +static BcStatus bc_program_pushArray(BcProgram *p, const char *restrict code, + size_t *restrict bgn, uchar inst) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcResult r; + BcNum *num = NULL; + + r.d.loc.loc = bc_program_index(code, bgn); + + if (inst == BC_INST_ARRAY) { + r.t = BC_RESULT_ARRAY; + bc_vec_push(&p->results, &r); + } + else { + + BcResult *operand; + BcBigDig temp; + + s = bc_program_prep(p, &operand, &num); + if (BC_ERR(s)) return s; + s = bc_num_bigdig(num, &temp); + if (BC_ERR(s)) return s; + + r.d.loc.idx = (size_t) temp; + bc_program_retire(p, &r, BC_RESULT_ARRAY_ELEM); + } + + return s; +} + +#if BC_ENABLED +static BcStatus bc_program_incdec(BcProgram *p, uchar inst) { + + BcStatus s; + BcResult *ptr, res, copy; + BcNum *num = NULL; + uchar inst2; + bool post, use_val; + + s = bc_program_prep(p, &ptr, &num); + if (BC_ERR(s)) return s; + + use_val = (inst != BC_INST_INC_NO_VAL && inst != BC_INST_DEC_NO_VAL); + post = use_val && (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST); + + if (post) { + copy.t = BC_RESULT_TEMP; + bc_num_createCopy(©.d.n, num); + } + + res.t = BC_RESULT_ONE; + inst2 = BC_INST_ASSIGN_PLUS; + if (!use_val) { + inst2 += (inst == BC_INST_DEC_NO_VAL); + inst2 += (BC_INST_ASSIGN_PLUS_NO_VAL - BC_INST_ASSIGN_PLUS); + } + else inst2 += (inst & 0x01); + + bc_vec_push(&p->results, &res); + s = bc_program_assign(p, inst2); + + if (post) { + + if (BC_ERR(s)) { + bc_num_free(©); + return s; + } + + bc_vec_pop(&p->results); + bc_vec_push(&p->results, ©); + } + + return s; +} + +static BcStatus bc_program_call(BcProgram *p, const char *restrict code, + size_t *restrict idx) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcInstPtr ip; + size_t i, nparams = bc_program_index(code, idx); + BcFunc *f; + BcVec *v; + BcLoc *a; + BcResultData param; + BcResult *arg; + + ip.idx = 0; + ip.func = bc_program_index(code, idx); + f = bc_vec_item(&p->fns, ip.func); + + if (BC_ERR(!f->code.len)) + return bc_vm_verr(BC_ERROR_EXEC_UNDEF_FUNC, f->name); + if (BC_ERR(nparams != f->nparams)) + return bc_vm_verr(BC_ERROR_EXEC_PARAMS, f->nparams, nparams); + ip.len = p->results.len - nparams; + + assert(BC_PROG_STACK(&p->results, nparams)); + + if (BC_G) bc_program_prepGlobals(p); + + for (i = 0; i < nparams; ++i) { + + size_t j; + bool last = true; + + a = bc_vec_item(&f->autos, nparams - 1 - i); + arg = bc_vec_top(&p->results); + + // If I have already pushed to a var, I need to make sure I + // get the previous version, not the already pushed one. + if (arg->t == BC_RESULT_VAR || arg->t == BC_RESULT_ARRAY) { + for (j = 0; j < i && last; ++j) { + BcLoc *loc = bc_vec_item(&f->autos, nparams - 1 - j); + last = (arg->d.loc.loc != loc->loc || + (!loc->idx) != (arg->t == BC_RESULT_VAR)); + } + } + + s = bc_program_copyToVar(p, a->loc, (BcType) a->idx, last); + if (BC_ERR(s)) return s; + } + + for (; i < f->autos.len; ++i) { + + a = bc_vec_item(&f->autos, i); + v = bc_program_vec(p, a->loc, (BcType) a->idx); + + if (a->idx == BC_TYPE_VAR) { + bc_num_init(¶m.n, BC_NUM_DEF_SIZE); + bc_vec_push(v, ¶m.n); + } + else { +#if BC_ENABLED + assert(a->idx == BC_TYPE_ARRAY); +#endif // BC_ENABLED + bc_array_init(¶m.v, true); + bc_vec_push(v, ¶m.v); + } + } + + bc_vec_push(&p->stack, &ip); + + return BC_STATUS_SUCCESS; +} + +static BcStatus bc_program_return(BcProgram *p, uchar inst) { + + BcStatus s; + BcResult res; + BcFunc *f; + size_t i; + BcInstPtr *ip = bc_vec_top(&p->stack); + + assert(BC_PROG_STACK(&p->stack, 2)); + +#ifndef BC_PROG_NO_STACK_CHECK + s = bc_program_checkStack(&p->results, ip->len + (inst == BC_INST_RET)); + if (BC_ERR(s)) return s; +#endif // BC_PROG_NO_STACK_CHECK + + f = bc_vec_item(&p->fns, ip->func); + res.t = BC_RESULT_TEMP; + + if (inst == BC_INST_RET) { + + BcNum *num = NULL; + BcResult *operand; + + s = bc_program_operand(p, &operand, &num, 0); + if (BC_ERR(s)) return s; + + bc_num_createCopy(&res.d.n, num); + } + else if (inst == BC_INST_RET_VOID) res.t = BC_RESULT_VOID; + else bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); + + // We need to pop arguments as well, so this takes that into account. + for (i = 0; i < f->autos.len; ++i) { + + BcLoc *a = bc_vec_item(&f->autos, i); + BcVec *v = bc_program_vec(p, a->loc, (BcType) a->idx); + + bc_vec_pop(v); + } + + bc_vec_npop(&p->results, p->results.len - ip->len); + + if (BC_G) { + + for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i) { + BcVec *v = p->globals_v + i; + bc_vec_pop(v); + p->globals[i] = BC_PROG_GLOBAL(v); + } + } + + bc_vec_push(&p->results, &res); + bc_vec_pop(&p->stack); + + return BC_STATUS_SUCCESS; +} +#endif // BC_ENABLED + +static BcStatus bc_program_builtin(BcProgram *p, uchar inst) { + + BcStatus s; + BcResult *opd; + BcResult res; + BcNum *num = NULL, *resn = &res.d.n; + bool len = (inst == BC_INST_LENGTH); + + assert(inst >= BC_INST_LENGTH && inst <= BC_INST_ABS); + + s = bc_program_operand(p, &opd, &num, 0); + if (BC_ERR(s)) return s; + + assert(num != NULL); + +#if DC_ENABLED + if (!len && inst != BC_INST_SCALE_FUNC) { + s = bc_program_type_num(opd, num); + if (BC_ERR(s)) return s; + } +#endif // DC_ENABLED + + if (inst == BC_INST_SQRT) { + s = bc_num_sqrt(num, resn, BC_PROG_SCALE(p)); + if (BC_ERR(s)) return s; + } + else if (inst == BC_INST_ABS) { + bc_num_createCopy(resn, num); + resn->neg = false; + } + else { + + BcBigDig val = 0; + + if (len) { + if (BC_IS_BC && opd->t == BC_RESULT_ARRAY) + val = (BcBigDig) ((BcVec*) num)->len; +#if DC_ENABLED + else if (!BC_PROG_NUM(opd, num)) { + size_t idx; + idx = opd->t == BC_RESULT_STR ? opd->d.loc.loc : num->rdx; + val = (BcBigDig) strlen(bc_program_str(p, idx)); + } +#endif // DC_ENABLED + else val = (BcBigDig) bc_num_len(num); + } + else if (BC_IS_BC || BC_PROG_NUM(opd, num)) + val = (BcBigDig) bc_num_scale(num); + + bc_num_createFromBigdig(resn, val); + } + + bc_program_retire(p, &res, BC_RESULT_TEMP); + + return s; +} + +#if DC_ENABLED +static BcStatus bc_program_divmod(BcProgram *p) { + + BcStatus s; + BcResult *opd1, *opd2, res, res2; + BcNum *n1, *n2 = NULL, *resn = &res.d.n, *resn2 = &res2.d.n; + size_t req; + + s = bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2); + if (BC_ERR(s)) return s; + + req = bc_num_mulReq(n1, n2, BC_PROG_SCALE(p)); + bc_num_init(resn, req); + bc_num_init(resn2, req); + + s = bc_num_divmod(n1, n2, resn2, resn, BC_PROG_SCALE(p)); + if (BC_ERR(s)) goto err; + + bc_program_binOpRetire(p, &res2); + res.t = BC_RESULT_TEMP; + bc_vec_push(&p->results, &res); + + return s; + +err: + bc_num_free(resn2); + bc_num_free(resn); + return s; +} + +static BcStatus bc_program_modexp(BcProgram *p) { + + BcStatus s; + BcResult *r1, *r2, *r3, res; + BcNum *n1 = NULL, *n2 = NULL, *n3, *resn = &res.d.n; + + s = bc_program_operand(p, &r1, &n1, 2); + if (BC_ERR(s)) return s; + s = bc_program_type_num(r1, n1); + if (BC_ERR(s)) return s; + + s = bc_program_binOpPrep(p, &r2, &n2, &r3, &n3); + if (BC_ERR(s)) return s; + + // Make sure that the values have their pointers updated, if necessary. + // Only array elements are possible. + if (r1->t == BC_RESULT_ARRAY_ELEM && (r1->t == r2->t || r1->t == r3->t)) { + s = bc_program_num(p, r1, &n1); + if (BC_ERR(s)) return s; + } + + bc_num_init(resn, n3->len); + s = bc_num_modexp(n1, n2, n3, resn); + if (BC_ERR(s)) goto err; + + bc_vec_pop(&p->results); + bc_program_binOpRetire(p, &res); + + return s; + +err: + bc_num_free(resn); + return s; +} + +static void bc_program_stackLen(BcProgram *p) { + BcResult res; + res.t = BC_RESULT_TEMP; + bc_num_createFromBigdig(&res.d.n, (BcBigDig) p->results.len); + bc_vec_push(&p->results, &res); +} + +static BcStatus bc_program_asciify(BcProgram *p) { + + BcStatus s; + BcResult *r, res; + BcNum *n = NULL, num; + char str[2], *str2, c; + size_t len; + BcBigDig val; + BcFunc f, *func; + + s = bc_program_operand(p, &r, &n, 0); + if (BC_ERR(s)) return s; + + assert(n != NULL); + + func = bc_vec_item(&p->fns, BC_PROG_MAIN); + len = func->strs.len; + + assert(len + BC_PROG_REQ_FUNCS == p->fns.len); + + if (BC_PROG_NUM(r, n)) { + + bc_num_createCopy(&num, n); + bc_num_truncate(&num, num.scale); + num.neg = false; + + // This is guaranteed to not have a divide by 0 + // because strmb is equal to UCHAR_MAX + 1. + s = bc_num_mod(&num, &p->strmb, &num, 0); + assert(!s || s == BC_STATUS_SIGNAL); +#if BC_ENABLE_SIGNALS + if (BC_ERROR_SIGNAL_ONLY(s)) goto num_err; +#endif // BC_ENABLE_SIGNALS + + // This is also guaranteed to not error because num is in the range + // [0, UCHAR_MAX], which is definitely in range for a BcBigDig. And + // it is not negative. + s = bc_num_bigdig(&num, &val); + assert(!s || s == BC_STATUS_SIGNAL); +#if BC_ENABLE_SIGNALS + if (BC_ERROR_SIGNAL_ONLY(s)) goto num_err; +#endif // BC_ENABLE_SIGNALS + + c = (char) val; + + bc_num_free(&num); + } + else { + size_t idx = r->t == BC_RESULT_STR ? r->d.loc.loc : n->rdx; + str2 = *((char**) bc_vec_item(&func->strs, idx)); + c = str2[0]; + } + + str[0] = c; + str[1] = '\0'; + + bc_program_addFunc(p, &f, bc_func_main); + str2 = bc_vm_strdup(str); + + // Make sure the pointer is updated. + func = bc_vec_item(&p->fns, BC_PROG_MAIN); + bc_vec_push(&func->strs, &str2); + + res.t = BC_RESULT_STR; + res.d.loc.loc = len; + bc_vec_pop(&p->results); + bc_vec_push(&p->results, &res); + + return BC_STATUS_SUCCESS; + +#if BC_ENABLE_SIGNALS +num_err: + bc_num_free(&num); + return s; +#endif // BC_ENABLE_SIGNALS +} + +static BcStatus bc_program_printStream(BcProgram *p) { + + BcStatus s; + BcResult *r; + BcNum *n = NULL; + + s = bc_program_operand(p, &r, &n, 0); + if (BC_ERR(s)) return s; + + assert(n != NULL); + + if (BC_PROG_NUM(r, n)) s = bc_num_stream(n, p->strm); + else { + size_t idx = (r->t == BC_RESULT_STR) ? r->d.loc.loc : n->rdx; + bc_program_printChars(bc_program_str(p, idx)); + } + + return s; +} + +static BcStatus bc_program_nquit(BcProgram *p) { + + BcStatus s; + BcResult *opnd; + BcNum *num = NULL; + BcBigDig val; + + s = bc_program_prep(p, &opnd, &num); + if (BC_ERR(s)) return s; + s = bc_num_bigdig(num, &val); + if (BC_ERR(s)) return s; + + bc_vec_pop(&p->results); + + s = bc_program_checkStack(&p->stack, val); + if (BC_ERR(s)) return s; + else if (p->stack.len == val) return BC_STATUS_QUIT; + + bc_vec_npop(&p->stack, val); + + return s; +} + +static BcStatus bc_program_execStr(BcProgram *p, const char *restrict code, + size_t *restrict bgn, bool cond) +{ + BcStatus s = BC_STATUS_SUCCESS; + BcResult *r; + char *str; + BcFunc *f; + BcParse prs; + BcInstPtr ip; + size_t fidx, sidx; + BcNum *n = NULL; + bool exec; + + s = bc_program_operand(p, &r, &n, 0); + if (BC_ERR(s)) return s; + + if (cond) { + + size_t idx = SIZE_MAX, then_idx, else_idx; + + then_idx = bc_program_index(code, bgn); + else_idx = bc_program_index(code, bgn); + + exec = (r->d.n.len != 0); + + if (exec) idx = then_idx; + else if (else_idx != SIZE_MAX) { + exec = true; + idx = else_idx; + } + + if (exec) n = bc_vec_top(bc_program_vec(p, idx, BC_TYPE_VAR)); + + if (!exec) goto exit; + if (BC_ERR(!BC_PROG_STR(n))) { + s = bc_vm_err(BC_ERROR_EXEC_TYPE); + goto exit; + } + + sidx = n->rdx; + } + else { + + // In non-conditional situations, only the top of stack can be executed, + // and in those cases, variables are not allowed to be "on the stack"; + // they are only put on the stack to be assigned to. + assert(r->t != BC_RESULT_VAR); + + if (r->t == BC_RESULT_STR) sidx = r->d.loc.loc; + else goto no_exec; + } + + fidx = sidx + BC_PROG_REQ_FUNCS; + str = bc_program_str(p, sidx); + f = bc_vec_item(&p->fns, fidx); + + if (!f->code.len) { + + bc_parse_init(&prs, p, fidx); + bc_lex_file(&prs.l, vm->file); + s = bc_parse_text(&prs, str); + if (BC_ERR(s)) goto err; + s = vm->expr(&prs, BC_PARSE_NOCALL); + if (BC_ERR(s)) goto err; + + // We can just assert this here because + // dc should parse everything until EOF. + assert(prs.l.t == BC_LEX_EOF); + + bc_parse_free(&prs); + } + + ip.idx = 0; + ip.len = p->results.len; + ip.func = fidx; + + bc_vec_pop(&p->results); + bc_vec_push(&p->stack, &ip); + + return BC_STATUS_SUCCESS; + +err: + bc_parse_free(&prs); + f = bc_vec_item(&p->fns, fidx); + bc_vec_npop(&f->code, f->code.len); +exit: + bc_vec_pop(&p->results); +no_exec: + return s; +} +#endif // DC_ENABLED + +static void bc_program_pushBigDig(BcProgram *p, BcBigDig dig, BcResultType type) +{ + BcResult res; + + bc_num_createFromBigdig(&res.d.n, dig); + + res.t = type; + bc_vec_push(&p->results, &res); +} + +static void bc_program_pushGlobal(BcProgram *p, uchar inst) { + + BcResultType t; + + assert(inst >= BC_INST_IBASE && inst <= BC_INST_SCALE); + + t = inst - BC_INST_IBASE + BC_RESULT_IBASE; + bc_program_pushBigDig(p, p->globals[inst - BC_INST_IBASE], t); +} + +#ifndef NDEBUG +void bc_program_free(BcProgram *p) { + + size_t i; + + assert(p); + + for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i) bc_vec_free(p->globals_v + i); + + bc_vec_free(&p->fns); +#if BC_ENABLED + bc_vec_free(&p->fn_map); +#endif // BC_ENABLED + bc_vec_free(&p->vars); + bc_vec_free(&p->var_map); + bc_vec_free(&p->arrs); + bc_vec_free(&p->arr_map); + bc_vec_free(&p->results); + bc_vec_free(&p->stack); +#if BC_ENABLED + bc_num_free(&p->last); +#endif // BC_ENABLED +} +#endif // NDEBUG + +void bc_program_init(BcProgram *p) { + + BcInstPtr ip; + size_t i; + BcBigDig val = BC_BASE; + + assert(p); + + memset(p, 0, sizeof(BcProgram)); + memset(&ip, 0, sizeof(BcInstPtr)); + + for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i) { + bc_vec_init(p->globals_v + i, sizeof(BcBigDig), NULL); + val = i == BC_PROG_GLOBALS_SCALE ? 0 : val; + bc_vec_push(p->globals_v + i, &val); + p->globals[i] = val; + } + +#if DC_ENABLED + p->strm = UCHAR_MAX + 1; + bc_num_setup(&p->strmb, p->strmb_num, BC_NUM_BIGDIG_LOG10); + bc_num_bigdig2num(&p->strmb, p->strm); +#endif // DC_ENABLED + + bc_num_setup(&p->one, p->one_num, BC_PROG_ONE_CAP); + bc_num_one(&p->one); + +#if BC_ENABLED + bc_num_init(&p->last, BC_NUM_DEF_SIZE); +#endif // BC_ENABLED + + bc_vec_init(&p->fns, sizeof(BcFunc), bc_func_free); +#if BC_ENABLED + bc_map_init(&p->fn_map); + bc_program_insertFunc(p, bc_vm_strdup(bc_func_main)); + bc_program_insertFunc(p, bc_vm_strdup(bc_func_read)); +#else // BC_ENABLED + { + BcFunc f; + bc_program_addFunc(p, &f, bc_func_main); + bc_program_addFunc(p, &f, bc_func_read); + } +#endif // BC_ENABLED + + bc_vec_init(&p->vars, sizeof(BcVec), bc_vec_free); + bc_map_init(&p->var_map); + + bc_vec_init(&p->arrs, sizeof(BcVec), bc_vec_free); + bc_map_init(&p->arr_map); + + bc_vec_init(&p->results, sizeof(BcResult), bc_result_free); + bc_vec_init(&p->stack, sizeof(BcInstPtr), NULL); + bc_vec_push(&p->stack, &ip); +} + +void bc_program_addFunc(BcProgram *p, BcFunc *f, const char *name) { + bc_func_init(f, name); + bc_vec_push(&p->fns, f); +} + +#if BC_ENABLED +size_t bc_program_insertFunc(BcProgram *p, char *name) { + + BcId id; + BcFunc f; + bool new; + size_t idx; + + assert(p && name); + + id.name = name; + id.idx = p->fns.len; + + new = bc_map_insert(&p->fn_map, &id, &idx); + idx = ((BcId*) bc_vec_item(&p->fn_map, idx))->idx; + + if (!new) { + BcFunc *func = bc_vec_item(&p->fns, idx); + bc_func_reset(func); + free(name); + } + else bc_program_addFunc(p, &f, name); + + return idx; +} +#endif // BC_ENABLED + +BcStatus bc_program_reset(BcProgram *p, BcStatus s) { + + BcFunc *f; + BcInstPtr *ip; + + bc_vec_npop(&p->stack, p->stack.len - 1); + bc_vec_npop(&p->results, p->results.len); + + f = bc_vec_item(&p->fns, 0); + ip = bc_vec_top(&p->stack); + ip->idx = f->code.len; + +#if BC_ENABLE_SIGNALS + if (BC_SIGTERM || (!s && BC_SIGINT && BC_I)) return BC_STATUS_QUIT; + + vm->sig_chk = vm->sig; + + if (!s || s == BC_STATUS_SIGNAL) { + + if (BC_TTYIN || BC_I) { + bc_vm_puts(bc_program_ready_msg, stderr); + bc_vm_fflush(stderr); + s = BC_STATUS_SUCCESS; + } + else s = BC_STATUS_QUIT; + } +#endif // BC_ENABLE_SIGNALS + + return s; +} + +BcStatus bc_program_exec(BcProgram *p) { + + BcStatus s = BC_STATUS_SUCCESS; + size_t idx; + BcResult r, *ptr; + BcInstPtr *ip = bc_vec_top(&p->stack); + BcFunc *func = bc_vec_item(&p->fns, ip->func); + char *code = func->code.v; + bool cond = false; +#if BC_ENABLED + BcNum *num = NULL; +#endif // BC_ENABLED + + while (BC_NO_SIG && BC_NO_ERR(!s) && ip->idx < func->code.len) { + + uchar inst = (uchar) code[(ip->idx)++]; + + switch (inst) { + +#if BC_ENABLED + case BC_INST_JUMP_ZERO: + { + s = bc_program_prep(p, &ptr, &num); + if (BC_ERR(s)) return s; + cond = !bc_num_cmpZero(num); + bc_vec_pop(&p->results); + } + // Fallthrough. + case BC_INST_JUMP: + { + size_t *addr; + + idx = bc_program_index(code, &ip->idx); + addr = bc_vec_item(&func->labels, idx); + + assert(*addr != SIZE_MAX); + + if (inst == BC_INST_JUMP || cond) ip->idx = *addr; + + break; + } + + case BC_INST_CALL: + { + s = bc_program_call(p, code, &ip->idx); + ip = bc_vec_top(&p->stack); + func = bc_vec_item(&p->fns, ip->func); + code = func->code.v; + break; + } + + case BC_INST_INC_PRE: + case BC_INST_DEC_PRE: + case BC_INST_INC_POST: + case BC_INST_DEC_POST: + case BC_INST_INC_NO_VAL: + case BC_INST_DEC_NO_VAL: + { + s = bc_program_incdec(p, inst); + break; + } + + case BC_INST_HALT: + { + s = BC_STATUS_QUIT; + break; + } + + case BC_INST_RET: + case BC_INST_RET0: + case BC_INST_RET_VOID: + { + s = bc_program_return(p, inst); + ip = bc_vec_top(&p->stack); + func = bc_vec_item(&p->fns, ip->func); + code = func->code.v; + break; + } + + case BC_INST_BOOL_OR: + case BC_INST_BOOL_AND: +#endif // BC_ENABLED + case BC_INST_REL_EQ: + case BC_INST_REL_LE: + case BC_INST_REL_GE: + case BC_INST_REL_NE: + case BC_INST_REL_LT: + case BC_INST_REL_GT: + { + s = bc_program_logical(p, inst); + break; + } + + case BC_INST_READ: + { + s = bc_program_read(p); + ip = bc_vec_top(&p->stack); + func = bc_vec_item(&p->fns, ip->func); + code = func->code.v; + break; + } + + case BC_INST_MAXIBASE: + case BC_INST_MAXOBASE: + case BC_INST_MAXSCALE: + { + BcBigDig dig = vm->maxes[inst - BC_INST_MAXIBASE]; + bc_program_pushBigDig(p, dig, BC_RESULT_TEMP); + break; + } + + case BC_INST_VAR: + { + s = bc_program_pushVar(p, code, &ip->idx, false, false); + break; + } + + case BC_INST_ARRAY_ELEM: + case BC_INST_ARRAY: + { + s = bc_program_pushArray(p, code, &ip->idx, inst); + break; + } + + case BC_INST_IBASE: + case BC_INST_SCALE: + case BC_INST_OBASE: + { + bc_program_pushGlobal(p, inst); + break; + } + + case BC_INST_LENGTH: + case BC_INST_SCALE_FUNC: + case BC_INST_SQRT: + case BC_INST_ABS: + { + s = bc_program_builtin(p, inst); + break; + } + + case BC_INST_NUM: + { + r.t = BC_RESULT_CONSTANT; + r.d.loc.loc = bc_program_index(code, &ip->idx); + bc_vec_push(&p->results, &r); + break; + } + + case BC_INST_ONE: +#if BC_ENABLED + case BC_INST_LAST: +#endif // BC_ENABLED + { + r.t = BC_RESULT_ONE + (inst - BC_INST_ONE); + bc_vec_push(&p->results, &r); + break; + } + + case BC_INST_PRINT: + case BC_INST_PRINT_POP: + case BC_INST_PRINT_STR: + { + s = bc_program_print(p, inst, 0); + break; + } + + case BC_INST_STR: + { + r.t = BC_RESULT_STR; + r.d.loc.loc = bc_program_index(code, &ip->idx); + bc_vec_push(&p->results, &r); + break; + } + + case BC_INST_POWER: + case BC_INST_MULTIPLY: + case BC_INST_DIVIDE: + case BC_INST_MODULUS: + case BC_INST_PLUS: + case BC_INST_MINUS: +#if BC_ENABLE_EXTRA_MATH + case BC_INST_PLACES: + case BC_INST_LSHIFT: + case BC_INST_RSHIFT: +#endif // BC_ENABLE_EXTRA_MATH + { + s = bc_program_op(p, inst); + break; + } + + case BC_INST_NEG: + case BC_INST_BOOL_NOT: +#if BC_ENABLE_EXTRA_MATH + case BC_INST_TRUNC: +#endif // BC_ENABLE_EXTRA_MATH + { + s = bc_program_unary(p, inst); + break; + } + +#if BC_ENABLED + case BC_INST_ASSIGN_POWER: + case BC_INST_ASSIGN_MULTIPLY: + case BC_INST_ASSIGN_DIVIDE: + case BC_INST_ASSIGN_MODULUS: + case BC_INST_ASSIGN_PLUS: + case BC_INST_ASSIGN_MINUS: +#if BC_ENABLE_EXTRA_MATH + case BC_INST_ASSIGN_PLACES: + case BC_INST_ASSIGN_LSHIFT: + case BC_INST_ASSIGN_RSHIFT: +#endif // BC_ENABLE_EXTRA_MATH + case BC_INST_ASSIGN: + case BC_INST_ASSIGN_POWER_NO_VAL: + case BC_INST_ASSIGN_MULTIPLY_NO_VAL: + case BC_INST_ASSIGN_DIVIDE_NO_VAL: + case BC_INST_ASSIGN_MODULUS_NO_VAL: + case BC_INST_ASSIGN_PLUS_NO_VAL: + case BC_INST_ASSIGN_MINUS_NO_VAL: + #if BC_ENABLE_EXTRA_MATH + case BC_INST_ASSIGN_PLACES_NO_VAL: + case BC_INST_ASSIGN_LSHIFT_NO_VAL: + case BC_INST_ASSIGN_RSHIFT_NO_VAL: + #endif // BC_ENABLE_EXTRA_MATH +#endif // BC_ENABLED + case BC_INST_ASSIGN_NO_VAL: + { + s = bc_program_assign(p, inst); + break; + } + +#if DC_ENABLED + case BC_INST_POP: + { +#ifndef BC_PROG_NO_STACK_CHECK + s = bc_program_checkStack(&p->results, 1); + if (BC_ERR(s)) return s; +#endif // BC_PROG_NO_STACK_CHECK + bc_vec_pop(&p->results); + break; + } + + case BC_INST_POP_EXEC: + { + assert(BC_PROG_STACK(&p->stack, 2)); + bc_vec_pop(&p->stack); + ip = bc_vec_top(&p->stack); + func = bc_vec_item(&p->fns, ip->func); + code = func->code.v; + break; + } + + case BC_INST_MODEXP: + { + s = bc_program_modexp(p); + break; + } + + case BC_INST_DIVMOD: + { + s = bc_program_divmod(p); + break; + } + + case BC_INST_EXECUTE: + case BC_INST_EXEC_COND: + { + cond = (inst == BC_INST_EXEC_COND); + s = bc_program_execStr(p, code, &ip->idx, cond); + ip = bc_vec_top(&p->stack); + func = bc_vec_item(&p->fns, ip->func); + code = func->code.v; + break; + } + + case BC_INST_PRINT_STACK: + { + for (idx = 0; BC_NO_ERR(!s) && idx < p->results.len; ++idx) + s = bc_program_print(p, BC_INST_PRINT, idx); + break; + } + + case BC_INST_CLEAR_STACK: + { + bc_vec_npop(&p->results, p->results.len); + break; + } + + case BC_INST_STACK_LEN: + { + bc_program_stackLen(p); + break; + } + + case BC_INST_DUPLICATE: + { + s = bc_program_checkStack(&p->results, 1); + if (BC_ERR(s)) break; + ptr = bc_vec_top(&p->results); + bc_result_copy(&r, ptr); + bc_vec_push(&p->results, &r); + break; + } + + case BC_INST_SWAP: + { + BcResult *ptr2; + + s = bc_program_checkStack(&p->results, 2); + if (BC_ERR(s)) break; + + ptr = bc_vec_item_rev(&p->results, 0); + ptr2 = bc_vec_item_rev(&p->results, 1); + memcpy(&r, ptr, sizeof(BcResult)); + memcpy(ptr, ptr2, sizeof(BcResult)); + memcpy(ptr2, &r, sizeof(BcResult)); + + break; + } + + case BC_INST_ASCIIFY: + { + s = bc_program_asciify(p); + ip = bc_vec_top(&p->stack); + func = bc_vec_item(&p->fns, ip->func); + code = func->code.v; + break; + } + + case BC_INST_PRINT_STREAM: + { + s = bc_program_printStream(p); + break; + } + + case BC_INST_LOAD: + case BC_INST_PUSH_VAR: + { + bool copy = (inst == BC_INST_LOAD); + s = bc_program_pushVar(p, code, &ip->idx, true, copy); + break; + } + + case BC_INST_PUSH_TO_VAR: + { + idx = bc_program_index(code, &ip->idx); + s = bc_program_copyToVar(p, idx, BC_TYPE_VAR, true); + break; + } + + case BC_INST_QUIT: + { + if (p->stack.len <= 2) s = BC_STATUS_QUIT; + else bc_vec_npop(&p->stack, 2); + ip = bc_vec_top(&p->stack); + func = bc_vec_item(&p->fns, ip->func); + code = func->code.v; + break; + } + + case BC_INST_NQUIT: + { + s = bc_program_nquit(p); + ip = bc_vec_top(&p->stack); + func = bc_vec_item(&p->fns, ip->func); + code = func->code.v; + break; + } +#endif // DC_ENABLED +#ifndef NDEBUG + default: + { + assert(false); + break; + } +#endif // NDEBUG + } + } + + if (BC_UNLIKELY(s && s != BC_STATUS_QUIT) || BC_SIG) + s = bc_program_reset(p, s); + + return s; +} + +#if BC_DEBUG_CODE +#if BC_ENABLED && DC_ENABLED +static void bc_program_printIndex(const char *restrict code, + size_t *restrict bgn) +{ + uchar byte, i, bytes = (uchar) code[(*bgn)++]; + ulong val = 0; + + for (byte = 1, i = 0; byte && i < bytes; ++i) { + byte = (uchar) code[(*bgn)++]; + if (byte) val |= ((ulong) byte) << (CHAR_BIT * i); + } + + bc_vm_printf(" (%lu) ", val); +} + +static void bc_program_printName(BcProgram *p, const char *restrict code, + size_t *restrict bgn) +{ + size_t idx = bc_program_index(code, bgn); + BcFunc *f = bc_program_func(p); + char **str = bc_vec_item(&f->strs, idx); + assert(str && *str); + bc_vm_printf(" (%s) ", *str); +} + +static void bc_program_printStr(BcProgram *p, const char *restrict code, + size_t *restrict bgn) +{ + size_t idx = bc_program_index(code, bgn); + char *s; + + s = bc_program_str(p, idx); + + bc_vm_printf(" (\"%s\") ", s); +} + +void bc_program_printInst(BcProgram *p, const char *restrict code, + size_t *restrict bgn) +{ + uchar inst = (uchar) code[(*bgn)++]; + + bc_vm_printf("Inst: %u, %c; ", inst, (char) inst); + + if (inst == BC_INST_VAR || inst == BC_INST_ARRAY_ELEM || + inst == BC_INST_ARRAY) + { + bc_program_printName(p, code, bgn); + } + else if (inst == BC_INST_STR) bc_program_printStr(p, code, bgn); + else if (inst == BC_INST_NUM) { + size_t idx = bc_program_index(code, bgn); + BcConst *c = bc_program_const(p, idx); + bc_vm_printf("(%s)", c->val); + } + else if (inst == BC_INST_CALL || + (inst > BC_INST_STR && inst <= BC_INST_JUMP_ZERO)) + { + bc_program_printIndex(code, bgn); + if (inst == BC_INST_CALL) bc_program_printIndex(code, bgn); + } + + bc_vm_putchar('\n'); +} + +void bc_program_code(BcProgram *p) { + + BcFunc *f; + char *code; + BcInstPtr ip; + size_t i; + + for (i = 0; i < p->fns.len; ++i) { + + ip.idx = ip.len = 0; + ip.func = i; + + f = bc_vec_item(&p->fns, ip.func); + code = f->code.v; + + bc_vm_printf("func[%zu]:\n", ip.func); + while (ip.idx < f->code.len) bc_program_printInst(p, code, &ip.idx); + bc_vm_printf("\n\n"); + } +} +#endif // BC_ENABLED && DC_ENABLED +#endif // BC_DEBUG_CODE Index: contrib/bc/src/read.c =================================================================== --- contrib/bc/src/read.c +++ contrib/bc/src/read.c @@ -0,0 +1,183 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code to handle special I/O for bc. + * + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include +#include + +static bool bc_read_binary(const char *buf, size_t size) { + + size_t i; + + for (i = 0; i < size; ++i) { + if (BC_ERR(BC_READ_BIN_CHAR(buf[i]))) return true; + } + + return false; +} + +BcStatus bc_read_chars(BcVec *vec, const char *prompt) { + + int i; + signed char c = 0; + + assert(vec && vec->size == sizeof(char)); + + bc_vec_npop(vec, vec->len); + + if (BC_USE_PROMPT) { + bc_vm_puts(prompt, stderr); + bc_vm_fflush(stderr); + } + + while (BC_NO_SIG && c != '\n') { + + i = fgetc(stdin); + + if (BC_UNLIKELY(i == EOF)) { + +#if BC_ENABLE_SIGNALS + if (errno == EINTR) { + + if (BC_SIGTERM) return BC_STATUS_QUIT; + + vm->sig_chk = vm->sig; + + if (BC_TTYIN || BC_I) { + bc_vm_puts(bc_program_ready_msg, stderr); + if (BC_USE_PROMPT) bc_vm_puts(prompt, stderr); + bc_vm_fflush(stderr); + } + else return BC_STATUS_SIGNAL; + + continue; + } +#endif // BC_ENABLE_SIGNALS + + bc_vec_pushByte(vec, '\0'); + return BC_STATUS_EOF; + } + + c = (signed char) i; + bc_vec_push(vec, &c); + } + + bc_vec_pushByte(vec, '\0'); + + return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; +} + +BcStatus bc_read_line(BcVec *vec, const char *prompt) { + + BcStatus s; + + // We are about to output to stderr, so flush stdout to + // make sure that we don't get the outputs mixed up. + bc_vm_fflush(stdout); + +#if BC_ENABLE_HISTORY + s = bc_history_line(&vm->history, vec, prompt); +#else // BC_ENABLE_HISTORY + s = bc_read_chars(vec, prompt); +#endif // BC_ENABLE_HISTORY + + if (BC_ERR(s && s != BC_STATUS_EOF)) return s; + if (BC_ERR(bc_read_binary(vec->v, vec->len - 1))) + return bc_vm_verr(BC_ERROR_FATAL_BIN_FILE, bc_program_stdin_name); + + return s; +} + +BcStatus bc_read_file(const char *path, char **buf) { + + BcError e = BC_ERROR_FATAL_IO_ERR; + FILE *f; + size_t size, read; + long res; + struct stat pstat; + + assert(path); + + f = fopen(path, "r"); + if (BC_ERR(!f)) return bc_vm_verr(BC_ERROR_FATAL_FILE_ERR, path); + if (BC_ERR(fstat(fileno(f), &pstat) == -1)) goto malloc_err; + + if (BC_ERR(S_ISDIR(pstat.st_mode))) { + e = BC_ERROR_FATAL_PATH_DIR; + goto malloc_err; + } + + if (BC_ERR(fseek(f, 0, SEEK_END) == -1)) goto malloc_err; + res = ftell(f); + if (BC_ERR(res < 0)) goto malloc_err; + if (BC_ERR(fseek(f, 0, SEEK_SET) == -1)) goto malloc_err; + + size = (size_t) res; + *buf = bc_vm_malloc(size + 1); + + read = fread(*buf, 1, size, f); + if (BC_ERR(read != size)) goto read_err; + + (*buf)[size] = '\0'; + + if (BC_ERR(bc_read_binary(*buf, size))) { + e = BC_ERROR_FATAL_BIN_FILE; + goto read_err; + } + + fclose(f); + + return BC_STATUS_SUCCESS; + +read_err: + free(*buf); +malloc_err: + fclose(f); + return bc_vm_verr(e, path); +} Index: contrib/bc/src/vector.c =================================================================== --- contrib/bc/src/vector.c +++ contrib/bc/src/vector.c @@ -0,0 +1,246 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code to manipulate vectors (resizable arrays). + * + */ + +#include +#include +#include + +#include +#include +#include +#include + +static void bc_vec_grow(BcVec *restrict v, size_t n) { + + size_t len, cap = v->cap; + + len = bc_vm_growSize(v->len, n); + + while (cap < len) cap = bc_vm_growSize(cap, cap); + + v->v = bc_vm_realloc(v->v, bc_vm_arraySize(cap, v->size)); + v->cap = cap; +} + +void bc_vec_init(BcVec *restrict v, size_t esize, BcVecFree dtor) { + assert(v && esize); + v->size = esize; + v->cap = BC_VEC_START_CAP; + v->len = 0; + v->dtor = dtor; + v->v = bc_vm_malloc(bc_vm_arraySize(BC_VEC_START_CAP, esize)); +} + +void bc_vec_expand(BcVec *restrict v, size_t req) { + assert(v); + if (v->cap < req) { + v->v = bc_vm_realloc(v->v, bc_vm_arraySize(req, v->size)); + v->cap = req; + } +} + +void bc_vec_npop(BcVec *restrict v, size_t n) { + assert(v && n <= v->len); + if (!v->dtor) v->len -= n; + else { + size_t len = v->len - n; + while (v->len > len) v->dtor(v->v + (v->size * --v->len)); + } +} + +void bc_vec_npush(BcVec *restrict v, size_t n, const void *data) { + assert(v && data); + if (v->len + n > v->cap) bc_vec_grow(v, n); + memcpy(v->v + (v->size * v->len), data, v->size * n); + v->len += n; +} + +void bc_vec_push(BcVec *restrict v, const void *data) { + bc_vec_npush(v, 1, data); +} + +void bc_vec_pushByte(BcVec *restrict v, uchar data) { + assert(v && v->size == sizeof(uchar)); + bc_vec_push(v, &data); +} + +void bc_vec_pushIndex(BcVec *restrict v, size_t idx) { + + uchar amt, nums[sizeof(size_t)]; + + assert(v->size == sizeof(uchar)); + + for (amt = 0; idx; ++amt) { + nums[amt] = (uchar) idx; + idx &= ((size_t) ~(UCHAR_MAX)); + idx >>= sizeof(uchar) * CHAR_BIT; + } + + bc_vec_push(v, &amt); + bc_vec_npush(v, amt, nums); +} + +static void bc_vec_pushAt(BcVec *restrict v, const void *data, size_t idx) { + + assert(v && data && idx <= v->len); + + if (idx == v->len) bc_vec_push(v, data); + else { + + char *ptr; + + if (v->len == v->cap) bc_vec_grow(v, 1); + + ptr = v->v + v->size * idx; + + memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); + memmove(ptr, data, v->size); + } +} + +void bc_vec_string(BcVec *restrict v, size_t len, const char *restrict str) { + + assert(v && v->size == sizeof(char)); + assert(!v->len || !v->v[v->len - 1]); + assert(v->v != str); + + bc_vec_npop(v, v->len); + bc_vec_expand(v, bc_vm_growSize(len, 1)); + memcpy(v->v, str, len); + v->len = len; + + bc_vec_pushByte(v, '\0'); +} + +void bc_vec_concat(BcVec *restrict v, const char *restrict str) { + + size_t len; + + assert(v && v->size == sizeof(char)); + assert(!v->len || !v->v[v->len - 1]); + + if (!v->len) bc_vec_pushByte(v, '\0'); + + len = strlen(str); + bc_vec_grow(v, len); + strcat(v->v + v->len - 1, str); + v->len += len; +} + +void bc_vec_empty(BcVec *restrict v) { + assert(v && v->size == sizeof(char)); + bc_vec_npop(v, v->len); + bc_vec_pushByte(v, '\0'); +} + +#if BC_ENABLE_HISTORY +void bc_vec_popAt(BcVec *restrict v, size_t idx) { + + char* ptr, *data; + + assert(idx + 1 < v->len); + + ptr = bc_vec_item(v, idx); + data = bc_vec_item(v, idx + 1); + + if (v->dtor) v->dtor(ptr); + + v->len -= 1; + memmove(ptr, data, v->len * v->size); +} + +void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data) { + char *ptr = bc_vec_item(v, idx); + if (v->dtor) v->dtor(ptr); + memcpy(ptr, data, v->size); +} +#endif // BC_ENABLE_HISTORY + +void* bc_vec_item(const BcVec *restrict v, size_t idx) { + assert(v && v->len && idx < v->len); + return v->v + v->size * idx; +} + +void* bc_vec_item_rev(const BcVec *restrict v, size_t idx) { + assert(v && v->len && idx < v->len); + return v->v + v->size * (v->len - idx - 1); +} + +void bc_vec_free(void *vec) { + BcVec *v = (BcVec*) vec; + bc_vec_npop(v, v->len); + free(v->v); +} + +static size_t bc_map_find(const BcVec *restrict v, const BcId *restrict ptr) { + + size_t low = 0, high = v->len; + + while (low < high) { + + size_t mid = (low + high) / 2; + BcId *id = bc_vec_item(v, mid); + int result = bc_id_cmp(ptr, id); + + if (!result) return mid; + else if (result < 0) high = mid; + else low = mid + 1; + } + + return low; +} + +bool bc_map_insert(BcVec *restrict v, const BcId *restrict ptr, + size_t *restrict i) +{ + assert(v && ptr && i); + + *i = bc_map_find(v, ptr); + + assert(*i <= v->len); + + if (*i == v->len) bc_vec_push(v, ptr); + else if (!bc_id_cmp(ptr, bc_vec_item(v, *i))) return false; + else bc_vec_pushAt(v, ptr, *i); + + return true; +} + +size_t bc_map_index(const BcVec *restrict v, const BcId *restrict ptr) { + assert(v && ptr); + size_t i = bc_map_find(v, ptr); + if (i >= v->len) return BC_VEC_INVALID_IDX; + return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; +} Index: contrib/bc/src/vm.c =================================================================== --- contrib/bc/src/vm.c +++ contrib/bc/src/vm.c @@ -0,0 +1,692 @@ +/* + * ***************************************************************************** + * + * Copyright (c) 2018-2019 Gavin D. Howard and contributors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * ***************************************************************************** + * + * Code common to all of bc and dc. + * + */ + +#include +#include +#include +#include +#include +#include + +#include + +#ifndef _WIN32 + +#include +#include + +#else // _WIN32 + +#define WIN32_LEAN_AND_MEAN +#include +#include + +#endif // _WIN32 + +#include +#include +#include +#include +#include + +#if BC_ENABLE_SIGNALS +#ifndef _WIN32 +static void bc_vm_sig(int sig) { + + int err = errno; + + if (sig == SIGINT) { + + size_t n = vm->siglen; + + if (BC_NO_ERR(write(STDERR_FILENO, vm->sigmsg, n) == (ssize_t) n)) + vm->sig += 1; + + if (vm->sig == BC_SIGTERM_VAL) vm->sig = 1; + } + else vm->sig = BC_SIGTERM_VAL; + + errno = err; +} +#else // _WIN32 +static BOOL WINAPI bc_vm_sig(DWORD sig) { + + if (sig == CTRL_C_EVENT) { + bc_vm_puts(vm->sigmsg, stderr); + vm->sig += 1; + if (vm->sig == BC_SIGTERM_VAL) vm->sig = 1; + } + else vm->sig = BC_SIGTERM_VAL; + + return TRUE; +} +#endif // _WIN32 +#endif // BC_ENABLE_SIGNALS + +void bc_vm_info(const char* const help) { + + bc_vm_printf("%s %s\n", vm->name, BC_VERSION); + bc_vm_puts(bc_copyright, stdout); + + if (help) { + bc_vm_putchar('\n'); + bc_vm_printf(help, vm->name, vm->name); + } +} + +BcStatus bc_vm_error(BcError e, size_t line, ...) { + + va_list args; + uchar id = bc_err_ids[e]; + const char* err_type = vm->err_ids[id]; + + assert(e < BC_ERROR_NELEMS); + +#if BC_ENABLED + if (!BC_S && e >= BC_ERROR_POSIX_START) { + if (BC_W) { + // Make sure to not return an error. + id = UCHAR_MAX; + err_type = vm->err_ids[BC_ERR_IDX_WARN]; + } + else return BC_STATUS_SUCCESS; + } +#endif // BC_ENABLED + + // Make sure all of stdout is written first. + fflush(stdout); + + va_start(args, line); + fprintf(stderr, "\n%s ", err_type); + vfprintf(stderr, vm->err_msgs[e], args); + va_end(args); + + if (BC_NO_ERR(vm->file)) { + + // This is the condition for parsing vs runtime. + // If line is not 0, it is parsing. + if (line) { + fprintf(stderr, "\n %s", vm->file); + fprintf(stderr, bc_err_line, line); + } + else { + + BcInstPtr *ip = bc_vec_item_rev(&vm->prog.stack, 0); + BcFunc *f = bc_vec_item(&vm->prog.fns, ip->func); + + fprintf(stderr, "\n %s %s", vm->func_header, f->name); + + if (BC_IS_BC && ip->func != BC_PROG_MAIN && + ip->func != BC_PROG_READ) + { + fprintf(stderr, "()"); + } + } + } + + fputs("\n\n", stderr); + fflush(stderr); + + return (BcStatus) (id + 1); +} + +static BcStatus bc_vm_envArgs(const char* const env_args_name) { + + BcStatus s; + BcVec v; + char *env_args = getenv(env_args_name), *buffer, *buf; + + if (!env_args) return BC_STATUS_SUCCESS; + + buffer = bc_vm_strdup(env_args); + buf = buffer; + + bc_vec_init(&v, sizeof(char*), NULL); + bc_vec_push(&v, &env_args_name); + + while (*buf) { + + if (!isspace(*buf)) { + + bc_vec_push(&v, &buf); + + while (*buf && !isspace(*buf)) buf += 1; + + if (*buf) { + *buf = '\0'; + buf += 1; + } + } + else buf += 1; + } + + // Make sure to push a NULL pointer at the end. + buf = NULL; + bc_vec_push(&v, &buf); + + s = bc_args((int) v.len - 1, bc_vec_item(&v, 0)); + + bc_vec_free(&v); + free(buffer); + + return s; +} + +static size_t bc_vm_envLen(const char *var) { + + char *lenv = getenv(var); + size_t i, len = BC_NUM_PRINT_WIDTH; + int num; + + if (!lenv) return len; + + len = strlen(lenv); + + for (num = 1, i = 0; num && i < len; ++i) num = isdigit(lenv[i]); + if (num) { + len = (size_t) atoi(lenv) - 1; + if (len < 2 || len >= UINT16_MAX) len = BC_NUM_PRINT_WIDTH; + } + else len = BC_NUM_PRINT_WIDTH; + + return len; +} + +void bc_vm_shutdown(void) { +#if BC_ENABLE_NLS + if (vm->catalog != BC_VM_INVALID_CATALOG) catclose(vm->catalog); +#endif // BC_ENABLE_NLS +#if BC_ENABLE_HISTORY + // This must always run to ensure that the terminal is back to normal. + bc_history_free(&vm->history); +#endif // BC_ENABLE_HISTORY +#ifndef NDEBUG + bc_vec_free(&vm->files); + bc_vec_free(&vm->exprs); + bc_program_free(&vm->prog); + bc_parse_free(&vm->prs); + free(vm); +#endif // NDEBUG +} + +static void bc_vm_exit(BcError e) { + BcStatus s = bc_vm_err(e); + bc_vm_shutdown(); + exit((int) s); +} + +size_t bc_vm_arraySize(size_t n, size_t size) { + size_t res = n * size; + if (BC_ERR(res >= SIZE_MAX || (n != 0 && res / n != size))) + bc_vm_exit(BC_ERROR_FATAL_ALLOC_ERR); + return res; +} + +size_t bc_vm_growSize(size_t a, size_t b) { + size_t res = a + b; + if (BC_ERR(res >= SIZE_MAX || res < a || res < b)) + bc_vm_exit(BC_ERROR_FATAL_ALLOC_ERR); + return res; +} + +void* bc_vm_malloc(size_t n) { + void* ptr = malloc(n); + if (BC_ERR(!ptr)) bc_vm_exit(BC_ERROR_FATAL_ALLOC_ERR); + return ptr; +} + +void* bc_vm_realloc(void *ptr, size_t n) { + void* temp = realloc(ptr, n); + if (BC_ERR(!temp)) bc_vm_exit(BC_ERROR_FATAL_ALLOC_ERR); + return temp; +} + +char* bc_vm_strdup(const char *str) { + char *s = strdup(str); + if (BC_ERR(!s)) bc_vm_exit(BC_ERROR_FATAL_ALLOC_ERR); + return s; +} + +size_t bc_vm_printf(const char *fmt, ...) { + + va_list args; + int ret; + + va_start(args, fmt); + ret = vfprintf(stdout, fmt, args); + va_end(args); + + if (BC_IO_ERR(ret, stdout)) bc_vm_exit(BC_ERROR_FATAL_IO_ERR); + + vm->nchars = 0; + + return (size_t) ret; +} + +void bc_vm_puts(const char *str, FILE *restrict f) { + if (BC_IO_ERR(fputs(str, f), f)) bc_vm_exit(BC_ERROR_FATAL_IO_ERR); +} + +void bc_vm_putchar(int c) { + if (BC_IO_ERR(fputc(c, stdout), stdout)) bc_vm_exit(BC_ERROR_FATAL_IO_ERR); + vm->nchars = (c == '\n' ? 0 : vm->nchars + 1); +} + +void bc_vm_fflush(FILE *restrict f) { + if (BC_IO_ERR(fflush(f), f)) bc_vm_exit(BC_ERROR_FATAL_IO_ERR); +} + +static void bc_vm_clean(void) { + + BcProgram *prog = &vm->prog; + BcVec *fns = &prog->fns; + BcFunc *f = bc_vec_item(fns, BC_PROG_MAIN); + BcInstPtr *ip = bc_vec_item(&prog->stack, 0); + bool good = false; + +#if BC_ENABLED + if (BC_IS_BC) good = !BC_PARSE_NO_EXEC(&vm->prs); +#endif // BC_ENABLED + +#if DC_ENABLED + if (!BC_IS_BC) { + + size_t i; + + for (i = 0; i < vm->prog.vars.len; ++i) { + BcVec *arr = bc_vec_item(&vm->prog.vars, i); + BcNum *n = bc_vec_top(arr); + if (arr->len != 1 || BC_PROG_STR(n)) break; + } + + if (i == vm->prog.vars.len) { + + for (i = 0; i < vm->prog.arrs.len; ++i) { + + BcVec *arr = bc_vec_item(&vm->prog.arrs, i); + size_t j; + + assert(arr->len == 1); + + arr = bc_vec_top(arr); + + for (j = 0; j < arr->len; ++j) { + BcNum *n = bc_vec_item(arr, j); + if (BC_PROG_STR(n)) break; + } + + if (j != arr->len) break; + } + + good = (i == vm->prog.arrs.len); + } + } +#endif // DC_ENABLED + + // If this condition is true, we can get rid of strings, + // constants, and code. This is an idea from busybox. + if (good && prog->stack.len == 1 && !prog->results.len && + ip->idx == f->code.len) + { +#if BC_ENABLED + bc_vec_npop(&f->labels, f->labels.len); +#endif // BC_ENABLED + bc_vec_npop(&f->strs, f->strs.len); + bc_vec_npop(&f->consts, f->consts.len); + bc_vec_npop(&f->code, f->code.len); + ip->idx = 0; +#if DC_ENABLED + if (!BC_IS_BC) bc_vec_npop(fns, fns->len - BC_PROG_REQ_FUNCS); +#endif // DC_ENABLED + } +} + +static BcStatus bc_vm_process(const char *text, bool is_stdin) { + + BcStatus s; + + s = bc_parse_text(&vm->prs, text); + if (BC_ERR(s)) goto err; + + while (vm->prs.l.t != BC_LEX_EOF) { + s = vm->parse(&vm->prs); + if (BC_ERR(s)) goto err; + } + +#if BC_ENABLED + { + uint16_t *flags = BC_PARSE_TOP_FLAG_PTR(&vm->prs); + + if (!is_stdin && vm->prs.flags.len == 1 && + *flags == BC_PARSE_FLAG_IF_END) + { + bc_parse_noElse(&vm->prs); + } + + if (BC_PARSE_NO_EXEC(&vm->prs)) goto err; + } +#endif // BC_ENABLED + + s = bc_program_exec(&vm->prog); + if (BC_I) bc_vm_fflush(stdout); + +err: + bc_vm_clean(); + return s == BC_STATUS_QUIT || !BC_I || !is_stdin ? s : BC_STATUS_SUCCESS; +} + +static BcStatus bc_vm_file(const char *file) { + + BcStatus s; + char *data; + + bc_lex_file(&vm->prs.l, file); + s = bc_read_file(file, &data); + if (BC_ERR(s)) return s; + + s = bc_vm_process(data, false); + if (BC_ERR(s)) goto err; + +#if BC_ENABLED + if (BC_ERR(BC_PARSE_NO_EXEC(&vm->prs))) + s = bc_parse_err(&vm->prs, BC_ERROR_PARSE_BLOCK); +#endif // BC_ENABLED + +err: + free(data); + return s; +} + +static BcStatus bc_vm_stdin(void) { + + BcStatus s = BC_STATUS_SUCCESS; + BcVec buf, buffer; + size_t string = 0; + bool comment = false, done = false; + + bc_lex_file(&vm->prs.l, bc_program_stdin_name); + + bc_vec_init(&buffer, sizeof(uchar), NULL); + bc_vec_init(&buf, sizeof(uchar), NULL); + bc_vec_pushByte(&buffer, '\0'); + s = bc_read_line(&buf, ">>> "); + + // This loop is complex because the vm tries not to send any lines that end + // with a backslash to the parser. The reason for that is because the parser + // treats a backslash+newline combo as whitespace, per the bc spec. In that + // case, and for strings and comments, the parser will expect more stuff. + for (; !BC_STATUS_IS_ERROR(s) && buf.len > 1 && BC_NO_SIG && + s != BC_STATUS_SIGNAL; s = bc_read_line(&buf, ">>> ")) + { + char c2, *str = buf.v; + size_t i, len = buf.len - 1; + + done = (s == BC_STATUS_EOF); + + for (i = 0; i < len; ++i) { + + bool notend = len > i + 1; + uchar c = (uchar) str[i]; + + if (!comment && (i - 1 > len || str[i - 1] != '\\')) { + if (BC_IS_BC) string ^= (c == '"'); + else if (c == ']') string -= 1; + else if (c == '[') string += 1; + } + + if (BC_IS_BC && !string && notend) { + + c2 = str[i + 1]; + + if (c == '/' && !comment && c2 == '*') { + comment = true; + i += 1; + } + else if (c == '*' && comment && c2 == '/') { + comment = false; + i += 1; + } + } + } + + bc_vec_concat(&buffer, buf.v); + + if (string || comment) continue; + if (len >= 2 && str[len - 2] == '\\' && str[len - 1] == '\n') continue; +#if BC_ENABLE_HISTORY + if (vm->history.stdin_has_data) continue; +#endif // BC_ENABLE_HISTORY + + s = bc_vm_process(buffer.v, true); + if (BC_ERR(s)) goto err; + + bc_vec_empty(&buffer); + + if (done) break; + } + + if (BC_ERR(s && s != BC_STATUS_EOF)) goto err; + else if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; + else if (!BC_STATUS_IS_ERROR(s)) { + if (BC_ERR(comment)) + s = bc_parse_err(&vm->prs, BC_ERROR_PARSE_COMMENT); + else if (BC_ERR(string)) + s = bc_parse_err(&vm->prs, BC_ERROR_PARSE_STRING); +#if BC_ENABLED + else if (BC_ERR(BC_PARSE_NO_EXEC(&vm->prs))) + s = bc_parse_err(&vm->prs, BC_ERROR_PARSE_BLOCK); +#endif // BC_ENABLED + } + +err: + bc_vec_free(&buf); + bc_vec_free(&buffer); + return s; +} + +#if BC_ENABLED +static BcStatus bc_vm_load(const char *name, const char *text) { + + BcStatus s; + + bc_lex_file(&vm->prs.l, name); + s = bc_parse_text(&vm->prs, text); + + while (BC_NO_ERR(!s) && vm->prs.l.t != BC_LEX_EOF) s = vm->parse(&vm->prs); + + return s; +} +#endif // BC_ENABLED + +static void bc_vm_defaultMsgs(void) { + + size_t i; + + vm->func_header = bc_err_func_header; + + for (i = 0; i < BC_ERR_IDX_NELEMS + BC_ENABLED; ++i) + vm->err_ids[i] = bc_errs[i]; + for (i = 0; i < BC_ERROR_NELEMS; ++i) vm->err_msgs[i] = bc_err_msgs[i]; +} + +static void bc_vm_gettext(void) { + +#if BC_ENABLE_NLS + uchar id = 0; + int set = 1, msg = 1; + size_t i; + + if (!vm->locale) { + vm->catalog = BC_VM_INVALID_CATALOG; + bc_vm_defaultMsgs(); + return; + } + + vm->catalog = catopen(BC_MAINEXEC, NL_CAT_LOCALE); + + if (vm->catalog == BC_VM_INVALID_CATALOG) { + bc_vm_defaultMsgs(); + return; + } + + vm->func_header = catgets(vm->catalog, set, msg, bc_err_func_header); + + for (set += 1; msg <= BC_ERR_IDX_NELEMS + BC_ENABLED; ++msg) + vm->err_ids[msg - 1] = catgets(vm->catalog, set, msg, bc_errs[msg - 1]); + + i = 0; + id = bc_err_ids[i]; + + for (set = id + 3, msg = 1; i < BC_ERROR_NELEMS; ++i, ++msg) { + + if (id != bc_err_ids[i]) { + msg = 1; + id = bc_err_ids[i]; + set = id + 3; + } + + vm->err_msgs[i] = catgets(vm->catalog, set, msg, bc_err_msgs[i]); + } +#else // BC_ENABLE_NLS + bc_vm_defaultMsgs(); +#endif // BC_ENABLE_NLS +} + +static BcStatus bc_vm_exec(const char* env_exp_exit) { + + BcStatus s = BC_STATUS_SUCCESS; + size_t i; + +#if BC_ENABLED + if (BC_IS_BC && (vm->flags & BC_FLAG_L)) { + + s = bc_vm_load(bc_lib_name, bc_lib); + if (BC_ERR(s)) return s; + +#if BC_ENABLE_EXTRA_MATH + if (!BC_IS_POSIX) { + s = bc_vm_load(bc_lib2_name, bc_lib2); + if (BC_ERR(s)) return s; + } +#endif // BC_ENABLE_EXTRA_MATH + } +#endif // BC_ENABLED + + if (vm->exprs.len) { + bc_lex_file(&vm->prs.l, bc_program_exprs_name); + s = bc_vm_process(vm->exprs.v, false); + if (BC_ERR(s) || getenv(env_exp_exit) != NULL) return s; + } + + for (i = 0; BC_NO_ERR(!s) && i < vm->files.len; ++i) + s = bc_vm_file(*((char**) bc_vec_item(&vm->files, i))); + if (BC_ERR(s)) return s; + + if (BC_IS_BC || !vm->files.len) s = bc_vm_stdin(); + + return s; +} + +BcStatus bc_vm_boot(int argc, char *argv[], const char *env_len, + const char* const env_args, const char* env_exp_exit) +{ + BcStatus s; + int ttyin, ttyout, ttyerr; +#if BC_ENABLE_SIGNALS +#ifndef _WIN32 + struct sigaction sa; + + sigemptyset(&sa.sa_mask); + sa.sa_handler = bc_vm_sig; + sa.sa_flags = 0; + + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + sigaction(SIGQUIT, &sa, NULL); +#else // _WIN32 + SetConsoleCtrlHandler(bc_vm_sig, TRUE); +#endif // _WIN32 +#endif // BC_ENABLE_SIGNALS + + bc_vm_gettext(); + + vm->line_len = (uint16_t) bc_vm_envLen(env_len); + + bc_vec_init(&vm->files, sizeof(char*), NULL); + bc_vec_init(&vm->exprs, sizeof(uchar), NULL); + + bc_program_init(&vm->prog); + bc_parse_init(&vm->prs, &vm->prog, BC_PROG_MAIN); + +#if BC_ENABLE_HISTORY + bc_history_init(&vm->history); +#endif // BC_ENABLE_HISTORY + +#if BC_ENABLED + if (BC_IS_BC) vm->flags |= BC_FLAG_S * (getenv("POSIXLY_CORRECT") != NULL); +#endif // BC_ENABLED + + s = bc_vm_envArgs(env_args); + if (BC_ERR(s)) goto exit; + + s = bc_args(argc, argv); + if (BC_ERR(s)) goto exit; + + ttyin = isatty(STDIN_FILENO); + ttyout = isatty(STDOUT_FILENO); + ttyerr = isatty(STDERR_FILENO); + + vm->flags |= ttyin ? BC_FLAG_TTYIN : 0; + vm->flags |= ttyin && ttyout ? BC_FLAG_I : 0; + + vm->tty = (ttyin != 0 && ttyerr != 0); + + if (BC_IS_POSIX) vm->flags &= ~(BC_FLAG_G); + + vm->maxes[BC_PROG_GLOBALS_IBASE] = BC_NUM_MAX_POSIX_IBASE; + vm->maxes[BC_PROG_GLOBALS_OBASE] = BC_MAX_OBASE; + vm->maxes[BC_PROG_GLOBALS_SCALE] = BC_MAX_SCALE; + + if (BC_IS_BC && !BC_IS_POSIX) + vm->maxes[BC_PROG_GLOBALS_IBASE] = BC_NUM_MAX_IBASE; + + if (BC_IS_BC && BC_I && !(vm->flags & BC_FLAG_Q)) bc_vm_info(NULL); + + s = bc_vm_exec(env_exp_exit); + +exit: + bc_vm_shutdown(); + return !BC_STATUS_IS_ERROR(s) ? BC_STATUS_SUCCESS : s; +} Index: share/mk/src.opts.mk =================================================================== --- share/mk/src.opts.mk +++ share/mk/src.opts.mk @@ -206,6 +206,8 @@ LOADER_FORCE_LE \ LOADER_VERBOSE \ LOADER_VERIEXEC_PASS_MANIFEST \ + GBC \ + GH_BC \ NAND \ OFED_EXTRA \ OPENLDAP \ Index: usr.bin/Makefile =================================================================== --- usr.bin/Makefile +++ usr.bin/Makefile @@ -218,6 +218,14 @@ SUBDIR+= dtc .endif .endif +.if ${MK_GH_BC} == "yes" +SUBDIR+= gh-bc +.elif ${MK_GBC} == "yes" +SUBDIR+= gbc +.else +SUBDIR.${MK_OPENSSL}+= bc +SUBDIR.${MK_OPENSSL}+= dc +.endif SUBDIR.${MK_HESIOD}+= hesinfo SUBDIR.${MK_ICONV}+= iconv SUBDIR.${MK_ICONV}+= mkcsmapper @@ -241,9 +249,7 @@ SUBDIR.${MK_NIS}+= ypmatch SUBDIR.${MK_NIS}+= ypwhich SUBDIR.${MK_OPENSSH}+= ssh-copy-id -SUBDIR.${MK_OPENSSL}+= bc SUBDIR.${MK_OPENSSL}+= chkey -SUBDIR.${MK_OPENSSL}+= dc SUBDIR.${MK_OPENSSL}+= newkey SUBDIR.${MK_QUOTAS}+= quota SUBDIR.${MK_SENDMAIL}+= vacation Index: usr.bin/gh-bc/Makefile =================================================================== --- usr.bin/gh-bc/Makefile +++ usr.bin/gh-bc/Makefile @@ -0,0 +1,82 @@ +## $FreeBSD$ + +.include + +PROG= gh-bc +PROGNAME= bc + +BCDIR= ${SRCTOP}/contrib/${PROGNAME} +BCVERSION!= sed -n -e '/.*VERSION *= *[0-9]/s/.*VERSION *= *//p' ${BCDIR}/Makefile.in + +SRCS= args.c data.c lang.c lex.c main.c num.c parse.c program.c read.c vector.c vm.c +SRCS+= bc/bc.c bc/lex.c bc/parse.c dc/dc.c dc/lex.c dc/parse.c history/history.c +SRCS+= bc_help.c dc_help.c lib.c lib2.c +MAN= bc.1 dc.1 + +LINKS= ${BINDIR}/bc ${BINDIR}/dc + +.PATH: ${BCDIR}/src ${BCDIR}/gen ${BCDIR}/manuals ${.OBJDIR} + +CATALOGS= en_US.UTF-8 +CATALOGS+= de_DE.UTF-8 de_DE.ISO8859-1 +CATALOGS+= fr_FR.UTF-8 fr_FR.ISO8859-1 + +NLSNAME= bc +NLSSRCDIR= ${BCDIR}/locales + +CFLAGS+= -DMAINEXEC=${PROGNAME} +CFLAGS+= -DNLSPATH=/usr/share/nls/%L/%N.cat +CFLAGS+= -DBC_ENABLED +CFLAGS+= -DBC_ENABLE_EXTRA_MATH +CFLAGS+= -DBC_ENABLE_HISTORY +CFLAGS+= -DBC_ENABLE_SIGNALS +CFLAGS+= -DBC_NUM_KARATSUBA_LEN=64 +CFLAGS+= -DDC_ENABLED +CFLAGS+= -DNDEBUG +CFLAGS+= -DVERSION=${BCVERSION} +CFLAGS+= -I${BCDIR}/include + +.if ${MK_NLS_CATALOGS} == "no" +CFLAGS+= -DBC_ENABLE_NLS=0 +.else +CFLAGS+= -DBC_ENABLE_NLS=1 + +.if ${CC} == gcc +CFLAGS+= -std=c99 +MK_WARNS= no +.else +CFLAGS+= -flto +.endif + +.for catalog in ${CATALOGS} +NLS+= ${catalog:C/.*://} +NLSSRCFILES_${catalog:C/.*://}= ${catalog:C/.*://}.msg +.endfor + +NLSLINKS_en_US.UTF-8+= en_AU.UTF-8 en_CA.UTF-8 en_GB.UTF-8 en_IE.UTF-8 en_NZ.UTF-8 C +NLSLINKS_en_US.UTF-8+= en_AU.US-ASCII en_CA.US-ASCII en_GB.US-ASCII en_NZ.US-ASCII +NLSLINKS_en_US.UTF-8+= en_AU.ISO8859-1 en_CA.ISO8859-1 en_GB.ISO8859-1 en_NZ.ISO8859-1 en_US.ISO8859-1 +NLSLINKS_en_US.UTF-8+= en_AU.ISO8859-15 en_CA.ISO8859-15 en_GB.ISO8859-15 en_US.ISO8859-15 + +NLSLINKS_de_DE.UTF-8+= de_AT.UTF-8 de_CH.UTF-8 +NLSLINKS_de_DE.ISO8859-1+= de_AT.ISO8859-1 de_CH.ISO8859-1 +NLSLINKS_de_DE.ISO8859-1+= de_AT.ISO8859-15 de_CH.ISO8859-15 + +NLSLINKS_fr_FR.UTF-8+= fr_BE.UTF-8 fr_CA.UTF-8 fr_CH.UTF-8 +NLSLINKS_fr_FR.ISO8859-1+= fr_BE.ISO8859-1 fr_CA.ISO8859-1 fr_CH.ISO8859-1 +NLSLINKS_fr_FR.ISO8859-1+= fr_BE.ISO8859-15 fr_CA.ISO8859-15 fr_CH.ISO8859-15 +.endif + +lib.c: lib.bc + cd ${BCDIR} && sh gen/strgen.sh gen/lib.bc ${.OBJDIR}/lib.c bc_lib bc.h bc_lib_name 1 1 + +lib2.c: lib2.bc + cd ${BCDIR} && sh gen/strgen.sh gen/lib2.bc ${.OBJDIR}/lib2.c bc_lib2 bc.h bc_lib2_name 1 1 + +bc_help.c: bc_help.txt + cd ${BCDIR} && sh gen/strgen.sh gen/bc_help.txt ${.OBJDIR}/bc_help.c bc_help bc.h + +dc_help.c: dc_help.txt + cd ${BCDIR} && sh gen/strgen.sh gen/dc_help.txt ${.OBJDIR}/dc_help.c dc_help dc.h + +.include