Re: Class programming with Euphoria

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

Here is the resulting code:

include std/datetime.e 
include std/console.e 
 
function get_years(atom secs) 
  atom days = floor(secs/86400) 
  return floor(days/365.25) 
end function 
 
enum type PERSON BIRTH_DATE,BIRTH_PLACE,MOTHER_TONGUE,DESCRIBE end type 
 
sequence person = {now(),"","",0} 
 
datetime birth_date 
sequence birth_place 
sequence mother_tongue 
procedure describe(sequence entity, sequence params) 
atom secs = diff(entity[BIRTH_DATE], now()) 
printf(1, "\nHello! I speak %s.\n", {entity[MOTHER_TONGUE]}) 
if length(params) then 
printf(1, "I am %d years old. I was born in %s (%s).\n", {get_years(secs), entity[BIRTH_PLACE], params[1]}) 
else 
printf(1, "I am %d years old. I was born in %s.\n", {get_years(secs), entity[BIRTH_PLACE]}) 
end if 
end procedure 
person[DESCRIBE] = routine_id("describe") 
 
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 
 
 
sequence englishman = person 
englishman[BIRTH_DATE] = datetime:new(2010, 5, 14) 
englishman[BIRTH_PLACE] = "Liverpool" 
englishman[MOTHER_TONGUE] = "english" 
 
sequence frenchman = person 
frenchman[BIRTH_DATE] = datetime:new(1960, 8, 10) 
frenchman[BIRTH_PLACE] = "Thionville" 
frenchman[MOTHER_TONGUE] = "french" 
 
sequence german = person 
german[BIRTH_DATE] = datetime:new(1993, 2, 23) 
german[BIRTH_PLACE] = "Munich" 
german[MOTHER_TONGUE] = "german" 
 
class_proc(englishman, DESCRIBE, {"Merseyside"}) 
 
class_proc(frenchman, DESCRIBE, {"Lorraine"}) 
 
class_proc(german, DESCRIBE, {"Bavaria"}) 
 
 
maybe_any_key() 

Here is the generated output of the converted code:

 
Hello! I speak english. 
I am 11 years old. I was born in Liverpool (Merseyside). 
 
Hello! I speak french. 
I am 61 years old. I was born in Thionville (Lorraine). 
 
Hello! I speak german. 
I am 28 years old. I was born in Munich (Bavaria). 
Press Any Key to continue... 

The preprocessor can be adapted to Euphoria 3.1. Enjoy!

Jean-Marc

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

Search



Quick Links

User menu

Not signed in.

Misc Menu