-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLispReader.h
165 lines (143 loc) · 4.04 KB
/
LispReader.h
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
#ifndef LISPREADER_H
#define LISPREADER_H
/*
* Copyright 2024 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Interlisp project.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include <QString>
#include <QList>
#include <QTextStream>
#include "LispRowCol.h"
class QIODevice;
namespace Lisp
{
class Lexer;
class Token;
class Reader
{
public:
class List;
class String;
class Object
{
union
{
double dbl;
quint64 bits;
};
public:
enum Type {
Float,
Integer,
Nil_,
String_,
List_,
Atom_,
};
Object();
Object(double);
Object(qint64);
Object(const char*);
Object(List*);
Object(String*);
Object(const Object&);
~Object();
Object& operator=(const Object&);
void set(double);
double getDouble() const;
void set(qint64);
qint64 getInt() const;
void set(const char*); // Atom
void set(String*);
String* getStr() const;
const char* getAtom() const;
int getAtomLen(bool inCode = false) const;
void set(List*);
List* getList() const;
Type type() const;
void nil();
void dump(QTextStream& out) const;
void print(QTextStream& out, int level = 0) const;
QByteArray toString(bool fullList = false) const;
};
struct List
{
quint32 refcount;
public:
QList<Object> list;
RowCol end;
List* outer;
QList<RowCol> elementPositions;
List():refcount(0),outer(0){}
void addRef();
void release();
Object getOuterFirst() const;
RowCol getStart() const;
};
struct String
{
quint32 refcount;
public:
QByteArray str;
String(const QByteArray& str = QByteArray()):refcount(0),str(str){}
void addRef();
void release();
};
typedef QHash<const char*,Object> Properties;
struct Atom
{
//const char* pname;
Object value;
// TODO: values have local scope, not so props; therefore in a PROG scope
// there must be a separate value entity in case of name override
Properties props;
QList<Object> vector;
};
typedef QHash<const char*,Atom> Atoms;
struct Ref
{
RowCol pos;
enum Role { Use, Call, Func, Param, Local, Lhs };
quint8 role;
quint16 len;
Ref(const RowCol& rc = RowCol(), quint16 l = 0, Role r = Use):pos(rc),role(r),len(l){}
};
typedef QList<Ref> Refs;
typedef QHash<const char*,Refs> Xref;
Reader();
bool read(QIODevice*, const QString& path);
const QString getError() const { return error; }
const RowCol& getPos() const { return pos; }
const Object& getAst() const { return ast; }
const Xref& getXref() const { return xref; }
const Atoms& getAtoms() const { return atoms; }
private:
enum Hint { None, Quoted, Local, Param };
Object next(Lexer&, List* outer, Hint hint = None);
Object list(Lexer& in, bool brack, List* outer, Hint outerHint);
void report(const Token&);
void report(const Token&, const QString&);
Object ast;
QString error;
RowCol pos;
Xref xref;
Atoms atoms;
};
}
Q_DECLARE_METATYPE(Lisp::Reader::Object)
Q_DECLARE_METATYPE(Lisp::Reader::Ref)
#endif // LISPREADER_H