Wc3C.net

Wc3C.net (http://www.wc3c.net/forums.php)
-   Warcraft Editing Tools (http://www.wc3c.net/forumdisplay.php?f=13)
-   -   JassHelper 0.A.2.B - A vJass and Zinc 2 Jass compiler (http://www.wc3c.net/showthread.php?t=88142)

Vexorian 12-20-2006 01:37 PM

Yes I think polishing is next, I finally got the ability to get common.j and blizzard.j in WEHelper (otherwise I would create a fork) so I can parse common.j for types which will allow me to add a typecast operator and also recognize bad type usage.

PipeDream 12-21-2006 10:40 PM

static methods don't seem to work with interfaced objects (0.9.4)
Collapse JASS:
interface Ord        //Object with an ordering defined on it
    method geq takes Ord right returns boolean
endinterface

struct KVPair extends Ord
    real key
    real value
    static method Create takes real key, integer value returns KVPair
        local KVPair p = KVPair.create()
        set p.key = key
        set p.value = value
        return p
    endmethod
    method geq takes Ord right returns boolean
        local KVPair p = right
        return this.key > p.key
    endmethod
endstruct
It turns Create() into a dynamic method but doesn't define the result_integer
It's np in this case since work around is just
Collapse JASS:
interface Ord        //Object with an ordering defined on it
    method geq takes Ord right returns boolean
endinterface

struct KVPair extends Ord
    real key
    real value
    method geq takes Ord right returns boolean
        local KVPair p = right
        return this.key > p.key
    endmethod
endstruct

function KVPair_Create takes real key, integer value returns KVPair
    local KVPair p = KVPair.create()
    set p.key = key
    set p.value = value
    return p
endfunction

-----

A practical example of why JASSHelper rocks the socks:
Collapse JASS:
interface Ord        //Object with an ordering defined on it
    method geq takes Ord right returns boolean
    method to_s takes nothing returns string
endinterface

struct KVPair extends Ord
    real key
    integer value
    method geq takes Ord right returns boolean
        local KVPair p = right
        return this.key > p.key
    endmethod
    method to_s takes nothing returns string
        return R2S(this.key)
    endmethod
endstruct

function KVPair_Create takes real key, integer value returns KVPair
    local KVPair p = KVPair.create()
    set p.key = key
    set p.value = value
    return p
endfunction

struct Node
    Ord p
    Node left
    Node right
    integer n
    static method Create takes Ord p returns Node
        local Node n = Node.create()
        set n.p = p
        set n.left = 0
        set n.right = 0
        set n.n = 0
        return n
    endmethod
    //must maintain heap property
    //parent > both children
    //both children heaps
    method Insert takes Ord p returns nothing
        local Ord displaced
        //Decide whether to displace self or new pair
        if p.geq(this.p) then
            set displaced = p
        else 
            set displaced = this.p
            set this.p = p
        endif
        //Push displaced element down while maintaining balance
        //if missing a child, fill the hole left to right
        if this.left == 0 then
            set this.left = Node.Create(displaced)
        elseif this.right == 0 then
            set this.right = Node.Create(displaced)
        //have both children, so add it where there are fewer children
        elseif this.left.n > this.right.n then
            call this.left.Insert(displaced)
        else
            call this.right.Insert(displaced)
        endif
        set this.n = this.n + 1
    endmethod
    method Pop takes nothing returns Node
        set this.n = this.n - 1
        //Leaf
        if this.left == 0 and this.right == 0 then
            call Node.destroy(this)
            return 0
        elseif this.left == 0 then
            set this.p = this.right.p
            set this.right = this.right.Pop()
            return this
        elseif this.right == 0 then
            set this.p = this.left.p
            set this.left = this.left.Pop()
            return this
        //Branch
        elseif this.left.p.geq(this.right.p) then
            set this.p = this.right.p
            set this.right = this.right.Pop()
            return this
        else
            set this.p = this.left.p
            set this.left = this.left.Pop()
            return this
        endif
    endmethod
    method to_s takes nothing returns string
        if this == 0 then
            return "0"
        else
            return "("+this.left.to_s()+","+this.p.to_s()+","+this.right.to_s()+")"
        endif
    endmethod
endstruct

//Does not destroy the Ord
struct Heap    //boxed heap
    Node top
    static method Create takes nothing returns Heap
        local Heap h = Heap.create()
        set h.top = 0
        return h
    endmethod
    method Insert takes Ord p returns nothing    //Here's where templating / polymorphism would be nice
        if this.top == 0 then
            set this.top = Node.Create(p)
        else
            call this.top.Insert(p)
        endif
    endmethod
    method Peek takes nothing returns Ord
        if this.top == 0 then
            return 0
        else
            return this.top.p
        endif
    endmethod
    method Pop takes nothing returns nothing
        if this.top != 0 then
            set this.top = this.top.Pop()
        endif
    endmethod
    method Print takes nothing returns nothing
        call BJDebugMsg(this.top.to_s())
    endmethod
endstruct

function OrdToKV takes Ord o returns KVPair
    return o
endfunction

function Trig_test_Actions takes nothing returns nothing
    local Heap h = Heap.Create()
    local KVPair p 
    set p = KVPair_Create(3.0,2)
    call h.Insert(p)
    set p = KVPair_Create(5.0,7)
    call h.Insert(p)
    set p = KVPair_Create(1.0,1)
    call h.Insert(p)
    set p = KVPair_Create(8.0,3)
    call h.Insert(p)
    call h.Print()
    call BJDebugMsg(I2S(OrdToKV(h.Peek()).value))
    call h.Pop()
    call h.Print()
    call BJDebugMsg(I2S(OrdToKV(h.Peek()).value))
    call h.Pop()
    call BJDebugMsg(I2S(OrdToKV(h.Peek()).value))
    call h.Pop()
    call BJDebugMsg(I2S(OrdToKV(h.Peek()).value))
    call h.Pop()
endfunction
This is a priority queue for a generic "Ord" type that requires only a compare method implemented.
We can now implement proper abstract data types! Well, we could before, but this takes out so much manual manipulation pain, and they're actually types now.

Vexorian 12-23-2006 02:47 AM

Yes pipe, I figured that out the other day, and added a known issues section to the first post...

Vexorian 12-24-2006 02:08 PM

Added yet another bug to known issues, these got big so an update is going to be a priority but I am busy this couple of days

SFilip 12-24-2006 02:26 PM

Something like this doesn't seem to be legal (the map wont load if you add it) - the callback should be above the create method, yet JassHelper parses it with no errors.
Collapse JASS:
struct timerstruct
    timer t
    static method CreateWithCallback takes nothing returns timerstruct
        local timerstruct ts = timerstruct.create()
        set ts.t = CreateTimer()
        call TimerStart(ts.t, 10., false, function timerstruct.callback)
        return ts
    endmethod
    static method callback takes nothing returns nothing
        // some actions here
    endmethod
endstruct
Not much of a big deal, but I found it weird that JH doesn't report it so I just thought you should know about it.

Vexorian 12-24-2006 02:32 PM

It should be legal actually.

But it would create a callback that calls back. If it doesn't load then it is a PJASS problem, let me test that code either way.

Edit: It is a JASSHelper bug and a PJASS bug, JASSHelper for some reason forgets to place a linebreak in the sc version of callback and PJASS ignores the lack of linebreak and validates the code.

BlinkBoy 12-26-2006 08:13 AM

So you adding to it anything new?. I think you should add Events for Objects.

Collapse JASS:
// Just a simple example, to help encapsulation
struct data
         string dat = ""
         // Events only takes values, they don't return any message
         Event CreatedData takes  data TriggeringData
         static method Create takes string s returns data
                  local data d = data.create()
                  set d.dat = s
                  // calls a function that makes every trigger with this event to run
                  call data.RaiseEvent(CreatedData(d))
                  return d
          endmethod
endstruct

function Trig_Test2_Actions takes nothing returns nothing
         call BJDebugMsg("Someone just created a Data type with dat: " + data.TriggeringData.dat)
endfunction

//===========================================================================
function InitTrig_Test2 takes nothing returns nothing
    set gg_trg_Test2 = CreateTrigger(  )
    call data.RegisterEvent(gg_trg_Test2, CreatedData)
    call TriggerAddAction( gg_trg_Test, function Trig_Test2_Actions )
endfunction

function Trig_Test_Actions takes nothing returns nothing
local data mydat = data.Create("I just filled the string")
call mydat.destroy()
endfunction

//===========================================================================
function InitTrig_Test takes nothing returns nothing
    set gg_trg_Test = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_Test, 2.00 )
    call TriggerAddAction( gg_trg_Test, function Trig_Test_Actions )
endfunction
       

Vexorian 12-26-2006 03:17 PM

maybe, but certainly not with that syntax, I'll have to wonder about how to implement it a lot.

...
0.9.4.1 is up, fixing some bugs and documenting the features.

shadow1500 12-26-2006 09:16 PM

Not really Object related but still very useful:
Collapse JASS:
function DoSomething takes nothing returns nothing
      call ForGroup(aGroup, function {call KillUnit(GetEnumUnit())} )
endfunction
Compiles to:
Collapse JASS:
function DoSomething_cb_1 takes nothing returns nothing
     call KillUnit(GetEnumUnit())
endfunction
function DoSomething takes nothing returns nothing
     call ForGroup(aGroup, function DoSomething_cb_1 )
endfunction

BlinkBoy 12-27-2006 02:58 AM

Ok, maybe I'll give you some ideas on how it should compile.

Here is the code of the last time, with some syntax changes

Collapse JASS:
// Just a simple example, to help encapsulation
struct data
         string dat = ""
         // Events only takes values, they don't return any message
         Event CreatedData takes  data TriggeringData
         static method Create takes string s returns data
                  local data d = data.create()
                  set d.dat = s
                  // calls a function that makes every trigger with this event to run
                  call data.CreatedData.RaiseEvent(d)
                  return d
          endmethod
endstruct

function Trig_Test2_Actions takes nothing returns nothing
         call BJDebugMsg("Someone just created a Data type with dat: " + data.TriggeringData.dat)
endfunction

//===========================================================================
function InitTrig_Test2 takes nothing returns nothing
    set gg_trg_Test2 = CreateTrigger(  )
    call data.CreatedData.RegisterEvent(gg_trg_Test2)
    call TriggerAddAction( gg_trg_Test, function Trig_Test2_Actions )
endfunction

function Trig_Test_Actions takes nothing returns nothing
local data mydat = data.Create("I just filled the string")
call mydat.destroy()
endfunction

//===========================================================================
function InitTrig_Test takes nothing returns nothing
    set gg_trg_Test = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_Test, 2.00 )
    call TriggerAddAction( gg_trg_Test, function Trig_Test_Actions )
endfunction


Now compiled without structs compiled yet, would look like this.


Collapse JASS:
globals
         data ss_data_CreatedData_TriggeringData
         trigger array ss_data_CreatedData_Triggers
         integer ss_data_CreatedData_Triggers_Index
endglobals

function ss_data_CreatedData_Add takes trigger trig returns nothing
local integer this = ss_data_CreatedData_Triggers_Index
if this < 8192 and trig != null then
   set ss_data_CreatedData_Triggers[trig]
   set ss_data_CreatedData_Triggers_Index = ss_data_CreatedData_Triggers_Index + 1
endif
endfunction

function ss_data_CreatedData_Run takes data this0 returns nothing
local integer this
if ss_data_CreatedData_Triggers_Index then
   set ss_data_CreatedData_TriggeringData = this0
   loop
        exitwhen this >= ss_data_CreatedData_Triggers_Index and this < 8192
        call ConditionalTriggerExecute( ss_data_CreatedData_Triggers[this] )
        set this = this + 1
   endloop
endif
endfunction

struct data
         string dat = ""
         static method Create takes string s returns data
                  local data d = data.create()
                  set d.dat = s
                  // calls a function that makes every trigger with this event to run
                  call ss_data_CreatedData_Run(d)
                  return d
          endmethod
endstruct
////////////////////////////////////////////////////////////////////////////////////////
// If conditions have wait calls or may make the thread go to sleep, then it is needed, to save the value in a local variable and 
//reassign it before a return action, if the return action specifies false, then this won't be done
////////////////////////////////////////////////////////////////////////////////////////

function Trig_Test2_Conditions takes nothing returns boolean
local data TriggeringData_Event = ss_data_CreatedData_TriggeringData
// some if statements and other things
set ss_data_CreatedData_TriggeringData = TriggeringData_Event
    return true
endfunction

function Trig_Test2_Actions takes nothing returns nothing
local data TriggeringData_Event = ss_data_CreatedData_TriggeringData
         call BJDebugMsg("Someone just created a Data type with dat: " + TriggeringData_Event)
endfunction

//===========================================================================
function InitTrig_Test2 takes nothing returns nothing
    set gg_trg_Test2 = CreateTrigger(  )
    call ss_data_CreatedData_Add(gg_trg_Test2)
    call TriggerAddAction( gg_trg_Test, function Trig_Test2_Actions )
    call TriggerAddCondition( gg_trg_Test2, Condition( function Trig_Test2_Conditions ) )
endfunction

function Trig_Test_Actions takes nothing returns nothing
local data mydat = data.Create("I just filled the string")
call mydat.destroy()
endfunction

//===========================================================================
function InitTrig_Test takes nothing returns nothing
    set gg_trg_Test = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_Test, 2.00 )
    call TriggerAddAction( gg_trg_Test, function Trig_Test_Actions )
