Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

184 строки
5.8 KiB

  1. %{
  2. //
  3. // Lol Engine
  4. //
  5. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  6. // (c) 2009-2012 Cédric Lecacheur <jordx@free.fr>
  7. // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
  8. // This program is free software; you can redistribute it and/or
  9. // modify it under the terms of the Do What The Fuck You Want To
  10. // Public License, Version 2, as published by Sam Hocevar. See
  11. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  12. //
  13. #if defined HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include "core.h"
  17. #include "easymesh/easymesh.h"
  18. #include <string>
  19. %}
  20. %require "2.3"
  21. %debug
  22. %defines
  23. %skeleton "lalr1.cc"
  24. %name-prefix="lol"
  25. %define parser_class_name "EasyMeshParser"
  26. %locations
  27. %parse-param { class EasyMeshCompiler& mc }
  28. %error-verbose
  29. %union
  30. {
  31. float fval;
  32. /* Can't use uin32_t here for some reason */
  33. unsigned u32val;
  34. struct { float f0, f1, f2, f3, f4, f5, f6, f7; } args;
  35. }
  36. %start mesh_description
  37. %token T_COLOR T_BGCOLOR
  38. %token T_TRANSLATEX T_ROTATEX T_TAPERX T_SCALEX T_MIRRORX
  39. %token T_TRANSLATEY T_ROTATEY T_TAPERY T_SCALEY T_MIRRORY
  40. %token T_TRANSLATEZ T_ROTATEZ T_TAPERZ T_SCALEZ T_MIRRORZ
  41. %token T_TRANSLATE T_SCALE
  42. %token T_CHAMFER
  43. %token T_CYLINDER T_BOX T_SMOOTHCHAMFBOX T_FLATCHAMFBOX T_SPHERE T_CAPSULE
  44. %token T_STAR T_EXPANDEDSTAR T_DISC T_TRIANGLE T_QUAD T_COG
  45. %token T_END 0
  46. %token T_ERROR
  47. %token <fval> NUMBER
  48. %token <u32val> COLOR
  49. %type <fval> number
  50. %type <args> args1 args2 args3 args4 args5 args6 args7 args8
  51. %{
  52. #include "easymesh/easymesh-compiler.h"
  53. #undef yylex
  54. #define yylex mc.m_lexer->lex
  55. %}
  56. %%
  57. mesh_description:
  58. mesh_expression_list T_END
  59. ;
  60. mesh_expression_list:
  61. mesh_expression
  62. | mesh_expression mesh_expression_list
  63. ;
  64. mesh_expression:
  65. mesh_command_list
  66. | mesh_open mesh_expression_list mesh_close
  67. ;
  68. mesh_open:
  69. '[' { mc.m_mesh.OpenBrace(); }
  70. ;
  71. mesh_close:
  72. ']' { mc.m_mesh.CloseBrace(); }
  73. ;
  74. mesh_command_list:
  75. mesh_command
  76. | mesh_command_list mesh_command
  77. ;
  78. mesh_command:
  79. color_command
  80. | transform_command
  81. | primitive_command
  82. ;
  83. color_command:
  84. T_COLOR args4 { mc.m_mesh.SetCurColor(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
  85. | T_COLOR COLOR { uint32_t x = $2;
  86. vec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  87. mc.m_mesh.SetCurColor(vec4(v) * (1. / 255)); }
  88. | T_BGCOLOR args4 { mc.m_mesh.SetCurColor2(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
  89. | T_BGCOLOR COLOR { uint32_t x = $2;
  90. vec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  91. mc.m_mesh.SetCurColor2(vec4(v) * (1. / 255)); }
  92. ;
  93. transform_command:
  94. T_CHAMFER args1 { mc.m_mesh.Chamfer($2.f0); }
  95. | T_TRANSLATEX args1 { mc.m_mesh.Translate(vec3($2.f0, 0, 0)); }
  96. | T_TRANSLATEY args1 { mc.m_mesh.Translate(vec3(0, $2.f0, 0)); }
  97. | T_TRANSLATEZ args1 { mc.m_mesh.Translate(vec3(0, 0, $2.f0)); }
  98. | T_TRANSLATE args3 { mc.m_mesh.Translate(vec3($2.f0, $2.f1, $2.f2)); }
  99. | T_ROTATEX args1 { mc.m_mesh.RotateX($2.f0); }
  100. | T_ROTATEY args1 { mc.m_mesh.RotateY($2.f0); }
  101. | T_ROTATEZ args1 { mc.m_mesh.RotateZ($2.f0); }
  102. | T_TAPERX args3 { mc.m_mesh.TaperX($2.f0, $2.f1, $2.f2); }
  103. | T_TAPERY args3 { mc.m_mesh.TaperY($2.f0, $2.f1, $2.f2); }
  104. | T_TAPERZ args3 { mc.m_mesh.TaperZ($2.f0, $2.f1, $2.f2); }
  105. | T_SCALEX args1 { mc.m_mesh.Scale(vec3($2.f0, 1.0, 1.0)); }
  106. | T_SCALEY args1 { mc.m_mesh.Scale(vec3(1.0, $2.f0, 1.0)); }
  107. | T_SCALEZ args1 { mc.m_mesh.Scale(vec3(1.0, 1.0, $2.f0)); }
  108. | T_SCALE args3 { mc.m_mesh.Scale(vec3($2.f0, $2.f1, $2.f2)); }
  109. | T_MIRRORX { mc.m_mesh.MirrorX(); }
  110. | T_MIRRORY { mc.m_mesh.MirrorY(); }
  111. | T_MIRRORZ { mc.m_mesh.MirrorZ(); }
  112. ;
  113. primitive_command:
  114. T_CYLINDER args6 { mc.m_mesh.AppendCylinder((int)$2.f0, $2.f1,
  115. $2.f2, $2.f3,
  116. (int)$2.f4, (int)$2.f5); }
  117. | T_BOX args3 { mc.m_mesh.AppendBox(vec3($2.f0, $2.f1, $2.f2)); }
  118. | T_SMOOTHCHAMFBOX args4 { mc.m_mesh.AppendSmoothChamfBox(vec3($2.f0, $2.f1,
  119. $2.f2), $2.f3); }
  120. | T_FLATCHAMFBOX args4 { mc.m_mesh.AppendFlatChamfBox(vec3($2.f0, $2.f1,
  121. $2.f2), $2.f3); }
  122. | T_SPHERE args4 { mc.m_mesh.AppendSphere($2.f0,
  123. vec3($2.f1, $2.f2, $2.f3)); }
  124. | T_CAPSULE args3 { mc.m_mesh.AppendCapsule($2.f0, $2.f1, $2.f2); }
  125. | T_STAR args5 { mc.m_mesh.AppendStar((int)$2.f0, $2.f1, $2.f2,
  126. (int)$2.f3, (int)$2.f4); }
  127. | T_EXPANDEDSTAR args4 { mc.m_mesh.AppendExpandedStar((int)$2.f0, $2.f1,
  128. $2.f2, $2.f3); }
  129. | T_DISC args3 { mc.m_mesh.AppendDisc((int)$2.f0, $2.f1, (int)$2.f2); }
  130. | T_TRIANGLE args2 { mc.m_mesh.AppendSimpleTriangle($2.f0, (int)$2.f1); }
  131. | T_QUAD args2 { mc.m_mesh.AppendSimpleQuad($2.f0, (int)$2.f1); }
  132. | T_COG args8 { mc.m_mesh.AppendCog((int)$2.f0, $2.f1, $2.f2, $2.f3,
  133. $2.f4, $2.f5, $2.f6, (int)$2.f7); }
  134. ;
  135. args1: number { $$.f0 = $1; } ;
  136. args2: args1 number { $$ = $1; $$.f1 = $2; } ;
  137. args3: args2 number { $$ = $1; $$.f2 = $2; } ;
  138. args4: args3 number { $$ = $1; $$.f3 = $2; } ;
  139. args5: args4 number { $$ = $1; $$.f4 = $2; } ;
  140. args6: args5 number { $$ = $1; $$.f5 = $2; } ;
  141. args7: args6 number { $$ = $1; $$.f6 = $2; } ;
  142. args8: args7 number { $$ = $1; $$.f7 = $2; } ;
  143. number:
  144. NUMBER { $$ = $1; }
  145. | '-' number { $$ = -$2; }
  146. ;
  147. %%
  148. void lol::EasyMeshParser::error(const EasyMeshParser::location_type& l,
  149. const std::string& m)
  150. {
  151. mc.Error(l, m);
  152. }