-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog.sml
More file actions
61 lines (49 loc) · 2.04 KB
/
prog.sml
File metadata and controls
61 lines (49 loc) · 2.04 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
structure Prog =
struct
val lineErr = "Undefined line number"
fun insert (prog, (ln, stm)) = let
fun loop (acc, rest) = case rest of
[] => List.revAppend (acc, [(ln, stm)])
| (l, s) :: xs =>
if l > ln then List.revAppend (acc, (ln, stm) :: rest)
else if l = ln then List.revAppend (acc, (ln, stm) :: xs)
else loop ((l, s) :: acc, xs)
in loop ([], prog) end
fun delete (prog, ln) = let
fun loop (acc, rest) = case rest of
[] => prog
| (l, s) :: xs =>
if l > ln then prog
else if l = ln then List.revAppend (acc, xs)
else loop ((l, s) :: acc, xs)
in loop ([], prog) end
fun getCode prog = map (fn (l, stm) => (SOME l, stm)) prog
fun getContinuation (rest, ln) = case rest of
[] => raise (BasicExn.Runtime lineErr)
| (l, _) :: xs =>
if l > ln then raise (BasicExn.Runtime lineErr)
else if l = ln then getCode rest
else getContinuation (xs, ln)
fun renum (prog, start, inc) = let
val start' = getOpt (start, 100)
val inc' = getOpt (inc, 10)
val _ = if inc' < 1
then raise (BasicExn.Runtime "Illegal renum increment")
else ()
fun mapLines (map, rest, n) = case rest of
[] => map
| (l, _) :: xs => mapLines (NumMap.insert (map, l, n), xs, n + inc')
val lines = mapLines (NumMap.empty, prog, start')
fun newTarget n =
getOpt (NumMap.lookup (lines, n), n)
fun fix s = case s of
Ast.GOTO n => Ast.GOTO (newTarget n)
| Ast.GOSUB n => Ast.GOSUB (newTarget n)
| Ast.IF (c, s) => Ast.IF (c, fix s)
| Ast.COMP ls => Ast.COMP (map fix ls)
| _ => s
fun loop (acc, rest, n) = case rest of
[] => List.rev acc
| (_, s) :: xs => loop ((n, (fix s)) :: acc, xs, n + inc')
in loop ([], prog, start') end
end