blob: 621b5c9b0c946f1d22844452c3967176e0f9ff25 [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
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000011static const char math_usage[] = "math expression ...\n"
12#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[] = {
Erik Andersen5e1189e2000-04-15 16:34:54 +000095 {"+", add},
96 {"-", sub},
97 {"*", mul},
98 {"/", divide},
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 {"and", and},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000101 {"not", not},
102 {"eor", eor},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000104};
105
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000107{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 char *endPointer = 0;
109 double d;
110 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000111
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 if (argument == 0) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000113 print();
114 return;
115 }
116
117 d = strtod(argument, &endPointer);
118
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000120 push(d);
121 return;
122 }
123
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 while (o->name != 0) {
125 if (strcmp(o->name, argument) == 0) {
126 (*(o->function)) ();
Eric Andersencc8ed391999-10-05 16:24:54 +0000127 return;
128 }
129 o++;
130 }
131 fprintf(stderr, "math: %s: syntax error.\n", argument);
132 exit(-1);
133}
134
John Beppu5db60a72000-06-12 22:59:12 +0000135/* return pointer to next token in buffer and set *buffer to one char
136 * past the end of the above mentioned token
137 */
138static char *get_token(char **buffer)
139{
140 char *start = NULL;
141 char *current = *buffer;
142
143 while (isspace(*current)) { current++; }
144 if (*current != 0) {
145 start = current;
146 while (!isspace(*current) && current != 0) { current++; }
147 *buffer = current;
148 }
149 return start;
150}
151
152/* In Perl one might say, scalar m|\s*(\S+)\s*|g */
153static int number_of_tokens(char *buffer)
154{
155 int i = 0;
156 char *b = buffer;
157 while (get_token(&b)) { i++; }
158 return i;
159}
160
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161int math_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000162{
John Beppu5db60a72000-06-12 22:59:12 +0000163 /* take stuff from stdin if no args are given */
164 if (argc <= 1) {
165 int i, len;
166 char *line = NULL;
167 char *cursor = NULL;
168 char *token = NULL;
169 while ((line = cstring_lineFromFile(stdin))) {
170 cursor = line;
171 len = number_of_tokens(line);
172 for (i = 0; i < len; i++) {
173 token = get_token(&cursor);
174 *cursor++ = 0;
175 stack_machine(token);
176 }
177 free(line);
178 }
179 } else {
180 if (*argv[1]=='-')
181 usage(math_usage);
182 while (argc >= 2) {
183 stack_machine(argv[1]);
184 argv++;
185 argc--;
186 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000187 }
188 stack_machine(0);
Erik Andersen5e1189e2000-04-15 16:34:54 +0000189 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000190}