Subversion Repositories Kolibri OS

Rev

Rev 7567 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7567 Rev 7744
1
/*
1
/*
2
 * Author Pavel Iakovlev
2
 * Author Pavel Iakovlev
3
*/
3
*/
4
 
4
 
5
#define MEMSIZE 4096*10
5
#define MEMSIZE 4096*10
6
 
6
 
7
#include "../lib/io.h"
7
#include "../lib/io.h"
8
#include "../lib/obj/console.h"
8
#include "../lib/obj/console.h"
9
#include "../lib/array.h"
9
#include "../lib/array.h"
10
 
10
 
11
byte initConsole = 0;
11
byte initConsole = 0;
12
Dictionary functions = {0};
12
Dictionary functions = {0};
13
Dictionary variables = {0};
13
Dictionary variables = {0};
14
 
14
 
15
#include "stdcall.h"
15
#include "stdcall.h"
16
 
16
 
17
#define bufferSize 10000;
17
#define bufferSize 10000;
18
#define memoryBrainfuck 30000*4
18
#define memoryBrainfuck 30000*4
19
#define memoryByteBF 1
19
#define memoryByteBF 1
20
#define stackBrainFuck 4*1024
20
#define stackBrainFuck 4*1024
-
 
21
 
-
 
22
#define TStr 1
-
 
23
#define TInt 2
-
 
24
#define TSym 3
-
 
25
#define TBol 4
-
 
26
 
-
 
27
#define TLen 4*5
21
 
28
 
22
dword buffer = 0;
29
dword buffer = 0;
23
word bufferSymbol = 0;
30
word bufferSymbol = 0;
24
dword memory = 0;
31
dword memory = 0;
25
 
32
 
26
dword stack = 0;
33
dword stack = 0;
27
dword code = 0;
34
dword code = 0;
28
byte tempBuffer[100] = {0};
35
byte tempBuffer[100] = {0};
29
 
36
 
