// test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
// test: echo -e '\n\n\n' | calc.exe -n | grep -qv 'error'
// test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
-// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 =
+// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c error | xargs test 4 =
// test: echo -e '1\nsto (2)\n3\nsto (5, 7)\nsto(9)\ndisp' | calc.exe | grep -q '0 1 0 0 7 0 0 0 7 0'
// test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
// test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1'
// test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 =
// test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6\nquit' | calc.exe -v 3 | grep -q '=> 1'
// test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 =
-// test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c invalid | xargs test 2 =
-// test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050'
+// test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c error | xargs test 2 =
+// test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 4950'
// test: echo -e 'while\nwhile (inc (1) < 3,\nwhile (inc (1) < 100, sto (2, rcl (1) + rcl (2))' | calc.exe | grep -c error | xargs test 3 =
// test: echo -e 'while (0, 1)\nquit' | calc.exe -v 3 | grep -q While
// test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}\nquit' | calc.exe -v 3 | grep -q 'Code'
// test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1'
// test: echo -e '\t\t' | calc.exe | grep -q 'print'
// test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2
-// test: echo -e 'mem (3)\nsto (4, pi)' | calc.exe | grep -q "invalid index"
+// test: echo -e 'mem (3)\nsto (4, pi)' | calc.exe | grep -q error
// test: echo -e 'sto (2, 3)\nmem (2)\ndisp' | calc.exe | grep -q 'storage: 0 3$'
// test: echo -e 'disp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
// test: echo -e 'sto (3, 10)\ndisp' | calc.exe | grep -q "storage: 0 0 10 0 0 0 0 0 0 0"
// test: echo -e 'min\nmean\nmed\nmax\nprod\nsum\nvar\nord' | calc.exe -n | grep -c error | xargs test 8 =
// test: echo -e 'prog (1, cos(pi * arg (1))) / 4' | calc.exe | grep -c error | xargs test 1 =
// Gauss sequence
-// test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) < 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})};' | calc.exe | grep -q '=> 5050'
+// test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) <= 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})};' | calc.exe | grep -q '=> 5050'
// Fibonacci sequence
-// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})};' | calc.exe | grep -q '=> 144'
+// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) <= 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})};' | calc.exe | grep -q '=> 144'
// Gold number
// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 15 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)) / rcl (1))})};' | calc.exe | grep -q '=> 1.61803'
/* clean old program */
if ((programs + n)->storage) {
- free ((programs + n)->storage);
+ free_tab ((programs + n)->storage);
}
if ((programs + n)->stack) {
free_tab ((programs + n)->stack);
(programs + n)->id = id;
(programs + n)->answer = 0;
(programs + n)->storage = NULL;
- (programs + n)->storage_size = 0;
(programs + n)->stack = NULL;
(programs + n)->root = dupelement (root);
}
tmp.argument = argument;
tmp.argument_size = argument_size;
tmp.storage = storage;
- tmp.storage_size = storage_size;
tmp.stack = stack;
/* change context */
answer = 0;
storage = (programs + n)->storage;
- storage_size = (programs + n)->storage_size;
argument = NULL;
argument_size = 0;
stack = (programs + n)->stack;
delelement (elements);
(programs + n)->answer = answer;
(programs + n)->storage = storage;
- (programs + n)->storage_size = storage_size;
if (argument) {
free (argument);
}
/* restore context */
answer = tmp.answer;
storage = tmp.storage;
- storage_size = tmp.storage_size;
argument = tmp.argument;
argument_size = tmp.argument_size;
stack = tmp.stack;
/* clean program */
if ((programs + n)->storage) {
- free ((programs + n)->storage);
+ free_tab ((programs + n)->storage);
}
if ((programs + n)->stack) {
free_tab ((programs + n)->stack);
typedef struct _workspace_t {
int id;
double answer;
- double *storage;
- int storage_size;
+ tab_t *storage;
double *argument;
int argument_size;
element_t *root;
#include <malloc.h>
+#include <math.h>
#include <stdio.h>
#include "alloc.h"
#include "debug.h"
#include "format.h"
+#include "tabular.h"
#include "storage.h"
/* global variables */
#define DEFAULT_STORAGE_SIZE 10
-double *storage = NULL;
-
-int storage_size = -1;
+tab_t *storage = NULL;
/* storage functions */
void memory (int nb)
{
- int i, l;
- double *tmp = NULL;
- if (nb != storage_size) {
- l = (nb < storage_size) ? nb : storage_size;
- tmp = (double *) callocordie (nb, sizeof (double));
- for (i = 0; i < l; i++) {
- tmp[i] = storage[i];
- }
- if (storage != NULL) {
- free (storage);
- }
- storage = tmp;
- storage_size = nb;
+ if (nb != size_tab (storage)) {
+ storage = resize_tab (storage, nb);
}
}
-double store (int index, double value)
+double store (int id, double value)
{
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- if ((index > 0) && (index <= storage_size)) {
- storage[index - 1] = value;
- } else {
- VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
- }
- return value;
+ return set_tab (storage, id, value);
}
-double recall (int index)
+double recall (int id)
{
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- if ((index > 0) && (index <= storage_size)) {
- return storage[index - 1];
- } else {
- VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
- }
- return 0;
+ return get_tab (storage, id);
}
-double increase (int index)
+double increase (int id)
{
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- if ((index > 0) && (index <= storage_size)) {
- return storage[index - 1]++;
- } else {
- VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+ double val = get_tab (storage, id);
+ if (!isnan (val)) {
+ set_tab (storage, id, ++val);
}
- return 0;
+ return val;
}
-double decrease (int index)
+double decrease (int id)
{
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- if ((index > 0) && (index <= storage_size)) {
- return storage[index - 1]--;
- } else {
- VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+ double val = get_tab (storage, id);
+ if (!isnan (val)) {
+ set_tab (storage, id, --val);
}
- return 0;
+ return val;
}
void display (void)
{
- int i;
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
+ int i, n = size_tab (storage);
fprintf (stdout, "storage:");
- for (i = 0; i < storage_size; i++) {
+ for (i = 0; i < n; i++) {
fprintf (stdout, " ");
- fprintf (stdout, minform, storage[i]);
+ fprintf (stdout, minform, get_tab (storage, i + 1));
}
fprintf (stdout, "\n");
}
void clear ()
{
- int i;
- for (i = 0; i < storage_size; i++) {
- storage[i] = 0;
+ if (storage == NULL) {
+ memory (DEFAULT_STORAGE_SIZE);
+ }
+ int i, n = size_tab (storage);
+ for (i = 0; i < n; i++) {
+ set_tab (storage, i, 0);
}
}
#ifndef __STORAGE_H__
#define __STORAGE_H__
-/* global variables */
+#include "tabular.h"
-extern double *storage;
+/* global variables */
-extern int storage_size;
+extern tab_t *storage;
/* storage functions */
void memory (int nb);
-double store (int index, double value);
-double recall (int index);
-double increase (int index);
-double decrease (int index);
+double store (int id, double value);
+double recall (int id);
+double increase (int id);
+double decrease (int id);
void display (void);
void clear ();