endfunction

Ryude 12-27-2006 09:52 PM

Will there be any tutorials coming out for this? I've got a little programming knowledge but I have no clue how to use this.

It looks like it could be very useful if I could just understand what it is.

Sorry for being dumb :P

SFilip 12-27-2006 11:45 PM

You will find the ever so useful jasshelpermanual.html inside the zip. I suggest that you read it.

Vexorian 12-29-2006 03:40 PM

0.9.4.2 is up, just a minor bug fix and also the beta of //! import for the grimoire version , the WEHelper version does not add it since WEHelper has a more stable and complete //! import already.

//! import currently only supports fixed paths

Collapse JASS:
   //! import c:\err.j
 function callit takes nothing returns nothing 
     call it() // for example , in theory the it  function was in err.j
 endfunction


//! import "C:\err.j" and //! import "C:\\err.j" should also work.

Themerion 12-29-2006 03:54 PM

I'm not sure if this is possible with the current version. It would be a neat add-on.

Collapse JASS:
//! library X requires CustomScriptSection

// globals, structs, functions, etc...

//! endlibrary

SFilip 12-29-2006 06:02 PM

Wasn't avoiding the use of the custom script section the point of having libraries in the first place?

Anyway Vexorian in the manual, the interface part, second example...you used "square" in a couple of places instead of the actual struct name.
Collapse JASS:
    interface withpos
        real x
        real y
    endinterface

    struct rectangle extends withpos
        real a
        real b

        static method from takes real x, real y, real a, real b returns square
         local square r=square.create()
            set r.x=x
            set r.y=y
            set r.a=a
            set r.b=b
        endmethod

    endstruct

    struct circle extends withpos
        real radius=67.0

        static method from takes real x, real y, real rad returns square
         local circle r=square.create()
            set r.x=x
            set r.y=y
            set r.radius=rad
        endmethod

    endstruct


    function distance takes withpos A, withpos B returns real
     local real dy=A.y-B.y
     local real dx=A.x-B.x

     return SquareRoot( dy*dy+dx*dx)
    endfunction

    function test takes nothing returns nothing
     local circle c= circle.from(12.0, 45.0 , 13.0)
     local rectangle r = rectangle.from ( 12.3 , 67.8, 12.0 , 10.0)

         call BJDebugMsg(R2S(distance(c,r)))

    endfunction


All times are GMT. The time now is 04:40 AM.

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services