30
void consoleInit()
37
void consoleInit()
31
{
38
{
32
	IF(!initConsole) 
39
	IF(!initConsole) 
33
	{
40
	{
34
		load_dll(libConsole, #con_init, 0);
41
		load_dll(libConsole, #con_init, 0);
35
		con_init stdcall (-1, -1, -1, -1, "Lisp interpreter");
42
		con_init stdcall (-1, -1, -1, -1, "Lisp interpreter");
36
		initConsole = 0xFF;
43
		initConsole = 0xFF;
37
	}
44
	}
38
}
45
}
39
 
46
 
40
dword evalLisp()
47
dword evalLisp()
41
{
48
{
42
	byte s = 0;
49
	byte s = 0;
43
	byte args = 0;
50
	byte args = 0;
44
	dword pos = 0;
51
	dword pos = 0;
45
	dword name = 0;
52
	dword name = 0;
46
	dword tmp = 0;
53
	dword tmp = 0;
-
 
54
	dword tmp2 = 0;
47
	dword dataArgs = 0;
55
	dword dataArgs = 0;
48
	dword posArgs = 0;
56
	dword posArgs = 0;
49
	dword ret = 0;
57
	dword ret = 0;
50
	dword p = 0;
58
	dword p = 0;
-
 
59
	dword i = 0;
-
 
60
	dword ii = 0;
51
	dataArgs = malloc(16*4);
61
	dataArgs = malloc(16*4);
52
	posArgs = dataArgs;
62
	posArgs = dataArgs;
53
 
63
 
54
	loop()
64
	loop()
55
	{
65
	{
56
		s = DSBYTE[code];
66
		s = DSBYTE[code];
57
		
-
 
58
		while (s == ' ') || (s == 9)
67
		while (s == ' ') || (s == 9) || (s == 10) || (s == 13)
59
		{
68
		{
60
			code++;
69
			code++;
61
			s = DSBYTE[code];
70
			s = DSBYTE[code];
62
		}
71
		}
63
		if (!s) return 0;
72
		if (!s) return 0;
64
		if (s==')') 
73
		if (s==')') 
65
		{
74
		{
66
			code++;
75
			code++;
67
			args--;
76
			args--;
68
			ret = StdCall(args, name, dataArgs);
77
			ret = StdCall(args, name, dataArgs);
69
			free(name);
78
			free(name);
70
			//free(dataArgs);
79
			//free(dataArgs);
71
			return ret;
80
			return ret;
72
		}
81
		}
73
		if(s == '(') 
82
		if(s == '(') 
74
		{
83
		{
75
			code++;
84
			code++;
76
			DSDWORD[posArgs] = evalLisp();
85
			DSDWORD[posArgs] = evalLisp();
77
			args++;
86
			args++;
78
			posArgs += 4;
87
			posArgs += 4;
79
			continue;
88
			continue;
80
		}
89
		}
81
		else if (!args) 
90
		else if (!args) 
82
		{
91
		{
83
			if (s != ')') // name function
92
			if (s != ')') // name function
84
			{
93
			{
85
				name = malloc(100);
94
				name = malloc(100);
86
				pos = name;
95
				pos = name;
87
				while (s) && (s != ' ') && (s != ')')
96
				while (s) && (s != ' ') && (s != ')')
88
				{
97
				{
89
					DSBYTE[pos] = s;
98
					DSBYTE[pos] = s;
90
					pos++;
99
					pos++;
91
					code++;
100
					code++;
92
					s = DSBYTE[code];
101
					s = DSBYTE[code];
93
				}	
102
				}	
94
				DSBYTE[pos] = 0;
103
				DSBYTE[pos] = 0;
95
				args++;
104
				args++;
96
				continue;
105
				continue;
97
			}
106
			}
98
		}
107
		}
99
		else
108
		else
100
		{
109
		{
101
			if (s >= '0') && (s <= '9')
110
			if (s >= '0') && (s <= '9')
102
			{
111
			{
103
				tmp = 0;
112
				tmp = 0;
104
				while (s >= '0') && (s <= '9')
113
				while (s >= '0') && (s <= '9')
105
				{
114
				{
106
					tmp *= 10;
115
					tmp *= 10;
107
					tmp += s-'0';
116
					tmp += s-'0';
108
					code++;
117
					code++;
109
					s = DSBYTE[code];
118
					s = DSBYTE[code];
110
				}
119
				}
111
				args++;
120
				args++;
-
 
121
				EDX = malloc(TLen);
-
 
122
				DSDWORD[EDX] = tmp;
-
 
123
				DSDWORD[EDX+4] = TInt;
112
				DSDWORD[posArgs] = tmp;
124
				DSDWORD[posArgs] = EDX;
113
				posArgs += 4;
125
				posArgs += 4;
114
				continue;
126
				continue;
115
			}
127
			}
116
			else if (s == '"')
128
			else if (s == '"')
117
			{
129
			{
-
 
130
				i = 1;
118
				tmp = malloc(100);
131
				tmp = malloc(1<
119
				p = tmp;
132
				p = tmp;
120
				code++;
133
				code++;
121
				s = DSBYTE[code];
134
				s = DSBYTE[code];
-
 
135
				ii = 0;
122
				while (s != '"') && (s)
136
				while (s != '"') && (s)
123
				{
137
				{
-
 
138
					ii++;
-
 
139
					if (1<
-
 
140
					{
-
 
141
						i++;
-
 
142
						tmp2 = p-tmp;
-
 
143
						tmp = realloc(tmp, 1<
-
 
144
						p = tmp+tmp2;
-
 
145
					}
124
					DSBYTE[p] = s;
146
					DSBYTE[p] = s;
125
					p++;
147
					p++;
126
					
148
					
127
					code++;
149
					code++;
128
					s = DSBYTE[code];
150
					s = DSBYTE[code];
129
				}
151
				}
130
				DSBYTE[p] = 0;
152
				DSBYTE[p] = 0;
-
 
153
				EDX = malloc(TLen);
-
 
154
				DSDWORD[EDX] = tmp;
-
 
155
				DSDWORD[EDX+4] = TStr;
-
 
156
				DSDWORD[EDX+8] = p-tmp;
-
 
157
				DSDWORD[posArgs] = EDX;
-
 
158
				posArgs += 4;
-
 
159
				code++;
-
 
160
				args++;
-
 
161
				continue;
131
			}
162
			}
132
			else if(s >= 'A') && (s <= 'z')
163
			else
133
			{
164
			{
134
				tmp = malloc(100);
165
				tmp = malloc(20);
135
				p = tmp;
166
				p = tmp;
136
				while (s >= 'A') && (s <= 'z') 
167
				while (s) && (s != ')') && (s != '(') && (s != ' ') && (s != 10) && (s != 13)
137
				{
168
				{
138
					DSBYTE[p] = s;
169
					DSBYTE[p] = s;
139
					p++;
170
					p++;
140
					
-
 
141
					code++;
171
					code++;
142
					s = DSBYTE[code];
172
					s = DSBYTE[code];
143
				}
173
				}
144
				DSBYTE[p] = 0;
174
				DSBYTE[p] = 0;
145
				args++;
175
				args++;
-
 
176
				EDX = malloc(TLen);
-
 
177
				DSDWORD[EDX] = tmp;
-
 
178
				DSDWORD[EDX+4] = TSym;
146
				DSDWORD[posArgs] = tmp;
179
				DSDWORD[posArgs] = EDX;
147
				posArgs += 4;
180
				posArgs += 4;
148
				continue;
181
				continue;
149
			}
182
			}
150
			
183
			
151
			DSDWORD[posArgs] = tmp;
184
			DSDWORD[posArgs] = tmp;
152
			posArgs += 4;
185
			posArgs += 4;
153
		}
186
		}
154
		code++;
187
		code++;
155
		args++;
188
		args++;
156
	}
189
	}
157
	args--;
190
	args--;
158
	ret = StdCall(args, name, dataArgs);
191
	ret = StdCall(args, name, dataArgs);
159
	free(name);
192
	free(name);
160
	//free(dataArgs);
193
	//free(dataArgs);
161
	return ret;
194
	return ret;
162
}
195
}
163
 
196
 
164
void main()
197
void main()
165
{
198
{
166
	dword brainFuckCode = 0;
199
	dword brainFuckCode = 0;
167
	word maxLoop = 1000;
200
	word maxLoop = 1000;
168
	dword txt = "(set name (input \"Enter you name: \"))(print \"You name \" (get name))";
201
	dword txt = "(set name (input \"Enter you name: \"))(print \"You name \" (get name))";
169
	
202
	
170
	buffer = malloc(bufferSize);
203
	buffer = malloc(bufferSize);
171
	memory = malloc(memoryBrainfuck);
204
	memory = malloc(memoryBrainfuck);
172
	stack = malloc(stackBrainFuck);
205
	stack = malloc(stackBrainFuck);
173
	
206
	
174
	Init();
207
	Init();
175
	
208
	
176
	IF(DSBYTE[I_Param])
209
	IF(DSBYTE[I_Param])
177
	{
210
	{
178
		IF(io.read(I_Param))
211
		IF(io.read(I_Param))
179
		{
212
		{
180
			code = EAX;
213
			code = EAX;
181
			evalLisp();
214
			evalLisp();
182
		}
215
		}
183
	}
216
	}
184
	else 
217
	else 
185
	{
218
	{
186
		consoleInit();
219
		consoleInit();
187
		con_printf stdcall ("Lisp interpreter v1.4\r\n");
220
		con_printf stdcall ("Lisp interpreter v1.5\r\n");
188
		while(maxLoop)
221
		while(maxLoop)
189
		{
222
		{
190
			con_printf stdcall ("\r\n$ ");
223
			con_printf stdcall ("\r\n$ ");
191
			con_gets stdcall(buffer, bufferSize);
224
			con_gets stdcall(buffer, bufferSize);
192
			code = EAX;
225
			code = EAX;
193
			//code = txt;
226
			//code = txt;
194
			con_printf stdcall ("Output: ");
227
			con_printf stdcall ("Output: ");
195
			evalLisp();
228
			evalLisp();
196
			maxLoop--;
229
			maxLoop--;
197
		}
230
		}
198
	}
231
	}
199
	
232
	
200
	IF(initConsole) con_exit stdcall (0);
233
	IF(initConsole) con_exit stdcall (0);
201
	ExitProcess();
234
	ExitProcess();
202
}
235
}
-
 
236
>
-
 
237
>
-
 
238
>
-
 
239
>