forked from aligrudi/neatroff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
161 lines (148 loc) · 3.05 KB
/
eval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* evaluation of integer expressions */
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "roff.h"
#define SCHAR "icpPvmnu" /* scale indicators */
static int defunit = 0; /* default scale indicator */
static int abspos = 0; /* absolute position like |1i */
static int readunit(int c, int n)
{
switch (c) {
case 'i':
return n * SC_IN;
case 'c':
return n * SC_IN * 50 / 127;
case 'p':
return n * SC_IN / 72;
case 'P':
return n * SC_IN / 6;
case 'v':
return n * n_v;
case 'm':
return n * n_s * SC_IN / 72;
case 'n':
return n * n_s * SC_IN / 144;
case 'u':
return n;
}
return n;
}
static int evalnum(char **_s)
{
char *s = *_s;
int n = 0; /* the result */
int mag = 0; /* n should be divided by mag */
while (isdigit((unsigned char) *s) || *s == '.') {
if (*s == '.') {
mag = 1;
s++;
continue;
}
mag *= 10;
n = n * 10 + *s++ - '0';
}
if (mag > MAXFRAC) {
n /= mag / MAXFRAC;
mag /= mag / MAXFRAC;
}
n = readunit(*s && strchr(SCHAR, *s) ? *s++ : defunit, n);
*_s = s;
return n / (mag > 0 ? mag : 1);
}
static int evaljmp(char **s, int c)
{
if (**s == c) {
(*s)++;
return 0;
}
return 1;
}
static int evalisnum(char **s)
{
return **s == '.' || isdigit((unsigned char) **s);
}
static int evalexpr(char **s);
static int evalatom(char **s);
static int evalatom(char **s)
{
int ret;
if (evalisnum(s))
return evalnum(s);
if (!evaljmp(s, '-'))
return -evalatom(s);
if (!evaljmp(s, '+'))
return evalatom(s);
if (!evaljmp(s, '|'))
return abspos + evalatom(s);
if (!evaljmp(s, '(')) {
ret = evalexpr(s);
evaljmp(s, ')');
return ret;
}
return 0;
}
static int nonzero(int n)
{
if (!n)
errdie("neatroff: divide by zero\n");
return n;
}
static int evalexpr(char **s)
{
int ret = evalatom(s);
while (**s) {
if (!evaljmp(s, '+'))
ret += evalatom(s);
else if (!evaljmp(s, '-'))
ret -= evalatom(s);
else if (!evaljmp(s, '/'))
ret /= nonzero(evalatom(s));
else if (!evaljmp(s, '*'))
ret *= evalatom(s);
else if (!evaljmp(s, '%'))
ret %= nonzero(evalatom(s));
else if (!evaljmp(s, '<'))
ret = !evaljmp(s, '=') ? ret <= evalatom(s) : ret < evalatom(s);
else if (!evaljmp(s, '>'))
ret = !evaljmp(s, '=') ? ret >= evalatom(s) : ret > evalatom(s);
else if (!evaljmp(s, '=') + !evaljmp(s, '='))
ret = ret == evalatom(s);
else if (!evaljmp(s, '&'))
ret = ret > 0 && evalatom(s) > 0;
else if (!evaljmp(s, ':'))
ret = ret > 0 || evalatom(s) > 0;
else
break;
}
return ret;
}
/* evaluate *s and update s to point to the last character read */
int eval_up(char **s, int unit)
{
defunit = unit;
if (unit == 'v')
abspos = -n_d;
if (unit == 'm')
abspos = n_lb - f_hpos();
return evalexpr(s);
}
/* evaluate s relative to its previous value */
int eval_re(char *s, int orig, int unit)
{
int n;
int rel = 0; /* n should be added to orig */
if (*s == '+' || *s == '-') {
rel = *s == '+' ? 1 : -1;
s++;
}
n = eval_up(&s, unit);
if (rel)
return rel > 0 ? orig + n : orig - n;
return n;
}
/* evaluate s */
int eval(char *s, int unit)
{
return eval_up(&s, unit);
}