Polly 22.0.0git
imcalc.c File Reference
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <getopt.h>
#include <unistd.h>
#include "imath.h"
#include "imrat.h"
#include "iprime.h"

Go to the source code of this file.

Classes

struct  cstate_t
struct  calcop_t

Macros

#define BUFFER_SIZE   16384 /* max. length of input values, in chars */

Typedefs

typedef mp_result(* op_func) (cstate_t *)

Enumerations

enum  token_t { t_eof , t_symbol , t_number , t_error }

Functions

static mp_result state_init (cstate_t *sp, mp_size n_elts)
static void state_clear (cstate_t *sp)
static void stack_flush (cstate_t *sp)
static mp_result stack_push (cstate_t *sp, mp_int elt)
static mp_result stack_pop (cstate_t *sp)
static mp_result mem_insert (cstate_t *sp, const char *name, mp_int value)
static mp_result mem_recall (cstate_t *sp, const char *name, mp_int value)
static mp_result mem_clear (cstate_t *sp)
static mp_result cf_abs (cstate_t *sp)
static mp_result cf_neg (cstate_t *sp)
static mp_result cf_add (cstate_t *sp)
static mp_result cf_sub (cstate_t *sp)
static mp_result cf_mul (cstate_t *sp)
static mp_result cf_divmod (cstate_t *sp)
static mp_result cf_div (cstate_t *sp)
static mp_result cf_mod (cstate_t *sp)
static mp_result cf_expt (cstate_t *sp)
static mp_result cf_exptmod (cstate_t *sp)
static mp_result cf_square (cstate_t *sp)
static mp_result cf_invmod (cstate_t *sp)
static mp_result cf_gcd (cstate_t *sp)
static mp_result cf_xgcd (cstate_t *sp)
static mp_result cf_sqrt (cstate_t *sp)
static mp_result cf_root (cstate_t *sp)
static mp_result cf_cmplt (cstate_t *sp)
static mp_result cf_cmpgt (cstate_t *sp)
static mp_result cf_cmple (cstate_t *sp)
static mp_result cf_cmpge (cstate_t *sp)
static mp_result cf_cmpeq (cstate_t *sp)
static mp_result cf_cmpne (cstate_t *sp)
static mp_result cf_inc (cstate_t *sp)
static mp_result cf_dec (cstate_t *sp)
static mp_result cf_fact (cstate_t *sp)
static mp_result cf_pprint (cstate_t *sp)
static mp_result cf_print (cstate_t *sp)
static mp_result cf_pstack (cstate_t *sp)
static mp_result cf_clstk (cstate_t *sp)
static mp_result cf_pop (cstate_t *sp)
static mp_result cf_dup (cstate_t *sp)
static mp_result cf_copy (cstate_t *sp)
static mp_result cf_swap (cstate_t *sp)
static mp_result cf_rot (cstate_t *sp)
static mp_result cf_pick (cstate_t *sp)
static mp_result cf_setr (cstate_t *sp)
static mp_result cf_setbin (cstate_t *sp)
static mp_result cf_help (cstate_t *sp)
static mp_result cf_store (cstate_t *sp)
static mp_result cf_recall (cstate_t *sp)
static mp_result cf_cmem (cstate_t *sp)
static mp_result cf_pmem (cstate_t *sp)
static mp_result cf_qrecall (cstate_t *sp)
static token_t next_token (FILE *ifp, char *buf, int size)
static mp_result read_number (char *buf, mp_int *out)
static int find_command (cstate_t *ops)
static void print_value (mp_int v)
static mp_result run_file (FILE *ifp, cstate_t *op_state)
int main (int argc, char *argv[])

Variables

static calcop_t g_ops []
static mp_result MP_INPUT
static int g_output_radix = 10
static FILE * g_output_file = NULL

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   16384 /* max. length of input values, in chars */

Definition at line 191 of file imcalc.c.

Referenced by state_init().

Typedef Documentation

◆ op_func

typedef mp_result(* op_func) (cstate_t *)

Definition at line 76 of file imcalc.c.

Enumeration Type Documentation

◆ token_t

enum token_t
Enumerator
t_eof 
t_symbol 
t_number 
t_error 

Definition at line 194 of file imcalc.c.

Function Documentation

◆ cf_abs()

mp_result cf_abs ( cstate_t * sp)
static

Definition at line 663 of file imcalc.c.

References a(), cstate_t::elts, mp_int_abs, and cstate_t::used.

◆ cf_add()

mp_result cf_add ( cstate_t * sp)
static

Definition at line 675 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_add, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_clstk()

mp_result cf_clstk ( cstate_t * sp)
static

Definition at line 938 of file imcalc.c.

References MP_OK, and stack_flush().

◆ cf_cmem()

mp_result cf_cmem ( cstate_t * sp)
static

