-
Notifications
You must be signed in to change notification settings - Fork 3
/
scc_parse.y
2164 lines (1819 loc) · 42.3 KB
/
scc_parse.y
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ScummC
* Copyright (C) 2004-2006 Alban Bedel
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file scc_parse.y
* @ingroup scc
* @brief ScummC compiler
*/
%{
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "scc_fd.h"
#include "scc_util.h"
#include "scc_parse.h"
#include "scc_ns.h"
#include "scc_img.h"
#include "scc_cost.h"
#include "scc.h"
#include "scc_roobj.h"
#include "scc_code.h"
#include "scc_param.h"
#include "scc_parse.tab.h"
#include "scc_lex.h"
#include "scc_help.h"
#define YYERROR_VERBOSE 1
struct scc_parser {
// targeted vm version
scc_target_t* target;;
scc_lex_t* lex;
scc_ns_t* ns;
scc_roobj_t* roobj_list;
scc_roobj_t* roobj;
scc_roobj_obj_t* obj;
int local_vars;
int local_scr;
int cycl;
// ressources include paths
char** res_path;
// deps
char do_deps;
int num_deps;
char** deps;
};
#define LEX_PARAM sccp->lex
// redefined the function names
#define yyparse scc_parser_parse_internal
#define yylex scc_lex_lex
#define yyerror(loc,val,msg) scc_parser_error(val,loc,msg)
int scc_parser_error (scc_parser_t* p,YYLTYPE *llocp, const char *s);
void scc_parser_add_dep(scc_parser_t* p, char* dep);
static void scc_parser_find_res(scc_parser_t* p, char** file_ptr);
#define SCC_LIST_ADD(list,last,c) if(c){ \
if(last) last->next = c; \
else list = c; \
for(last = c ; last && last->next ; last = last->next); \
}
#define SCC_BOP(d,bop,a,cop,b) { \
if(a->type == SCC_ST_VAL && \
b->type == SCC_ST_VAL) { \
a->val.i = ((int16_t)a->val.i) bop ((int16_t)b->val.i); \
free(b); \
d = a; \
} else { \
d = calloc(1,sizeof(scc_statement_t)); \
d->type = SCC_ST_OP; \
d->val.o.type = SCC_OT_BINARY; \
d->val.o.op = cop; \
d->val.o.argc = 2; \
d->val.o.argv = a; \
a->next = b; \
} \
}
#define SCC_ABORT(at,msg...) { \
scc_log(LOG_ERR,"%s: ",scc_lex_get_file(sccp->lex)); \
scc_log(LOG_ERR,msg); \
YYABORT; \
}
// output filename
static char* scc_output = NULL;
scc_func_t* scc_get_func(scc_parser_t* p, char* sym) {
int i,j;
if(!sym) return NULL;
for(i = 0 ; p->target->func_list[i] ; i++) {
scc_func_t* list = p->target->func_list[i];
for(j = 0 ; list[j].sym ; j++) {
if(strcmp(sym,list[j].sym)) continue;
return &list[j];
}
}
return NULL;
}
char* scc_statement_check_func(scc_call_t* c) {
int n,min_argc = c->func->argc;
scc_statement_t* a;
// should be big enouth
static char func_err[2048];
while(min_argc > 0 && (c->func->argt[min_argc-1] & SCC_FA_DEFAULT))
min_argc--;
if(c->argc > c->func->argc || c->argc < min_argc) {
sprintf(func_err,"Function %s needs %d args, %d found.\n",
c->func->sym,c->func->argc,c->argc);
return func_err;
}
for(n = 0, a = c->argv ; a ; n++, a = a->next) {
if(c->func->argt[n] == SCC_FA_VAL) {
if(a->type == SCC_ST_STR ||
a->type == SCC_ST_LIST) {
sprintf(func_err,"Argument %d of call to %s is of the wrong type.\n",
n+1,c->func->sym);
return func_err;
}
} else if(c->func->argt[n] == SCC_FA_ARRAY) {
if(a->type != SCC_ST_VAR ||
!(a->val.v.r->subtype & SCC_VAR_ARRAY)) {
sprintf(func_err,"Argument %d of call to %s must be an array variable.\n",
n+1,c->func->sym);
return func_err;
}
} else if(c->func->argt[n] == SCC_FA_LIST &&
a->type != SCC_ST_LIST) {
sprintf(func_err,"Argument %d of %s must be a list.\n",
n+1,c->func->sym);
return func_err;
} else if(c->func->argt[n] == SCC_FA_STR &&
a->type != SCC_ST_STR) {
sprintf(func_err,"Argument %d of %s must be a string.\n",
n+1,c->func->sym);
return func_err;
}
}
return NULL;
}
%}
%pure-parser
%parse-param {struct scc_parser* sccp}
%lex-param {scc_lex_t* LEX_PARAM}
%union {
int integer;
char* str;
char** strlist;
scc_symbol_t* sym;
scc_statement_t* st;
scc_instruct_t* inst;
scc_scr_arg_t* arg;
scc_script_t* scr;
scc_str_t* strvar;
int* intlist;
scc_verb_script_t* vscr;
}
// Operators, the order is defining the priority.
%token AADD
%token ASUB
%token AMUL
%token ADIV
%token AAND
%token AOR
%token INC
%token DEC
%token PREINC
%token POSTINC
%token PREDEC
%token POSTDEC
%right <integer> ASSIGN
%right <integer> '?' ':'
%left <integer> LOR
%left <integer> LAND
%nonassoc <integer> IS
%left <integer> '|'
%left <integer> '&'
%left <integer> NEQ EQ
%left <integer> '>' GE
%left <integer> '<' LE
%left <integer> '-' '+'
%left <integer> '*' '/'
%right <integer> NEG
%right <integer> '!'
%nonassoc <integer> SUFFIX
%right PREFIX
%left POSTFIX
%token <integer> INTEGER
%token <integer> RESTYPE
%token <str> STRING
%token <strvar> STRVAR
%token <str> SYM
%token <integer> TYPE
%token <integer> NUL
%token <integer> BRANCH RETURN
%token ROOM
%token OBJECT
%token NS
%token SCRIPT
%token VERB
%token ACTOR
%token VOICE
%token CYCL
%nonassoc <integer> IF
%nonassoc ELSE
%nonassoc FOR
%nonassoc <integer> WHILE
%nonassoc DO
%nonassoc SWITCH
%nonassoc CASE
%nonassoc DEFAULT
%nonassoc CUTSCENE
%nonassoc CLASS
%nonassoc TRY
%nonassoc OVERRIDE
%token <integer> SCRTYPE
%token ERROR
%type <strvar> str
%type <strvar> string
%type <integer> location
%type <st> dval
%type <st> cargs
%type <st> var
%type <st> call
%type <st> isargs
%type <st> isarglist
%type <st> isarg
%type <sym> vdecl
%type <st> statement
%type <st> statements
%type <st> opt_statements
%type <inst> instruct
%type <inst> ifblock
%type <st> caseval
%type <st> caseblock
%type <inst> switchblock
%type <inst> loopblock
%type <inst> cutsceneblock
%type <inst> block
%type <inst> instructions
%type <inst> oneinstruct
%type <inst> dobody
%type <inst> body
%type <inst> loophead
%type <str> label
%type <inst> dohead
%type <inst> switchhead
%type <scr> scriptbody
%type <inst> verbcode
%type <sym> verbentry
%type <vscr> verbsblock
%type <integer> gvardecl
%type <integer> gresdecl
%type <integer> groomresdecl
%type <integer> globalres
%type <integer> roomres
%type <integer> resdecl
%type <str> resdef
%type <integer> scripttype
%type <arg> scriptargs
%type <sym> roomscrdecl
%type <sym> roomobjdecl
%type <sym> cycledecl
%type <sym> voicedecl
%type <strlist> zbufs
%type <intlist> synclist
%type <integer> typemod
%type <integer> natural
%%
srcfile: /* empty */
| srcs
;
srcs: src
| srcs src
;
src: room
| gdecl
;
gdecl: gvardecl ';'
{}
| gresdecl ';'
{}
| groomresdecl ';'
{}
| groomdecl ';'
;
gvardecl: TYPE typemod SYM location
{
if($1 == SCC_VAR_BIT && !$2)
scc_ns_decl(sccp->ns,NULL,$3,SCC_RES_BVAR,$1,$4);
else
scc_ns_decl(sccp->ns,NULL,$3,SCC_RES_VAR,$1 | $2,$4);
$$ = $1;
}
| gvardecl ',' typemod SYM location
{
if($1 == SCC_VAR_BIT && !$3)
scc_ns_decl(sccp->ns,NULL,$4,SCC_RES_BVAR,$1,$5);
else
scc_ns_decl(sccp->ns,NULL,$4,SCC_RES_VAR,$1 | $3,$5);
$$ = $1;
}
;
typemod: /* nothing */
{
$$ = 0;
}
| '*'
{
$$ = SCC_VAR_ARRAY;
}
;
// room must be put outside of globalres
// otherwise it conflict with roombdecl
groomdecl: ROOM SYM location
{
scc_ns_decl(sccp->ns,NULL,$2,SCC_RES_ROOM,0,$3);
}
| groomdecl ',' SYM location
{
scc_ns_decl(sccp->ns,NULL,$3,SCC_RES_ROOM,0,$4);
}
;
gresdecl: globalres SYM location
{
scc_ns_decl(sccp->ns,NULL,$2,$1,0,$3);
}
| gresdecl ',' SYM location
{
scc_ns_decl(sccp->ns,NULL,$3,$1,0,$4);
}
;
globalres: ACTOR
{
$$ = SCC_RES_ACTOR;
}
| VERB
{
$$ = SCC_RES_VERB;
}
| CLASS
{
$$ = SCC_RES_CLASS;
}
;
// cost, sound, chset, object, script, voice, cycl
groomresdecl: roomres SYM NS SYM location
{
scc_ns_decl(sccp->ns,$2,$4,$1,0,$5);
$$ = $1; // hack to propagte the type
}
| groomresdecl ',' SYM NS SYM location
{
scc_ns_decl(sccp->ns,$3,$5,$1,0,$6);
}
;
roomres: RESTYPE
| OBJECT
{
$$ = SCC_RES_OBJ;
}
| SCRIPT
{
$$ = SCC_RES_SCR;
}
| VOICE
{
$$ = SCC_RES_VOICE;
}
| CYCL
{
$$ = SCC_RES_CYCL;
}
;
// This allow us to have the room declared before we parse the body.
// At this point the roobj object is created.
roombdecl: ROOM SYM location
{
scc_symbol_t* sym = scc_ns_decl(sccp->ns,NULL,$2,SCC_RES_ROOM,0,$3);
scc_ns_get_rid(sccp->ns,sym);
scc_ns_push(sccp->ns,sym);
sccp->roobj = scc_roobj_new(sccp->target,sym);
}
;
// Here we should have the whole room.
room: roombdecl roombody
{
scc_log(LOG_DBG,"Room done :)\n");
sccp->local_scr = sccp->target->max_global_scr;
memset(&sccp->ns->as[SCC_RES_LSCR],0,0x10000/8);
sccp->cycl = 1;
scc_ns_clear(sccp->ns,SCC_RES_CYCL);
scc_ns_pop(sccp->ns);
if(!sccp->roobj->scr &&
!sccp->roobj->lscr &&
!sccp->roobj->obj &&
!sccp->roobj->res &&
!sccp->roobj->cycl &&
!sccp->roobj->image) {
scc_log(LOG_DBG,"Room is empty, only declarations.\n");
scc_roobj_free(sccp->roobj);
} else {
sccp->roobj->next = sccp->roobj_list;
sccp->roobj_list = sccp->roobj;
}
sccp->roobj = NULL;
}
;
roombody: '{' '}'
| '{' roombodyentries '}'
//| '{' roomdecls '}'
| '{' roomdecls roombodyentries '}'
;
roombodyentries: roombodyentry
| roombodyentries roombodyentry
;
// scripts, both local and global
roombodyentry: roomscrdecl '{' scriptbody '}'
{
if(!$1->rid) scc_ns_get_rid(sccp->ns,$1);
if($3) {
$3->sym = $1;
scc_roobj_add_scr(sccp->roobj,$3);
}
scc_ns_clear(sccp->ns,SCC_RES_LVAR);
scc_ns_pop(sccp->ns);
}
// forward declaration
| roomscrdecl ';'
{
// well :)
scc_ns_clear(sccp->ns,SCC_RES_LVAR);
scc_ns_pop(sccp->ns);
}
// objects
| roomobjdecl '{' objectparams objectverbs '}'
{
if(!$1->rid) scc_ns_get_rid(sccp->ns,$1);
// add the obj to the room
scc_roobj_add_obj(sccp->roobj,sccp->obj);
sccp->obj = NULL;
}
// forward declaration
| roomobjdecl ';'
{
scc_roobj_obj_free(sccp->obj);
sccp->obj = NULL;
}
| voicedef ';'
{}
| voicedecl ';'
{}
| cycledef ';'
{}
| cycledecl ';'
{}
| resdecl ';'
{}
| gvardecl ';'
{}
;
roomscrdecl: scripttype SCRIPT SYM '(' scriptargs ')' location
{
scc_scr_arg_t* a = $5;
scc_symbol_t* s;
s = scc_ns_decl(sccp->ns,NULL,$3,$1,0,$7);
if(!s) SCC_ABORT(@3,"Failed to declare script %s.\n",$3);
if($1 == SCC_RES_LSCR && s->addr < 0 &&
strcmp($3,"entry") && strcmp($3,"exit"))
if(!scc_ns_alloc_sym_addr(sccp->ns,s,&sccp->local_scr))
SCC_ABORT(@3,"Failed to allocate local script address.\n");
scc_ns_push(sccp->ns,s);
// declare the arguments
sccp->local_vars = 0;
while(a) {
scc_ns_decl(sccp->ns,NULL,a->sym,SCC_RES_LVAR,a->type,sccp->local_vars);
sccp->local_vars++;
a = a->next;
}
$$ = s;
}
;
roomobjdecl: OBJECT SYM location
{
scc_symbol_t* sym;
if(sccp->obj)
SCC_ABORT(@1,"Something went wrong with object declarations.\n");
sym = scc_ns_decl(sccp->ns,NULL,$2,SCC_RES_OBJ,0,$3);
if(!sym) SCC_ABORT(@1,"Failed to declare object %s.\n",$2);
sccp->obj = scc_roobj_obj_new(sym);
$$ = sym;
}
;
// the basic room parameters such as image, box, zplanes, etc
roomdecls: roomdecl
| roomdecls roomdecl
;
roomdecl: SYM ASSIGN STRING ';'
{
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for parameter setting.\n");
scc_parser_find_res(sccp,&$3);
if(!scc_roobj_set_param(sccp->roobj,sccp->ns,$1,$3))
SCC_ABORT(@1,"Failed to set room %s.\n",$1);
// add dep
if(sccp->do_deps) scc_parser_add_dep(sccp,$3);
}
| SYM ASSIGN '{' zbufs '}' ';'
{
int i;
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for parameter setting.\n");
for(i = 0 ; $4[i] ; i++) {
if($4[i][0] == '\0') continue;
if(!scc_roobj_set_zplane(sccp->roobj,i+1,$4[i]))
SCC_ABORT(@1,"Failed to set room zplane %d.\n",i+1);
}
}
| SYM ASSIGN INTEGER ';'
{
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for parameter setting.\n");
if(strcmp($1,"trans"))
SCC_ABORT(@1,"Rooms have no parameter named %s.\n",$1);
if($3 < 0 || $3 > 255)
SCC_ABORT(@3,"Invalid transparent color index: %d\n",$3);
sccp->roobj->trans = $3;
}
;
// The optional room resources like cycle, voice, charset, etc
cycledef: cycledecl ASSIGN '{' INTEGER ',' INTEGER ',' INTEGER ',' INTEGER '}'
{
if($2 != '=')
SCC_ABORT(@3,"Invalid operator for cycle declaration.\n");
if(!scc_roobj_add_cycl(sccp->roobj,$1,$4,$6,$8,$10))
SCC_ABORT(@1,"Failed to add cycl.\n");
}
;
cycledecl: CYCL SYM location
{
$$ = scc_ns_decl(sccp->ns,NULL,$2,SCC_RES_CYCL,0,$3);
if(!$$) SCC_ABORT(@1,"Cycl declaration failed.\n");
if($$->addr < 0 && !scc_ns_alloc_sym_addr(sccp->ns,$$,&sccp->cycl))
SCC_ABORT(@1,"Failed to allocate cycle address.\n");
}
;
voicedef: voicedecl ASSIGN '{' STRING ',' synclist '}'
{
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for voice declaration.\n");
if(!$1->rid) scc_ns_get_rid(sccp->ns,$1);
scc_parser_find_res(sccp,&$4);
if(!scc_roobj_add_voice(sccp->roobj,$1,$4,$6[0],$6+1))
SCC_ABORT(@1,"Failed to add voice.");
if(sccp->do_deps) scc_parser_add_dep(sccp,$4);
free($6);
}
| voicedecl ASSIGN '{' STRING '}'
{
if($2 != '=')
SCC_ABORT(@3,"Invalid operator for voice declaration.\n");
if(!$1->rid) scc_ns_get_rid(sccp->ns,$1);
scc_parser_find_res(sccp,&$4);
if(!scc_roobj_add_voice(sccp->roobj,$1,$4,0,NULL))
SCC_ABORT(@1,"Failed to add voice.");
if(sccp->do_deps) scc_parser_add_dep(sccp,$4);
}
;
voicedecl: VOICE SYM
{
$$ = scc_ns_decl(sccp->ns,NULL,$2,SCC_RES_VOICE,0,-1);
if(!$$) SCC_ABORT(@1,"Declaration failed.\n");
}
;
synclist: INTEGER
{
$$ = malloc(2*sizeof(int));
$$[0] = 1;
$$[1] = $1;
}
| synclist ',' INTEGER
{
int l = $1[0]+1;
$$ = realloc($1,(l+1)*sizeof(int));
$$[l] = $3;
$$[0] = l;
}
;
// generic resource declaration/definition
resdecl: RESTYPE SYM location resdef
{
scc_symbol_t* r = scc_ns_decl(sccp->ns,NULL,$2,$1,0,$3);
if($4) {
scc_parser_find_res(sccp,&$4);
// then we need to add that to the roobj
if(!sccp->do_deps && !scc_roobj_add_res(sccp->roobj,r,$4))
SCC_ABORT(@2,"Failed to declare %s.\n",$2);
if(sccp->do_deps) scc_parser_add_dep(sccp,$4);
if(!r->rid) scc_ns_get_rid(sccp->ns,r);
}
$$ = $1; // propagate type
}
| resdecl ',' SYM location resdef
{
scc_symbol_t* r = scc_ns_decl(sccp->ns,NULL,$3,$1,0,$4);
if($5) {
scc_parser_find_res(sccp,&$5);
// then we need to add that to the roobj
if(!sccp->do_deps && !scc_roobj_add_res(sccp->roobj,r,$5))
SCC_ABORT(@3,"Failed to declare %s.\n",$3);
if(sccp->do_deps) scc_parser_add_dep(sccp,$5);
if(!r->rid) scc_ns_get_rid(sccp->ns,r);
}
$$ = $1; // propagate type
}
;
resdef: /* NOTHING */
{
$$ = NULL;
}
| ASSIGN STRING
{
if($1 != '=')
SCC_ABORT(@1,"Invalid operator for resource definition.\n");
$$ = $2;
}
;
// params chain, we can't use the same as the room
// bcs we don't want ressource declaration here
objectparams: objectparam ';'
| objectparams objectparam ';'
;
// used by objects
objectparam: SYM ASSIGN STRING
{
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for parameter setting.\n");
if(!scc_roobj_obj_set_param(sccp->obj,$1,$3))
SCC_ABORT(@1,"Failed to set object parameter.\n");
}
| SYM ASSIGN natural
{
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for parameter setting.\n");
if(!scc_roobj_obj_set_int_param(sccp->obj,$1,$3))
SCC_ABORT(@1,"Failed to set object parameter.\n");
}
| SYM ASSIGN '{' imgdecls '}'
{
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for parameter setting.\n");
// bitch on the keyword
if(strcmp($1,"states"))
SCC_ABORT(@1,"Expected \"images\".\n");
}
| SYM ASSIGN SYM
{
scc_symbol_t* sym;
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for parameter setting.\n");
if(!strcmp($1,"owner")) {
sym = scc_ns_get_sym(sccp->ns,NULL,$3);
if(!sym)
SCC_ABORT(@3,"%s is not a declared actor.\n",$3);
if(sym->type != SCC_RES_ACTOR)
SCC_ABORT(@3,"%s is not an actor.\n",sym->sym);
sccp->obj->owner = sym;
} else if(!strcmp($1,"parent")) {
sym = scc_ns_get_sym(sccp->ns,NULL,$3);
if(!sym)
SCC_ABORT(@3,"%s is not a declared object.\n",$3);
if(sym->type != SCC_RES_OBJ)
SCC_ABORT(@3,"%s is not an object.\n",sym->sym);
if(sym->parent != sccp->roobj->sym)
SCC_ABORT(@3,"%s doesn't belong to room %s.\n",sym->sym,sccp->roobj->sym->sym);
sccp->obj->parent = sym;
} else
SCC_ABORT(@1,"Expected 'owner' or 'parent'.\n");
}
| CLASS ASSIGN '{' classlist '}'
{
if($2 != '=')
SCC_ABORT(@2,"Invalid operator for parameter setting.\n");
}
;
classlist: SYM
{
scc_symbol_t* sym = scc_ns_get_sym(sccp->ns,NULL,$1);
if(!sym)
SCC_ABORT(@1,"%s is not a declared class.\n",$1);
if(sym->type != SCC_RES_CLASS)
SCC_ABORT(@1,"%s is not a class in the current context.\n",$1);
// allocate an rid
if(!sym->rid) scc_ns_get_rid(sccp->ns,sym);
if(!scc_roobj_obj_set_class(sccp->obj,sym))
SCC_ABORT(@1,"Failed to set object class.\n");
}
| classlist ',' SYM
{
scc_symbol_t* sym = scc_ns_get_sym(sccp->ns,NULL,$3);
if(!sym)
SCC_ABORT(@3,"%s is not a declared class.\n",$3);
if(sym->type != SCC_RES_CLASS)
SCC_ABORT(@3,"%s is not a class in the current context.\n",$3);
// allocate an rid
if(!sym->rid) scc_ns_get_rid(sccp->ns,sym);
if(!scc_roobj_obj_set_class(sccp->obj,sym))
SCC_ABORT(@3,"Failed to set object class.\n");
}
;
imgdecls: imgdecl
| imgdecls ',' imgdecl
;
imgdecl: '{' natural ',' natural ',' STRING '}'
{
scc_parser_find_res(sccp,&$6);
if(!scc_roobj_obj_add_state(sccp->obj,$2,$4,$6,NULL))
SCC_ABORT(@1,"Failed to add room state\n");
if(sccp->do_deps) scc_parser_add_dep(sccp,$6);
}
| '{' natural ',' natural ',' STRING ',' '{' zbufs '}' '}'
{
scc_parser_find_res(sccp,&$6);
if(!scc_roobj_obj_add_state(sccp->obj,$2,$4,$6,$9))
SCC_ABORT(@2,"Failed to add room state.\n");
if(sccp->do_deps) scc_parser_add_dep(sccp,$6);
}
;
zbufs: STRING
{
scc_parser_find_res(sccp,&$1);
scc_parser_add_dep(sccp,$1);
$$ = malloc(2*sizeof(char*));
$$[0] = $1;
$$[1] = NULL;
}
| zbufs ',' STRING
{
int l;
scc_parser_find_res(sccp,&$3);
scc_parser_add_dep(sccp,$3);
for(l = 0 ; $1[l] ; l++);
$$ = realloc($1,(l+2)*sizeof(char*));
$$[l] = $3;
$$[l+1] = NULL;
}
;
objectverbs: /* nothing */
{
}
| verbentrydecl '{' vardecl verbsblock '}'
{
scc_verb_script_t* v,*l;
scc_script_t* scr;
for(v = $4 ; v ; l = v, v = v->next,free(l) ) {
if(!sccp->do_deps && v->inst)
scr = scc_script_new(sccp->ns,v->inst,SCC_OP_VERB_RET,v->next ? 0 : 1);
else
scr = calloc(1,sizeof(scc_script_t));
scr->sym = v->sym;
if(!scc_roobj_obj_add_verb(sccp->obj,scr))
SCC_ABORT(@1,"Failed to add verb %s.\n",v->sym ? v->sym->sym : "default");
}
scc_ns_clear(sccp->ns,SCC_RES_LVAR);
scc_ns_pop(sccp->ns);
}
;
verbentrydecl: VERB '(' scriptargs ')'
{
scc_scr_arg_t* a = $3;
// push the obj for the local vars
scc_ns_push(sccp->ns,sccp->obj->sym);
// declare the arguments
sccp->local_vars = 0;
while(a) {
scc_ns_decl(sccp->ns,NULL,a->sym,SCC_RES_LVAR,a->type,sccp->local_vars);
sccp->local_vars++;
a = a->next;
}
}
;
verbsblock: verbentry verbcode
{
$$ = calloc(1,sizeof(scc_verb_script_t));
$$->sym = $1;
$$->inst = $2;
}
| verbsblock verbentry verbcode
{
$$ = $1;
for(; $1 ; $1 = $1->next) {
if($1->sym == $2)
SCC_ABORT(@2,"Verb %s is already defined.\n",
$2 ? $2->sym : "default");
if(!$1->next) break;
}
$1->next = calloc(1,sizeof(scc_verb_script_t));
$1 = $1->next;
$1->sym = $2;
$1->inst = $3;
}
;
verbcode: /* nothing */
{
$$ = NULL;
}
| instructions
;
verbentry: CASE SYM ':'
{
scc_symbol_t* sym = scc_ns_get_sym(sccp->ns,NULL,$2);
if(!sym)
SCC_ABORT(@1,"%s is not a declared verb.\n",$2);
if(sym->type != SCC_RES_VERB)
SCC_ABORT(@1,"%s is not a verb in the current context.\n",$2);
// allocate an rid
if(!sym->rid) scc_ns_get_rid(sccp->ns,sym);
$$ = sym;
}
| DEFAULT ':'
{
$$ = NULL;
}
;
// script can be local or global. Dunno yet what will be default,
// probably global.
scripttype: /* empty */
{
$$ = SCC_RES_SCR;
}
| SCRTYPE
{
$$ = $1;
}
;
scriptargs: /* */
{
$$ = NULL;
}