Commits

Anonymous committed d98d20f

Catch bad integer errors too

  • Participants
  • Parent commits 7d4743d

Comments (0)

Files changed (2)

File file_test.go

 	{"1 dup +", "2", ""},
 	{"4095 4096 roundup", "4096", ""},
 	{"4097 8192 roundup", "8192", ""},
+	{"2 x +", "", "parsing \"x\": invalid syntax"},
 }
 
 func TestForth(t *testing.T) {
 		var err error
 		res, err := Eval(f, tt.val)
 		if res == tt.res || (err != nil && err.Error() == tt.err) {
+			if err != nil {
+				/* stack is not going to be right; reset it. */
+				f.Reset()
+			}
 			fmt.Printf("Test: '%v' '%v' '%v': Pass\n", tt.val, res, err)
 		} else {
 			t.Errorf("Test: '%v' '%v' '%v': Fail\n", tt.val, res, err)
 
 }
 
+func toInt(f Forth) int {
+	i, err := strconv.Atoi(f.Pop())
+	if err != nil {
+		panic(err)
+	}
+	return i
+}
+
 func plus(f Forth) {
-	x, _ := strconv.Atoi(f.Pop())
-	y, _ := strconv.Atoi(f.Pop())
+	x := toInt(f)
+	y := toInt(f)
 	z := x + y
 	f.Push(strconv.Itoa(z))
 }
 
 func times(f Forth) {
-	x, _ := strconv.Atoi(f.Pop())
-	y, _ := strconv.Atoi(f.Pop())
+	x := toInt(f)
+	y := toInt(f)
 	z := x * y
 	f.Push(strconv.Itoa(z))
 }
 
 func sub(f Forth) {
-	x, _ := strconv.Atoi(f.Pop())
-	y, _ := strconv.Atoi(f.Pop())
+	x := toInt(f)
+	y := toInt(f)
 	z := y - x
 	f.Push(strconv.Itoa(z))
 }
 
 func div(f Forth) {
-	x, _ := strconv.Atoi(f.Pop())
-	y, _ := strconv.Atoi(f.Pop())
+	x := toInt(f)
+	y := toInt(f)
 	z := y / x
 	f.Push(strconv.Itoa(z))
 }
 
 func mod(f Forth) {
-	x, _ := strconv.Atoi(f.Pop())
-	y, _ := strconv.Atoi(f.Pop())
+	x := toInt(f)
+	y := toInt(f)
 	z := y % x
 	f.Push(strconv.Itoa(z))
 }
 
 func roundup(f Forth) {
-	rnd, _ := strconv.Atoi(f.Pop())
-	v, _ := strconv.Atoi(f.Pop())
+	rnd := toInt(f)
+	v := toInt(f)
 	v = ((v + rnd - 1) / rnd) * rnd
 	f.Push(strconv.Itoa(v))
 }
 }
 
 func ifelse(f Forth) {
-	x, _ := strconv.Atoi(f.Pop())
+	x := toInt(f)
 	y := f.Pop()
 	z := f.Pop()
 	if x != 0 {