summaryrefslogtreecommitdiff
path: root/ppc.fun
blob: 0b67e396606df24f08f6fa3afe946701dc5c89d2 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
functor ppc(structure H: HASHTABLE; structure T: TOKENIZER): PPC =
struct

  structure T = T

  datatype tkPos = TkPos of T.S.pos

  type t = {
    streams: T.S.t list,

    buffer: (T.token * T.S.pos) list,
    macros: (T.token * T.S.pos) list H.t, (* body is stored reversed *)

    EOSreached: bool,
    incDirs: string list
  }

  type tkErrorVal = tkPos * string
  exception TkError of tkErrorVal

  datatype expTk =
    ExpTk of T.token |
    ExpId |
    ExpConst |
    ExpUnop |
    ExpBinop |
    ExpOp

  type tkErrorExpVal = tkPos * expTk list
  exception TkErrorExp of tkErrorExpVal

  fun raiseTkError msg pos = raise TkError (pos, msg)
  fun raiseTkErrorSPos pos msg = raiseTkError msg (TkPos pos)

  fun tkErrorPrint (TkPos pos, msg) =
    printLn $ T.S.pos2str pos ^ ": " ^ msg

  fun raiseTkErrorExp pos exp = raise TkErrorExp (pos, exp)
  fun raiseTkErrorExpSPos pos exp = raiseTkErrorExp (TkPos pos) exp

  fun tkErrorExpPrint (TkPos pos, exp) =
  let
    val printEtk = fn
        ExpTk tk => T.printToken tk
      | ExpId => print "identifier"
      | ExpConst => print "constant"
      | ExpUnop => print "unary operator"
      | ExpBinop => print "binary operator"
      | ExpOp => print "operator"

    fun printExpList [] = raise Unreachable
      | printExpList [etk] = printEtk etk
      | printExpList [etk1, etk2] = (
        printEtk etk1;
        print " or ";
        printEtk etk2
      )
      | printExpList (etk :: etks) = (
        printEtk etk;
        print ", ";
        printExpList etks
      )
  in
    print $ T.S.pos2str pos ^ ": expected ";
    printExpList exp;
    print "\n"
  end

  val updatePpc = fn z =>
    let
      fun from streams buffer macros EOSreached incDirs =
        { streams, buffer, macros, EOSreached, incDirs }
      fun to f { streams, buffer, macros, EOSreached, incDirs } =
        f streams buffer macros EOSreached incDirs
    in
      FRU.makeUpdate5 (from, from, to)
    end
    z

  fun create { fname, incDirs } =
    { streams = [T.S.create fname], buffer = [], macros = H.create 10,
        EOSreached = false, incDirs }

  datatype IncludeArg =
    LocalInc of string * T.S.pos |
    ExternalInc of string * T.S.pos

  fun parseIncludeArg stream =
  let
    fun eat pred stream skipFirst acc =
    let
      val (c, stream') = T.S.getcharEx stream
    in
      if pred c then
        eat pred stream' skipFirst (c :: acc)
      else
        (implode o rev $ acc, if skipFirst then stream' else stream)
    end
    val (_, stream) = eat Char.isSpace stream false []
    val (pos, stream) = T.S.getPos stream
    val (start, stream) = T.S.getcharEx stream
    val finish =
      if start = #"\"" then
        #"\""
      else if start = #"<" then
        #">"
      else
        raiseTkErrorSPos pos "expected \" or <"

    val (arg, stream) = eat (fn c => c <> finish) stream true []
    val (_, stream) =
      eat (fn c => c = #" " orelse c = #"\t") stream false []
    val (c, stream) = T.S.getcharEx stream
  in
    if c <> #"\n" then
      let
        val (pos, _) = T.S.getPosAfterChar stream
      in
        raiseTkErrorSPos pos "expected '\n'"
      end
    else
      (if start = #"\"" then LocalInc (arg, pos)
        else ExternalInc (arg, pos), stream)
  end handle T.S.EOF =>
  let
    val (pos, _) = T.S.EOFpos stream
  in
    raiseTkErrorSPos pos "unexpected EOF during #include argument parsing"
  end

  fun findFile arg (stream, incDirs) = 
    case arg of
        LocalInc (arg, pos) => (
        let
          val dir = OS.Path.getParent o T.S.getFname $ stream
          val path = OS.Path.concat (dir, arg)

          val (path, instream) = (path, TextIO.openIn path)
        in
          (path, instream)
        end handle
        OS.Path.Path => raiseTkErrorSPos pos "invalid argument"
        | Size => raiseTkErrorSPos pos "resulting path is too long"
        | IO.Io v => raise IO.Io v
      )
      | ExternalInc (arg, pos) =>
      let
        fun try (dir :: tail) = (
          let
            val path = OS.Path.concat (dir, arg)
          in
            SOME (path, TextIO.openIn path)
          end handle _ => try tail
        )
          | try [] = NONE
      in
        case try incDirs of
          SOME pair => pair
        | NONE => raiseTkErrorSPos pos "unable to find header"
      end

  fun handleInclude (P as { streams = head :: tail, incDirs, ... }: t) =
  let
    val (arg, oldHead) = parseIncludeArg head
    val (path, instream) = findFile arg (head, incDirs)
    val head = T.S.createFromInstream path instream
  in
    updatePpc P s#streams (head :: oldHead :: tail) %
  end
    | handleInclude _ = raise Unreachable

  fun handleDefine (P as { streams = head :: _, ... }: t) =
  let
    fun getName stream =
    let
        val (tk, pos, stream) = T.getToken stream
    in
      case tk of
           T.Id id => (id, pos, stream)
         | _ => raiseTkErrorExpSPos pos [ExpId]
    end

    fun getBody stream acc =
    let
      val (tk, pos, stream) = T.getToken stream
    in
      case tk of
           T.NewLine => (acc, stream)
         | T.EOS => raiseTkErrorExpSPos pos [ExpTk T.NewLine]
         | _ => getBody stream ((tk, pos) :: acc)
    end

    val (macroName, pos, head) = getName head
    val (macroBody, head) = getBody head []

    val () = H.insert (#macros P) macroName macroBody handle
        H.Exists => raiseTkErrorSPos pos "macro redefinition"
  in
    updatePpc P u#streams (fn s => head :: tl s) %
  end
    | handleDefine _ = raise Unreachable

  fun handleRegularToken tk pos P =
  let
    fun def () = (tk, TkPos pos, P)
  in
    case tk of
         T.Id id => (
           case H.lookup (#macros P) id of
                NONE => def ()
              | SOME body =>
              let
                val mstart = (T.MacroStart id, pos)
                val mend = (T.MacroEnd,
                    if body = [] then pos else (#2 o hd) body)
                val revBody = List.concat [[mend], body, [mstart]]
              in
                getToken $ updatePpc P
                      u#buffer (fn buf => List.revAppend (revBody, buf)) %
              end
       )
       | _ => def ()
  end

  and getToken (P as { streams = [stream], EOSreached = true, ... }) =
  let
    val (pos, stream) = T.S.EOFpos stream
  in
    (T.EOS, TkPos pos, updatePpc P s#streams [stream] %)
  end
    | getToken (P as { buffer = (tk, pos) :: tail, ... }: t) =
      handleRegularToken tk pos (updatePpc P s#buffer tail %)
    | getToken (P as { streams = head :: tail, ... }: t) =
  let
    val (tk, pos, head) = T.getToken head
    val directiveTable = [
        (T.CppInclude, handleInclude),
        (T.CppDefine, handleDefine)
    ]
  in
    if tk = T.EOS then
      case tail of
           [] => getToken $ updatePpc P s#EOSreached true %
         | _ => getToken $ updatePpc P s#streams tail %
    else
      case List.find (fn (tk', _) => tk' = tk) directiveTable of
        SOME (_, f) => getToken o f $
         updatePpc P s#streams (head :: tail) %
      | NONE =>
      let
        val ppc = updatePpc P u#streams (fn s => head :: tl s) %
      in
        handleRegularToken tk pos ppc
      end
  end
  | getToken _ = raise Unreachable


  fun debugPrint' ppc shouldPrintLine (fname, line) =
  let
    val (tk, pos, ppc) = getToken ppc
    val TkPos (t as (T.S.Pos (fname', line', col))) = pos

    fun printLine () =
      printOnNL $ fname' ^ ":" ^ Int.toString line' ^ "\n\t"

    fun finish shouldPrintLine =
      if tk <> T.EOS then
        debugPrint' ppc shouldPrintLine (fname', line')
      else
        ()
  in
    case tk of
         T.MacroStart macroName => (
           printOnNL $ "macro " ^ macroName ^ " (" ^ T.S.pos2str t ^ ") {";
           finish true
       )
       | T.MacroEnd => (printOnNL "}\n"; finish true)
       | _ => (
          if shouldPrintLine orelse fname' <> fname
                orelse line' <> line
          then
            printLine()
          else
            ();
          print $ Int.toString col ^ ":";
          T.printToken tk;
          print " ";
          finish false
       )
  end

  fun debugPrint fname incDirs =
  let
    val ppc = create { fname, incDirs }
  in
    debugPrint' ppc true ("", 0);
    print "\n"
  end
end