Re: Architectural Discussion: Implementing ECS and Data-Oriented Programming (DOP)
- Posted by jmduro 3 weeks ago
- 195 views
Five years ago, I proposed a way to do OOP-like programming using OEU's preprocessor.
Here is its test example, test_classes.oex:
include builtins\timedate.e function get_years(atom secs) atom days = floor(secs/86400) return floor(days/365.25) end function class person public timedate birth_date = date() public sequence birth_place = "" public sequence mother_tongue = "" procedure describe() atom secs = timedate_diff(this.birth_date, date()) printf(1, "\nHello! I speak %s.\n", {this.mother_tongue}) printf(1, "I am %d years old. I was born in %s.\n", {get_years(secs), this.birth_place}) end procedure end class person englishman = new() englishman.birth_date = parse_date_string("May 14 2010",{"Mmmm d yyyy"}) englishman.birth_place = "Liverpool" englishman.mother_tongue = "english" person frenchman = new() frenchman.birth_date = parse_date_string("August 10 1960",{"Mmmm d yyyy"}) frenchman.birth_place = "Thionville" frenchman.mother_tongue = "french" person german = new() german.birth_date = parse_date_string("February 11 1993",{"Mmmm d yyyy"}) german.birth_place = "Munich" german.mother_tongue = "german" englishman.describe() frenchman.describe() german.describe() maybe_any_key()
It converted this code to OEU code, test_classes.ex:
include std/datetime.e include std/console.e include classes.e ------------------------------------------------------------------------------ function get_years(atom secs) atom days = floor(secs/86400) return floor(days/365.25) end function ------------------------------------------------------------------------------ function describe(sequence entity, sequence params) atom secs = diff(getProperty(entity, "birth_date"), now()) printf(1, "\nHello! I speak %s.\n", {getProperty(entity, "mother_tongue")}) if length(params) then printf(1, "I am %d years old. I was born in %s (%s).\n", {get_years(secs), getProperty(entity, "birth_place"), params[1]}) else printf(1, "I am %d years old. I was born in %s.\n", {get_years(secs), getProperty(entity, "birth_place")}) end if return 0 end function ------------------------------------------------------------------------------ procedure main(sequence cmd) object void sequence TPerson = class("TPerson") TPerson = addProperty(TPerson, "birth_date", now()) TPerson = addProperty(TPerson, "birth_place", "") TPerson = addProperty(TPerson, "mother_tongue", "") TPerson = addMethod(TPerson, "describe", routine_id("describe")) sequence englishman = classes:new(TPerson) englishman = setProperty(englishman, "birth_date", datetime:new(2010, 5, 14)) englishman = setProperty(englishman, "birth_place", "Liverpool") englishman = setProperty(englishman, "mother_tongue", "english") sequence frenchman = classes:new(TPerson) frenchman = setProperty(frenchman, "birth_date", datetime:new(1960, 8, 10)) frenchman = setProperty(frenchman, "birth_place", "Thionville") frenchman = setProperty(frenchman, "mother_tongue", "french") sequence german = classes:new(TPerson) german = setProperty(german, "birth_date", datetime:new(1993, 2, 23)) german = setProperty(german, "birth_place", "Munich") german = setProperty(german, "mother_tongue", "german") void = callMethod(englishman, "describe", {"Merseyside"}) void = callMethod(frenchman, "describe", {"Lorraine"}) void = callMethod(german, "describe", {"Bavaria"}) end procedure -- main ------------------------------------------------------------------------------ -- Main main(command_line()) maybe_any_key()
I don't remember all the details. I will rework this to make it run again.
Jean-Marc

