Kaynağa Gözat

Sol Part 60: I AM A CARPENTER

Graham Northup 4 yıl önce
ebeveyn
işleme
0e6a437a42
İmzalayan: Graham Northup <grissess@nexusg.org> GPG Key ID: 5D000E6F539376FB
8 değiştirilmiş dosya ile 917 ekleme ve 122 silme
  1. 1
    0
      .gitignore
  2. 6
    2
      Makefile
  3. 56
    0
      ast.h
  4. 1
    1
      astprint.c
  5. 182
    112
      lex.yy.c
  6. 1
    1
      runtime.c
  7. 642
    0
      ser.c
  8. 28
    6
      solrun.c

+ 1
- 0
.gitignore Dosyayı Görüntüle

@@ -9,6 +9,7 @@ gclog.txt
9 9
 gcstat.txt
10 10
 iss*
11 11
 /*.sol
12
+*.slc
12 13
 _build
13 14
 doxyxml
14 15
 valgrind.log

+ 6
- 2
Makefile Dosyayı Görüntüle

@@ -1,6 +1,6 @@
1 1
 CFLAGS= -g 
2 2
 LDFLAGS= -lm -ldl -lreadline
3
-OBJ= lex.yy.o parser.tab.o dsl/seq.o dsl/list.o dsl/array.o dsl/generic.o astprint.o runtime.o gc.o object.o state.o builtins.o solrun.o
3
+OBJ= lex.yy.o parser.tab.o dsl/seq.o dsl/list.o dsl/array.o dsl/generic.o astprint.o runtime.o gc.o object.o state.o builtins.o solrun.o ser.o
4 4
 
5 5
 .PHONY: all test
6 6
 
@@ -9,11 +9,15 @@ all: dsl sol
9 9
 sol: $(OBJ)
10 10
 	gcc $(CFLAGS) $? $(LDFLAGS) -o sol
11 11
 
12
-test: all $(sort $(patsubst tests/%.sol,test_%,$(wildcard tests/*.sol)))
12
+test: all $(sort $(patsubst tests/%.sol,test_%,$(wildcard tests/*.sol))) $(sort $(patsubst tests/%.sol,testcomp_%,$(wildcard tests/*.sol)))
13
+
13 14
 
14 15
 test_%: tests/%.sol
15 16
 	./sol r $?
16 17
 
18
+testcomp_%: tests/%.sol
19
+	./sol rc $? /dev/fd/1 | ./sol C
20
+
17 21
 dsl:
18 22
 	git submodule init && git submodule sync && git submodule update
19 23
 

+ 56
- 0
ast.h Dosyayı Görüntüle

@@ -213,6 +213,42 @@ typedef struct tag_stmt_node {
213 213
 	};
214 214
 } stmt_node;
215 215
 
216
+typedef enum {
217
+	BC_NULL,
218
+	BC_ST_EXPR,
219
+	BC_ST_LIST,
220
+	BC_ST_RET,
221
+	BC_ST_CONT,
222
+	BC_ST_BREAK,
223
+	BC_EX_LIT,
224
+	BC_EX_LISTGEN,
225
+	BC_EX_MAPGEN,
226
+	BC_EX_BINOP,
227
+	BC_EX_UNOP,
228
+	BC_EX_INDEX,
229
+	BC_EX_SETINDEX,
230
+	BC_EX_ASSIGN,
231
+	BC_EX_REF,
232
+	BC_EX_CALL,
233
+	BC_EX_FUNCDECL,
234
+	BC_EX_IFELSE,
235
+	BC_EX_LOOP,
236
+	BC_EX_ITER,
237
+	BC_LIT_INT,
238
+	BC_LIT_FLOAT,
239
+	BC_LIT_STRING,
240
+	BC_LIT_NONE,
241
+	BC_INT,
242
+	BC_FLOAT,
243
+	BC_STRING,
244
+	BC_LIST_ST,
245
+	BC_LIST_EX,
246
+	BC_LIST_AS,
247
+	BC_LIST_ID,
248
+	BC_LIST_PM,
249
+	BC_ENDLIST,
250
+} bytecode;
251
+
216 252
 #define AS_ST(arg) ((stmt_node *) (arg))
217 253
 #define AS_EX(arg) ((expr_node *) (arg))
218 254
 #define AS(arg, tp) ((tp *) (arg))
@@ -284,4 +320,24 @@ void ob_print(sol_object_t *);
284 320
 sol_object_t *sol_eval(sol_state_t *, expr_node *);
285 321
 void sol_exec(sol_state_t *, stmt_node *);
286 322
 
323
+// ser.c
324
+
325
+void sol_ser_stmt(FILE *, stmt_node *);
326
+void sol_ser_expr(FILE *, expr_node *);
327
+void sol_ser_stl(FILE *, stmtlist_node *);
328
+void sol_ser_exl(FILE *, exprlist_node *);
329
+void sol_ser_asl(FILE *, assoclist_node *);
330
+void sol_ser_idl(FILE *, identlist_node *);
331
+void sol_ser_pl(FILE *, paramlist_node *);
332
+void sol_ser_lit(FILE *, lit_node *);
333
+void sol_ser_int(FILE *, long);
334
+void sol_ser_float(FILE *, double);
335
+void sol_ser_str(FILE *, const char *);
336
+
337
+void *sol_deser(FILE *);
338
+void *sol_deser_checked(FILE *, bytecode);
339
+void *sol_deser_stmt(FILE *);
340
+void *sol_deser_expr(FILE *);
341
+void *sol_deser_lit(FILE *);
342
+
287 343
 #endif

+ 1
- 1
astprint.c Dosyayı Görüntüle

@@ -261,7 +261,7 @@ void prex(sol_state_t *state, expr_node *node, int lev) {
261 261
 			lev++;
262 262
 			prlev(state, lev, "Expr:");
263 263
 			prex(state, node->call->expr, lev + 1);
264
-			prlev(state, lev, "Method: %s\n", node->call->method);
264
+			prlev(state, lev, "Method: %s", node->call->method);
265 265
 			prlev(state, lev, "Args:");
266 266
 			cure = node->call->args;
267 267
 			while(cure && cure->expr) {

+ 182
- 112
lex.yy.c Dosyayı Görüntüle

@@ -1,5 +1,5 @@
1 1
 
2
-#line 3 "lex.yy.c"
2
+#line 2 "lex.yy.c"
3 3
 
4 4
 #define  YY_INT_ALIGNED short int
5 5
 
@@ -8,11 +8,97 @@
8 8
 #define FLEX_SCANNER
9 9
 #define YY_FLEX_MAJOR_VERSION 2
10 10
 #define YY_FLEX_MINOR_VERSION 6
11
-#define YY_FLEX_SUBMINOR_VERSION 1
11
+#define YY_FLEX_SUBMINOR_VERSION 3
12 12
 #if YY_FLEX_SUBMINOR_VERSION > 0
13 13
 #define FLEX_BETA
14 14
 #endif
15 15
 
16
+    #define yy_create_buffer yy_create_buffer
17
+
18
+    #define yy_delete_buffer yy_delete_buffer
19
+
20
+    #define yy_scan_buffer yy_scan_buffer
21
+
22
+    #define yy_scan_string yy_scan_string
23
+
24
+    #define yy_scan_bytes yy_scan_bytes
25
+
26
+    #define yy_init_buffer yy_init_buffer
27
+
28
+    #define yy_flush_buffer yy_flush_buffer
29
+
30
+    #define yy_load_buffer_state yy_load_buffer_state
31
+
32
+    #define yy_switch_to_buffer yy_switch_to_buffer
33
+
34
+    #define yypush_buffer_state yypush_buffer_state
35
+
36
+    #define yypop_buffer_state yypop_buffer_state
37
+
38
+    #define yyensure_buffer_stack yyensure_buffer_stack
39
+
40
+    #define yylex yylex
41
+
42
+    #define yyrestart yyrestart
43
+
44
+    #define yylex_init yylex_init
45
+
46
+    #define yylex_init_extra yylex_init_extra
47
+
48
+    #define yylex_destroy yylex_destroy
49
+
50
+    #define yyget_debug yyget_debug
51
+
52
+    #define yyset_debug yyset_debug
53
+
54
+    #define yyget_extra yyget_extra
55
+
56
+    #define yyset_extra yyset_extra
57
+
58
+    #define yyget_in yyget_in
59
+
60
+    #define yyset_in yyset_in
61
+
62
+    #define yyget_out yyget_out
63
+
64
+    #define yyset_out yyset_out
65
+
66
+    #define yyget_leng yyget_leng
67
+
68
+    #define yyget_text yyget_text
69
+
70
+    #define yyget_lineno yyget_lineno
71
+
72
+    #define yyset_lineno yyset_lineno
73
+
74
+    #define yywrap yywrap
75
+
76
+    #define yyget_lval yyget_lval
77
+
78
+    #define yyset_lval yyset_lval
79
+
80
+    #define yyget_lloc yyget_lloc
81
+
82
+    #define yyset_lloc yyset_lloc
83
+
84
+    #define yyalloc yyalloc
85
+
86
+    #define yyrealloc yyrealloc
87
+
88
+    #define yyfree yyfree
89
+
90
+    #define yytext yytext
91
+
92
+    #define yyleng yyleng
93
+
94
+    #define yyin yyin
95
+
96
+    #define yyout yyout
97
+
98
+    #define yy_flex_debug yy_flex_debug
99
+
100
+    #define yylineno yylineno
101
+
16 102
 /* First, we deal with  platform-specific or compiler-specific issues. */
