summaryrefslogtreecommitdiff
path: root/ipl/packs/skeem/skin.icn
blob: 1fc8ed7584c26b275f9104691cf17617f4a44df8 (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
############################################################################
#
#	Name:	skin.icn
#
#	Title:	Scheme in Icon
#
#	Author: Bob Alexander
#
#	Date:	February 19, 1995
#
#	Description: see skeem.icn
#
############################################################################

#
# skeem -- Scheme in Icon
#
# Input utility procedures
#

global BackToken

#
# ReadAllExprs() - Generate expressions from file f
#
procedure ReadAllExprs(f)
   "" ? (suspend |ScanExpr(FileRec(f)))
end

#
# ReadOneExpr() - Read one expression from f.
#
procedure ReadOneExpr(f)
   local result,fRec
   "" ? {
      result := ScanExpr(fRec := FileRec(f))
      seek(f,fRec.where + &pos - 1)
      }
   return result
end

#
# StringToExpr() - Generate expressions from string s
#
procedure StringToExpr(s)
   s ? (suspend |ScanExpr())
end

procedure ScanExpr(f)
   local token
   return case token := ScanToken(f) | fail of {
      "(": ScanList(f)
      "#(": ScanVector(f)
      !"'`," | ",@": ScanQuote(f,token)
      default:
	 if type(token) == "Symbol" then token.string
	 else token
      }
end

procedure ScanList(f)
   local result,token,dot
   result := LLNull
   while (token := ScanToken(f)) ~=== ")" do {
      if token === "." then {
	 dot := ScanExpr(f)
	 }
      else {
	 BackToken := token
	 result := LLPair(ScanExpr(f),result)
	 }
      }
   return LLInvert(result,dot)
end

procedure ScanVector(f)
   local result,token
   result := []
   while (token := ScanToken(f)) ~=== ")" do {
      BackToken := token
      put(result,ScanExpr(f))
      }
   return result
end

procedure ScanQuote(f,token)
   return LList(
      case token of {
	 "'": "QUOTE"
	 "`": "QUASIQUOTE"
	 ",": "UNQUOTE"
	 ",@": "UNQUOTE-SPLICING"
	 },
      ScanExpr(f))
end

procedure ScanToken(f)
   return 1(\.BackToken,BackToken := &null) | {
      #
      # Skip over leading white space (including comments, possibly
      # spanning lines).
      #
      #showscan("before space")
      while {
	 tab(many(Space)) |
	 (if pos(0) then &subject := ReadFileRec(\f)) |
	 (if =";" then tab(0)) |
	 (if ="#|" then {
	    until tab(find("|#") + 2) do &subject := ReadFileRec(\f) | fail
	    &null
	    })
	 }
      #showscan("after space")
      #
      # Scan then token.
      #
      ScanSymbol() | ScanNumber() | ScanSpecial() | ScanString() |
	    ScanChar() | ScanBoolean() | move(1)
      }
end

procedure ScanSymbol()
   static symFirst,symRest,nonSym
   initial {
      symFirst := &letters ++ '!$%&*/:<=>?~_^'
      symRest := symFirst ++ &digits ++ '.+-'
      nonSym := ~symRest
      }
   return Symbol(
      (match("|"),escape(quotedstring("|")[2:-1])) |
      map(1((tab(any(symFirst)) || (tab(many(symRest)) | "") |
	    =("+" | "-" | "...")),
	 (any(nonSym) | pos(0))),&lcase,&ucase))
end

procedure ScanNumber()
   local nbr
   static nbrFirst,nbrRest
   initial {
      nbrFirst := &digits ++ 'eE.'
      nbrRest := nbrFirst ++ &letters ++ '#+-'
      }
   (nbr := ((tab(any('+-')) | "") || tab(any(nbrFirst))  |
      ="#" || tab(any('bodxeiBODXEI'))) || (tab(many(nbrRest)) | "") &
      nbr ~== ".") | fail
   return StringToNumber(nbr) |
      Error("READER","bad number: ",image(nbr))
end

procedure StringToNumber(nbr,radix)
   local exact,sign,number,c
   radix := if \radix ~= 10 then radix || "r" else ""
   sign := ""
   exact := 1
   map(nbr) ? return {
      while ="#" do case move(1) of {
	 "b": radix := "2r"
	 "o": radix := "8r"
	 "d": radix := ""
	 "x": radix := "16r"
	 "e": exact := Round
	 "i": exact := real
	 default: &null # this case prevents the expression from failing
	 }
      sign := tab(any('+-'))
      number := ""
      while number ||:= tab(upto('#sfdl')) do {
	 c := move(1)
	 number ||:=
	    if c == "#" then {
	       if exact === 1 then exact := real
	       "0"
	       }
	    else "e"
	 }
      number ||:= tab(0)
      #write(&errout,"+++++ exact = ",image(exact),
      #  "; radix = ",image(radix),"; sign = ",image(sign),
      #  "; number = ",image(number))
      exact(numeric(sign || radix || number))
      }
end

procedure ScanSpecial()
   return =("#(" | ",@" | !"()'`,") |
      (="#<",Error("READER","unreadable object #<",tab(find(">") + 1 | 0)),F)
end

procedure ScanBoolean()
   return (="#",(=!"fF",F) | (=!"tT",T))
end

procedure ScanString()
   return String((match("\""),escape(quotedstring()[2:-1])))
end

procedure ScanChar()
   local chName
   return Char((="#\\",
      (case map(1(chName := tab(many(&letters)),*chName > 1)) of {
	 "space": " "
	 "tab": "\t"
	 "newline": "\n"
	 "backspace": "\b"
	 "delete": "\d"
	 "escape": "\e"
	 "formfeed": "\f"
	 "return": "\r"
	 "verticaltab": "\v"
	 default: Error("READER","unknown character name")
	 }) | move(1)))
end

record FileRec(file,where)

procedure ReadFileRec(f)
   local line
   static doPrompt
   initial  doPrompt := if find("MPW",&host) then &null else "true"
   f.where := where(f.file)
   if f.file === &input then {
      if \doPrompt then
	 writes(if BreakLevel = 0 then "> " else "[" || BreakLevel || "] ")
      line := read() | fail
##	 line ? {
##	    if =">" | (="[" || tab(find("]") + 1)) then
##	       \f.where +:= &pos - 1
##	    line := tab(0)
##	    }
      return line
      }
   else return read(f.file)
end