aboutsummaryrefslogtreecommitdiff
path: root/math.c
diff options
context:
space:
mode:
Diffstat (limited to 'math.c')
-rw-r--r--math.c106
1 files changed, 47 insertions, 59 deletions
diff --git a/math.c b/math.c
index 1f6d093b9..75b4cdeb5 100644
--- a/math.c
+++ b/math.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1#include "internal.h" 2#include "internal.h"
2#include <stdio.h> 3#include <stdio.h>
3#include <stdlib.h> 4#include <stdlib.h>
@@ -8,126 +9,114 @@
8 9
9static const char math_usage[] = "math expression ..."; 10static const char math_usage[] = "math expression ...";
10 11
11static double stack[100]; 12static double stack[100];
12static unsigned int pointer; 13static unsigned int pointer;
13 14
14static void 15static void push(double a)
15push(double a)
16{ 16{
17 if ( pointer >= (sizeof(stack) / sizeof(*stack)) ) { 17 if (pointer >= (sizeof(stack) / sizeof(*stack))) {
18 fprintf(stderr, "math: stack overflow\n"); 18 fprintf(stderr, "math: stack overflow\n");
19 exit(-1); 19 exit(-1);
20 } 20 } else
21 else
22 stack[pointer++] = a; 21 stack[pointer++] = a;
23} 22}
24 23
25static double 24static double pop()
26pop()
27{ 25{
28 if ( pointer == 0 ) { 26 if (pointer == 0) {
29 fprintf(stderr, "math: stack underflow\n"); 27 fprintf(stderr, "math: stack underflow\n");
30 exit(-1); 28 exit(-1);
31 } 29 }
32 return stack[--pointer]; 30 return stack[--pointer];
33} 31}
34 32
35static void 33static void add()
36add()
37{ 34{
38 push(pop() + pop()); 35 push(pop() + pop());
39} 36}
40 37
41static void 38static void sub()
42sub()
43{ 39{
44 double subtrahend = pop(); 40 double subtrahend = pop();
45 41
46 push(pop() - subtrahend); 42 push(pop() - subtrahend);
47} 43}
48 44
49static void 45static void mul()
50mul()
51{ 46{
52 push(pop() * pop()); 47 push(pop() * pop());
53} 48}
54 49
55static void 50static void divide()
56divide()
57{ 51{
58 double divisor = pop(); 52 double divisor = pop();
53
59 push(pop() / divisor); 54 push(pop() / divisor);
60} 55}
61 56
62static void 57static void and()
63and()
64{ 58{
65 push((unsigned int)pop() & (unsigned int)pop()); 59 push((unsigned int) pop() & (unsigned int) pop());
66} 60}
67 61
68static void 62static void or()
69or()
70{ 63{
71 push((unsigned int)pop() | (unsigned int)pop()); 64 push((unsigned int) pop() | (unsigned int) pop());
72} 65}
73 66
74static void 67static void eor()
75eor()
76{ 68{
77 push((unsigned int)pop() ^ (unsigned int)pop()); 69 push((unsigned int) pop() ^ (unsigned int) pop());
78} 70}
79 71
80static void 72static void not()
81not()
82{ 73{
83 push(~(unsigned int)pop()); 74 push(~(unsigned int) pop());
84} 75}
85 76
86static void 77static void print()
87print()
88{ 78{
89 printf("%g\n", pop()); 79 printf("%g\n", pop());
90} 80}
91 81
92struct op { 82struct op {
93 const char * name; 83 const char *name;
94 void (*function)(); 84 void (*function) ();
95}; 85};
96 86
97static const struct op operators[] = { 87static const struct op operators[] = {
98 { "add", add }, 88 {"add", add},
99 { "and", and }, 89 {"and", and},
100 { "div", divide }, 90 {"div", divide},
101 { "eor", eor }, 91 {"eor", eor},
102 { "mul", mul }, 92 {"mul", mul},
103 { "not", not }, 93 {"not", not},
104 { "or", or }, 94 {"or", or},
105 { "sub", sub }, 95 {"sub", sub},
106 { 0, 0 } 96 {0, 0}
107}; 97};
108 98
109static void 99static void stack_machine(const char *argument)
110stack_machine(const char * argument)
111{ 100{
112 char * endPointer = 0; 101 char *endPointer = 0;
113 double d; 102 double d;
114 const struct op * o = operators; 103 const struct op *o = operators;
115 104
116 if ( argument == 0 ) { 105 if (argument == 0) {
117 print(); 106 print();
118 return; 107 return;
119 } 108 }
120 109
121 d = strtod(argument, &endPointer); 110 d = strtod(argument, &endPointer);
122 111
123 if ( endPointer != argument ) { 112 if (endPointer != argument) {
124 push(d); 113 push(d);
125 return; 114 return;
126 } 115 }
127 116
128 while ( o->name != 0 ) { 117 while (o->name != 0) {
129 if ( strcmp(o->name, argument) == 0 ) { 118 if (strcmp(o->name, argument) == 0) {
130 (*(o->function))(); 119 (*(o->function)) ();
131 return; 120 return;
132 } 121 }
133 o++; 122 o++;
@@ -136,10 +125,9 @@ stack_machine(const char * argument)
136 exit(-1); 125 exit(-1);
137} 126}
138 127
139int 128int math_main(int argc, char **argv)
140math_main(int argc, char * * argv)
141{ 129{
142 while ( argc >= 2 ) { 130 while (argc >= 2) {
143 stack_machine(argv[1]); 131 stack_machine(argv[1]);
144 argv++; 132 argv++;
145 argc--; 133 argc--;