summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/scheme/io.hpp
blob: c7177b54ae4a4a468190efaa08181a4cf614d80b (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#ifndef tuscany_scheme_io_hpp
#define tuscany_scheme_io_hpp

/**
 * Script evaluator IO functions.
 */

#include <ctype.h>
#include "stream.hpp"
#include "string.hpp"

#include "list.hpp"
#include "value.hpp"
#include "primitive.hpp"

namespace tuscany {
namespace scheme {

const value rightParenthesis(")");
const value leftParenthesis("(");

inline const double stringToNumber(const string& str) {
    return atof(c_str(str));
}

inline const bool isWhitespace(const char ch) {
    return ch != -1 && isspace(ch);
}

inline const bool isIdentifierStart(const char ch) {
    return ch != -1 && !isspace(ch) && !isdigit(ch);
}

inline const bool isIdentifierPart(const char ch) {
    return ch != -1 && !isspace(ch) && ch != '(' && ch != ')';
}

inline const bool isDigit(const char ch) {
    return isdigit(ch) || ch == '.';
}

inline const bool isLeftParenthesis(const value& token) {
    return leftParenthesis == token;
}

inline const bool isRightParenthesis(const value& token) {
    return rightParenthesis == token;
}

inline const char readChar(istream& in) {
    if(in.eof()) {
        return -1;
    }
    char c = (char)get(in);
    return c;
}

inline const char peekChar(istream& in) {
    if(eof(in))
        return -1;
    char c = (char)peek(in);
    return c;
}

inline const bool isQuote(const value& token) {
    return token == quoteSymbol;
}

const failable<value> skipComment(istream& in);
const value readQuoted(istream& in);
const value readIdentifier(const char chr, istream& in);
const value readString(istream& in);
const value readNumber(const char chr, istream& in);
const failable<value> readValue(istream& in);

inline const failable<value> readToken(istream& in) {
    const char firstChar = readChar(in);
    if(isWhitespace(firstChar))
        return readToken(in);
    if(firstChar == ';')
        return skipComment(in);
    if(firstChar == '\'')
        return readQuoted(in);
    if(firstChar == '(')
        return leftParenthesis;
    if(firstChar == ')')
        return rightParenthesis;
    if(firstChar == '"')
        return readString(in);
    if(isIdentifierStart(firstChar))
        return readIdentifier(firstChar, in);
    if(isDigit(firstChar))
        return readNumber(firstChar, in);
    if(firstChar == -1)
        return mkfailure<value>();
    logStream() << "Illegal lexical syntax '" << firstChar << "'" << endl;
    return readToken(in);
}

inline const failable<value> skipComment(istream& in) {
    while(true) {
        const char nextChar = readChar(in);
        if (nextChar == '\n')
            return readToken(in);
    }
}

inline const value readQuoted(istream& in) {
    return mklist(quoteSymbol, content(readValue(in)));
}

inline const list<value> readList(const list<value>& listSoFar, istream& in) {
    const failable<value> ftoken = readToken(in);
    if (!hasContent(ftoken))
        return reverse(listSoFar);
    const value token = content(ftoken);
    if(isRightParenthesis(token))
        return reverse(listSoFar);
    if(isLeftParenthesis(token))
        return readList(cons(value(readList(list<value> (), in)), listSoFar), in);
    return readList(cons(token, listSoFar), in);
}

inline const string readIdentifierHelper(const char chr, istream& in) {
    ostringstream buf;
    buf << chr;
    while(true) {
        const char nextChar = peekChar(in);
        if(!isIdentifierPart(nextChar))
            return str(buf);
        buf << readChar(in);
    }
}

inline const value readIdentifier(const char chr, istream& in) {
    const value val = c_str(readIdentifierHelper(chr, in));
    if (val == "false")
        return value((bool)false);
    if (val == "true")
        return value((bool)true);
    if (val == "nil")
        return nilValue;
    return val;
}

inline const value readString(istream& in) {
    ostringstream buf;
    while(true) {
        const char nextChar = readChar(in);
        if(nextChar == -1 || nextChar == '"')
            return (value)str(buf);
        if (nextChar == '\\') {
            const char escapedChar = readChar(in);
            if (escapedChar == -1)
                return (value)str(buf);
            buf << escapedChar;
        }
        buf << nextChar;
    }
}

inline const value readNumber(const char chr, istream& in) {
    ostringstream buf;
    buf << chr;
    while(true) {
        const char nextChar = peekChar(in);
        if(!isDigit(nextChar))
            return stringToNumber(str(buf));
        buf << readChar(in);
    }
}

inline const failable<value> readValue(istream& in) {
    const failable<value> fnextToken = readToken(in);
    if (!hasContent(fnextToken))
        return nilValue;
    const value nextToken = content(fnextToken);
    if(isLeftParenthesis(nextToken))
        return (value)readList(nilListValue, in);
    return nextToken;
}

inline const failable<value> readValue(const string& s) {
    istringstream in(s);
    const failable<value> fnextToken = readToken(in);
    if (!hasContent(fnextToken))
        return nilValue;
    const value nextToken = content(fnextToken);
    if(isLeftParenthesis(nextToken))
        return (value)readList(nilListValue, in);
    return nextToken;
}

inline const failable<value> readValue(const list<string>& l) {
    ostringstream os;
    write(l, os);
    return readValue(str(os));
}

inline const failable<ostream&> writeValue(const value& val, ostream& out) {
    out << val;
    return out;
}

inline const failable<list<string> > writeValue(const value& val) {
    ostringstream out;
    out << val;
    return mklist<string>(str(out));
}

inline const value readScript(istream& in) {
    const value val = content(readValue(in));
    if (isNull(val))
        return nilListValue;
    return cons(val, (list<value>)readScript(in));
}

}
}
#endif /* tuscany_scheme_io_hpp */