Re: Object-oriented preprocessor

new topic     » goto parent     » topic index » view thread      » older message » newer message

If the preprocessor was not in alpha stage following win32lib demo (mletext.exw) could have been written in OOP style:

include win32lib_r2.ew 
without warning 
 
constant w = create(Window, "MLE Test", 0, 0, 0, 400, 300, 0), 
         m = create(MleText, "", w, 10, 10, 200, 200, 0) 
          
setText(m, "Line 1\nLine 2\nLine 3") 
 
WinMain(w, Normal)          

OOP style:

include win32lib_r2.ew as win 
 
class TWindow 
  integer id 
  sequence caption 
  atom pOwner 
  object x 
  object y 
  object cx 
  object cy 
  object styleFlags 
   
  procedure create(sequence params) 
    self.caption = params[1] 
    self.pOwner = params[2] 
    self.x = params[3] 
    self.y = params[4] 
    self.cx = params[5] 
    self.cy = params[6] 
    self.styleFlags = params[7] 
    self.id = win:create(Window, 
      self.caption, self.pOwner, 
      self.x, self.y, self.cx, self.cy, 
      self.styleFlags) 
  end procedure 
end class 
 
class TMleText 
  integer id 
  sequence caption 
  atom pOwner 
  object x 
  object y 
  object cx 
  object cy 
  object styleFlags 
   
  procedure create(sequence params) 
    self.caption = params[1] 
    self.pOwner = params[2] 
    self.x = params[3] 
    self.y = params[4] 
    self.cx = params[5] 
    self.cy = params[6] 
    self.styleFlags = params[7] 
    self.id = win:create(MleText, 
      self.caption, self.pOwner, 
      self.x, self.y, self.cx, self.cy, 
      self.styleFlags) 
  end procedure 
   
  procedure set(sequence params) 
    win:setText(self.id, params[1]) 
  end procedure 
   
  function get(sequence params) 
    win:getText(MleText) 
  end function 
end class 
 
sequence win = new(TWindow) 
win.create({"MLE Test", 0, 0, 0, 400, 300, 0}) 
 
sequence mle = new(TMleText) 
mle.create({"", w, 10, 10, 200, 200, 0}) 
mle.set({"Line 1\nLine 2\nLine 3"}) 
 
WinMain(win.id, Normal)          

This sould have been translated to:

include win32lib_r2.ew as win 
without warning 
 
procedure class_proc(sequence entity, integer method, sequence params = {}) 
  call_proc(entity[method], {entity, params}) 
end procedure 
 
function class_func(sequence entity, integer method, sequence params = {}) 
  return call_func(entity[method], {entity, params}) 
end function 
 
 
enum type TWINDOW 
  TWINDOW_ID, TWINDOW_CAPTION, TWINDOW_POWNER, 
  TWINDOW_X, TWINDOW_Y, TWINDOW_CX, TWINDOW_CY, 
  TWINDOW_STYLEFLAGS, 
 
  TWINDOW_CREATE 
end type 
 
sequence TWindow = {0,"",0,0,0,0,0,0, 
                    routine_id("TWindow_create")} 
 
function TWindow_create(sequence entity, sequence params) 
  entity[TWINDOW_CAPTION] = params[1] 
  entity[TWINDOW_POWNER] = params[2] 
  entity[TWINDOW_X] = params[3] 
  entity[TWINDOW_Y] = params[4] 
  entity[TWINDOW_CX] = params[5] 
  entity[TWINDOW_CY] = params[6] 
  entity[TWINDOW_STYLEFLAGS] = params[7] 
  entity[TWINDOW_ID] = win:create(Window, 
    entity[TWINDOW_CAPTION], entity[TWINDOW_POWNER], 
    entity[TWINDOW_X], entity[TWINDOW_Y], entity[TWINDOW_CX], entity[TWINDOW_CY], 
    entity[TWINDOW_STYLEFLAGS]) 
  return entity 
end function 
 
enum type TMLETEXT 
  TMLETEXT_ID, TMLETEXT_CAPTION, TMLETEXT_POWNER, 
  TMLETEXT_X, TMLETEXT_Y, TMLETEXT_CX, TMLETEXT_CY, 
  TMLETEXT_STYLEFLAGS, 
 
  TMLETEXT_CREATE, 
  TMLETEXT_SET, 
  TMLETEXT_GET 
end type 
 
sequence TMleText = {0,"",0.0,0,0,0,0,0, 
                     routine_id("TMleText_create"), 
                     routine_id("TMleText_set"), 
                     routine_id("TMleText_get")} 
 
function TMleText_create(sequence entity, sequence params) 
  entity[TMLETEXT_CAPTION] = params[1] 
  entity[TMLETEXT_POWNER] = params[2] 
  entity[TMLETEXT_X] = params[3] 
  entity[TMLETEXT_Y] = params[4] 
  entity[TMLETEXT_CX] = params[5] 
  entity[TMLETEXT_CY] = params[6] 
  entity[TMLETEXT_STYLEFLAGS] = params[7] 
  entity[TMLETEXT_ID] = win:create(MleText, 
    entity[TMLETEXT_CAPTION], entity[TMLETEXT_POWNER], 
    entity[TMLETEXT_X], entity[TMLETEXT_Y], entity[TMLETEXT_CX], entity[TMLETEXT_CY], 
    entity[TMLETEXT_STYLEFLAGS]) 
  return entity 
end function 
 
procedure TMleText_set(sequence entity, sequence params) 
  win:setText(entity[TMLETEXT_ID], params[1]) 
end procedure 
 
function TMleText_get(sequence entity, sequence params) 
  return win:getText(entity[TMLETEXT_ID]) 
end function 
 
sequence win = TWindow 
win = class_func(win, TWINDOW_CREATE, {"MLE Test", 0, 0, 0, 400, 300, 0}) 
 
sequence mle = TMleText 
mle = class_func(mle, TMLETEXT_CREATE, {"", win[TWINDOW_ID], 10, 10, 200, 200, 0}) 
class_proc(mle, TMLETEXT_SET, {"Line 1\nLine 2\nLine 3"}) 
 
WinMain(win[TWINDOW_ID], Normal)          

Targeted translated code has been tested: it works like the original demo.

Unfortunately, the preprocessor is far from being able to translate the code correctly. It doesn't work with multiple classes in same file. It doesn't work with trailing spaces, and so on.

Jean-Marc

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu