130 {
"abs", 1,
cf_abs,
"x -- |x|"},
131 {
"neg", 1,
cf_neg,
"x -- (-x)"},
132 {
"+", 2,
cf_add,
"x y -- (x+y)"},
133 {
"add", 2,
cf_add,
"x y -- (x+y)"},
134 {
"-", 2,
cf_sub,
"x y -- (x-y)"},
135 {
"sub", 2,
cf_sub,
"x y -- (x-y)"},
136 {
"*", 2,
cf_mul,
"x y -- (x*y)"},
137 {
"mul", 2,
cf_mul,
"x y -- (x*y)"},
138 {
"/", 2,
cf_divmod,
"x y -- q r ; x = yq + r, 0 <= r < y"},
139 {
"//", 2,
cf_div,
"x y -- (x div y)"},
140 {
"div", 2,
cf_div,
"x y -- (x div y)"},
141 {
"%", 2,
cf_mod,
"x y -- (x mod y)"},
142 {
"mod", 2,
cf_mod,
"x y -- (x mod y)"},
143 {
"^", 2,
cf_expt,
"x y -- (x^y)"},
144 {
"expt", 2,
cf_expt,
"x y -- (x^y)"},
145 {
"^^", 3,
cf_exptmod,
"x y m -- (x^y mod m)"},
146 {
"emod", 3,
cf_exptmod,
"x y m -- (x^y mod m)"},
148 {
"inv", 2,
cf_invmod,
"x m -- (1/x mod m)"},
149 {
"gcd", 2,
cf_gcd,
"x y -- gcd(x, y)"},
150 {
"xgcd", 2,
cf_xgcd,
"x y -- g u v ; g = ux + vy"},
151 {
"sqrt", 1,
cf_sqrt,
"x -- floor(sqrt(x))"},
152 {
"root", 2,
cf_root,
"x y -- floor(x^{1/y}) ; y > 0"},
155 {
"<=", 2,
cf_cmple,
"x y -- (x<=y)"},
156 {
">=", 2,
cf_cmpge,
"x y -- (x>=y)"},
158 {
"<>", 2,
cf_cmpne,
"x y -- (x<>y)"},
159 {
"inc", 1,
cf_inc,
"x -- (x+1)"},
160 {
"dec", 1,
cf_dec,
"x -- (x-1)"},
162 {
"fact", 1,
cf_fact,
"x -- x!"},
164 {
".", 1,
cf_pprint,
"x -- ; print x in current output mode"},
165 {
";", 1,
cf_print,
"x -- x ; print x in current output mode"},
167 {
"cls", 0,
cf_clstk,
"... -- ; clear stack"},
169 {
"drop", 1,
cf_pop,
"x --"},
170 {
"dup", 1,
cf_dup,
"x -- x x"},
171 {
"copy", 2,
cf_copy,
"vn ... v1 v0 n -- vn ... v0 vn ... v0"},
172 {
"swap", 2,
cf_swap,
"x y -- y x"},
173 {
"rot", 3,
cf_rot,
"a b c -- b c a"},
174 {
"pick", 2,
cf_pick,
"... v2 v1 v0 n -- ... v2 v1 v0 vn"},
176 {
">>", 1,
cf_store,
"x -- ; save in named variable"},
177 {
"<<", 0,
cf_recall,
"-- x ; recall from named variable"},
178 {
"clm", 0,
cf_cmem,
"-- ; clear memory"},
179 {
"??", 0,
cf_pmem,
"-- ; print memory"},
181 {
"out", 1,
cf_setr,
"r -- ; set output radix to r"},
182 {
"bin", 0,
cf_setbin,
"-- ; set output format to binary"},
183 {
"help", 0,
cf_help,
"-- ; print help message"},
188 {NULL, 0,
cf_qrecall,
"-- x ; recall from named variable"},
191#define BUFFER_SIZE 16384
208int main(
int argc,
char *argv[]) {
221 while ((opt = getopt(argc, argv,
"ho:")) != EOF) {
226 "Usage: imcalc [-h] [-o <output>] input*\n\n"
228 " -h : display this help message.\n"
229 " -o <output> : send output to file.\n\n"
231 "If no input files are given, the standard input is read. The\n"
232 "special file name \"-\" is interpreted to mean the standard "
234 "Output goes to standard output unless \"-o\" is used.\n\n");
239 fprintf(stderr,
"Unable to open \"%s\" for writing: %s\n", optarg,
247 "Usage: imcalc [-h] [-o <output>] input*\n"
248 " [use \"imcalc -h\" to get help]\n\n");
261 for (ix = optind; ix < argc; ++ix) {
262 if (strcmp(argv[ix],
"-") == 0)
264 else if ((ifp = fopen(argv[optind],
"rt")) == NULL) {
265 fprintf(stderr,
"Unable to open \"%s\" for reading: %s\n", argv[optind],
288 while ((ch = fgetc(ifp)) != EOF && isspace(ch))
297 int next = fgetc(ifp);
298 if (next == EOF || !isdigit(next))
303 }
else if (isdigit(ch) || ch ==
'#')
309 while ((ch = fgetc(ifp)) != EOF) {
310 if ((
res ==
t_number && ispunct(ch) && ch !=
'-') ||
326 int radix = 10,
pos = 0;
330 assert(buf != NULL && out != NULL);
332 if (buf[
pos] ==
'#') {
374 char *buf =
op->ibuf;
377 for (ix = 0;
g_ops[ix].
name != NULL; ++ix) {
378 if (strcasecmp(buf,
g_ops[ix].
name) == 0)
return ix;
382 for (jx = 0; (
mp_size)jx < op->mused; ++jx) {
383 if (strcmp(buf,
op->names[jx]) == 0)
return ix;
393 unsigned char *buf = malloc(len);
398 for (ix = 0; ix < len - 1; ++ix) {
408 char *buf = malloc(len);
433 fprintf(stderr,
"error: invalid number syntax: %s\n", op_state->
ibuf);
439 fprintf(stderr,
"error: command not understood: %s\n",
442 fprintf(stderr,
"error: not enough arguments (have %d, want %d)\n",
444 }
else if ((
res = (
g_ops[cpos].handler)(op_state)) !=
MP_OK) {
446 fprintf(stderr,
"error: incorrect input format\n");
453 fprintf(stderr,
"error: invalid input token: %s\n", op_state->
ibuf);
466 assert(sp != NULL && n_elts > 0);
468 if ((sp->
elts = malloc(n_elts *
sizeof(*(sp->
elts)))) == NULL)
470 if ((sp->
mem = malloc(n_elts *
sizeof(*(sp->
mem)))) == NULL) {
474 if ((sp->
names = malloc(n_elts *
sizeof(*(sp->
names)))) == NULL) {
486 for (ix = 0; (
mp_size)ix < n_elts; ++ix) {
489 sp->
names[ix] = NULL;
504 if (sp->
elts != NULL) {
507 for (ix = 0; (
mp_size)ix < sp->used; ++ix) {
517 if (sp->
mem != NULL) {
520 for (ix = 0; (
mp_size)ix < sp->mused; ++ix) {
524 sp->
names[ix] = NULL;
535 if (sp->
ibuf != NULL) {
539 if (sp->
ifp != NULL) {
550 for (ix = 0; (
mp_size)ix < sp->used; ++ix) {
564 if ((tmp = malloc(nsize *
sizeof(*(sp->
elts)))) == NULL)
return MP_MEMORY;
566 for (ix = 0; (
mp_size)ix < sp->used; ++ix) {
567 tmp[ix] = sp->
elts[ix];
594 for (ix = 0; (
mp_size)ix < sp->mused; ++ix) {
595 if (strcmp(
name, sp->
names[ix]) == 0)
break;
611 if ((tz = malloc(nsize *
sizeof(*(sp->
mem)))) == NULL)
return MP_MEMORY;
612 if ((tc = malloc(nsize *
sizeof(*(sp->
names)))) == NULL) {
617 for (jx = 0; (
mp_size)jx < sp->mused; ++jx) {
618 tz[jx] = sp->
mem[jx];
619 tc[jx] = sp->
names[jx];
631 sp->
names[ix] = malloc(1 + strlen(
name));
642 for (ix = 0; (
mp_size)ix < sp->mused; ++ix) {
654 for (ix = 0; (
mp_size)ix < sp->mused; ++ix) {
929 for (ix = 0; (
mp_size)ix < sp->used; ++ix) {
974 for (ix = 0; ix < ncopy; ++ix) {
1041 int ix, maxlen = 10;
1043 for (ix = 0;
g_ops[ix].
name != NULL; ++ix) {
1046 if (len > maxlen) maxlen = len;
1049 fprintf(stderr,
"Operators understood:\n");
1050 for (ix = 0;
g_ops[ix].
name != NULL; ++ix) {
1054 while (len++ <= maxlen) fputc(
' ', stderr);
1056 fprintf(stderr,
"%s\n",
g_ops[ix].descript);
1058 fputc(
'\n', stderr);
1092 int ix, max_len = 0;
1094 if (sp->
mused == 0) {
1099 for (ix = 0; (
mp_size)ix < sp->mused; ++ix) {
1100 int ln = strlen(sp->
names[ix]);
1102 if (ln > max_len) max_len = ln;
1107 for (ix = 0; (
mp_size)ix < sp->mused; ++ix) {
1108 int ln = strlen(sp->
names[ix]);
__isl_export __isl_give ISL_HMAP __isl_take ISL_KEY __isl_take ISL_VAL * val
static mp_result mp_int_sqrt(mp_int a, mp_int c)
Sets c to the greatest integer not less than the square root of a.
static mp_result cf_cmpeq(cstate_t *sp)
static mp_result stack_pop(cstate_t *sp)
static mp_result cf_print(cstate_t *sp)
static mp_result cf_square(cstate_t *sp)
static mp_result cf_gcd(cstate_t *sp)
static mp_result cf_abs(cstate_t *sp)
static mp_result cf_root(cstate_t *sp)
static int find_command(cstate_t *ops)
static mp_result cf_setbin(cstate_t *sp)
static mp_result cf_exptmod(cstate_t *sp)
static FILE * g_output_file
static mp_result cf_cmpgt(cstate_t *sp)
static mp_result read_number(char *buf, mp_int *out)
static mp_result cf_expt(cstate_t *sp)
static mp_result cf_cmpne(cstate_t *sp)
static mp_result cf_store(cstate_t *sp)
static mp_result cf_dec(cstate_t *sp)
static mp_result cf_help(cstate_t *sp)
static mp_result cf_div(cstate_t *sp)
static mp_result cf_add(cstate_t *sp)
static mp_result cf_clstk(cstate_t *sp)
static mp_result cf_cmpge(cstate_t *sp)
static mp_result mem_recall(cstate_t *sp, const char *name, mp_int value)
static mp_result cf_pop(cstate_t *sp)
static void stack_flush(cstate_t *sp)
static mp_result cf_sub(cstate_t *sp)
static mp_result cf_recall(cstate_t *sp)
static mp_result cf_invmod(cstate_t *sp)
static mp_result cf_pmem(cstate_t *sp)
static mp_result cf_pick(cstate_t *sp)
static token_t next_token(FILE *ifp, char *buf, int size)
static mp_result state_init(cstate_t *sp, mp_size n_elts)
static mp_result cf_cmplt(cstate_t *sp)
static mp_result cf_pprint(cstate_t *sp)
static mp_result run_file(FILE *ifp, cstate_t *op_state)
static mp_result cf_setr(cstate_t *sp)
static void state_clear(cstate_t *sp)
static mp_result cf_mul(cstate_t *sp)
static mp_result cf_swap(cstate_t *sp)
static mp_result cf_xgcd(cstate_t *sp)
static mp_result mem_insert(cstate_t *sp, const char *name, mp_int value)
static mp_result cf_cmem(cstate_t *sp)
static mp_result cf_qrecall(cstate_t *sp)
static mp_result cf_copy(cstate_t *sp)
mp_result(* op_func)(cstate_t *)
static mp_result cf_neg(cstate_t *sp)
static mp_result cf_mod(cstate_t *sp)
static mp_result cf_pstack(cstate_t *sp)
static mp_result cf_divmod(cstate_t *sp)
static mp_result stack_push(cstate_t *sp, mp_int elt)
static mp_result cf_inc(cstate_t *sp)
static mp_result cf_cmple(cstate_t *sp)
static mp_result cf_sqrt(cstate_t *sp)
static void print_value(mp_int v)
static mp_result cf_fact(cstate_t *sp)
static mp_result MP_INPUT
static mp_result cf_dup(cstate_t *sp)
static mp_result cf_rot(cstate_t *sp)
static mp_result mem_clear(cstate_t *sp)
static int g_output_radix
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
__isl_give isl_val *(* op)(__isl_take isl_val *v)
#define mp_int_compare_zero
#define mp_int_string_len
#define mp_int_read_string
#define mp_int_init_value
#define mp_int_compare_value
#define mp_int_binary_len