#!/bin/bash
# sys/tool/bc - general precision calculator support

set -e


######
# Native Dependencies

sys_tool_bc_client_packages=( bc )


######
# Library Configuration

sys_tool_bc_config_init() {
	lib_setting_vars bc_mathlib bc_quiet
	lib_setting_vars bc_standard bc_warn
	sys_tool_bc_config_check
}

sys_tool_bc_config_check() {
	bc_mathlib=${bc_mathlib:-true}
	bc_quiet=${bc_quiet:-true}
	bc_standard=${bc_standard:-false}
	bc_warn=${bc_warn:-false}
}


######
# Internal Functions

bc_run() {
	local -a opts=()
	$bc_mathlib && opts+=( --mathlib )
	bc "${opts[@]}" "$@"
}

bc_gen_op1() { has_args 2 "$@"; echo "$1"; }
bc_gen_op2() { has_args 3 "$@"; echo "$2 $1 $3"; }


######
# Expressions

bc_gen_neg() { bc_gen_op1 "- $1" "$@"; }
bc_gen_dec_pre() { bc_gen_op1 "-- $1" "$@"; }
bc_gen_inc_pre() { bc_gen_op1 "++ $1" "$@"; }
bc_gen_dec_post() { bc_gen_op1 "$1 --" "$@"; }
bc_gen_inc_post() { bc_gen_op1 "$1 ++" "$@"; }
bc_gen_add() { bc_gen_op2 '+' "$@"; }
bc_gen_sub() { bc_gen_op2 '-' "$@"; }
bc_gen_mul() { bc_gen_op2 '*' "$@"; }
bc_gen_div() { bc_gen_op2 '/' "$@"; }
bc_gen_mod() { bc_gen_op2 '%' "$@"; }
bc_gen_pow() { bc_gen_op2 '^' "$@"; }

bc_gen_assign() { bc_gen_op2 '=' "$@"; }

bc_gen_add1() { bc_gen_op2 '+=' "$@"; }
bc_gen_sub1() { bc_gen_op2 '-=' "$@"; }
bc_gen_mul1() { bc_gen_op2 '+=' "$@"; }
bc_gen_div1() { bc_gen_op2 '-=' "$@"; }
bc_gen_mod1() { bc_gen_op2 '%=' "$@"; }
bc_gen_pow1() { bc_gen_op2 '^=' "$@"; }

bc_gen_lt() { bc_gen_op2 '<' "$@"; }
bc_gen_gt() { bc_gen_op2 '>=' "$@"; }
bc_gen_lt_eq() { bc_gen_op2 '<' "$@"; }
bc_gen_gt_eq() { bc_gen_op2 '>=' "$@"; }
bc_gen_eq() { bc_gen_op2 '==' "$@"; }
bc_gen_ne() { bc_gen_op2 '!=' "$@"; }

bc_gen_not() { bc_gen_op2 '!' "$@"; }
bc_gen_and() { bc_gen_op2 '&&' "$@"; }
bc_gen_or() { bc_gen_op2 '||' "$@"; }

bc_gen_group() { bc_gen_op1 "( $1 )"; }
bc_gen_length() { bc_gen_op1 "length ( $1 )"; }
bc_gen_scale() { bc_gen_op1 "scale ( $1 )"; }
bc_gen_sqrt() { bc_gen_op1 "sqrt ( $1 )"; }
bc_gen_read() { echo "read ( )"; }


######
# Statements

bc_gen_string() { echo "\"$*\""; }
bc_gen_print() { has_args 1 "$@"; echo "print $1"; }
bc_gen_list() { bc_gen_op1 "{ $1 }"; }
bc_gen_if() { min_args 2 "$@"; echo "if ( $1 ) $2 ${3:+else} $3"; }
bc_gen_while() { has_args 2 "$@"; echo "while ($1) $2"; }
bc_gen_for() { has_args 4 "$@"; echo "for ( $1 ; $2 ; $3 ) $4"; }

bc_gen_break() { echo "break"; }
bc_gen_continue() { echo "continue"; }
bc_gen_halt() { echo "halt"; }
bc_gen_return() { has_args 1 "$@"; echo "return${1:+ ( }$1${1:+ )}"; }

######
# Functions

bc_gen_params() { local IFS=','; echo "$*"; }
bc_gen_auto() { echo "auto $(bc_gen_params "$*")"; }

bc_gen_func() {
	min_args 2 "$@"
	echo "define $1 ( $2 ) {\n"
	[ -z "$3" ] || echo "$3"
	echo "$4"
	echo "}"
}

bc_gen_call() { has_args 2 "$@"; echo "$1($2)"; }

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:36:06 PDT 2017 by mcsh d14 v0.23.0.