17 103
 
18 104
 /* begin standard C headers. */
@@ -99,32 +185,26 @@ typedef unsigned int flex_uint32_t;
99 185
 /* Returned upon end-of-file. */
100 186
 #define YY_NULL 0
101 187
 
102
-/* Promotes a possibly negative, possibly signed char to an unsigned
103
- * integer for use as an array index.  If the signed char is negative,
104
- * we want to instead treat it as an 8-bit unsigned char, hence the
105
- * double cast.
188
+/* Promotes a possibly negative, possibly signed char to an
189
+ *   integer in range [0..255] for use as an array index.
106 190
  */
107
-#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c)
191
+#define YY_SC_TO_UI(c) ((YY_CHAR) (c))
108 192
 
109 193
 /* Enter a start condition.  This macro really ought to take a parameter,
110 194
  * but we do it the disgusting crufty way forced on us by the ()-less
111 195
  * definition of BEGIN.
112 196
  */
113 197
 #define BEGIN (yy_start) = 1 + 2 *
114
-
115 198
 /* Translate the current start state into a value that can be later handed
116 199
  * to BEGIN to return to the state.  The YYSTATE alias is for lex
117 200
  * compatibility.
118 201
  */
119 202
 #define YY_START (((yy_start) - 1) / 2)
120 203
 #define YYSTATE YY_START
121
-
122 204
 /* Action number for EOF rule of a given start state. */
123 205
 #define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1)
124
-
125 206
 /* Special action meaning "start processing a new file". */
126 207
 #define YY_NEW_FILE yyrestart(yyin  )
127
-
128 208
 #define YY_END_OF_BUFFER_CHAR 0
129 209
 
130 210
 /* Size of default input buffer. */
@@ -161,7 +241,7 @@ extern FILE *yyin, *yyout;
161 241
 #define EOB_ACT_CONTINUE_SCAN 0
162 242
 #define EOB_ACT_END_OF_FILE 1
163 243
 #define EOB_ACT_LAST_MATCH 2
164
-
244
+    
165 245
     #define YY_LESS_LINENO(n)
166 246
     #define YY_LINENO_REWIND_TO(ptr)
167 247
     
@@ -178,7 +258,6 @@ extern FILE *yyin, *yyout;
178 258
 		YY_DO_BEFORE_ACTION; /* set up yytext again */ \
179 259
 		} \
180 260
 	while ( 0 )
181
-
182 261
 #define unput(c) yyunput( c, (yytext_ptr)  )
183 262
 
184 263
 #ifndef YY_STRUCT_YY_BUFFER_STATE
@@ -221,7 +300,7 @@ struct yy_buffer_state
221 300
 
222 301
     int yy_bs_lineno; /**< The line count. */
223 302
     int yy_bs_column; /**< The column count. */
224
-    
303
+
225 304
 	/* Whether to try to fill the input buffer when we reach the
226 305
 	 * end of it.
227 306
 	 */
@@ -260,7 +339,6 @@ static YY_BUFFER_STATE * yy_buffer_stack = NULL; /**< Stack as an array. */
260 339
 #define YY_CURRENT_BUFFER ( (yy_buffer_stack) \
261 340
                           ? (yy_buffer_stack)[(yy_buffer_stack_top)] \
262 341
                           : NULL)
263
-
264 342
 /* Same as previous macro, but useful when we know that the buffer stack is not
265 343
  * NULL or when we need an lvalue. For internal use only.
266 344
  */
@@ -281,30 +359,28 @@ static int yy_start = 0;	/* start state number */
281 359
  */
282 360
 static int yy_did_buffer_switch_on_eof;
283 361
 
284
-void yyrestart (FILE *input_file  );
285
-void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer  );
286
-YY_BUFFER_STATE yy_create_buffer (FILE *file,int size  );
287
-void yy_delete_buffer (YY_BUFFER_STATE b  );
288
-void yy_flush_buffer (YY_BUFFER_STATE b  );
289
-void yypush_buffer_state (YY_BUFFER_STATE new_buffer  );
290
-void yypop_buffer_state (void );
291
-
292
-static void yyensure_buffer_stack (void );
293
-static void yy_load_buffer_state (void );
294
-static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file  );
295
-
362
+void yyrestart ( FILE *input_file  );
363
+void yy_switch_to_buffer ( YY_BUFFER_STATE new_buffer  );
364
+YY_BUFFER_STATE yy_create_buffer ( FILE *file, int size  );
365
+void yy_delete_buffer ( YY_BUFFER_STATE b  );
366
+void yy_flush_buffer ( YY_BUFFER_STATE b  );
367
+void yypush_buffer_state ( YY_BUFFER_STATE new_buffer  );
368
+void yypop_buffer_state ( void );
369
+
370
+static void yyensure_buffer_stack ( void );
371
+static void yy_load_buffer_state ( void );
372
+static void yy_init_buffer ( YY_BUFFER_STATE b, FILE *file  );
296 373
 #define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER )
297 374
 
298
-YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size  );
299
-YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str  );
300
-YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len  );
375
+YY_BUFFER_STATE yy_scan_buffer ( char *base, yy_size_t size  );
376
+YY_BUFFER_STATE yy_scan_string ( const char *yy_str  );
377
+YY_BUFFER_STATE yy_scan_bytes ( const char *bytes, int len  );
301 378
 
302
-void *yyalloc (yy_size_t  );
303
-void *yyrealloc (void *,yy_size_t  );
304
-void yyfree (void *  );
379
+void *yyalloc ( yy_size_t  );
380
+void *yyrealloc ( void *, yy_size_t  );
381
+void yyfree ( void *  );
305 382
 
306 383
 #define yy_new_buffer yy_create_buffer
307
-
308 384
 #define yy_set_interactive(is_interactive) \
309 385
 	{ \
310 386
 	if ( ! YY_CURRENT_BUFFER ){ \
@@ -314,7 +390,6 @@ void yyfree (void *  );
314 390
 	} \
315 391
 	YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \
316 392
 	}
317
-
318 393
 #define yy_set_bol(at_bol) \
319 394
 	{ \
320 395
 	if ( ! YY_CURRENT_BUFFER ){\
@@ -324,19 +399,16 @@ void yyfree (void *  );
324 399
 	} \
325 400
 	YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \
326 401
 	}
327
-
328 402
 #define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol)
329 403
 
330 404
 /* Begin user sect3 */
331
-
332
-typedef unsigned char YY_CHAR;
405
+typedef flex_uint8_t YY_CHAR;
333 406
 
334 407
 FILE *yyin = NULL, *yyout = NULL;
335 408
 
336 409
 typedef int yy_state_type;
337 410
 
338 411
 extern int yylineno;
339
-
340 412
 int yylineno = 1;
341 413
 
342 414
 extern char *yytext;
@@ -345,10 +417,10 @@ extern char *yytext;
345 417
 #endif
346 418
 #define yytext_ptr yytext
347 419
 
348
-static yy_state_type yy_get_previous_state (void );
349
-static yy_state_type yy_try_NUL_trans (yy_state_type current_state  );
350
-static int yy_get_next_buffer (void );
351
-static void yynoreturn yy_fatal_error (yyconst char* msg  );
420
+static yy_state_type yy_get_previous_state ( void );
421
+static yy_state_type yy_try_NUL_trans ( yy_state_type current_state  );
422
+static int yy_get_next_buffer ( void );
423
+static void yynoreturn yy_fatal_error ( const char* msg  );
352 424
 
