Advertisement
Guest User

Eval

a guest
Feb 21st, 2011
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.02 KB | None | 0 0
  1. tree grammar eval;
  2.  
  3. options {
  4. language = Java;
  5. tokenVocab = hhl;
  6. ASTLabelType = CommonTree;
  7. }
  8.  
  9. @header {
  10. package haven.hhl;
  11. import java.util.Vector;
  12. import haven.ark_bot;
  13. import haven.Coord;
  14. }
  15.  
  16. @members{
  17. Stack<TreeNodeStream> Inputs = new Stack<TreeNodeStream>();
  18.  
  19.  
  20. public void PushInputTree(CommonTree tree) {
  21. CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
  22. Inputs.push(getTreeNodeStream());
  23. setTreeNodeStream(nodes);
  24. }
  25.  
  26. public void PopInputTree() {
  27. setTreeNodeStream(Inputs.pop());
  28. }
  29. }
  30. program
  31. : ^(INCLUDELIST .*) ^(PROGRAM ^(VARIABLES varDecl*) ^(FUNCTIONS .*) call) EOF
  32. ;
  33.  
  34. varDecl
  35. : EMPTY
  36. | ^(VARDECL TYPE name=IDENT) {hhl_main.symbols.declare_variable($name.text,$TYPE.text);}
  37. | ^(ASSIGN IDENT a=expression) {hhl_main.symbols.set_value($IDENT.text,a.value); }
  38. | ^(ARRAYDECL TYPE name=IDENT size=INT) {hhl_main.symbols.declare_array($name.text,$TYPE.text,Integer.parseInt($size.text));}
  39. ;
  40.  
  41. expression returns [int value, String id_name]
  42. @init { $id_name = null; }
  43. : orStatement {$value = $orStatement.value;}
  44. | andStatement {$value = $andStatement.value;}
  45. | ^('==' a=expression b=expression) {$value = (a.value == b.value)? 1 : 0;}
  46. | ^('!=' a=expression b=expression) {$value = (a.value != b.value)? 1 : 0;}
  47. | ^('>' a=expression b=expression) {$value = (a.value > b.value)? 1 : 0;}
  48. | ^('<' a=expression b=expression) {$value = (a.value < b.value)? 1 : 0;}
  49. | ^('>=' a=expression b=expression) {$value = (a.value >= b.value)? 1 : 0;}
  50. | ^('<=' a=expression b=expression) {$value = (a.value <= b.value)? 1 : 0;}
  51. | ^('<>' a=expression b=expression) {$value = (a.value != b.value)? 1 : 0;}
  52. | ^('+' a=expression b=expression) {$value = a.value+b.value;}
  53. | ^('-' a=expression b=expression) {$value = a.value-b.value;}
  54. | ^('*' a=expression b=expression) {$value = a.value*b.value;}
  55. | ^('/' a=expression b=expression) {$value = a.value/b.value;}
  56. | ^(PREFIX ^(VAR IDENT) ^(NUM INT)) {$value = hhl_main.symbols.get_value($IDENT.text)+Integer.parseInt($INT.text); hhl_main.symbols.set_value($IDENT.text,$value); }
  57. | ^(POSTFIX ^(VAR IDENT) ^(NUM INT)) {$value = hhl_main.symbols.get_value($IDENT.text); hhl_main.symbols.set_value($IDENT.text,$value+Integer.parseInt($INT.text)); }
  58. | ^(NEGATE a=expression) {$value = -a.value;}
  59. | ^(NOT a=expression) {$value = (a.value!=0)? 0 : 1;}
  60. | ^(NUM INT) {$value = Integer.parseInt($INT.text);}
  61. | ^(VAR IDENT) {$value = hhl_main.symbols.get_value($IDENT.text); $id_name=$IDENT.text;}
  62. | ^(INDEX IDENT i=expression) {$value = hhl_main.symbols.get_value($IDENT.text,i.value);}
  63. | call {$value = $call.value;}
  64. | ^(ASSIGN IDENT a=expression) {hhl_main.symbols.set_value($IDENT.text,a.value); $value=$a.value;}
  65. | ^(ASSIGN ^(INDEX IDENT i=expression) a=expression) {hhl_main.symbols.set_value($IDENT.text,a.value,i.value); ; $value=$a.value;}
  66. | ^(IS_CURSOR cname=STRING) {$value = (ark_bot.cursor_name.equals($cname.text))? 1 : 0;}
  67. | ^(INPUT_GET_OBJECT msg=STRING) {$value = ark_bot.input_get_object($msg.text); }
  68. | ^(HAVE_INVENTORY action=STRING) {$value = ark_bot.HaveInventory($action.text); }
  69. | ^(SET_INVENTORY name=STRING) {$value = ark_bot.set_inventory($name.text); }
  70. | NEXT_ITEM {$value = ark_bot.next_item(); }
  71. | GET_ITEMS_COUNT {$value = ark_bot.get_items_count(); }
  72. | ^(IS_ITEM_NAME name=STRING) {$value = ark_bot.is_item_name($name.text); }
  73. | ^(IS_ITEM_TOOLTIP name=STRING) {$value = ark_bot.is_item_tooltip($name.text); }
  74. | ITEM_QUALITY {$value = ark_bot.item_quality(); }
  75. | ITEM_COORD_X {$value = ark_bot.item_coord_x();}
  76. | ITEM_COORD_Y {$value = ark_bot.item_coord_y();}
  77. | ITEM_NUM {$value = ark_bot.item_num();}
  78. | ITEM_METER {$value = ark_bot.item_meter();}
  79. | ^(FIND_OBJECT_BY_NAME n=STRING r=expression) {$value = ark_bot.find_object_by_name($n.text,$r.value);}
  80. | ^(FIND_OBJECT_BY_TYPE n=STRING r=expression) {$value = ark_bot.find_object_by_type($n.text,$r.value);}
  81. | MY_COORD_X {$value = ark_bot.my_coord_x();}
  82. | MY_COORD_Y {$value = ark_bot.my_coord_y();}
  83. | ^(CHECK_CRAFT wnd=STRING) {$value = ark_bot.check_craft($wnd.text); }
  84. | ^(FIND_MAP_OBJECT n=STRING r=expression x=expression y=expression) {$value = ark_bot.find_map_object($n.text, $r.value, $x.value, $y.value); }
  85. | ^(GET_OBJECT_BLOB id=expression ind=expression) { $value =ark_bot.get_object_blob($id.value, $ind.value); }
  86. | NEXT_BUFF { $value = ark_bot.next_buff(); }
  87. | BUFF_METER { $value = ark_bot.buff_meter(); }
  88. | BUFF_TIME_METER { $value = ark_bot.buff_time_meter(); }
  89. | ^(IS_BUFF_NAME n=STRING) { $value = ark_bot.is_buff_name($n.text); }
  90. ;
  91.  
  92. block
  93. @init{
  94. hhl_main.symbols.enter_block();
  95. }
  96. @after {
  97. hhl_main.symbols.exit_block();
  98. }
  99. : EMPTY
  100. | ^(BLOCK statement+)
  101. ;
  102.  
  103. statement
  104. options {backtrack = true;}
  105. @init{
  106. try{
  107. if((! $call::can_run) || $whileStatement::breaked || $whileStatement::continued){
  108. matchAny(input);
  109. return;
  110. }
  111. }catch(java.util.EmptyStackException ignore){}
  112. }
  113. : expression
  114. | varDecl
  115. | ^(RETURN expression) {$call::return_val=$expression.value; $call::can_run=false;}
  116. | ^(PRINT expression) {System.out.println($expression.value); }
  117. | ^(PRINT_STRING s=STRING) {System.out.println($s.text); }
  118. | ^(SLEEP expression) {try {hhl_main.thread.sleep($expression.value);} catch (Exception e) {e.printStackTrace();} }
  119. | BREAK {$whileStatement::breaked=true;}
  120. | CONTINUE {$whileStatement::continued=true;}
  121. | EXIT { ark_bot.exit_command(); }
  122. | LOGOUT { ark_bot.logout_command(); }
  123. | ^(SAY s=STRING) { ark_bot.say($s.text); }
  124.  
  125.  
  126. | ifStatement
  127. | whileStatement
  128. | ^(DO_CLICK obj=expression btn=expression mod=expression)
  129. {ark_bot.DoClick($obj.value, $btn.value, $mod.value);}
  130. | ^(SELECT_CONTEXT_MENU sname=STRING)
  131. { ark_bot.SelectFlowerMenuOpt($sname.text); }
  132. | ^(MAP_MOVE id=expression x=expression y=expression)
  133. { if (ark_bot.mapview != null) ark_bot.mapview.map_move($id.value, new Coord($x.value, $y.value)); }
  134. | ^(MAP_MOVE_STEP x=expression y=expression)
  135. { if (ark_bot.mapview != null) ark_bot.mapview.map_move_step($x.value, $y.value); }
  136. | ^(SEND_ACTION sname=STRING)
  137. { ark_bot.SendAction($sname.text); }
  138. | ^(SEND_ACTION2 sname=STRING sname2=STRING)
  139. {ark_bot.SendAction($sname.text, $sname2.text); }
  140. | ^(MAP_CLICK x=expression y=expression btn=expression mod=expression)
  141. { if (ark_bot.mapview != null) ark_bot.mapview.map_click($x.value, $y.value, $btn.value, $mod.value); }
  142. | ^(MAP_INTERACT_CLICK x=expression y=expression mod=expression)
  143. { if (ark_bot.mapview != null) ark_bot.mapview.map_interact_click($x.value, $y.value, $mod.value); }
  144. | ^(MAP_INTERACT_CLICK2 id=expression mod=expression)
  145. { if (ark_bot.mapview != null) ark_bot.mapview.map_interact_click($id.value, $mod.value); }
  146. | ^(MAP_ABS_INTERACT_CLICK x=expression y=expression mod=expression)
  147. { if (ark_bot.mapview != null) ark_bot.mapview.map_abs_interact_click($x.value, $y.value, $mod.value); }
  148. | ^(DROP mod=expression)
  149. { if (ark_bot.mapview != null) ark_bot.mapview.drop_thing($mod.value); }
  150. | RESET_INVENTORY
  151. { ark_bot.reset_inventory(); }
  152. | ^(SET_ITEM_INDEX i=expression)
  153. { ark_bot.set_item_index($i.value); }
  154. | ^(ITEM_CLICK action=STRING)
  155. { ark_bot.item_click($action.text, 0); }
  156. | ^(ITEM_CLICK2 action=STRING mod=expression)
  157. { ark_bot.item_click($action.text,$mod.value); }
  158. | ^(ITEM_DROP x=expression y=expression)
  159. { ark_bot.item_drop(new Coord($x.value, $y.value)); }
  160. | ^(ITEM_DROP_TO_INVENTORY n=STRING x=expression y=expression)
  161. { ark_bot.item_drop_to_inventory($n.text, new Coord($x.value, $y.value)); }
  162. | ^(CRAFT a=expression)
  163. { ark_bot.craft($a.value); }
  164. | ^(EQUIP slot=expression act=STRING )
  165. { ark_bot.equip($slot.value, $act.text); }
  166. | ^(INVENTORY n=STRING x=expression y=expression act=STRING)
  167. { ark_bot.inventory($n.text, $x.value, $y.value, $act.text, 0); }
  168. | ^(INVENTORY2 n=STRING x=expression y=expression act=STRING mod=expression)
  169. { ark_bot.inventory($n.text, $x.value, $y.value, $act.text, $mod.value); }
  170. | ^(MAP_ABS_CLICK x=expression y=expression btn=expression mod=expression)
  171. { if (ark_bot.mapview != null) ark_bot.mapview.map_abs_click($x.value, $y.value, $btn.value, $mod.value); }
  172. | ^(WAIT_CRAFT wnd=STRING)
  173. { ark_bot.wait_craft($wnd.text); }
  174. | OPEN_INVENTORY
  175. { ark_bot.OpenInventory(); }
  176. | ^(SET_SHOW_VAR s=STRING)
  177. { synchronized (hhl_main.symbols.ShowNames) { hhl_main.symbols.ShowNames.add($s.text); } }
  178. | SET_ITEM_DRAG
  179. { ark_bot.set_item_drag(); }
  180. | ^(RENDER_MODE x=expression)
  181. { ark_bot.set_render_mode($x.value); }
  182. | RESET_BUFF
  183. { ark_bot.reset_buff_iterator(); }
  184. | ^(MAP_PLACE x=expression y=expression btn=expression mod=expression)
  185. { if (ark_bot.mapview != null) ark_bot.mapview.map_place($x.value, $y.value, $btn.value, $mod.value); }
  186. | BUILD_CLICK
  187. { ark_bot.build_click(); }
  188. | ^(SET_ITEM_EQUIP i=expression)
  189. { ark_bot.set_item_equip($i.value); }
  190. | block
  191. | NOP
  192. ;
  193.  
  194. // cycle statements
  195. whileStatement
  196. scope{
  197. Boolean breaked;
  198. Boolean continued;
  199. }
  200. @after{
  201. CommonTree stmtNode=(CommonTree)$whileStatement.start.getChild(1);
  202. CommonTree exprNode=(CommonTree)$whileStatement.start.getChild(0);
  203.  
  204. int test;
  205.  
  206. $whileStatement::breaked=false;
  207. while($whileStatement::breaked==false){
  208. PushInputTree(exprNode);
  209. $whileStatement::continued=false;
  210. test=expression().value;
  211. PopInputTree();
  212. if (test==0) break;
  213. PushInputTree(stmtNode);
  214. statement();
  215. PopInputTree();
  216. }
  217.  
  218. }
  219. : ^(WHILE . .)
  220. ;
  221.  
  222. ifStatement
  223. @after{
  224. CommonTree stmtNode = null;
  225. if ($v.value!=0){
  226. stmtNode = (CommonTree)$ifStatement.start.getChild(1);
  227. }else{
  228. stmtNode = (CommonTree)$ifStatement.start.getChild(2);
  229. }
  230. PushInputTree(stmtNode);
  231. statement();
  232. PopInputTree();
  233. }
  234. : ^(IF ^(EXPR v=expression) . .)
  235. ;
  236.  
  237. //short circuiting or statement
  238. orStatement returns [int value]
  239. @after{
  240. if ($a.value!=0){
  241. $value=1;
  242. }else{
  243. PushInputTree((CommonTree)$orStatement.start.getChild(1));
  244. $value = expression().value;
  245. if ($value != 0) $value = 1;
  246. PopInputTree();
  247. }
  248. }
  249. :
  250. ^(('or'|'||') a=expression .)
  251. ;
  252.  
  253. //short circuiting and statement
  254. andStatement returns [int value]
  255. @after{
  256. if ($a.value==0){
  257. $value=0;
  258. }else{
  259. PushInputTree((CommonTree)$andStatement.start.getChild(1));
  260. $value = expression().value;
  261. if ($value != 0) $value = 1;
  262. PopInputTree();
  263. }
  264. }
  265. :
  266. ^(('and'|'&&') a=expression .)
  267. ;
  268.  
  269. call returns [int value]
  270. scope{
  271. int return_val;
  272. Boolean can_run;
  273. Boolean in_paramater_defs;
  274.  
  275. }
  276. @init {
  277.  
  278. Vector params;
  279. Function fcn = null;
  280. int p_i;
  281. $call::can_run=true;
  282. params=new Vector();
  283. p_i=0;
  284. }
  285. : ^(CALL IDENT {} ^(EXPRLIST (expression
  286. {
  287. if ($expression.id_name != null) {
  288. params.add(hhl_main.symbols.get_variable($expression.id_name));
  289. }else{
  290. params.add($expression.value);
  291. }
  292. p_i++;
  293. }
  294. )* ))
  295.  
  296. {
  297. if (hhl_main.IncludeDepth <= 1)
  298. fcn=hhl_main.symbols.get_function($IDENT.text, params);
  299. if (fcn == null) { hhl_main.PrintError("No such function: " + $IDENT.text); }
  300. if ((fcn != null) && (hhl_main.IncludeDepth <= 1)) {
  301. hhl_main.symbols.enter_frame(fcn,params);
  302.  
  303. PushInputTree(fcn.tree);
  304. block();
  305. PopInputTree();
  306. $value = $call::return_val;
  307.  
  308. hhl_main.symbols.exit_frame();
  309. }
  310. } ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement