aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/lang/PyrParseNode.h
blob: cd6688cdefa156667ab8445b4fb85fcb9a6f727b (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef _PYRPARSENODE_H_
#define _PYRPARSENODE_H_

#include "PyrSlot.h"
#include "PyrKernel.h"
#include "ByteCodeArray.h"
#include "Opcodes.h"
#include "AdvancingAllocPool.h"

extern AdvancingAllocPool gParseNodePool;

#define ALLOCNODE(type)  (type*)gParseNodePool.Alloc(sizeof(type))
//#define FREENODE(node)	if (node) (*parseNodeClasses[(node)->classno]->deleteFunc)(node);
#define DUMPNODE(node, level)	if (node) (*parseNodeClasses[(node)->classno]->dumpFunc)((node),(level));
#define COMPILENODE(node, result)	(*parseNodeClasses[(node)->classno]->compileFunc)((node),(result));

typedef void (*PyrCompileNodeFunc)(void*, void*);
typedef void (*PyrDumpNodeFunc)(void*,int);

typedef struct pyrparsenodeclass {
	int type;
	PyrCompileNodeFunc compileFunc;
	PyrDumpNodeFunc dumpFunc;
} PyrParseNodeClass;


struct PyrParseNode {
	struct PyrParseNode *next;
	struct PyrParseNode *tail;
	short lineno;
	unsigned char charno, classno;
};


struct PyrSlotNode : public PyrParseNode {
	PyrSlot slot;
};

extern PyrParseNodeClass *pyrSlotNodeClass;

struct PyrPushNameNode : public PyrParseNode {
	PyrSlot varName;
} ;

extern PyrParseNodeClass *pyrPushNameNodeClass;

struct PyrClassExtNode : public PyrParseNode {
	struct PyrSlotNode* className;
	struct PyrMethodNode *methods;
} ;

extern PyrParseNodeClass *pyrClassExtNodeClass;

struct PyrClassNode : public PyrParseNode {
	struct PyrSlotNode* className;
	struct PyrSlotNode* superClassName;
	struct PyrSlotNode* indexType;
	struct PyrVarListNode *varlists;
	struct PyrMethodNode *methods;
	int varTally[3];
	int numsuperinstvars;
} ;

extern PyrParseNodeClass *pyrClassNodeClass;

struct PyrMethodNode : public PyrParseNode {
	struct PyrSlotNode* methodName;
	struct PyrSlotNode* primitiveName;
	struct PyrArgListNode *arglist;
	struct PyrVarListNode *varlist;
	struct PyrParseNode *body;
	int isClassMethod; // is class method?
	bool extension;
} ;

extern PyrParseNodeClass *pyrMethodNodeClass;

struct PyrVarListNode : public PyrParseNode {
	struct PyrVarDefNode *varDefs;
	int flags; 
} ;

extern PyrParseNodeClass *pyrVarListNodeClass;

struct PyrVarDefNode : public PyrParseNode {
	struct PyrSlotNode* varName;
	struct PyrLiteralNode* defVal;
	int flags;
} ;

extern PyrParseNodeClass *pyrVarDefNodeClass;

struct PyrCallNode : public PyrParseNode {
	struct PyrSlotNode* selector;
	struct PyrParseNode *arglist;
	struct PyrParseNode *keyarglist;
} ;

extern PyrParseNodeClass *pyrCallNodeClass;

struct PyrBinopCallNode : public PyrParseNode {
	struct PyrSlotNode* selector;
	struct PyrParseNode *arg1;
	struct PyrParseNode *arg2;
	struct PyrParseNode *arg3;
} ;

extern PyrParseNodeClass *pyrBinopCallNodeClass;

struct PyrDropNode : public PyrParseNode {
	struct PyrParseNode *expr1;
	struct PyrParseNode *expr2;
} ;

extern PyrParseNodeClass *pyrDropNodeClass;

struct PyrPushLitNode : public PyrParseNode {
	PyrSlot literalSlot;
} ;

extern PyrParseNodeClass *pyrPushLitNodeClass;

struct PyrPushKeyArgNode : public PyrParseNode {
	struct PyrSlotNode* selector;
	struct PyrParseNode *expr;
} ;

extern PyrParseNodeClass *pyrPushKeyArgNodeClass;

struct PyrLiteralNode : public PyrParseNode {
	PyrSlot literalSlot;
} ;

extern PyrParseNodeClass *pyrLiteralNodeClass;


struct PyrReturnNode : public PyrParseNode {
	struct PyrParseNode *expr; // if null, return self
} ;

extern PyrParseNodeClass *pyrReturnNodeClass;

struct PyrBlockReturnNode : public PyrParseNode {
	struct PyrParseNode *expr; // if null, return self
} ;

extern PyrParseNodeClass *pyrBlockReturnNodeClass;


struct PyrAssignNode : public PyrParseNode {
	struct PyrSlotNode* varName;
	struct PyrParseNode *expr;
	bool drop; // allow drop
} ;

extern PyrParseNodeClass *pyrAssignNodeClass;

struct PyrSetterNode : public PyrParseNode {
	struct PyrSlotNode* selector;
	struct PyrParseNode *expr1;
	struct PyrParseNode *expr2;
	int flags; // is a var def ?
} ;

extern PyrParseNodeClass *pyrSetterNodeClass;
	
struct PyrMultiAssignNode : public PyrParseNode {
	struct PyrMultiAssignVarListNode *varList;
	struct PyrParseNode *expr;
	bool drop; // allow drop
} ;

extern PyrParseNodeClass *pyrMultiAssignNodeClass;

struct PyrMultiAssignVarListNode : public PyrParseNode {
	struct PyrSlotNode *varNames;
	struct PyrSlotNode *rest;
} ;

extern PyrParseNodeClass *pyrMultiAssignVarListNodeClass;
	
struct PyrBlockNode : public PyrParseNode {
	struct PyrArgListNode *arglist;
	struct PyrVarListNode *varlist;
	struct PyrParseNode *body;
	bool isTopLevel;
	int beginCharNo;
} ;


extern PyrParseNodeClass *pyrBlockNodeClass;
	
struct PyrArgListNode : public PyrParseNode {
	struct PyrVarDefNode *varDefs;
	struct PyrSlotNode *rest;
} ;

extern PyrParseNodeClass *pyrArgListNodeClass;
	
struct PyrDynListNode : public PyrParseNode {
	struct PyrParseNode *classname;
	struct PyrParseNode *elems;
} ;

extern PyrParseNodeClass *pyrDynListNodeClass;
		
struct PyrDynDictNode : public PyrParseNode {
	struct PyrParseNode *elems;
} ;

extern PyrParseNodeClass *pyrDynDictNodeClass;
	
struct PyrLitListNode : public PyrParseNode {
	struct PyrParseNode *classname;
	struct PyrParseNode *elems;
} ;

extern PyrParseNodeClass *pyrLitListNodeClass;
	
extern PyrParseNode* gRootParseNode;
extern int gParserResult;

enum { rwPrivate=0, rwReadOnly=1, rwWriteOnly=2, rwReadWrite=3 };

enum { varInst, varClass, varTemp, varPseudo };

enum {
	/* structural units */
	pn_ClassNode,
	pn_ClassExtNode,
	pn_MethodNode,
	pn_BlockNode,
	pn_SlotNode,

	/* variable declarations */
	pn_VarListNode,
	pn_VarDefNode,
	pn_DynDictNode,
	pn_DynListNode,
	pn_LitListNode,
	
	pn_StaticVarListNode,
	pn_InstVarListNode,
	pn_PoolVarListNode,
	pn_ArgListNode,
	pn_SlotDefNode,
	
	/* selectors */
	pn_LiteralNode,
	
	/* code */
	pn_PushLitNode,
	pn_PushNameNode,
	pn_PushKeyArgNode,
	pn_CallNode,
	pn_BinopCallNode,
	pn_DropNode,
	pn_AssignNode,
	pn_MultiAssignNode,
	pn_MultiAssignVarListNode,
	pn_SetterNode,
	
	pn_ReturnNode,
	pn_BlockReturnNode,
	
	pn_NumTypes
};

extern char *parseNodeFormat[pn_NumTypes];
extern PyrParseNodeClass* parseNodeClasses[pn_NumTypes];


void initParseNodes();

PyrParseNodeClass* newParseNodeClass(int type, PyrCompileNodeFunc compileFunc, 
	PyrDumpNodeFunc dumpFunc);

PyrSlotNode* newPyrSlotNode(PyrSlot *slot);
PyrClassNode* newPyrClassNode(PyrSlotNode* className, PyrSlotNode* superClassName,
	PyrVarListNode* varlists, PyrMethodNode* methods, PyrSlotNode* indexType);
PyrClassExtNode* newPyrClassExtNode(PyrSlotNode* className, PyrMethodNode* methods);
PyrMethodNode* newPyrMethodNode(PyrSlotNode* methodName, PyrSlotNode* primitiveName,
	PyrArgListNode* arglist, PyrVarListNode *varlist, PyrParseNode* body, int isClassMethod);
PyrArgListNode* newPyrArgListNode(PyrVarDefNode* varDefs, PyrSlotNode* rest);
PyrVarListNode* newPyrVarListNode(PyrVarDefNode* vardefs, int flags);
PyrVarDefNode* newPyrVarDefNode(PyrSlotNode* varName, PyrLiteralNode* defVal, int flags);
PyrCallNode* newPyrCallNode(PyrSlotNode* selector, PyrParseNode* arglist,
	PyrParseNode* keyarglist, PyrParseNode* blocklist);
PyrBinopCallNode* newPyrBinopCallNode(PyrSlotNode* selector,
	PyrParseNode* arg1, PyrParseNode* arg2, PyrParseNode* arg3);
PyrDropNode* newPyrDropNode(PyrParseNode* expr1, PyrParseNode* expr2);
PyrPushKeyArgNode* newPyrPushKeyArgNode(PyrSlotNode* selector, PyrParseNode* expr);
PyrPushLitNode* newPyrPushLitNode(PyrSlotNode* literalSlot, PyrParseNode* literalObj);
PyrLiteralNode* newPyrLiteralNode(PyrSlotNode* literalSlot, PyrParseNode* literalObj);
PyrReturnNode* newPyrReturnNode(PyrParseNode* expr);
PyrBlockReturnNode* newPyrBlockReturnNode();
PyrAssignNode* newPyrAssignNode(PyrSlotNode* varName, PyrParseNode* expr, int flags);
PyrSetterNode* newPyrSetterNode(PyrSlotNode* varName, 
	PyrParseNode* expr1, PyrParseNode* expr2);
PyrMultiAssignNode* newPyrMultiAssignNode(PyrMultiAssignVarListNode* varList, 
	PyrParseNode* expr, int flags);
PyrPushNameNode* newPyrPushNameNode(PyrSlotNode *slotNode);
PyrDynDictNode* newPyrDynDictNode(PyrParseNode *elems);
PyrDynListNode* newPyrDynListNode(PyrParseNode *classname, PyrParseNode *elems);
PyrLitListNode* newPyrLitListNode(PyrParseNode *classname, PyrParseNode *elems);
PyrMultiAssignVarListNode* newPyrMultiAssignVarListNode(PyrSlotNode* varNames, 
	PyrSlotNode* rest);
PyrBlockNode* newPyrBlockNode(PyrArgListNode *arglist, PyrVarListNode *varlist, PyrParseNode *body, bool isTopLevel);

void compilePyrSlotNode(PyrSlotNode* node, void *result);
void compilePyrClassNode(PyrClassNode* node, void *result);
void compilePyrClassExtNode(PyrClassExtNode* node, void *result);
void compilePyrMethodNode(PyrMethodNode* node, void *result);
void compilePyrArgListNode(PyrArgListNode* node, void *result);
void compilePyrVarListNode(PyrVarListNode* node, void *result);
void compilePyrVarDefNode(PyrVarDefNode* node, void *result);
void compilePyrCallNode(PyrCallNode* node, void *result);
void compilePyrBinopCallNode(PyrBinopCallNode* node, void *result);
void compilePyrPushLitNode(PyrPushLitNode* node, void *result);
void compilePyrLiteralNode(PyrLiteralNode* node, void *result);
void compilePyrReturnNode(PyrReturnNode* node, void *result);
void compilePyrBlockReturnNode(PyrBlockReturnNode* node, void *result);
void compilePyrAssignNode(PyrAssignNode* node, void *result);
void compilePyrSetterNode(PyrSetterNode* node, void* result);
void compilePyrMultiAssignNode(PyrMultiAssignNode* node, void *result);
void compilePyrMultiAssignVarListNode(PyrMultiAssignVarListNode* node, void *result);
void compilePyrDynDictNode(PyrDynDictNode* node, void *result);
void compilePyrDynListNode(PyrDynListNode* node, void *result);
void compilePyrLitListNode(PyrLitListNode* node, void *result);
void compilePyrBlockNode(PyrBlockNode* node, void *result);
void compilePyrPushNameNode(PyrPushNameNode* node, void *result);
void compilePyrDropNode(PyrDropNode* node, void *result);
void compilePyrPushKeyArgNode(PyrPushKeyArgNode* node, void *result);

void dumpPyrSlotNode(PyrSlotNode* node, int level);
void dumpPyrClassNode(PyrClassNode* node, int level);
void dumpPyrClassExtNode(PyrClassExtNode* node, int level);
void dumpPyrMethodNode(PyrMethodNode* node, int level);
void dumpPyrArgListNode(PyrArgListNode* node, int level);
void dumpPyrVarListNode(PyrVarListNode* node, int level);
void dumpPyrVarDefNode(PyrVarDefNode* node, int level);
void dumpPyrCallNode(PyrCallNode* node, int level);
void dumpPyrBinopCallNode(PyrBinopCallNode* node, int level);
void dumpPyrPushLitNode(PyrPushLitNode* node, int level);
void dumpPyrLiteralNode(PyrLiteralNode* node, int level);
void dumpPyrReturnNode(PyrReturnNode* node, int level);
void dumpPyrBlockReturnNode(PyrBlockReturnNode* node, int level);
void dumpPyrAssignNode(PyrAssignNode* node, int level);
void dumpPyrSetterNode(PyrSetterNode* node, int level);
void dumpPyrMultiAssignNode(PyrMultiAssignNode* node, int level);
void dumpPyrMultiAssignVarListNode(PyrMultiAssignVarListNode* node, int level);
void dumpPyrDynDictNode(PyrDynDictNode* node, int level);
void dumpPyrDynListNode(PyrDynListNode* node, int level);
void dumpPyrLitListNode(PyrLitListNode* node, int level);
void dumpPyrBlockNode(PyrBlockNode* node, int level);
void dumpPyrPushNameNode(PyrPushNameNode* node, int level);
void dumpPyrPushKeyArgNode(PyrPushKeyArgNode* node, int level);
void dumpPyrDropNode(PyrDropNode* node, int level);

PyrClass* getNodeSuperclass(PyrClassNode *node);
void countNodeMethods(PyrClassNode* node, int *numClassMethods, int *numInstMethods);
void compileExtNodeMethods(PyrClassExtNode* node);
void countVarDefs(PyrClassNode* node);
bool compareVarDefs(PyrClassNode* node, PyrClass* classobj);
void recompileSubclasses(PyrClass* classobj);
void compileNodeMethods(PyrClassNode* node);
void fillClassPrototypes(PyrClassNode *node, PyrClass *classobj, PyrClass *superclassobj);

int nodeListLength(PyrParseNode *node);
bool isSuperObjNode(PyrParseNode *node);
bool isThisObjNode(PyrParseNode *node);
int conjureSelectorIndex(PyrParseNode *node, PyrBlock* func, 
		bool isSuper, PyrSymbol *selector, int *selType);
int conjureLiteralSlotIndex(PyrParseNode *node, PyrBlock* func, PyrSlot *slot);
bool findVarName(PyrBlock* func, PyrClass **classobj, PyrSymbol *name, 
	int *varType, int *level, int *index, PyrBlock** tempfunc);
void countClassVarDefs(PyrClassNode* node, int *numClassMethods, int *numInstMethods);
void compileNodeList(PyrParseNode *node);
void dumpNodeList(PyrParseNode *node);
int compareCallArgs(PyrMethodNode* node, PyrCallNode *cnode, int *varIndex);

bool findSpecialClassName(PyrSymbol *className, int *index);
int getIndexType(PyrClassNode *classnode);

void compileIfMsg(PyrCallNode* node);
void compileWhileMsg(PyrCallNode* node);
void compileLoopMsg(PyrCallNode* node);
void compileAndMsg(PyrParseNode* arg1, PyrParseNode* arg2);
void compileOrMsg(PyrParseNode* arg1, PyrParseNode* arg2);

void compilePushInt(int value);
void compileAssignVar(PyrParseNode *node, PyrSymbol* varName, bool drop);
void compilePushVar(PyrParseNode *node, PyrSymbol *varName);
bool isAnInlineableBlock(PyrParseNode *node);
bool isWhileTrue(PyrParseNode *node);
void installByteCodes(PyrBlock *block);

ByteCodes compileSubExpression(PyrPushLitNode* litnode);
ByteCodes compileSubExpressionWithGoto(PyrPushLitNode* litnode, int branchLen);
//ByteCodes compileDefaultValue(int litIndex, int realExprLen);

void initParser();
void finiParser();
void initParserPool();
void freeParserPool();

void initSpecialSelectors();
void initSpecialClasses();

void nodePostErrorLine(PyrParseNode* node);

PyrParseNode* linkNextNode(PyrParseNode* a, PyrParseNode* b);
PyrParseNode* linkAfterHead(PyrParseNode* a, PyrParseNode* b);

extern int compileErrors;

extern long zzval;
extern PyrSymbol *ps_newlist;
extern PyrSymbol *gSpecialUnarySelectors[opNumUnarySelectors];
extern PyrSymbol *gSpecialBinarySelectors[opNumBinarySelectors];
extern PyrSymbol *gSpecialSelectors[opmNumSpecialSelectors];
extern PyrSymbol* gSpecialClasses[op_NumSpecialClasses];

extern PyrClass *gCurrentClass;
extern PyrClass *gCurrentMetaClass;
extern PyrClass *gCompilingClass;
extern PyrMethod *gCompilingMethod;
extern PyrBlock *gCompilingBlock;

/* 
	compiling
	"inlining" of special arithmetic opcodes.
	inlining of IF, WHILE, AND, OR
*/

#endif