353 425
 /* Done after the current pattern has been matched and before the
354 426
  * corresponding action - sets up yytext.
@@ -359,7 +431,6 @@ static void yynoreturn yy_fatal_error (yyconst char* msg  );
359 431
 	(yy_hold_char) = *yy_cp; \
360 432
 	*yy_cp = '\0'; \
361 433
 	(yy_c_buf_p) = yy_cp;
362
-
363 434
 #define YY_NUM_RULES 73
364 435
 #define YY_END_OF_BUFFER 74
365 436
 /* This struct is not used in this scanner,
@@ -369,7 +440,7 @@ struct yy_trans_info
369 440
 	flex_int32_t yy_verify;
370 441
 	flex_int32_t yy_nxt;
371 442
 	};
372
-static yyconst flex_int16_t yy_accept[152] =
443
+static const flex_int16_t yy_accept[152] =
373 444
     {   0,
374 445
         0,    0,   74,   73,   72,   34,   73,   68,   23,   26,
375 446
        73,   62,   63,   21,   19,   67,   20,   64,   22,    2,
@@ -390,7 +461,7 @@ static yyconst flex_int16_t yy_accept[152] =
390 461
         0
391 462
     } ;
392 463
 
393
-static yyconst YY_CHAR yy_ec[256] =
464
+static const YY_CHAR yy_ec[256] =
394 465
     {   0,
395 466
         1,    1,    1,    1,    1,    1,    1,    1,    2,    3,
396 467
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
@@ -422,7 +493,7 @@ static yyconst YY_CHAR yy_ec[256] =
422 493
         1,    1,    1,    1,    1
423 494
     } ;
424 495
 
425
-static yyconst YY_CHAR yy_meta[53] =
496
+static const YY_CHAR yy_meta[53] =
426 497
     {   0,
427 498
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
428 499
         1,    1,    1,    1,    1,    1,    1,    2,    1,    1,
@@ -432,7 +503,7 @@ static yyconst YY_CHAR yy_meta[53] =
432 503
         1,    1
433 504
     } ;
434 505
 
435
-static yyconst flex_uint16_t yy_base[156] =
506
+static const flex_int16_t yy_base[156] =
436 507
     {   0,
437 508
         0,   51,  169,  170,   52,   52,  163,  170,  170,   49,
438 509
       158,  170,  170,   46,  144,  170,   44,  170,  143,   44,
@@ -453,7 +524,7 @@ static yyconst flex_uint16_t yy_base[156] =
453 524
       170,  104,  106,   81,  108
454 525
     } ;
455 526
 
456
-static yyconst flex_int16_t yy_def[156] =
527
+static const flex_int16_t yy_def[156] =
457 528
     {   0,
458 529
       151,    1,  151,  151,  151,  151,  152,  151,  151,  151,
459 530
       153,  151,  151,  151,  151,  151,  151,  151,  151,  151,
@@ -474,7 +545,7 @@ static yyconst flex_int16_t yy_def[156] =
474 545
         0,  151,  151,  151,  151
475 546
     } ;
476 547
 
477
-static yyconst flex_uint16_t yy_nxt[223] =
548
+static const flex_int16_t yy_nxt[223] =
478 549
     {   0,
479 550
         4,    5,    5,    6,    7,    8,    9,   10,   11,   12,
480 551
        13,   14,   15,   16,   17,   18,   19,   20,   21,   22,
@@ -503,7 +574,7 @@ static yyconst flex_uint16_t yy_nxt[223] =
503 574
       151,  151
504 575
     } ;
505 576
 
506
-static yyconst flex_int16_t yy_chk[223] =
577
+static const flex_int16_t yy_chk[223] =
507 578
     {   0,
508 579
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
509 580
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
@@ -606,6 +677,7 @@ static void update_loc(YYLTYPE *yylloc, char *yytext){
606 677
 
607 678
 #define YY_USER_ACTION update_loc(yylloc, yytext);
608 679
 
680
+#line 680 "lex.yy.c"
609 681
 /* This is the right way to do it, but it keeps generating token $undefined.
610 682
 
611 683
 %x STRING
@@ -621,7 +693,7 @@ static void update_loc(YYLTYPE *yylloc, char *yytext){
621 693
 <STRING>. { str_putc(*yytext); }
622 694
 
623 695
 */
624
-#line 625 "lex.yy.c"
696
+#line 696 "lex.yy.c"
625 697
 
626 698
 #define INITIAL 0
627 699
 
@@ -637,44 +709,44 @@ static void update_loc(YYLTYPE *yylloc, char *yytext){
637 709
 #define YY_EXTRA_TYPE void *
638 710
 #endif
639 711
 
640
-static int yy_init_globals (void );
712
+static int yy_init_globals ( void );
641 713
 
642 714
 /* Accessor methods to globals.
643 715
    These are made visible to non-reentrant scanners for convenience. */
644 716
 
645
-int yylex_destroy (void );
717
+int yylex_destroy ( void );
646 718
 
647
-int yyget_debug (void );
719
+int yyget_debug ( void );
648 720
 
649
-void yyset_debug (int debug_flag  );
721
+void yyset_debug ( int debug_flag  );
650 722
 
651
-YY_EXTRA_TYPE yyget_extra (void );
723
+YY_EXTRA_TYPE yyget_extra ( void );
652 724
 
653
-void yyset_extra (YY_EXTRA_TYPE user_defined  );
725
+void yyset_extra ( YY_EXTRA_TYPE user_defined  );
654 726
 
655
-FILE *yyget_in (void );
727
+FILE *yyget_in ( void );
656 728
 
657
-void yyset_in  (FILE * _in_str  );
729
+void yyset_in  ( FILE * _in_str  );
658 730
 
659
-FILE *yyget_out (void );
731
+FILE *yyget_out ( void );
660 732
 
661
-void yyset_out  (FILE * _out_str  );
733
+void yyset_out  ( FILE * _out_str  );
662 734
 
663
-			int yyget_leng (void );
735
+			int yyget_leng ( void );
664 736
 
665
-char *yyget_text (void );
737
+char *yyget_text ( void );
666 738
 
667
-int yyget_lineno (void );
739
+int yyget_lineno ( void );
668 740
 
669
-void yyset_lineno (int _line_number  );
741
+void yyset_lineno ( int _line_number  );
670 742
 
671
-YYSTYPE * yyget_lval (void );
743
+YYSTYPE * yyget_lval ( void );
672 744
 
673
-void yyset_lval (YYSTYPE * yylval_param  );
745
+void yyset_lval ( YYSTYPE * yylval_param  );
674 746
 
675
-       YYLTYPE *yyget_lloc (void );
747
+       YYLTYPE *yyget_lloc ( void );
676 748
     
677
-        void yyset_lloc (YYLTYPE * yylloc_param  );
749
+        void yyset_lloc ( YYLTYPE * yylloc_param  );
678 750
     
679 751
 /* Macros after this point can all be overridden by user definitions in
680 752
  * section 1.
@@ -682,32 +754,31 @@ void yyset_lval (YYSTYPE * yylval_param  );
682 754
 
683 755
 #ifndef YY_SKIP_YYWRAP
684 756
 #ifdef __cplusplus
685
-extern "C" int yywrap (void );
757
+extern "C" int yywrap ( void );
686 758
 #else
687
-extern int yywrap (void );
759
+extern int yywrap ( void );
688 760
 #endif
689 761
 #endif
690 762
 
691 763
 #ifndef YY_NO_UNPUT
692 764
     
693
-    static void yyunput (int c,char *buf_ptr  );
765
+    static void yyunput ( int c, char *buf_ptr  );
694 766
     
695 767
 #endif
696 768
 
697 769
 #ifndef yytext_ptr
698
-static void yy_flex_strncpy (char *,yyconst char *,int );
770
+static void yy_flex_strncpy ( char *, const char *, int );
699 771
 #endif
700 772
 
701 773
 #ifdef YY_NEED_STRLEN
702
-static int yy_flex_strlen (yyconst char * );
774
+static int yy_flex_strlen ( const char * );
703 775
 #endif
704 776
 
705 777
 #ifndef YY_NO_INPUT
706
-
707 778
 #ifdef __cplusplus
708
-static int yyinput (void );
779
+static int yyinput ( void );
709 780
 #else
710
-static int input (void );
781
+static int input ( void );
711 782
 #endif
712 783
 
713 784
 #endif
@@ -738,7 +809,7 @@ static int input (void );
738 809
 	if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
739 810
 		{ \
740 811
 		int c = '*'; \
741
-		size_t n; \
812
+		int n; \
742 813
 		for ( n = 0; n < max_size && \
743 814
 			     (c = getc( yyin )) != EOF && c != '\n'; ++n ) \
744 815
 			buf[n] = (char) c; \
@@ -751,7 +822,7 @@ static int input (void );
751 822
 	else \
752 823
 		{ \
753 824
 		errno=0; \
754
-		while ( (result = (int) fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
825
+		while ( (result = (int) fread(buf, 1, (yy_size_t) max_size, yyin)) == 0 && ferror(yyin)) \
755 826
 			{ \
756 827
 			if( errno != EINTR) \
757 828
 				{ \
@@ -793,7 +864,7 @@ static int input (void );
793 864
 #define YY_DECL_IS_OURS 1
794 865
 
795 866
 extern int yylex \
796
-               (YYSTYPE * yylval_param,YYLTYPE * yylloc_param );
867
+               (YYSTYPE * yylval_param, YYLTYPE * yylloc_param );
797 868
 
798 869
 #define YY_DECL int yylex \
799 870
                (YYSTYPE * yylval_param, YYLTYPE * yylloc_param )
@@ -863,7 +934,7 @@ YY_DECL
863 934
 #line 85 "tokenizer.lex"
864 935
 
865 936
 
866
-#line 867 "lex.yy.c"
937
+#line 937 "lex.yy.c"
867 938
 
868 939
 	while ( /*CONSTCOND*/1 )		/* loops until end-of-file is reached */
