-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmlbf.sml
More file actions
47 lines (43 loc) · 1.62 KB
/
smlbf.sml
File metadata and controls
47 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
datatype bf = INC | DEC | NXT | PRV | PUT | GET | RPT of bf list
fun parse str =
let infix <>>
fun op <>> (x, (xs, y)) = (x::xs, y)
fun parse' [] = ([], [])
| parse' (c::cs) =
case c of #"+" => INC <>> parse' cs
| #"-" => DEC <>> parse' cs
| #">" => NXT <>> parse' cs
| #"<" => PRV <>> parse' cs
| #"," => GET <>> parse' cs
| #"." => PUT <>> parse' cs
| #"[" => let val p = parse' cs
in RPT (#1 p) <>> (parse' o #2) p
end
| #"]" => ([], cs)
| _ => parse' cs
in (#1 o parse' o explode) str
end
fun run bs =
let fun run' (INC :: bs) (xs, y, zs) = run' bs (xs, y+1, zs)
| run' (DEC :: bs) (xs, y, zs) = run' bs (xs, y-1, zs)
| run' (NXT :: bs) (xs, y, []) = run' bs (y::xs, 0, [])
| run' (NXT :: bs) (xs, y, z::zs) = run' bs (y::xs, z, zs)
| run' (PRV :: bs) ([], y, zs) = run' bs ([], 0, y::zs)
| run' (PRV :: bs) (x::xs, y, zs) = run' bs (xs, x, y::zs)
| run' (GET :: bs) (xs, y, zs) =
let val getChar = (valOf o TextIO.input1) TextIO.stdIn
in run' bs (xs, ord getChar, zs)
end
| run' (PUT :: bs) (xs, y, zs) = ((print o implode) [chr y]; run' bs (xs, y, zs))
| run' (RPT bs' :: bs) (xs, y, zs) =
if y = 0 then run' bs (xs, y, zs)
else run' (bs' @ RPT bs' :: bs) (xs, y, zs)
| run' [] (xs, y, zs) = (xs, y, zs)
in run' bs ([], 0, [])
end
fun main () =
case CommandLine.arguments () of
[fileName] => (ignore o run o parse o TextIO.inputAll o TextIO.openIn) fileName
| _ => print ("usage: " ^ CommandLine.name () ^ " FileName\n")
val _ = main ()
val _ = OS.Process.exit (OS.Process.success)