Definition at line 1089 of file imcalc.c.

References mem_clear().

◆ cf_cmpeq()

mp_result cf_cmpeq ( cstate_t * sp)
static

Definition at line 859 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_compare, mp_int_set_value, res, stack_pop(), and cstate_t::used.

◆ cf_cmpge()

mp_result cf_cmpge ( cstate_t * sp)
static

Definition at line 849 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_compare, mp_int_set_value, res, stack_pop(), and cstate_t::used.

◆ cf_cmpgt()

mp_result cf_cmpgt ( cstate_t * sp)
static

Definition at line 829 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_compare, mp_int_set_value, res, stack_pop(), and cstate_t::used.

◆ cf_cmple()

mp_result cf_cmple ( cstate_t * sp)
static

Definition at line 839 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_compare, mp_int_set_value, res, stack_pop(), and cstate_t::used.

◆ cf_cmplt()

mp_result cf_cmplt ( cstate_t * sp)
static

Definition at line 819 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_compare, mp_int_set_value, res, stack_pop(), and cstate_t::used.

◆ cf_cmpne()

mp_result cf_cmpne ( cstate_t * sp)
static

Definition at line 869 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_compare, mp_int_set_value, res, stack_pop(), and cstate_t::used.

◆ cf_copy()

◆ cf_dec()

mp_result cf_dec ( cstate_t * sp)
static

Definition at line 885 of file imcalc.c.

References a(), cstate_t::elts, mp_int_sub_value, and cstate_t::used.

◆ cf_div()

mp_result cf_div ( cstate_t * sp)
static

Definition at line 712 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_div, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_divmod()

mp_result cf_divmod ( cstate_t * sp)
static

Definition at line 705 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_div, and cstate_t::used.

◆ cf_dup()

mp_result cf_dup ( cstate_t * sp)
static

◆ cf_expt()

mp_result cf_expt ( cstate_t * sp)
static

Definition at line 732 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_expt, mp_int_to_int, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_exptmod()

mp_result cf_exptmod ( cstate_t * sp)
static

Definition at line 744 of file imcalc.c.

References a(), b(), cstate_t::elts, m, mp_int_exptmod, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_fact()

◆ cf_gcd()

mp_result cf_gcd ( cstate_t * sp)
static

Definition at line 774 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_gcd, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_help()

mp_result cf_help ( cstate_t * sp)
static

Definition at line 1040 of file imcalc.c.

References g_ops, MP_OK, and name.

◆ cf_inc()

mp_result cf_inc ( cstate_t * sp)
static

Definition at line 879 of file imcalc.c.

References a(), cstate_t::elts, mp_int_add_value, and cstate_t::used.

◆ cf_invmod()

mp_result cf_invmod ( cstate_t * sp)
static

Definition at line 764 of file imcalc.c.

References a(), cstate_t::elts, m, mp_int_invmod, res, stack_pop(), and cstate_t::used.

◆ cf_mod()

mp_result cf_mod ( cstate_t * sp)
static

Definition at line 722 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_mod, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_mul()

mp_result cf_mul ( cstate_t * sp)
static

Definition at line 695 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_mul, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_neg()

mp_result cf_neg ( cstate_t * sp)
static

Definition at line 669 of file imcalc.c.

References a(), cstate_t::elts, mp_int_neg, and cstate_t::used.

◆ cf_pick()

mp_result cf_pick ( cstate_t * sp)
static

Definition at line 1009 of file imcalc.c.

References cstate_t::elts, mp_int_copy, mp_int_to_int, MP_OK, MP_RANGE, n, pos(), res, and cstate_t::used.

◆ cf_pmem()

mp_result cf_pmem ( cstate_t * sp)
static

Definition at line 1091 of file imcalc.c.

References g_output_file, cstate_t::mem, MP_OK, cstate_t::mused, cstate_t::names, and print_value().

◆ cf_pop()

mp_result cf_pop ( cstate_t * sp)
static

Definition at line 944 of file imcalc.c.

References stack_pop().

◆ cf_pprint()

mp_result cf_pprint ( cstate_t * sp)
static

Definition at line 912 of file imcalc.c.

References cstate_t::elts, MP_OK, print_value(), stack_pop(), and cstate_t::used.

◆ cf_print()

mp_result cf_print ( cstate_t * sp)
static

Definition at line 918 of file imcalc.c.

References cstate_t::elts, MP_OK, print_value(), and cstate_t::used.

◆ cf_pstack()

mp_result cf_pstack ( cstate_t * sp)
static

Definition at line 923 of file imcalc.c.

References cstate_t::elts, g_output_file, MP_OK, print_value(), and cstate_t::used.

◆ cf_qrecall()

mp_result cf_qrecall ( cstate_t * sp)
static

Definition at line 1120 of file imcalc.c.

References cstate_t::ibuf, mem_recall(), mp_int_alloc, mp_int_free, MP_MEMORY, MP_OK, res, stack_push(), and val.

◆ cf_recall()

◆ cf_root()

mp_result cf_root ( cstate_t * sp)
static

Definition at line 807 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_root, mp_int_to_int, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_rot()

mp_result cf_rot ( cstate_t * sp)
static

Definition at line 999 of file imcalc.c.

References cstate_t::elts, MP_OK, t, and cstate_t::used.

◆ cf_setbin()

mp_result cf_setbin ( cstate_t * sp)
static

Definition at line 1035 of file imcalc.c.

References g_output_radix, and MP_OK.

◆ cf_setr()

mp_result cf_setr ( cstate_t * sp)
static

◆ cf_sqrt()

mp_result cf_sqrt ( cstate_t * sp)
static

Definition at line 801 of file imcalc.c.

References a(), cstate_t::elts, mp_int_sqrt(), and cstate_t::used.

◆ cf_square()

mp_result cf_square ( cstate_t * sp)
static

Definition at line 758 of file imcalc.c.

References a(), cstate_t::elts, mp_int_sqr, and cstate_t::used.

◆ cf_store()

◆ cf_sub()

mp_result cf_sub ( cstate_t * sp)
static

Definition at line 685 of file imcalc.c.

References a(), b(), cstate_t::elts, mp_int_sub, MP_OK, res, stack_pop(), and cstate_t::used.

◆ cf_swap()

mp_result cf_swap ( cstate_t * sp)
static

Definition at line 990 of file imcalc.c.

References cstate_t::elts, MP_OK, t, and cstate_t::used.

◆ cf_xgcd()

mp_result cf_xgcd ( cstate_t * sp)
static

◆ find_command()

int find_command ( cstate_t * ops)
static

Definition at line 372 of file imcalc.c.

References g_ops, name, and op.

Referenced by run_file().

◆ main()

int main ( int argc,
char * argv[] )

◆ mem_clear()

mp_result mem_clear ( cstate_t * sp)
static

Definition at line 651 of file imcalc.c.

References cstate_t::mem, mp_int_free, MP_OK, cstate_t::mused, and cstate_t::names.

Referenced by cf_cmem().

◆ mem_insert()

mp_result mem_insert ( cstate_t * sp,
const char * name,
mp_int value )
static

◆ mem_recall()

mp_result mem_recall ( cstate_t * sp,
const char * name,
mp_int value )
static

Definition at line 639 of file imcalc.c.

References cstate_t::mem, mp_int_copy, MP_UNDEF, cstate_t::mused, name, and cstate_t::names.

Referenced by cf_qrecall(), and cf_recall().

◆ next_token()

token_t next_token ( FILE * ifp,
char * buf,
int size )
static

Definition at line 282 of file imcalc.c.

References assert, pos(), res, size, t_eof, t_error, t_number, and t_symbol.

Referenced by cf_recall(), cf_store(), and run_file().

◆ print_value()

void print_value ( mp_int v)
static

◆ read_number()

mp_result read_number ( char * buf,
mp_int * out )
static

Definition at line 325 of file imcalc.c.

References assert, MP_BADARG, mp_int_alloc, mp_int_free, mp_int_read_string, MP_MEMORY, MP_OK, pos(), and res.

Referenced by run_file().

◆ run_file()

mp_result run_file ( FILE * ifp,
cstate_t * op_state )
static

◆ stack_flush()

void stack_flush ( cstate_t * sp)
static

Definition at line 545 of file imcalc.c.

References assert, cstate_t::elts, mp_int_clear, and cstate_t::used.

Referenced by cf_clstk().

◆ stack_pop()

◆ stack_push()

mp_result stack_push ( cstate_t * sp,
mp_int elt )
static

Definition at line 558 of file imcalc.c.

References cstate_t::alloc, cstate_t::elts, MP_MEMORY, MP_OK, and cstate_t::used.

Referenced by cf_copy(), cf_dup(), cf_qrecall(), cf_recall(), cf_xgcd(), and run_file().

◆ state_clear()

◆ state_init()

Variable Documentation

◆ g_ops

calcop_t g_ops[]
static

Definition at line 129 of file imcalc.c.

Referenced by cf_help(), find_command(), and run_file().

◆ g_output_file

FILE* g_output_file = NULL
static

Definition at line 206 of file imcalc.c.

Referenced by cf_pmem(), cf_pstack(), main(), and print_value().

◆ g_output_radix

int g_output_radix = 10
static

Definition at line 205 of file imcalc.c.

Referenced by cf_setbin(), cf_setr(), and print_value().

◆ MP_INPUT

mp_result MP_INPUT
static

Definition at line 203 of file imcalc.c.

Referenced by cf_recall(), cf_store(), main(), and run_file().