Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tree grammar eval;
- options {
- language = Java;
- tokenVocab = hhl;
- ASTLabelType = CommonTree;
- }
- @header {
- package haven.hhl;
- import java.util.Vector;
- import haven.ark_bot;
- import haven.Coord;
- }
- @members{
- Stack<TreeNodeStream> Inputs = new Stack<TreeNodeStream>();
- public void PushInputTree(CommonTree tree) {
- CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
- Inputs.push(getTreeNodeStream());
- setTreeNodeStream(nodes);
- }
- public void PopInputTree() {
- setTreeNodeStream(Inputs.pop());
- }
- }
- program
- : ^(INCLUDELIST .*) ^(PROGRAM ^(VARIABLES varDecl*) ^(FUNCTIONS .*) call) EOF
- ;
- varDecl
- : EMPTY
- | ^(VARDECL TYPE name=IDENT) {hhl_main.symbols.declare_variable($name.text,$TYPE.text);}
- | ^(ASSIGN IDENT a=expression) {hhl_main.symbols.set_value($IDENT.text,a.value); }
- | ^(ARRAYDECL TYPE name=IDENT size=INT) {hhl_main.symbols.declare_array($name.text,$TYPE.text,Integer.parseInt($size.text));}
- ;
- expression returns [int value, String id_name]
- @init { $id_name = null; }
- : orStatement {$value = $orStatement.value;}
- | andStatement {$value = $andStatement.value;}
- | ^('==' a=expression b=expression) {$value = (a.value == b.value)? 1 : 0;}
- | ^('!=' a=expression b=expression) {$value = (a.value != b.value)? 1 : 0;}
- | ^('>' a=expression b=expression) {$value = (a.value > b.value)? 1 : 0;}
- | ^('<' a=expression b=expression) {$value = (a.value < b.value)? 1 : 0;}
- | ^('>=' a=expression b=expression) {$value = (a.value >= b.value)? 1 : 0;}
- | ^('<=' a=expression b=expression) {$value = (a.value <= b.value)? 1 : 0;}
- | ^('<>' a=expression b=expression) {$value = (a.value != b.value)? 1 : 0;}
- | ^('+' a=expression b=expression) {$value = a.value+b.value;}
- | ^('-' a=expression b=expression) {$value = a.value-b.value;}
- | ^('*' a=expression b=expression) {$value = a.value*b.value;}
- | ^('/' a=expression b=expression) {$value = a.value/b.value;}
- | ^(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); }
- | ^(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)); }
- | ^(NEGATE a=expression) {$value = -a.value;}
- | ^(NOT a=expression) {$value = (a.value!=0)? 0 : 1;}
- | ^(NUM INT) {$value = Integer.parseInt($INT.text);}
- | ^(VAR IDENT) {$value = hhl_main.symbols.get_value($IDENT.text); $id_name=$IDENT.text;}
- | ^(INDEX IDENT i=expression) {$value = hhl_main.symbols.get_value($IDENT.text,i.value);}
- | call {$value = $call.value;}
- | ^(ASSIGN IDENT a=expression) {hhl_main.symbols.set_value($IDENT.text,a.value); $value=$a.value;}
- | ^(ASSIGN ^(INDEX IDENT i=expression) a=expression) {hhl_main.symbols.set_value($IDENT.text,a.value,i.value); ; $value=$a.value;}
- | ^(IS_CURSOR cname=STRING) {$value = (ark_bot.cursor_name.equals($cname.text))? 1 : 0;}
- | ^(INPUT_GET_OBJECT msg=STRING) {$value = ark_bot.input_get_object($msg.text); }
- | ^(HAVE_INVENTORY action=STRING) {$value = ark_bot.HaveInventory($action.text); }
- | ^(SET_INVENTORY name=STRING) {$value = ark_bot.set_inventory($name.text); }
- | NEXT_ITEM {$value = ark_bot.next_item(); }
- | GET_ITEMS_COUNT {$value = ark_bot.get_items_count(); }
- | ^(IS_ITEM_NAME name=STRING) {$value = ark_bot.is_item_name($name.text); }
- | ^(IS_ITEM_TOOLTIP name=STRING) {$value = ark_bot.is_item_tooltip($name.text); }
- | ITEM_QUALITY {$value = ark_bot.item_quality(); }
- | ITEM_COORD_X {$value = ark_bot.item_coord_x();}
- | ITEM_COORD_Y {$value = ark_bot.item_coord_y();}
- | ITEM_NUM {$value = ark_bot.item_num();}
- | ITEM_METER {$value = ark_bot.item_meter();}
- | ^(FIND_OBJECT_BY_NAME n=STRING r=expression) {$value = ark_bot.find_object_by_name($n.text,$r.value);}
- | ^(FIND_OBJECT_BY_TYPE n=STRING r=expression) {$value = ark_bot.find_object_by_type($n.text,$r.value);}
- | MY_COORD_X {$value = ark_bot.my_coord_x();}
- | MY_COORD_Y {$value = ark_bot.my_coord_y();}
- | ^(CHECK_CRAFT wnd=STRING) {$value = ark_bot.check_craft($wnd.text); }
- | ^(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); }
- | ^(GET_OBJECT_BLOB id=expression ind=expression) { $value =ark_bot.get_object_blob($id.value, $ind.value); }
- | NEXT_BUFF { $value = ark_bot.next_buff(); }
- | BUFF_METER { $value = ark_bot.buff_meter(); }
- | BUFF_TIME_METER { $value = ark_bot.buff_time_meter(); }
- | ^(IS_BUFF_NAME n=STRING) { $value = ark_bot.is_buff_name($n.text); }
- ;
- block
- @init{
- hhl_main.symbols.enter_block();
- }
- @after {
- hhl_main.symbols.exit_block();
- }
- : EMPTY
- | ^(BLOCK statement+)
- ;
- statement
- options {backtrack = true;}
- @init{
- try{
- if((! $call::can_run) || $whileStatement::breaked || $whileStatement::continued){
- matchAny(input);
- return;
- }
- }catch(java.util.EmptyStackException ignore){}
- }
- : expression
- | varDecl
- | ^(RETURN expression) {$call::return_val=$expression.value; $call::can_run=false;}
- | ^(PRINT expression) {System.out.println($expression.value); }
- | ^(PRINT_STRING s=STRING) {System.out.println($s.text); }
- | ^(SLEEP expression) {try {hhl_main.thread.sleep($expression.value);} catch (Exception e) {e.printStackTrace();} }
- | BREAK {$whileStatement::breaked=true;}
- | CONTINUE {$whileStatement::continued=true;}
- | EXIT { ark_bot.exit_command(); }
- | LOGOUT { ark_bot.logout_command(); }
- | ^(SAY s=STRING) { ark_bot.say($s.text); }
- | ifStatement
- | whileStatement
- | ^(DO_CLICK obj=expression btn=expression mod=expression)
- {ark_bot.DoClick($obj.value, $btn.value, $mod.value);}
- | ^(SELECT_CONTEXT_MENU sname=STRING)
- { ark_bot.SelectFlowerMenuOpt($sname.text); }
- | ^(MAP_MOVE id=expression x=expression y=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.map_move($id.value, new Coord($x.value, $y.value)); }
- | ^(MAP_MOVE_STEP x=expression y=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.map_move_step($x.value, $y.value); }
- | ^(SEND_ACTION sname=STRING)
- { ark_bot.SendAction($sname.text); }
- | ^(SEND_ACTION2 sname=STRING sname2=STRING)
- {ark_bot.SendAction($sname.text, $sname2.text); }
- | ^(MAP_CLICK x=expression y=expression btn=expression mod=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.map_click($x.value, $y.value, $btn.value, $mod.value); }
- | ^(MAP_INTERACT_CLICK x=expression y=expression mod=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.map_interact_click($x.value, $y.value, $mod.value); }
- | ^(MAP_INTERACT_CLICK2 id=expression mod=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.map_interact_click($id.value, $mod.value); }
- | ^(MAP_ABS_INTERACT_CLICK x=expression y=expression mod=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.map_abs_interact_click($x.value, $y.value, $mod.value); }
- | ^(DROP mod=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.drop_thing($mod.value); }
- | RESET_INVENTORY
- { ark_bot.reset_inventory(); }
- | ^(SET_ITEM_INDEX i=expression)
- { ark_bot.set_item_index($i.value); }
- | ^(ITEM_CLICK action=STRING)
- { ark_bot.item_click($action.text, 0); }
- | ^(ITEM_CLICK2 action=STRING mod=expression)
- { ark_bot.item_click($action.text,$mod.value); }
- | ^(ITEM_DROP x=expression y=expression)
- { ark_bot.item_drop(new Coord($x.value, $y.value)); }
- | ^(ITEM_DROP_TO_INVENTORY n=STRING x=expression y=expression)
- { ark_bot.item_drop_to_inventory($n.text, new Coord($x.value, $y.value)); }
- | ^(CRAFT a=expression)
- { ark_bot.craft($a.value); }
- | ^(EQUIP slot=expression act=STRING )
- { ark_bot.equip($slot.value, $act.text); }
- | ^(INVENTORY n=STRING x=expression y=expression act=STRING)
- { ark_bot.inventory($n.text, $x.value, $y.value, $act.text, 0); }
- | ^(INVENTORY2 n=STRING x=expression y=expression act=STRING mod=expression)
- { ark_bot.inventory($n.text, $x.value, $y.value, $act.text, $mod.value); }
- | ^(MAP_ABS_CLICK x=expression y=expression btn=expression mod=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.map_abs_click($x.value, $y.value, $btn.value, $mod.value); }
- | ^(WAIT_CRAFT wnd=STRING)
- { ark_bot.wait_craft($wnd.text); }
- | OPEN_INVENTORY
- { ark_bot.OpenInventory(); }
- | ^(SET_SHOW_VAR s=STRING)
- { synchronized (hhl_main.symbols.ShowNames) { hhl_main.symbols.ShowNames.add($s.text); } }
- | SET_ITEM_DRAG
- { ark_bot.set_item_drag(); }
- | ^(RENDER_MODE x=expression)
- { ark_bot.set_render_mode($x.value); }
- | RESET_BUFF
- { ark_bot.reset_buff_iterator(); }
- | ^(MAP_PLACE x=expression y=expression btn=expression mod=expression)
- { if (ark_bot.mapview != null) ark_bot.mapview.map_place($x.value, $y.value, $btn.value, $mod.value); }
- | BUILD_CLICK
- { ark_bot.build_click(); }
- | ^(SET_ITEM_EQUIP i=expression)
- { ark_bot.set_item_equip($i.value); }
- | block
- | NOP
- ;
- // cycle statements
- whileStatement
- scope{
- Boolean breaked;
- Boolean continued;
- }
- @after{
- CommonTree stmtNode=(CommonTree)$whileStatement.start.getChild(1);
- CommonTree exprNode=(CommonTree)$whileStatement.start.getChild(0);
- int test;
- $whileStatement::breaked=false;
- while($whileStatement::breaked==false){
- PushInputTree(exprNode);
- $whileStatement::continued=false;
- test=expression().value;
- PopInputTree();
- if (test==0) break;
- PushInputTree(stmtNode);
- statement();
- PopInputTree();
- }
- }
- : ^(WHILE . .)
- ;
- ifStatement
- @after{
- CommonTree stmtNode = null;
- if ($v.value!=0){
- stmtNode = (CommonTree)$ifStatement.start.getChild(1);
- }else{
- stmtNode = (CommonTree)$ifStatement.start.getChild(2);
- }
- PushInputTree(stmtNode);
- statement();
- PopInputTree();
- }
- : ^(IF ^(EXPR v=expression) . .)
- ;
- //short circuiting or statement
- orStatement returns [int value]
- @after{
- if ($a.value!=0){
- $value=1;
- }else{
- PushInputTree((CommonTree)$orStatement.start.getChild(1));
- $value = expression().value;
- if ($value != 0) $value = 1;
- PopInputTree();
- }
- }
- :
- ^(('or'|'||') a=expression .)
- ;
- //short circuiting and statement
- andStatement returns [int value]
- @after{
- if ($a.value==0){
- $value=0;
- }else{
- PushInputTree((CommonTree)$andStatement.start.getChild(1));
- $value = expression().value;
- if ($value != 0) $value = 1;
- PopInputTree();
- }
- }
- :
- ^(('and'|'&&') a=expression .)
- ;
- call returns [int value]
- scope{
- int return_val;
- Boolean can_run;
- Boolean in_paramater_defs;
- }
- @init {
- Vector params;
- Function fcn = null;
- int p_i;
- $call::can_run=true;
- params=new Vector();
- p_i=0;
- }
- : ^(CALL IDENT {} ^(EXPRLIST (expression
- {
- if ($expression.id_name != null) {
- params.add(hhl_main.symbols.get_variable($expression.id_name));
- }else{
- params.add($expression.value);
- }
- p_i++;
- }
- )* ))
- {
- if (hhl_main.IncludeDepth <= 1)
- fcn=hhl_main.symbols.get_function($IDENT.text, params);
- if (fcn == null) { hhl_main.PrintError("No such function: " + $IDENT.text); }
- if ((fcn != null) && (hhl_main.IncludeDepth <= 1)) {
- hhl_main.symbols.enter_frame(fcn,params);
- PushInputTree(fcn.tree);
- block();
- PopInputTree();
- $value = $call::return_val;
- hhl_main.symbols.exit_frame();
- }
- } ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement