Debian Codegraph @

argcount.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9 #include "jctype.h"
10
11 int arg_state = AS_NONE;
12 long arg_count;
13
14 void
15 negate_arg(void)
16 {
17 if (arg_count < 0) {
18 arg_count = -arg_count;
19 if (arg_count < 0) {
20 complain("arg count overflow");
21 /* NOTREACHED */
22 }
23 } else {
24 arg_count = -arg_count;
25 }
26 }
27
28 private void
29 gather_argument(int ns, int nc) /* new state, new count. */
30 {
31 for (;;) {
32 ZXchar c;
33 jbool neg = NO;
34
35 if (arg_count < 0) {
36 neg = YES;
37 negate_arg();
38 }
39 if (ns != arg_state) {
40 /* First time in this state */
41 arg_state = ns;
42 arg_count = nc; /* ignore previous value (but remember sign) */
43 } else {
44 /* Continuing in this state. */
45 long t = arg_count;
46
47 switch (ns) {
48 case AS_NUMERIC:
49 t = t*10 + nc; /* add a digit to previous value */
50 break;
51 case AS_NEGSIGN:
52 neg = !neg; /* change previous sign */
53 break;
54 case AS_TIMES:
55 t *= nc; /* multiply by factor */
56 break;
57 }
58 if (t < arg_count) {
59 complain("arg count overflow");
60 /* NOTREACHED */
61 }
62 arg_count = t;
63 }
64 if (neg)
65 negate_arg();
66
67 /* Treat a following digit as AS_NUMERIC.
68 * If in AS_TIMES, accept a '-'.
69 */
70 c = waitchar();
71 if (jisdigit(c)) {
72 ns = AS_NUMERIC;
73 nc = c - '0';
74 } else if (arg_state==AS_TIMES && c=='-') {
75 ns = AS_NEGSIGN; /* forget multiplication */
76 nc = -1;
77 } else {
78 Ungetc(c);
79 break;
80 }
81 }
82 this_cmd = ARG_CMD;
83 }
84
85 void
86 TimesFour(void)
87 {
88 gather_argument(AS_TIMES, 4);
89 }
90
91 void
92 Digit(void)
93 {
94 if (LastKeyStruck == '-') {
95 gather_argument(AS_NEGSIGN, -1);
96 } else if (jisdigit(LastKeyStruck)) {
97 gather_argument(AS_NUMERIC, LastKeyStruck - '0');
98 } else {
99 complain((char *)NULL);
100 /* NOTREACHED */
101 }
102 }
103
104 void
105 Digit0(void)
106 {
107 gather_argument(AS_NUMERIC, 0);
108 }
109
110 void
111 Digit1(void)
112 {
113 gather_argument(AS_NUMERIC, 1);
114 }
115
116 void
117 Digit2(void)
118 {
119 gather_argument(AS_NUMERIC, 2);
120 }
121
122 void
123 Digit3(void)
124 {
125 gather_argument(AS_NUMERIC, 3);
126 }
127
128 void
129 Digit4(void)
130 {
131 gather_argument(AS_NUMERIC, 4);
132 }
133
134 void
135 Digit5(void)
136 {
137 gather_argument(AS_NUMERIC, 5);
138 }
139
140 void
141 Digit6(void)
142 {
143 gather_argument(AS_NUMERIC, 6);
144 }
145
146 void
147 Digit7(void)
148 {
149 gather_argument(AS_NUMERIC, 7);
150 }
151
152 void
153 Digit8(void)
154 {
155 gather_argument(AS_NUMERIC, 8);
156 }
157
158 void
159 Digit9(void)
160 {
161 gather_argument(AS_NUMERIC, 9);
162 }
163
164 void
165 DigitMinus(void)
166 {
167 gather_argument(AS_NEGSIGN, -1);
168 }