Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. %{
  2. //
  3. // Lol Engine
  4. //
  5. // Copyright: (c) 2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. // (c) 2013 Sam Hocevar <sam@hocevar.net>
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the Do What The Fuck You Want To
  9. // Public License, Version 2, as published by Sam Hocevar. See
  10. // http://www.wtfpl.net/ for more details.
  11. //
  12. #if defined HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include "core.h"
  16. #include "../scenesetup.h"
  17. %}
  18. %require "2.3"
  19. %debug
  20. %defines
  21. %skeleton "lalr1.cc"
  22. %name-prefix="lol"
  23. %define parser_class_name "SceneSetupParser"
  24. %locations
  25. %parse-param { class SceneSetupCompiler& uc }
  26. %error-verbose
  27. %union
  28. {
  29. float fval;
  30. int ival;
  31. bool bval;
  32. float vval[4];
  33. int ivval[4];
  34. char* sval;
  35. char* svval;
  36. /* Can't use uin32_t here for some reason */
  37. unsigned u32val;
  38. }
  39. %start sstp_description
  40. %token T_COLOR
  41. %token T_ADDLIGHT T_OBJPOSITION T_OBJLOOKAT T_OBJCOLOR T_CLEARCOLOR
  42. %token T_SHOWGIZMO T_SHOWLIGHT
  43. %token T_CUSTOMCMD
  44. %token T_END 0
  45. %token T_ERROR
  46. %token <fval> F_NUMBER
  47. %token <ival> I_NUMBER
  48. %token <sval> STRING
  49. %token <svval> STRING_VAR
  50. %token <bval> BOOLEAN
  51. %token <u32val> COLOR
  52. /* Base Number types */
  53. %type <fval> fv
  54. %type <ival> iv
  55. /* Vector types */
  56. %type <vval> v3
  57. %type <ivval> iv3
  58. %type <vval> v4
  59. /* Special types */
  60. %type <bval> bv
  61. %type <sval> sv
  62. %type <svval> svv
  63. %{
  64. #include "../scenesetup-compiler.h"
  65. #undef yylex
  66. #define yylex uc.m_lexer->lex
  67. /* HACK: workaround for Bison who insists on using exceptions */
  68. #define try if (true)
  69. #define catch(...) if (false)
  70. #define throw (void)0
  71. %}
  72. %%
  73. sstp_description:
  74. sstp_expression_list T_END
  75. ;
  76. sstp_expression_list:
  77. sstp_expression
  78. | sstp_expression sstp_expression_list
  79. ;
  80. sstp_expression:
  81. sstp_command_list
  82. ;
  83. sstp_command_list:
  84. sstp_command
  85. | sstp_command_list sstp_command
  86. ;
  87. sstp_command:
  88. light_command
  89. | setup_command
  90. | scene_command
  91. | custom_command
  92. ;
  93. light_command:
  94. T_ADDLIGHT { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT"; }
  95. | T_ADDLIGHT fv { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT";
  96. uc.m_sstp.m_lights.Last()->SetType(LightType($2)); }
  97. | T_ADDLIGHT svv { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT";
  98. uc.m_sstp.m_lights.Last()->SetType(FindValue<LightType>($2)); }
  99. ;
  100. setup_command:
  101. T_OBJPOSITION v3 { if (uc.m_last_cmd == "ADDLIGHT")
  102. uc.m_sstp.m_lights.Last()->SetPosition(vec3($2[0], $2[1], $2[2])); }
  103. | T_OBJLOOKAT v3 { if (uc.m_last_cmd == "ADDLIGHT")
  104. { /* */ } }
  105. | T_OBJCOLOR v4 { if (uc.m_last_cmd == "ADDLIGHT")
  106. uc.m_sstp.m_lights.Last()->SetColor(vec4($2[0], $2[1], $2[2], $2[3])); }
  107. | T_OBJCOLOR COLOR { uint32_t x = $2;
  108. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  109. vec4 vv = vec4(v) * (1.f / 255.f);
  110. if (uc.m_last_cmd == "ADDLIGHT")
  111. uc.m_sstp.m_lights.Last()->SetColor(vv); }
  112. ;
  113. scene_command:
  114. T_CLEARCOLOR v4 { uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], $2[3]); }
  115. T_CLEARCOLOR v3 { uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], 1.f); }
  116. | T_CLEARCOLOR COLOR { uint32_t x = $2; ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  117. uc.m_sstp.m_clear_color = vec4(v) * (1.f / 255.f); }
  118. | T_SHOWGIZMO bv { uc.m_sstp.m_show_gizmo = $2; }
  119. | T_SHOWLIGHT bv { uc.m_sstp.m_show_lights = $2; }
  120. ;
  121. custom_command:
  122. T_CUSTOMCMD svv sv { uc.m_sstp.m_custom_cmd.Push($2, $3); }
  123. ;
  124. /* Base Number types */
  125. fv:
  126. F_NUMBER { $$ = $1; }
  127. | '-' fv { $$ = -$2; }
  128. | I_NUMBER { $$ = (float)$1; }
  129. | '-' iv { $$ = -(float)$2; }
  130. ;
  131. iv:
  132. I_NUMBER { $$ = $1; }
  133. | '-' iv { $$ = -$2; }
  134. | F_NUMBER { $$ = (int)$1; }
  135. | '-' fv { $$ = -(int)$2; }
  136. ;
  137. /* Vector types */
  138. v3:
  139. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  140. | '('fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  141. ;
  142. iv3:
  143. '('iv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  144. | '('iv iv iv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  145. ;
  146. v4:
  147. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; $$[3] = $2; }
  148. | '('fv fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; $$[3] = $5; }
  149. ;
  150. /* Special types */
  151. bv:
  152. BOOLEAN { $$ = $1; }
  153. | I_NUMBER { $$ = !!$1; }
  154. | F_NUMBER { $$ = !!$1; }
  155. ;
  156. svv:
  157. STRING_VAR { $$ = $1; }
  158. ;
  159. sv:
  160. STRING { String t = $1;
  161. t.Replace('"', ' ', true);
  162. free($1);
  163. $$ = strdup((const char *)t.C()); }
  164. | STRING sv { String t = $1;
  165. t += $2;
  166. t.Replace('"', ' ', true);
  167. free($1);
  168. free($2);
  169. $$ = strdup((const char *)t.C()); }
  170. ;
  171. %%
  172. void lol::SceneSetupParser::error(const SceneSetupParser::location_type& l,
  173. const std::string& m)
  174. {
  175. uc.Error(l, m);
  176. }