Commits

Anonymous committed d98d20f

Catch bad integer errors too

Comments (0)

Files changed (2)

 	{"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 {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.