summaryrefslogtreecommitdiffstats
path: root/dimension/directives.rules
blob: e199fe333a0f1723eb5f023982f3c917b5d0c037 (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
#line 2 "directives.rules"

/*
 * Start symbol
 */

LANGUAGE_DIRECTIVE: "#include" STRING {
                    dmnsn_declare_symbol(symtable, "$include", $2);
                    dmnsn_delete_astnode($2);
                  }
                  | "#declare" IDENTIFIER "=" RVALUE {
                    dmnsn_declare_symbol(symtable, $2.ptr, $4);
                    dmnsn_delete_astnode($2);
                    dmnsn_delete_astnode($4);
                  }
                  | "#local" IDENTIFIER "=" RVALUE {
                    dmnsn_local_symbol(symtable, $2.ptr, $4);
                    dmnsn_delete_astnode($2);
                    dmnsn_delete_astnode($4);
                  }
                  | "#undef" IDENTIFIER {
                    dmnsn_undef_symbol(symtable, $2.ptr);
                    dmnsn_delete_astnode($2);
                  }
                  | "#if" "(" CONDITIONAL ")" {
                    dmnsn_astnode cond = dmnsn_eval($3, symtable);
                    dmnsn_delete_astnode($3);

                    if (cond.type == DMNSN_AST_NONE) {
                      dmnsn_delete_astnode(cond);
                      YYERROR;
                    }

                    dmnsn_local_symbol(symtable, "$cond", cond);
                    dmnsn_delete_astnode(cond);
                  }
                  | "#ifdef" "(" IDENTIFIER ")" {
                    dmnsn_astnode *node = dmnsn_find_symbol(symtable, $3.ptr);
                    dmnsn_local_symbol(symtable, "$cond",
                                       dmnsn_new_ast_integer(node ? 1 : 0));
                    dmnsn_delete_astnode($3);
                  }
                  | "#ifndef" "(" IDENTIFIER ")" {
                    dmnsn_astnode *node = dmnsn_find_symbol(symtable, $3.ptr);
                    dmnsn_local_symbol(symtable, "$cond",
                                       dmnsn_new_ast_integer(node ? 0 : 1));
                    dmnsn_delete_astnode($3);
                  }
                  | "#version" FLOAT ";" {
                    dmnsn_diagnostic(@$, "WARNING: POV-Ray #version backwards-"
                                         "compatibility not supported");
                    dmnsn_delete_astnode($2);
                  }
                  | "#debug" STRING {
                    fprintf(stderr, "%s\n", (const char *)$2.ptr);
                    dmnsn_delete_astnode($2);
                  }
                  | "#warning" STRING {
                    dmnsn_diagnostic(@$, "WARNING: %s", (const char *)$2.ptr);
                    dmnsn_delete_astnode($2);
                  }
                  | "#error" STRING {
                    dmnsn_diagnostic(@$, "%s", (const char *)$2.ptr);
                    dmnsn_delete_astnode($2);
                    YYERROR;
                  }
                  | "#macro" IDENTIFIER "(" DECL_PARAMS ")" {
                    dmnsn_declare_symbol(symtable, $2.ptr, $4);
                    dmnsn_local_symbol(symtable, "$macro",
                                       dmnsn_new_ast_string($2.ptr));
                    dmnsn_delete_astnode($2);
                    dmnsn_delete_astnode($4);
                  }
                  | IDENTIFIER "(" PARAMS ")" {
                    dmnsn_astnode *node = dmnsn_find_symbol(symtable, $1.ptr);
                    dmnsn_assert(node && node->type == DMNSN_AST_MACRO,
                                 "Attempt to expand non-macro.");
                    dmnsn_delete_astnode($1);

                    unsigned int nparams = dmnsn_array_size(node->children);
                    unsigned int nparams_given = dmnsn_array_size($3.children);

                    if (nparams_given != nparams) {
                      dmnsn_diagnostic(@$, "wrong number of macro arguments"
                                           " (%u; should be %u)",
                                       nparams_given, nparams);
                      dmnsn_delete_astnode($3);
                      YYERROR;
                    }

                    unsigned int i;
                    for (i = 0; i < nparams; ++i) {
                      dmnsn_astnode id, param;
                      dmnsn_array_get(node->children, i, &id);
                      dmnsn_array_get($3.children, i, &param);
                      dmnsn_local_symbol(symtable, id.ptr, param);
                    }

                    dmnsn_delete_astnode($3);
                  }
;

RVALUE: ARITH_EXPR ";" %dprec 2 {
        $$ = dmnsn_eval($1, symtable);
        dmnsn_delete_astnode($1);

        if ($$.type == DMNSN_AST_NONE) {
          dmnsn_delete_astnode($$);
          YYERROR;
        }
      }
      | COLOR ";" %dprec 1
      | OBJECT
      | TEXTURE
      | PIGMENT
      | FINISH
      | INTERIOR
      | CAMERA
      | TRANSFORMATION
;

DECL_PARAMS: /* empty */ {
             $$ = dmnsn_new_astnode(DMNSN_AST_MACRO, @$);
           }
           | DECL_PARAM_LIST
;

DECL_PARAM_LIST: IDENTIFIER {
                 $$ = dmnsn_new_astnode(DMNSN_AST_MACRO, @$);
                 dmnsn_array_push($$.children, &$1);
               }
               | DECL_PARAM_LIST "," IDENTIFIER {
                 $$ = $1;
                 dmnsn_array_push($$.children, &$3);
               }
;

PARAMS: /* empty */ {
        $$ = dmnsn_new_astnode(DMNSN_AST_ARRAY, @$);
      }
      | PARAM_LIST
;

PARAM_LIST: IDENTIFIER %dprec 2 {
            $$ = dmnsn_new_astnode(DMNSN_AST_MACRO, @$);
            dmnsn_array_push($$.children, &$1);
          }
          | PARAM      %dprec 1 {
            $$ = dmnsn_new_astnode(DMNSN_AST_MACRO, @$);
            dmnsn_array_push($$.children, &$1);
          }
          | PARAM_LIST "," IDENTIFIER %dprec 2 {
            $$ = $1;
            dmnsn_array_push($$.children, &$3);
          }
          | PARAM_LIST "," PARAM      %dprec 1 {
            $$ = $1;
            dmnsn_array_push($$.children, &$3);
          }
;

PARAM: ARITH_EXPR %dprec 2 {
       $$ = dmnsn_eval($1, symtable);
       dmnsn_delete_astnode($1);

       if ($$.type == DMNSN_AST_NONE) {
         dmnsn_delete_astnode($$);
         YYERROR;
       }
     }
     | COLOR %dprec 1
     | OBJECT
     | TEXTURE
     | PIGMENT
     | FINISH
     | INTERIOR
     | CAMERA
     | TRANSFORMATION
;