| @@ -145,44 +145,52 @@ func Eval(f Forth, s string) (ret string, err error) { · 144 unmodified lines |
| 145 | 145 | |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | +func toInt(f Forth) int { |
| 149 | + i, err := strconv.Atoi(f.Pop()) |
| 150 | + if err != nil { |
| 151 | + panic(err) |
| 152 | + } |
| 153 | + return i |
| 154 | +} |
| 155 | + |
| 148 | 156 | func plus(f Forth) { |
| 149 | | − x, _ := strconv.Atoi(f.Pop()) |
| 150 | | − y, _ := strconv.Atoi(f.Pop()) |
| 157 | + x := toInt(f) |
| 158 | + y := toInt(f) |
| 151 | 159 | z := x + y |
| 152 | 160 | f.Push(strconv.Itoa(z)) |
| 153 | 161 | } |
| 154 | 162 | |
| 155 | 163 | func times(f Forth) { |
| 156 | | − x, _ := strconv.Atoi(f.Pop()) |
| 157 | | − y, _ := strconv.Atoi(f.Pop()) |
| 164 | + x := toInt(f) |
| 165 | + y := toInt(f) |
| 158 | 166 | z := x * y |
| 159 | 167 | f.Push(strconv.Itoa(z)) |
| 160 | 168 | } |
| 161 | 169 | |
| 162 | 170 | func sub(f Forth) { |
| 163 | | − x, _ := strconv.Atoi(f.Pop()) |
| 164 | | − y, _ := strconv.Atoi(f.Pop()) |
| 171 | + x := toInt(f) |
| 172 | + y := toInt(f) |
| 165 | 173 | z := y - x |
| 166 | 174 | f.Push(strconv.Itoa(z)) |
| 167 | 175 | } |
| 168 | 176 | |
| 169 | 177 | func div(f Forth) { |
| 170 | | − x, _ := strconv.Atoi(f.Pop()) |
| 171 | | − y, _ := strconv.Atoi(f.Pop()) |
| 178 | + x := toInt(f) |
| 179 | + y := toInt(f) |
| 172 | 180 | z := y / x |
| 173 | 181 | f.Push(strconv.Itoa(z)) |
| 174 | 182 | } |
| 175 | 183 | |
| 176 | 184 | func mod(f Forth) { |
| 177 | | − x, _ := strconv.Atoi(f.Pop()) |
| 178 | | − y, _ := strconv.Atoi(f.Pop()) |
| 185 | + x := toInt(f) |
| 186 | + y := toInt(f) |
| 179 | 187 | z := y % x |
| 180 | 188 | f.Push(strconv.Itoa(z)) |
| 181 | 189 | } |
| 182 | 190 | |
| 183 | 191 | func roundup(f Forth) { |
| 184 | | − rnd, _ := strconv.Atoi(f.Pop()) |
| 185 | | − v, _ := strconv.Atoi(f.Pop()) |
| 192 | + rnd := toInt(f) |
| 193 | + v := toInt(f) |
| 186 | 194 | v = ((v + rnd - 1) / rnd) * rnd |
| 187 | 195 | f.Push(strconv.Itoa(v)) |
| 188 | 196 | } |
| @@ -207,7 +215,7 @@ func dup(f Forth) { · 18 unmodified lines |
| 207 | 215 | } |
| 208 | 216 | |
| 209 | 217 | func ifelse(f Forth) { |
| 210 | | − x, _ := strconv.Atoi(f.Pop()) |
| 218 | + x := toInt(f) |
| 211 | 219 | y := f.Pop() |
| 212 | 220 | z := f.Pop() |
| 213 | 221 | if x != 0 { |