Skip to content

Commit

Permalink
Fix off by one error in getPos.
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpulvis committed Jan 30, 2016
1 parent 926dff1 commit d428c5f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
18 changes: 4 additions & 14 deletions src/lexer/newline.sig
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@ signature NEWLINE =
sig
type yypos = int

(*
Resets the internal state for this structure.
*)
(* Resets the internal state for this structure. *)
val reset : unit -> unit
(*
Adds a newline at the given yypos.
*)
(* Adds a newline at the given yypos. *)
val add : yypos -> unit
(*
Returns the line number which the given yypos is in. Newlines
themselves are considered to be the 0th character of a line.
*)
(* Returns the line number which the given yypos is in. *)
val getLine : yypos -> int
(*
Returns the character position relative to the given yypos's line.
Newlines themselves are considered to be the 0th character of a line.
*)
(* Returns the character position relative to the given yypos's line. *)
val getPos : yypos -> int
end
10 changes: 5 additions & 5 deletions src/lexer/newline.sml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
structure Newline :> NEWLINE =
struct
val lines = ref [1]
val lines = ref [0]

type yypos = int

fun reset () =
lines := [1]
lines := [0]

fun add yypos =
lines := yypos :: !lines
Expand All @@ -18,9 +18,9 @@ struct
end

fun getPos yypos =
let fun look (p::ps, n) = if p <= yypos then yypos - p else look(ps, n-1)
| look _ = 0
let fun look (p::ps) = if p <= yypos then yypos - p else look(ps)
| look [] = 0
in
look(!lines, List.length(!lines))
look(!lines)
end
end
4 changes: 2 additions & 2 deletions test/newline.sml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Test.test(fn () =>
Test.test(fn () =>
(Newline.reset();
Newline.add(5);
Test.assertEq(0, Newline.getPos(1), Int.toString);
Test.assertEq(3, Newline.getPos(4), Int.toString);
Test.assertEq(1, Newline.getPos(1), Int.toString);
Test.assertEq(4, Newline.getPos(4), Int.toString);
Test.assertEq(0, Newline.getPos(5), Int.toString);
Test.assertEq(1, Newline.getPos(6), Int.toString);
Newline.add(20);
Expand Down

0 comments on commit d428c5f

Please sign in to comment.