blob: 37c7731d2ba2a9b0388135c19abc1c3312006ba0 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002#include "internal.h"
John Beppu5db60a72000-06-12 22:59:12 +00003#include <ctype.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00004#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <math.h>
8
9/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
10
John Beppu00216792000-06-21 19:06:16 +000011static const char dc_usage[] = "math expression ...\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000012#ifndef BB_FEATURE_TRIVIAL_HELP
13 "\nThis is a Tiny RPN calculator that understands the\n"
Erik Andersen5e1189e2000-04-15 16:34:54 +000014 "following operations: +, -, /, *, and, or, not, eor.\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000015 "i.e. 'math 2 2 add' -> 4, and 'math 8 8 \\* 2 2 + /' -> 16\n"
16#endif
17 ;
Eric Andersencc8ed391999-10-05 16:24:54 +000018
Erik Andersene49d5ec2000-02-08 19:58:47 +000019static double stack[100];
20static unsigned int pointer;
Eric Andersencc8ed391999-10-05 16:24:54 +000021
Erik Andersene49d5ec2000-02-08 19:58:47 +000022static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000023{
Erik Andersene49d5ec2000-02-08 19:58:47 +000024 if (pointer >= (sizeof(stack) / sizeof(*stack))) {
Eric Andersencc8ed391999-10-05 16:24:54 +000025 fprintf(stderr, "math: stack overflow\n");
26 exit(-1);
Erik Andersene49d5ec2000-02-08 19:58:47 +000027 } else
Eric Andersencc8ed391999-10-05 16:24:54 +000028 stack[pointer++] = a;
29}
30
Erik Andersene49d5ec2000-02-08 19:58:47 +000031static double pop()
Eric Andersencc8ed391999-10-05 16:24:54 +000032{
Erik Andersene49d5ec2000-02-08 19:58:47 +000033 if (pointer == 0) {
Eric Andersencc8ed391999-10-05 16:24:54 +000034 fprintf(stderr, "math: stack underflow\n");
35 exit(-1);
36 }
37 return stack[--pointer];
38}
39
Erik Andersene49d5ec2000-02-08 19:58:47 +000040static void add()
Eric Andersencc8ed391999-10-05 16:24:54 +000041{
42 push(pop() + pop());
43}
44
Erik Andersene49d5ec2000-02-08 19:58:47 +000045static void sub()
Eric Andersencc8ed391999-10-05 16:24:54 +000046{
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000048
49 push(pop() - subtrahend);
50}
51
Erik Andersene49d5ec2000-02-08 19:58:47 +000052static void mul()
Eric Andersencc8ed391999-10-05 16:24:54 +000053{
54 push(pop() * pop());
55}
56
Erik Andersene49d5ec2000-02-08 19:58:47 +000057static void divide()
Eric Andersencc8ed391999-10-05 16:24:54 +000058{
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 double divisor = pop();
60
Eric Andersencc8ed391999-10-05 16:24:54 +000061 push(pop() / divisor);
62}
63
Erik Andersene49d5ec2000-02-08 19:58:47 +000064static void and()
Eric Andersencc8ed391999-10-05 16:24:54 +000065{
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 push((unsigned int) pop() & (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000067}
68
Erik Andersene49d5ec2000-02-08 19:58:47 +000069static void or()
Eric Andersencc8ed391999-10-05 16:24:54 +000070{
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 push((unsigned int) pop() | (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000072}
73
Erik Andersene49d5ec2000-02-08 19:58:47 +000074static void eor()
Eric Andersencc8ed391999-10-05 16:24:54 +000075{
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 push((unsigned int) pop() ^ (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000077}
78
Erik Andersene49d5ec2000-02-08 19:58:47 +000079static void not()
Eric Andersencc8ed391999-10-05 16:24:54 +000080{
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 push(~(unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000082}
83
Erik Andersene49d5ec2000-02-08 19:58:47 +000084static void print()
Eric Andersencc8ed391999-10-05 16:24:54 +000085{
86 printf("%g\n", pop());
87}
88
89struct op {
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 const char *name;
91 void (*function) ();
Eric Andersencc8ed391999-10-05 16:24:54 +000092};
93
Erik Andersene49d5ec2000-02-08 19:58:47 +000094static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +000095 {"+", add},
96 {"add", add},
97 {"-", sub},
98 {"sub", sub},
99 {"*", mul},
100 {"mul", mul},
101 {"/", divide},
102 {"div", divide},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000104 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000105 {"not", not},
106 {"eor", eor},
John Beppuc0352542000-06-21 18:00:46 +0000107 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000108};
109
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000111{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 char *endPointer = 0;
113 double d;
114 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000115
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 if (argument == 0) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000117 print();
118 return;
119 }
120
121 d = strtod(argument, &endPointer);
122
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000124 push(d);
125 return;
126 }
127
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 while (o->name != 0) {
129 if (strcmp(o->name, argument) == 0) {
130 (*(o->function)) ();
Eric Andersencc8ed391999-10-05 16:24:54 +0000131 return;
132 }
133 o++;
134 }
135 fprintf(stderr, "math: %s: syntax error.\n", argument);
136 exit(-1);
137}
138
John Beppu5db60a72000-06-12 22:59:12 +0000139/* return pointer to next token in buffer and set *buffer to one char
140 * past the end of the above mentioned token
141 */
142static char *get_token(char **buffer)
143{
144 char *start = NULL;
145 char *current = *buffer;
146
147 while (isspace(*current)) { current++; }
148 if (*current != 0) {
149 start = current;
150 while (!isspace(*current) && current != 0) { current++; }
151 *buffer = current;
152 }
153 return start;
154}
155
156/* In Perl one might say, scalar m|\s*(\S+)\s*|g */
157static int number_of_tokens(char *buffer)
158{
159 int i = 0;
160 char *b = buffer;
161 while (get_token(&b)) { i++; }
162 return i;
163}
164
John Beppu00216792000-06-21 19:06:16 +0000165int dc_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000166{
John Beppu5db60a72000-06-12 22:59:12 +0000167 /* take stuff from stdin if no args are given */
168 if (argc <= 1) {
169 int i, len;
170 char *line = NULL;
171 char *cursor = NULL;
172 char *token = NULL;
173 while ((line = cstring_lineFromFile(stdin))) {
174 cursor = line;
175 len = number_of_tokens(line);
176 for (i = 0; i < len; i++) {
177 token = get_token(&cursor);
178 *cursor++ = 0;
179 stack_machine(token);
180 }
181 free(line);
182 }
183 } else {
184 if (*argv[1]=='-')
John Beppu00216792000-06-21 19:06:16 +0000185 usage(dc_usage);
John Beppu5db60a72000-06-12 22:59:12 +0000186 while (argc >= 2) {
187 stack_machine(argv[1]);
188 argv++;
189 argc--;
190 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000191 }
192 stack_machine(0);
Eric Andersenb6106152000-06-19 17:25:40 +0000193 return( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000194}