869 940
 		{
@@ -892,9 +963,9 @@ yy_match:
892 963
 				{
893 964
 				yy_current_state = (int) yy_def[yy_current_state];
894 965
 				if ( yy_current_state >= 152 )
895
-					yy_c = yy_meta[(unsigned int) yy_c];
966
+					yy_c = yy_meta[yy_c];
896 967
 				}
897
-			yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
968
+			yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
898 969
 			++yy_cp;
899 970
 			}
900 971
 		while ( yy_base[yy_current_state] != 170 );
@@ -1290,7 +1361,7 @@ YY_RULE_SETUP
1290 1361
 #line 231 "tokenizer.lex"
1291 1362
 ECHO;
1292 1363
 	YY_BREAK
1293
-#line 1294 "lex.yy.c"
1364
+#line 1364 "lex.yy.c"
1294 1365
 case YY_STATE_EOF(INITIAL):
1295 1366
 	yyterminate();
1296 1367
 
@@ -1435,7 +1506,7 @@ static int yy_get_next_buffer (void)
1435 1506
 {
1436 1507
     	char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf;
1437 1508
 	char *source = (yytext_ptr);
1438
-	yy_size_t number_to_move, i;
1509
+	int number_to_move, i;
1439 1510
 	int ret_val;
1440 1511
 
1441 1512
 	if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] )
@@ -1464,7 +1535,7 @@ static int yy_get_next_buffer (void)
1464 1535
 	/* Try to read more data. */
1465 1536
 
1466 1537
 	/* First move last chars to start of buffer. */
1467
-	number_to_move = (yy_size_t) ((yy_c_buf_p) - (yytext_ptr)) - 1;
1538
+	number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr) - 1);
1468 1539
 
1469 1540
 	for ( i = 0; i < number_to_move; ++i )
1470 1541
 		*(dest++) = *(source++);
@@ -1500,7 +1571,7 @@ static int yy_get_next_buffer (void)
1500 1571
 
1501 1572
 				b->yy_ch_buf = (char *)
1502 1573
 					/* Include room in for 2 EOB chars. */
1503
-					yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2  );
1574
+					yyrealloc((void *) b->yy_ch_buf,(yy_size_t) (b->yy_buf_size + 2)  );
1504 1575
 				}
1505 1576
 			else
1506 1577
 				/* Can't grow it, we don't own it. */
@@ -1546,10 +1617,10 @@ static int yy_get_next_buffer (void)
1546 1617
 	else
1547 1618
 		ret_val = EOB_ACT_CONTINUE_SCAN;
1548 1619
 
1549
-	if ((int) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
1620
+	if (((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
1550 1621
 		/* Extend the array by 50%, plus the number we really need. */
1551 1622
 		int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1);
1552
-		YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size  );
1623
+		YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,(yy_size_t) new_size  );
1553 1624
 		if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
1554 1625
 			YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" );
1555 1626
 	}
@@ -1585,9 +1656,9 @@ static int yy_get_next_buffer (void)
1585 1656
 			{
1586 1657
 			yy_current_state = (int) yy_def[yy_current_state];
1587 1658
 			if ( yy_current_state >= 152 )
1588
-				yy_c = yy_meta[(unsigned int) yy_c];
1659
+				yy_c = yy_meta[yy_c];
1589 1660
 			}
1590
-		yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
1661
+		yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
1591 1662
 		}
1592 1663
 
1593 1664
 	return yy_current_state;
@@ -1613,9 +1684,9 @@ static int yy_get_next_buffer (void)
1613 1684
 		{
1614 1685
 		yy_current_state = (int) yy_def[yy_current_state];
1615 1686
 		if ( yy_current_state >= 152 )
1616
-			yy_c = yy_meta[(unsigned int) yy_c];
1687
+			yy_c = yy_meta[yy_c];
1617 1688
 		}
1618
-	yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
1689
+	yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
1619 1690
 	yy_is_jam = (yy_current_state == 151);
1620 1691
 
1621 1692
 		return yy_is_jam ? 0 : yy_current_state;
@@ -1686,7 +1757,7 @@ static int yy_get_next_buffer (void)
1686 1757
 
1687 1758
 		else
1688 1759
 			{ /* need more input */
1689
-			int offset = (yy_c_buf_p) - (yytext_ptr);
1760
+			int offset = (int) ((yy_c_buf_p) - (yytext_ptr));
1690 1761
 			++(yy_c_buf_p);
1691 1762
 
1692 1763
 			switch ( yy_get_next_buffer(  ) )
@@ -1813,12 +1884,12 @@ static void yy_load_buffer_state  (void)
1813 1884
 	if ( ! b )
1814 1885
 		YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
1815 1886
 
1816
-	b->yy_buf_size = (yy_size_t)size;
1887
+	b->yy_buf_size = size;
1817 1888
 
1818 1889
 	/* yy_ch_buf has to be 2 characters longer than the size given because
1819 1890
 	 * we need to put in 2 end-of-buffer characters.
1820 1891
 	 */
1821
-	b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2  );
1892
+	b->yy_ch_buf = (char *) yyalloc((yy_size_t) (b->yy_buf_size + 2)  );
1822 1893
 	if ( ! b->yy_ch_buf )
1823 1894
 		YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
1824 1895
 
@@ -1960,7 +2031,7 @@ void yypop_buffer_state (void)
1960 2031
  */
1961 2032
 static void yyensure_buffer_stack (void)
1962 2033
 {
1963
-	int num_to_alloc;
2034
+	yy_size_t num_to_alloc;
1964 2035
     
1965 2036
 	if (!(yy_buffer_stack)) {
1966 2037
 
@@ -1974,9 +2045,9 @@ static void yyensure_buffer_stack (void)
1974 2045
 								);
1975 2046
 		if ( ! (yy_buffer_stack) )
1976 2047
 			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
1977
-								  
2048
+
1978 2049
 		memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*));
1979
-				
2050
+
1980 2051
 		(yy_buffer_stack_max) = num_to_alloc;
1981 2052
 		(yy_buffer_stack_top) = 0;
1982 2053
 		return;
@@ -2005,7 +2076,7 @@ static void yyensure_buffer_stack (void)
2005 2076
  * @param base the character buffer
2006 2077
  * @param size the size in bytes of the character buffer
2007 2078
  * 
2008
- * @return the newly allocated buffer state object. 
2079
+ * @return the newly allocated buffer state object.
2009 2080
  */
2010 2081
 YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size )
2011 2082
 {
@@ -2021,7 +2092,7 @@ YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size )
2021 2092
 	if ( ! b )
2022 2093
 		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" );
2023 2094
 
2024
-	b->yy_buf_size = size - 2;	/* "- 2" to take care of EOB's */
2095
+	b->yy_buf_size = (int) (size - 2);	/* "- 2" to take care of EOB's */
2025 2096
 	b->yy_buf_pos = b->yy_ch_buf = base;
