Skip to content

Instantly share code, notes, and snippets.

@nishio
Created July 15, 2010 08:36
Show Gist options
  • Save nishio/476670 to your computer and use it in GitHub Desktop.
Save nishio/476670 to your computer and use it in GitHub Desktop.
let rec get_chained_token (data : char list) : (int * char list) =
let (first::rest) = data in
let rec _get data count =
match data with
| x::xs -> if x == first then
_get xs (count + 1)
else
(count, data)
| [] -> (count, [])
in
_get rest 1
;;
let rec tokenize (data : char list) : token list =
match data with
| '['::xs -> Begin::(tokenize xs)
| ']'::xs -> End::(tokenize xs)
| '.'::xs -> Print::(tokenize xs)
| ','::xs -> Read::(tokenize xs)
| '+'::xs ->
let num, rest = get_chained_token data in
(Inc num)::(tokenize rest)
| '-'::xs ->
let num, rest = get_chained_token data in
(Dec num)::(tokenize rest)
| '>'::xs ->
let num, rest = get_chained_token data in
(Right num)::(tokenize rest)
| '<'::xs ->
let num, rest = get_chained_token data in
(Left num)::(tokenize rest)
| _::xs -> (tokenize xs)
| [] -> []
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment