source: trunk/orbital/mesh-parser.y @ 1431

Last change on this file since 1431 was 1431, checked in by sam, 9 years ago

orbital: add missing <stdint.h>.

File size: 5.3 KB
Line 
1%{
2//
3// Orbital
4//
5// Copyright: (c) 2009-2012 Cédric Lecacheur <jordx@free.fr>
6//            (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
7//            (c) 2012 Sam Hocevar <sam@hocevar.net>
8//
9
10#if defined HAVE_CONFIG_H
11#   include "config.h"
12#endif
13
14#include "core.h"
15#include "loldebug.h"
16
17using namespace lol;
18
19#include "../mesh.h"
20
21#include <string>
22#include <stdint.h>
23%}
24
25%require "2.3"
26%debug
27%defines
28%skeleton "lalr1.cc"
29%name-prefix="orbital"
30%define parser_class_name "MeshParser"
31%locations
32%parse-param { class MeshCompiler& mc }
33%error-verbose
34
35%union
36{
37    float fval;
38    uint32_t u32val;
39    struct { float f0, f1, f2, f3, f4, f5, f6, f7; } args;
40}
41
42%start mesh_description
43
44%token T_FLUSH T_INITRB T_FREERB
45
46%token T_COLOR T_BGCOLOR
47
48%token T_TRANSLATEX T_ROTATEX T_TAPERX T_SCALEX T_MIRRORX
49%token T_TRANSLATEY T_ROTATEY T_TAPERY T_SCALEY T_MIRRORY
50%token T_TRANSLATEZ T_ROTATEZ T_TAPERZ T_SCALEZ T_MIRRORZ
51%token T_TRANSLATE T_SCALE
52
53%token T_CYLINDER T_BOX T_SMOOTHCHAMFBOX T_FLATCHAMFBOX T_SPHERE T_STAR
54%token T_EXPANDEDSTAR T_DISC T_TRIANGLE T_QUAD T_COG
55
56%token T_END 0
57
58%token <fval> NUMBER
59%token <u32val> COLOR
60
61%type <fval> number
62%type <args> args1 args2 args3 args4 args5 args6 args7 args8
63
64%{
65#include "../mesh-compiler.h"
66
67#undef yylex
68#define yylex mc.m_lexer->lex
69%}
70
71%%
72
73mesh_description:
74    mesh_command_list T_END
75    ;
76
77mesh_command_list:
78    mesh_command
79  | mesh_command_list ',' mesh_command
80    ;
81
82mesh_command:
83    rb_command
84  | color_command
85  | transform_command
86  | primitive_command
87    ;
88
89rb_command:
90    T_FLUSH    { mc.m_mesh.Flush(); }
91  | T_INITRB   { mc.m_mesh.MeshConvert(); }
92  | T_FREERB   { /* TODO */ }
93    ;
94
95color_command:
96    T_COLOR args4   { mc.m_mesh.SetCurColor(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
97  | T_COLOR COLOR   { uint16_t x = (uint16_t)$2;
98                      vec4 v(x >> 12, (x >> 8) & 0xf, (x >> 4) & 0xf, x & 0xf);
99                      mc.m_mesh.SetCurColor(vec4(v) * (1. / 15)); }
100  | T_BGCOLOR args4 { mc.m_mesh.SetCurColor2(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
101  | T_BGCOLOR COLOR { uint16_t x = (uint16_t)$2;
102                      vec4 v(x >> 12, (x >> 8) & 0xf, (x >> 4) & 0xf, x & 0xf);
103                      mc.m_mesh.SetCurColor(vec4(v) * (1. / 15)); }
104    ;
105
106transform_command:
107    T_TRANSLATEX args1  { mc.m_mesh.Translate(vec3($2.f0, 0, 0)); }
108  | T_TRANSLATEY args1  { mc.m_mesh.Translate(vec3(0, $2.f0, 0)); }
109  | T_TRANSLATEZ args1  { mc.m_mesh.Translate(vec3(0, 0, $2.f0)); }
110  | T_TRANSLATE args3   { mc.m_mesh.Translate(vec3($2.f0, $2.f1, $2.f2)); }
111  | T_ROTATEX args1     { mc.m_mesh.RotateX($2.f0); }
112  | T_ROTATEY args1     { mc.m_mesh.RotateY($2.f0); }
113  | T_ROTATEZ args1     { mc.m_mesh.RotateZ($2.f0); }
114  | T_TAPERX args3      { mc.m_mesh.TaperX($2.f0, $2.f1, $2.f2); }
115  | T_TAPERY args3      { mc.m_mesh.TaperY($2.f0, $2.f1, $2.f2); }
116  | T_TAPERZ args3      { mc.m_mesh.TaperZ($2.f0, $2.f1, $2.f2); }
117  | T_SCALEX args1      { mc.m_mesh.Scale(vec3($2.f0, 0, 0)); }
118  | T_SCALEY args1      { mc.m_mesh.Scale(vec3(0, $2.f0, 0)); }
119  | T_SCALEZ args1      { mc.m_mesh.Scale(vec3(0, 0, $2.f0)); }
120  | T_SCALE args3       { mc.m_mesh.Scale(vec3($2.f0, $2.f1, $2.f2)); }
121  | T_MIRRORX           { mc.m_mesh.MirrorX(); }
122  | T_MIRRORY           { mc.m_mesh.MirrorY(); }
123  | T_MIRRORZ           { mc.m_mesh.MirrorZ(); }
124    ;
125
126primitive_command:
127    T_CYLINDER args6       { mc.m_mesh.AppendCylinder((int)$2.f0, $2.f1,
128                                                 $2.f2, $2.f3,
129                                                 (int)$2.f4, (int)$2.f5); }
130  | T_BOX args3            { mc.m_mesh.AppendBox(vec3($2.f0, $2.f1, $2.f2)); }
131  | T_SMOOTHCHAMFBOX args4 { mc.m_mesh.AppendSmoothChamfBox(vec3($2.f0, $2.f1,
132                                                            $2.f2), $2.f3); }
133  | T_FLATCHAMFBOX args4   { mc.m_mesh.AppendFlatChamfBox(vec3($2.f0, $2.f1,
134                                                          $2.f2), $2.f3); }
135  | T_SPHERE args4         { mc.m_mesh.AppendSphere($2.f0,
136                                               vec3($2.f1, $2.f2, $2.f3)); }
137  | T_STAR args5           { mc.m_mesh.AppendStar((int)$2.f0, $2.f1, $2.f2,
138                                             (int)$2.f3, (int)$2.f4); }
139  | T_EXPANDEDSTAR args4   { mc.m_mesh.AppendExpandedStar((int)$2.f0, $2.f1,
140                                                     $2.f2, $2.f3); }
141  | T_DISC args3           { mc.m_mesh.AppendDisc((int)$2.f0, $2.f1, (int)$2.f2); }
142  | T_TRIANGLE args2       { mc.m_mesh.AppendSimpleTriangle($2.f0, (int)$2.f1); }
143  | T_QUAD args2           { mc.m_mesh.AppendSimpleQuad($2.f0, (int)$2.f1); }
144  | T_COG args8            { mc.m_mesh.AppendCog((int)$2.f0, $2.f1, $2.f2, $2.f3,
145                                            $2.f4, $2.f5, $2.f6, (int)$2.f7); }
146    ;
147
148args1: number { $$.f0 = $1; } ;
149args2: args1 ',' number { $$ = $1; $$.f1 = $3; } ;
150args3: args2 ',' number { $$ = $1; $$.f2 = $3; } ;
151args4: args3 ',' number { $$ = $1; $$.f3 = $3; } ;
152args5: args4 ',' number { $$ = $1; $$.f4 = $3; } ;
153args6: args5 ',' number { $$ = $1; $$.f5 = $3; } ;
154args7: args6 ',' number { $$ = $1; $$.f6 = $3; } ;
155args8: args7 ',' number { $$ = $1; $$.f7 = $3; } ;
156
157number:
158    NUMBER       { $$ = $1; }
159  | '-' number   { $$ = -$2; }
160    ;
161
162%%
163
164void orbital::MeshParser::error(const MeshParser::location_type& l,
165                                const std::string& m)
166{
167    mc.Error(l, m);
168}
169
Note: See TracBrowser for help on using the repository browser.