File: SExpr.h

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (162 lines) | stat: -rw-r--r-- 3,470 bytes parent folder | download | duplicates (3)
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
#pragma once
#include "Core/Object.h"
#include "Core/EnginePtr.h"
#include "Core/Array.h"
#include "Core/Io/Stream.h"
#include "Compiler/Exception.h"

namespace storm {
	namespace server {
		STORM_PKG(core.lang.server);

		class Connection;
		class Cons;
		class Number;
		class String;
		class Symbol;

		/**
		 * Messages sent to and from the client. This is based on S-Expressions from LISP.
		 */
		class SExpr : public Object {
			STORM_CLASS;
		public:
			STORM_CTOR SExpr();

			/**
			 * Tags in the stream for the different objects.
			 */
			enum Tag {
				nil = 0x00,
				cons = 0x01,
				number = 0x02,
				string = 0x03,
				newSymbol = 0x04,
				oldSymbol = 0x05,
			};

			// Expect this SExpr to be of some specific sub-type. Throws an exception if not.
			Cons *asCons();
			Number *asNum();
			String *asStr();
			Symbol *asSym();

		protected:
			// Write to a stream.
			virtual void STORM_FN write(OStream *to, Connection *sym);

			friend class Connection;
		};

		/**
		 * Cons-cell.
		 */
		class Cons : public SExpr {
			STORM_CLASS;
		public:
			STORM_CTOR Cons();
			STORM_CTOR Cons(MAYBE(SExpr *) first, MAYBE(SExpr *) rest);

			MAYBE(SExpr *) first;
			MAYBE(SExpr *) rest;

			virtual void STORM_FN deepCopy(CloneEnv *env);
			virtual void STORM_FN toS(StrBuf *to) const;
		protected:
			// Write to a stream.
			virtual void STORM_FN write(OStream *to, Connection *sym);
		};

		// Convenience functions.
		Cons *STORM_FN cons(EnginePtr e, MAYBE(SExpr *) first, MAYBE(SExpr *) rest);
		MAYBE(Cons *) STORM_FN list(Array<SExpr *> *data);
		MAYBE(Cons *) list(Engine &e, Nat count, ...);

		// Access elements.
		SExpr *STORM_FN nth(Nat id, SExpr *l);

		// Extract elements of a list one at a time.
		SExpr *next(SExpr *&pos);

		/**
		 * Number.
		 */
		class Number : public SExpr {
			STORM_CLASS;
		public:
			STORM_CTOR Number(Int v);

			Int v;

			virtual void STORM_FN deepCopy(CloneEnv *env);
			virtual void STORM_FN toS(StrBuf *to) const;
		protected:
			// Write to a stream.
			virtual void STORM_FN write(OStream *to, Connection *sym);
		};

		/**
		 * String.
		 */
		class String : public SExpr {
			STORM_CLASS;
		public:
			String(const wchar *str);
			STORM_CTOR String(Str *v);

			Str *v;

			virtual void STORM_FN deepCopy(CloneEnv *env);
			virtual void STORM_FN toS(StrBuf *to) const;
		protected:
			// Write to a stream.
			virtual void STORM_FN write(OStream *to, Connection *sym);
		};

		/**
		 * Symbol.
		 *
		 * Very similar to a string in this representation, but the underlying Symbol objects are
		 * shared. If we ever create a symbol type for Storm, we should use it here!
		 */
		class Symbol : public SExpr {
			STORM_CLASS;
		public:
			virtual void STORM_FN deepCopy(CloneEnv *env);
			virtual void STORM_FN toS(StrBuf *to) const;
			virtual Bool STORM_FN operator ==(const Symbol &o) const;
			virtual Nat STORM_FN hash() const;

		protected:
			// Write to a stream.
			virtual void STORM_FN write(OStream *to, Connection *sym);

		private:
			Symbol(Str *v, Nat id);

			// String.
			Str *v;

			// The symbol ID this symbol is assigned.
			Nat id;

			friend class Connection;
		};

		/**
		 * Exception.
		 */
		class EXCEPTION_EXPORT MsgError : public Exception {
			STORM_EXCEPTION;
		public:
			MsgError(const wchar *what, SExpr *expr);
			STORM_CTOR MsgError(Str *what, SExpr *expr);

			virtual void STORM_FN message(StrBuf *to) const;
		private:
			Str *msg;
			SExpr *expr;
		};

	}
}