2026 2097
 	b->yy_is_our_buffer = 0;
2027 2098
 	b->yy_input_file = NULL;
@@ -2044,7 +2115,7 @@ YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size )
2044 2115
  * @note If you want to scan bytes that may contain NUL values, then use
2045 2116
  *       yy_scan_bytes() instead.
2046 2117
  */
2047
-YY_BUFFER_STATE yy_scan_string (yyconst char * yystr )
2118
+YY_BUFFER_STATE yy_scan_string (const char * yystr )
2048 2119
 {
2049 2120
     
2050 2121
 	return yy_scan_bytes(yystr,(int) strlen(yystr) );
@@ -2057,15 +2128,15 @@ YY_BUFFER_STATE yy_scan_string (yyconst char * yystr )
2057 2128
  * 
2058 2129
  * @return the newly allocated buffer state object.
2059 2130
  */
2060
-YY_BUFFER_STATE yy_scan_bytes  (yyconst char * yybytes, int  _yybytes_len )
2131
+YY_BUFFER_STATE yy_scan_bytes  (const char * yybytes, int  _yybytes_len )
2061 2132
 {
2062 2133
 	YY_BUFFER_STATE b;
2063 2134
 	char *buf;
2064 2135
 	yy_size_t n;
2065
-	yy_size_t i;
2136
+	int i;
2066 2137
     
2067 2138
 	/* Get memory for full buffer, including space for trailing EOB's. */
2068
-	n = (yy_size_t) _yybytes_len + 2;
2139
+	n = (yy_size_t) (_yybytes_len + 2);
2069 2140
 	buf = (char *) yyalloc(n  );
2070 2141
 	if ( ! buf )
2071 2142
 		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );
@@ -2091,7 +2162,7 @@ YY_BUFFER_STATE yy_scan_bytes  (yyconst char * yybytes, int  _yybytes_len )
2091 2162
 #define YY_EXIT_FAILURE 2
2092 2163
 #endif
2093 2164
 
2094
-static void yynoreturn yy_fatal_error (yyconst char* msg )
2165
+static void yynoreturn yy_fatal_error (const char* msg )
2095 2166
 {
2096 2167
 			(void) fprintf( stderr, "%s\n", msg );
2097 2168
 	exit( YY_EXIT_FAILURE );
@@ -2121,7 +2192,7 @@ static void yynoreturn yy_fatal_error (yyconst char* msg )
2121 2192
  */
2122 2193
 int yyget_lineno  (void)
2123 2194
 {
2124
-        
2195
+    
2125 2196
     return yylineno;
2126 2197
 }
2127 2198
 
@@ -2249,7 +2320,7 @@ int yylex_destroy  (void)
2249 2320
  */
2250 2321
 
2251 2322
 #ifndef yytext_ptr
2252
-static void yy_flex_strncpy (char* s1, yyconst char * s2, int n )
2323
+static void yy_flex_strncpy (char* s1, const char * s2, int n )
2253 2324
 {
2254 2325
 		
2255 2326
 	int i;
@@ -2259,7 +2330,7 @@ static void yy_flex_strncpy (char* s1, yyconst char * s2, int n )
2259 2330
 #endif
2260 2331
 
2261 2332
 #ifdef YY_NEED_STRLEN
2262
-static int yy_flex_strlen (yyconst char * s )
2333
+static int yy_flex_strlen (const char * s )
2263 2334
 {
2264 2335
 	int n;
2265 2336
 	for ( n = 0; s[n]; ++n )
@@ -2297,7 +2368,6 @@ void yyfree (void * ptr )
2297 2368
 #line 231 "tokenizer.lex"
2298 2369
 
2299 2370
 
2300
-
2301 2371
 int yywrap(void) {
2302 2372
 	return 1;
2303 2373
 }
@@ -2317,7 +2387,7 @@ stmt_node *sol_compile(const char *prgstr) {
2317 2387
 
2318 2388
 stmt_node *sol_compile_file(FILE *prgfile) {
2319 2389
     stmt_node *program = NULL;
2320
-    YY_BUFFER_STATE buf = yy_create_buffer(prgfile,YY_BUF_SIZE);
2390
+    YY_BUFFER_STATE buf = yy_create_buffer(prgfile, YY_BUF_SIZE);
2321 2391
     yy_switch_to_buffer(buf);
2322 2392
     yyparse(&program);
2323 2393
     yy_delete_buffer(buf);

+ 1
- 1
runtime.c Dosyayı Görüntüle

@@ -46,7 +46,7 @@ stmt_node *st_copy(stmt_node *old) {
46 46
 
47 47
 		case ST_BREAK:
48 48
 			new->brk = NEW(break_node);
49
-			new->brk->val = ex_copy(old->cont->val);
49
+			new->brk->val = ex_copy(old->brk->val);
50 50
 			break;
51 51
 
52 52
 		default:

+ 642
- 0
ser.c Dosyayı Görüntüle

@@ -0,0 +1,642 @@
1
+#include <string.h>
2
+#include <stdlib.h>
3
+
4
+#include "ast.h"
5
+
6
+extern int yydebug;
7
+
8
+char *sol_BytecodeNames[] = {
9
+	"BC_NULL",
10
+	"BC_ST_EXPR",
11
+	"BC_ST_LIST",
12
+	"BC_ST_RET",
13
+	"BC_ST_CONT",
14
+	"BC_ST_BREAK",
15
+	"BC_EX_LIT",
16
+	"BC_EX_LISTGEN",
17
+	"BC_EX_MAPGEN",
18
+	"BC_EX_BINOP",
19
+	"BC_EX_UNOP",
20
+	"BC_EX_INDEX",
21
+	"BC_EX_SETINDEX",
22
+	"BC_EX_ASSIGN",
23
+	"BC_EX_REF",
24
+	"BC_EX_CALL",
25
+	"BC_EX_FUNCDECL",
26
+	"BC_EX_IFELSE",
27
+	"BC_EX_LOOP",
28
+	"BC_EX_ITER",
29
+	"BC_LIT_INT",
30
+	"BC_LIT_FLOAT",
31
+	"BC_LIT_STRING",
32
+	"BC_LIT_NONE",
33
+	"BC_INT",
34
+	"BC_FLOAT",
35
+	"BC_STRING",
36
+	"BC_LIST_ST",
37
+	"BC_LIST_EX",
38
+	"BC_LIST_AS",
39
+	"BC_LIST_ID",
40
+	"BC_LIST_PM",
41
+	"BC_ENDLIST",
42
+};
43
+
44
+void sol_ser_stmt(FILE *io, stmt_node *st) {
45
+	if(!st) {
46
+		fputc(BC_NULL, io);
47
+		return;
48
+	}
49
+	switch(st->type) {
50
+		case ST_EXPR:
51
+			fputc(BC_ST_EXPR, io);
52
+			sol_ser_expr(io, st->expr);
53
+			break;
54
+
55
+		case ST_LIST:
56
+			fputc(BC_ST_LIST, io);
57
+			sol_ser_stl(io, st->stmtlist);
58
+			break;
59
+
60
+		case ST_RET:
61
+			fputc(BC_ST_RET, io);
62
+			sol_ser_expr(io, st->ret->ret);
63
+			break;
64
+
65
+		case ST_CONT:
66
+			fputc(BC_ST_CONT, io);
67
+			sol_ser_expr(io, st->cont->val);
68
+			break;
69
+
70
+		case ST_BREAK:
71
+			fputc(BC_ST_BREAK, io);
72
+			sol_ser_expr(io, st->brk->val);
73
+			break;
74
+
75
+		default:
76
+			printf("WARNING: Unknown statement type to serialize: %d\n", st->type);
77
+			break;
78
+	}
79
+}
80
+
81
+void sol_ser_stl(FILE *io, stmtlist_node *stl) {
82
+	fputc(BC_LIST_ST, io);
83
+	while(stl) {
84
+		sol_ser_stmt(io, stl->stmt);
85
+		stl = stl->next;
86
+	}
87
+	fputc(BC_ENDLIST, io);
88
+}
89
+
90
+void sol_ser_expr(FILE *io, expr_node *ex) {
91
+	if(!ex) {
92
+		fputc(BC_NULL, io);
93
+		return;
94
+	}
95
+	switch(ex->type) {
96
+		case EX_LIT:
97
+			fputc(BC_EX_LIT, io);
98
+			sol_ser_lit(io, ex->lit);
99
+			break;
100
+
101
+		case EX_LISTGEN:
102
+			fputc(BC_EX_LISTGEN, io);
103
+			sol_ser_exl(io, ex->listgen->list);
104
+			break;
105
+
106
+		case EX_MAPGEN:
107
+			fputc(BC_EX_MAPGEN, io);
108
+			sol_ser_asl(io, ex->mapgen->map);
109
+			break;
110
+
111
+		case EX_BINOP:
112
+			fputc(BC_EX_BINOP, io);
113
+			fputc(ex->binop->type - OP_ADD, io);
114
+			sol_ser_expr(io, ex->binop->left);
115
+			sol_ser_expr(io, ex->binop->right);
116
+			break;
117
+
118
+		case EX_UNOP:
119
+			fputc(BC_EX_UNOP, io);
120
+			fputc(ex->unop->type - OP_NEG, io);
121
+			sol_ser_expr(io, ex->unop->expr);
122
+			break;
123
+
124
+		case EX_INDEX:
125
+			fputc(BC_EX_INDEX, io);
126
+			sol_ser_expr(io, ex->index->expr);
127
+			sol_ser_expr(io, ex->index->index);
128
+			break;
129
+
130
+		case EX_SETINDEX:
131
+			fputc(BC_EX_SETINDEX, io);
132
+			sol_ser_expr(io, ex->setindex->expr);
133
+			sol_ser_expr(io, ex->setindex->index);
134
+			sol_ser_expr(io, ex->setindex->value);
135
+			break;
136
+
137
+		case EX_ASSIGN:
138
+			fputc(BC_EX_ASSIGN, io);
139
+			sol_ser_str(io, ex->assign->ident);
140
+			sol_ser_expr(io, ex->assign->value);
141
+			break;
142
+
143
+		case EX_REF:
144
+			fputc(BC_EX_REF, io);
145
+			sol_ser_str(io, ex->ref->ident);
146
+			break;
147
+
148
+		case EX_CALL:
149
+			fputc(BC_EX_CALL, io);
150
+			sol_ser_expr(io, ex->call->expr);
151
+			sol_ser_exl(io, ex->call->args);
152
+			sol_ser_str(io, ex->call->method);
153
+			break;
154
+
155
+		case EX_FUNCDECL:
156
+			fputc(BC_EX_FUNCDECL, io);
157
+			sol_ser_str(io, ex->funcdecl->name);
158
+			sol_ser_pl(io, ex->funcdecl->params);
159
+			sol_ser_expr(io, ex->funcdecl->anno);
160
+			sol_ser_stmt(io, ex->funcdecl->body);
161
+			break;
162
+
163
+		case EX_IFELSE:
164
+			fputc(BC_EX_IFELSE, io);
165
+			sol_ser_expr(io, ex->ifelse->cond);
166
+			sol_ser_stmt(io, ex->ifelse->iftrue);
167
+			sol_ser_stmt(io, ex->ifelse->iffalse);
168
+			break;
169
+
170
+		case EX_LOOP:
171
+			fputc(BC_EX_LOOP, io);
172
+			sol_ser_expr(io, ex->loop->cond);
173
+			sol_ser_stmt(io, ex->loop->loop);
174
+			break;
175
+
176
+		case EX_ITER:
177
+			fputc(BC_EX_ITER, io);
178
+			sol_ser_str(io, ex->iter->var);
179
+			sol_ser_expr(io, ex->iter->iter);
180
+			sol_ser_stmt(io, ex->iter->loop);
181
+			break;
182
+
183
+		default:
184
+			printf("WARNING: Unknown expression type to serialize: %d\n", ex->type);
185
+			break;
186
+	}
187
+}
188
+
189
+void sol_ser_exl(FILE *io, exprlist_node *exn) {
190
+	fputc(BC_LIST_EX, io);
191
+	while(exn) {
192
+		sol_ser_expr(io, exn->expr);
193
+		exn = exn->next;
194
+	}
195
+	fputc(BC_ENDLIST, io);
196
+}
197
+
198
+void sol_ser_asl(FILE *io, assoclist_node *asl) {
199
+	fputc(BC_LIST_AS, io);
200
+	while(asl) {
201
+		if(asl->item) {
202
+			sol_ser_expr(io, asl->item->key);
203
+			sol_ser_expr(io, asl->item->value);
204
+		}
205
+		asl = asl->next;
206
+	}
207
+	fputc(BC_ENDLIST, io);
208
+}
209
+
210
+void sol_ser_idl(FILE *io, identlist_node *idl) {
211
+	fputc(BC_LIST_ID, io);
212
+	while(idl) {
213
+		sol_ser_str(io, idl->ident);
214
+		idl = idl->next;
215
+	}
216
+	fputc(BC_ENDLIST, io);
217
+}
218
+
219
+void sol_ser_pl(FILE *io, paramlist_node *pl) {
220
+	if(!pl) {
221
+		fputc(BC_NULL, io);
222
+		return;
223
+	}
224
+	fputc(BC_LIST_PM, io);
225
+	sol_ser_idl(io, pl->args);
226
+	sol_ser_exl(io, pl->annos);
227
+	sol_ser_idl(io, pl->clkeys);
228
+	sol_ser_exl(io, pl->clvalues);
229
+	sol_ser_str(io, pl->rest);
230
+	fputc(BC_ENDLIST, io);
231
+}
232
+
233
+void sol_ser_lit(FILE *io, lit_node *lit) {
234
+	if(!lit) {
235
+		fputc(BC_NULL, io);
236
+		return;
237
+	}
238
+	switch(lit->type) {
239
+		case LIT_INT:
240
+			fputc(BC_LIT_INT, io);
241
+			sol_ser_int(io, lit->ival);
242
+			break;
243
+
244
+		case LIT_FLOAT:
245
+			fputc(BC_LIT_FLOAT, io);
246
+			sol_ser_float(io, lit->fval);
247
+			break;
248
+
249
+		case LIT_STRING:
250
+			fputc(BC_LIT_STRING, io);
251
+			sol_ser_str(io, lit->str);
252
+			break;
253
+
254
+		case LIT_NONE:
255
+			fputc(BC_LIT_NONE, io);
256
+			break;
257
+
258
+		default:
259
+			printf("WARNING: Unknown literal type to serialize: %d\n", lit->type);
260
+			break;
261
+	}
262
+}
263
+
264
+void sol_ser_str(FILE *io, const char *s) {
265
+	size_t len;
266
+	if(!s) {
267
+		fputc(BC_NULL, io);
268
+		return;
269
+	}
270
+	fputc(BC_STRING, io);
271
+	len = strlen(s);
272
+	fwrite(&len, sizeof(size_t), 1, io);
273
+	fwrite(s, sizeof(char), len, io);
274
+}
275
+
276
+void sol_ser_int(FILE *io, long i) {
277
+	fputc(BC_INT, io);
278
+	fwrite(&i, sizeof(long), 1, io);
279
+}
280
+
281
+void sol_ser_float(FILE *io, double f) {
282
+	fputc(BC_FLOAT, io);
283
+	fwrite(&f, sizeof(double), 1, io);
284
+}
285
+
286
+void *sol_deser_checked(FILE *io, bytecode b) {
287
+	int c = fgetc(io);
288
+	if(c != b && c != BC_NULL) {
289
+		printf("WARNING: Deserialization failed; expected %d, got %d\n", b, c);
290
+	}
291
+	ungetc(c, io);
292
+	return sol_deser(io);
293
+}
294
+
295
+void *sol_deser_stmt(FILE *io) {
296
+	int c = fgetc(io);
297
+	switch(c) {
298
+		default:
299
+			printf("WARNING: Deserialization failed; expected stmt type, got %d\n", c);
300
+			break;
301
+
302
+		case BC_NULL:
303
+		case BC_ST_EXPR:
304
+		case BC_ST_LIST:
305
+		case BC_ST_RET:
306
+		case BC_ST_CONT:
307
+		case BC_ST_BREAK:
308
+			;
309
+	}
310
+	ungetc(c, io);
311
+	return sol_deser(io);
312
+}
313
+
314
+void *sol_deser_expr(FILE *io) {
315
+	int c = fgetc(io);
316
+	switch(c) {
317
+		default:
318
+			printf("WARNING: Deserialization failed; expected expr type, got %d\n", c);
319
+			break;
320
+
321
+		case BC_NULL:
322
+		case BC_EX_LIT:
323
+		case BC_EX_LISTGEN:
324
+		case BC_EX_MAPGEN:
325
+		case BC_EX_BINOP:
326
+		case BC_EX_UNOP:
327
+		case BC_EX_INDEX:
328
+		case BC_EX_SETINDEX:
329
+		case BC_EX_ASSIGN:
330
+		case BC_EX_REF:
331
+		case BC_EX_CALL:
332
+		case BC_EX_FUNCDECL:
333
+		case BC_EX_IFELSE:
334
+		case BC_EX_LOOP:
335
+		case BC_EX_ITER:
336
+			;
337
+	}
338
+	ungetc(c, io);
339
+	return sol_deser(io);
340
+}
341
+
342
+void *sol_deser_lit(FILE *io) {
343
+	int c = fgetc(io);
344
+	switch(c) {
345
+		default:
346
+			printf("WARNING: Deserialization failed; expected lit type, got %d\n", c);
347
+			break;
348
+
349
+		case BC_NULL:
350
+		case BC_LIT_INT:
351
+		case BC_LIT_FLOAT:
352
+		case BC_LIT_STRING:
353
+		case BC_LIT_NONE:
354
+			;
355
+	}
356
+	ungetc(c, io);
357
+	return sol_deser(io);
358
+}
359
+
360
+void *sol_deser(FILE *io) {
361
+	bytecode b = fgetc(io);
362
+	void *obj = NULL, *node = NULL;
363
+
364
+	if(yydebug) {
365
+		fprintf(stderr, "Encountered BC %s", sol_BytecodeNames[b]);
366
+	}
367
+
368
+	switch(b) {
369
+		case BC_NULL:
370
+			return NULL;
371
+			break;
372
+
373
+		case BC_ST_EXPR:
374
+			obj = NEW(stmt_node);
375
+			AS_ST(obj)->type = ST_EXPR;
376
+			AS_ST(obj)->expr = sol_deser_expr(io);
377
+			return obj;
378
+			break;
379
+
380
+		case BC_ST_LIST:
381
+			obj = NEW(stmt_node);
382
+			AS_ST(obj)->type = ST_LIST;
383
+			AS_ST(obj)->stmtlist = sol_deser_checked(io, BC_LIST_ST);
384
+			return obj;
385
+
386
+		case BC_ST_RET:
387
+			obj = NEW(stmt_node);
388
+			AS_ST(obj)->type = ST_RET;
389
+			AS_ST(obj)->ret = NEW(ret_node);
390
+			AS_ST(obj)->ret->ret = sol_deser_expr(io);
391
+			return obj;
392
+
393
+		case BC_ST_CONT:
394
+			obj = NEW(stmt_node);
395
+			AS_ST(obj)->type = ST_CONT;
396
+			AS_ST(obj)->cont = NEW(cont_node);
397
+			AS_ST(obj)->cont->val = sol_deser_expr(io);
398
+			return obj;
399
+
400
+		case BC_ST_BREAK:
401
+			obj = NEW(stmt_node);
402
+			AS_ST(obj)->type = ST_BREAK;
403
+			AS_ST(obj)->brk = NEW(break_node);
404
+			AS_ST(obj)->brk->val = sol_deser_expr(io);
405
+			return obj;
406
+
407
+		case BC_EX_LIT:
408
+			obj = NEW(expr_node);
409
+			AS_EX(obj)->type = EX_LIT;
410
+			AS_EX(obj)->lit = sol_deser_lit(io);
411
+			return obj;
412
+
413
+		case BC_EX_LISTGEN:
414
+			obj = NEW(expr_node);
415
+			AS_EX(obj)->type = EX_LISTGEN;
416
+			AS_EX(obj)->listgen = NEW(listgen_node);
417
+			AS_EX(obj)->listgen->list = sol_deser_checked(io, BC_LIST_EX);
418
+			return obj;
419
+
420
+		case BC_EX_MAPGEN:
421
+			obj = NEW(expr_node);
422
+			AS_EX(obj)->type = EX_MAPGEN;
423
+			AS_EX(obj)->mapgen = NEW(mapgen_node);
424
+			AS_EX(obj)->mapgen->map = sol_deser_checked(io, BC_LIST_AS);
425
+			return obj;
426
+
427
+		case BC_EX_BINOP:
428
+			obj = NEW(expr_node);
429
+			AS_EX(obj)->type = EX_BINOP;
430
+			AS_EX(obj)->binop = NEW(binop_node);
431
+			AS_EX(obj)->binop->type = OP_ADD + fgetc(io);
432
+			AS_EX(obj)->binop->left = sol_deser_expr(io);
433
+			AS_EX(obj)->binop->right = sol_deser_expr(io);
434
+			return obj;
435
+
436
+		case BC_EX_UNOP:
437
+			obj = NEW(expr_node);
438
+			AS_EX(obj)->type = EX_UNOP;
439
+			AS_EX(obj)->unop = NEW(unop_node);
440
+			AS_EX(obj)->unop->type = OP_NEG + fgetc(io);
441
+			AS_EX(obj)->unop->expr = sol_deser_expr(io);
442
+			return obj;
443
+
444
+		case BC_EX_INDEX:
445
+			obj = NEW(expr_node);
446
+			AS_EX(obj)->type = EX_INDEX;
447
+			AS_EX(obj)->index = NEW(index_node);
448
+			AS_EX(obj)->index->expr = sol_deser_expr(io);
449
+			AS_EX(obj)->index->index = sol_deser_expr(io);
450
+			return obj;
451
+
452
+		case BC_EX_SETINDEX:
453
+			obj = NEW(expr_node);
454
+			AS_EX(obj)->type = EX_SETINDEX;
455
+			AS_EX(obj)->setindex = NEW(setindex_node);
456
+			AS_EX(obj)->setindex->expr = sol_deser_expr(io);
457
+			AS_EX(obj)->setindex->index = sol_deser_expr(io);
458
+			AS_EX(obj)->setindex->value = sol_deser_expr(io);
459
+			return obj;
460
+
461
+		case BC_EX_ASSIGN:
462
+			obj = NEW(expr_node);
463
+			AS_EX(obj)->type = EX_ASSIGN;
464
+			AS_EX(obj)->assign = NEW(assign_node);
465
+			AS_EX(obj)->assign->ident = sol_deser_checked(io, BC_STRING);
466
+			AS_EX(obj)->assign->value = sol_deser_expr(io);
467
+			return obj;
468
+
469
+		case BC_EX_REF:
470
+			obj = NEW(expr_node);
471
+			AS_EX(obj)->type = EX_REF;
472
+			AS_EX(obj)->ref = NEW(ref_node);
473
+			AS_EX(obj)->ref->ident = sol_deser_checked(io, BC_STRING);
474
+			return obj;
475
+
476
+		case BC_EX_CALL:
477
+			obj = NEW(expr_node);
478
+			AS_EX(obj)->type = EX_CALL;
479
+			AS_EX(obj)->call = NEW(call_node);
480
+			AS_EX(obj)->call->expr = sol_deser_expr(io);
481
+			AS_EX(obj)->call->args = sol_deser_checked(io, BC_LIST_EX);
482
+			AS_EX(obj)->call->method = sol_deser_checked(io, BC_STRING);
483
+			return obj;
484
+
485
+		case BC_EX_FUNCDECL:
486
+			obj = NEW(expr_node);
487
+			AS_EX(obj)->type = EX_FUNCDECL;
488
+			AS_EX(obj)->funcdecl = NEW(funcdecl_node);
489
+			AS_EX(obj)->funcdecl->name = sol_deser_checked(io, BC_STRING);
490
+			AS_EX(obj)->funcdecl->params = sol_deser_checked(io, BC_LIST_PM);
491
+			AS_EX(obj)->funcdecl->anno = sol_deser_expr(io);
492
+			AS_EX(obj)->funcdecl->body = sol_deser_stmt(io);
493
+			return obj;
494
+
495
+		case BC_EX_IFELSE:
496
+			obj = NEW(expr_node);
497
+			AS_EX(obj)->type = EX_IFELSE;
498
+			AS_EX(obj)->ifelse = NEW(ifelse_node);
499
+			AS_EX(obj)->ifelse->cond = sol_deser_expr(io);
500
+			AS_EX(obj)->ifelse->iftrue = sol_deser_stmt(io);
501
+			AS_EX(obj)->ifelse->iffalse = sol_deser_stmt(io);
502
+			return obj;
503
+
504
+		case BC_EX_LOOP:
505
+			obj = NEW(expr_node);
506
+			AS_EX(obj)->type = EX_LOOP;
507
+			AS_EX(obj)->loop = NEW(loop_node);
508
+			AS_EX(obj)->loop->cond = sol_deser_expr(io);
509
+			AS_EX(obj)->loop->loop = sol_deser_stmt(io);
510
+			return obj;
511
+
512
+		case BC_EX_ITER:
513
+			obj = NEW(expr_node);
514
+			AS_EX(obj)->type = EX_ITER;
515
+			AS_EX(obj)->iter = NEW(iter_node);
516
+			AS_EX(obj)->iter->var = sol_deser_checked(io, BC_STRING);
517
+			AS_EX(obj)->iter->iter = sol_deser_expr(io);
518
+			AS_EX(obj)->iter->loop = sol_deser_stmt(io);
519
+			return obj;
520
+
521
+		case BC_LIT_INT:
522
+			obj = NEW(lit_node);
523
+			AS(obj, lit_node)->type = LIT_INT;
524
+			node = sol_deser_checked(io, BC_INT);
525
+			AS(obj, lit_node)->ival = *AS(node, long);
526
+			free(node);
527
+			return obj;
528
+
529
+		case BC_LIT_FLOAT:
530
+			obj = NEW(lit_node);
531
+			AS(obj, lit_node)->type = LIT_FLOAT;
532
+			node = sol_deser_checked(io, BC_FLOAT);
533
+			AS(obj, lit_node)->fval = *AS(node, double);
534
+			free(node);
535
+			return obj;
536
+
537
+		case BC_LIT_STRING:
538
+			obj = NEW(lit_node);
539
+			AS(obj, lit_node)->type = LIT_STRING;
540
+			AS(obj, lit_node)->str = sol_deser_checked(io, BC_STRING);
541
+			return obj;
542
+
543
+		case BC_LIT_NONE:
544
+			obj = NEW(lit_node);
545
+			AS(obj, lit_node)->type = LIT_NONE;
546
+			return obj;
547
+
548
+		case BC_INT:
549
+			obj = NEW(long);
550
+			fread(obj, sizeof(long), 1, io);
551
+			return obj;
552
+
553
+		case BC_FLOAT:
554
+			obj = NEW(double);
555
+			fread(obj, sizeof(double), 1, io);
556
+			return obj;
557
+
558
+		case BC_STRING:
559
+			node = NEW(size_t);
560
+			fread(node, sizeof(size_t), 1, io);
561
+			obj = malloc(*AS(node, size_t) + 1);
562
+			fread(obj, sizeof(char), *AS(node, size_t), io);
563
+			AS(obj, char)[*AS(node, size_t)] = 0;
564
+			free(node);
565
+			return obj;
566
+
567
+		case BC_LIST_ST:
568
+			while((b = fgetc(io)) != BC_ENDLIST) {
569
+				ungetc(b, io);
570
+				if(!node) {
571
+					node = NEW(stmtlist_node);
572
+					obj = node;
573
+				} else {
574
+					AS(node, stmtlist_node)->next = NEW(stmtlist_node);
575
+					node = AS(node, stmtlist_node)->next;
576
+				}
577
+				AS(node, stmtlist_node)->stmt = sol_deser_stmt(io);
578
+			}
579
+			return obj;
580
+
581
+		case BC_LIST_EX:
582
+			while((b = fgetc(io)) != BC_ENDLIST) {
583
+				ungetc(b, io);
584
+				if(!node) {
585
+					node = NEW(exprlist_node);
586
+					obj = node;
587
+				} else {
588
+					AS(node, exprlist_node)->next = NEW(exprlist_node);
589
+					node = AS(node, exprlist_node)->next;
590
+				}
591
+				AS(node, exprlist_node)->expr = sol_deser_expr(io);
592
+			}
593
+			return obj;
594
+
595
+		case BC_LIST_AS:
596
+			while((b = fgetc(io)) != BC_ENDLIST) {
597
+				ungetc(b, io);
598
+				if(!node) {
599
+					node = NEW(assoclist_node);
600
+					obj = node;
601
+				} else {
602
+					AS(node, assoclist_node)->next = NEW(assoclist_node);
603
+					node = AS(node, assoclist_node)->next;
604
+				}
605
+				AS(node, assoclist_node)->item = NEW(associtem_node);
606
+				AS(node, assoclist_node)->item->key = sol_deser_expr(io);
607
+				AS(node, assoclist_node)->item->value = sol_deser_expr(io);
608
+			}
609
+			return obj;
610
+
611
+		case BC_LIST_ID:
612
+			while((b = fgetc(io)) != BC_ENDLIST) {
613
+				ungetc(b, io);
614
+				if(!node) {
615
+					node = NEW(identlist_node);
616
+					obj = node;
617
+				} else {
618
+					AS(node, identlist_node)->next = NEW(identlist_node);
619
+					node = AS(node, identlist_node)->next;
620
+				}
621
+				AS(node, identlist_node)->ident = sol_deser_checked(io, BC_STRING);
622
+			}
623
+			return obj;
624
+
625
+		case BC_LIST_PM:
626
+			obj = NEW(paramlist_node);
627
+			AS(obj, paramlist_node)->args = sol_deser_checked(io, BC_LIST_ID);
628
+			AS(obj, paramlist_node)->annos = sol_deser_checked(io, BC_LIST_EX);
629
+			AS(obj, paramlist_node)->clkeys = sol_deser_checked(io, BC_LIST_ID);
630
+			AS(obj, paramlist_node)->clvalues = sol_deser_checked(io, BC_LIST_EX);
631
+			AS(obj, paramlist_node)->rest = sol_deser_checked(io, BC_STRING);
632
+			if(fgetc(io) != BC_ENDLIST) {
633
+				printf("WARNING: Missed ENDLIST for paramlist_node\n");
634
+			}
635
+			return obj;
636
+
637
+		default:
638
+			printf("WARNING: Illegal bytecode %d\n", b);
639
+			break;
640
+	}
641
+	return NULL;
642
+}

+ 28
- 6
solrun.c Dosyayı Görüntüle

@@ -6,9 +6,9 @@ int main(int argc, char **argv) {
6 6
 	stmt_node *program;
7 7
 	sol_state_t state;
8 8
 	char *c;
9
-	int printtree = 0, clean = 1;
10
-	FILE *prgstream = stdin;
11
-	int result = 0;
9
+	int printtree = 0, clean = 1, argidx = 2;
10
+	FILE *prgstream = stdin, *compstream = NULL;
11
+	int result = 0, compile = 0, compiled = 0;
12 12
 
13 13
 	state.features = 0;
14 14
 
@@ -25,16 +25,29 @@ int main(int argc, char **argv) {
25 25
 					break;
26 26
 
27 27
 				case 'r':
28
-					if(argc < 2) {
28
+					if(argc < argidx) {
29 29
 						printf("r option requires file\n");
30 30
 						return 2;
31 31
 					}
32
-					prgstream = fopen(argv[2], "r");
32
+					prgstream = fopen(argv[argidx++], "r");
33 33
 					break;
34 34
 
35 35
 				case 'i':
36 36
 					state.features |= SOL_FT_NO_USR_INIT;
37 37
 					break;
38
+
39
+				case 'c':
40
+					compile = 1;
41
+					if(argc < argidx) {
42
+						printf("c option requires file\n");
43
+						return 2;
44
+					}
45
+					compstream = fopen(argv[argidx++], "wb");
46
+					break;
47
+
48
+				case 'C':
49
+					compiled = 1;
50
+					break;
38 51
 			}
39 52
 			c++;
40 53
 		}
@@ -45,7 +58,11 @@ int main(int argc, char **argv) {
45 58
 		return 2;
46 59
 	}
47 60
 
48
-	program = sol_compile_file(prgstream);
61
+	if(compiled) {
62
+		program = sol_deser_stmt(prgstream);
63
+	} else {
64
+		program = sol_compile_file(prgstream);
65
+	}
49 66
 	
50 67
 	if(!program) {
51 68
 		printf("NULL program (probably a syntax error)\n");
@@ -56,6 +73,11 @@ int main(int argc, char **argv) {
56 73
 		fclose(prgstream);
57 74
 	}
58 75
 
76
+	if(compile) {
77
+		sol_ser_stmt(compstream, program);
78
+		return 0;
79
+	}
80
+
59 81
 	if(!sol_state_init(&state)) {
60 82
 		printf("State init error (internal bug)\n");
61 83
 		result = 2;

Loading…
İptal
Kaydet