Re: Class programming with Euphoria
- Posted by jmduro Oct 24, 2021
- 842 views
procedure convertOOP(sequence inputFile, sequence outputFile) sequence lines = read_lines(inputFile) integer f_out = open(outputFile, "w") integer from, upto object found, found2 sequence class_name = "", name, params, elements = {} integer start = 0 sequence code = "" -- find class definition regex r = regex:new("class (.*)$") for i = 1 to length(lines) do found = regex:find(r, lines[i]) if sequence(found) then {from, upto} = found[2] class_name = lines[i][from..upto] start = i exit else code &= lines[i] & "\n" end if end for -- add target sequence attributes code &= "enum type %ENUM_TYPE% %ENUM_ITEMS% end type\n\n" code &= "sequence %CLASS_NAME% = {%CLASS_CONTENT%}\n\n" -- analyze class definition sequence enums = {} sequence class_definition = {} integer i = start+1 while i <= length(lines) do sequence trimmed = trim(lines[i]) if match("end class", lines[i]) then start = i exit end if if begins("integer", trimmed) then r = regex:new("integer (.*)$") class_definition= append(class_definition, "0") found = regex:find(r, lines[i]) {from, upto} = found[2] name = lines[i][from..upto] enums = append(enums, upper(name)) --code &= lines[i] & "\n" elements = append(elements, {name, ATTR}) elsif begins("atom", trimmed) then r = regex:new("atom (.*)$") class_definition= append(class_definition, "0.0") found = regex:find(r, lines[i]) {from, upto} = found[2] name = lines[i][from..upto] enums = append(enums, upper(name)) --code &= lines[i] & "\n" elements = append(elements, {name, ATTR}) elsif begins("sequence", trimmed) then r = regex:new("sequence (.*)$") class_definition= append(class_definition, "\"\"") found = regex:find(r, lines[i]) {from, upto} = found[2] name = lines[i][from..upto] enums = append(enums, upper(name)) --code &= lines[i] & "\n" elements = append(elements, {name, ATTR}) elsif begins("datetime", trimmed) then r = regex:new("datetime (.*)$") class_definition= append(class_definition, "now()") found = regex:find(r, lines[i]) {from, upto} = found[2] name = lines[i][from..upto] enums = append(enums, upper(name)) --code &= lines[i] & "\n" elements = append(elements, {name, ATTR}) elsif begins("procedure", trimmed) then r = regex:new("procedure (.*)\\((.*)\\)$") found = regex:find(r, lines[i]) {from, upto} = found[2] name = lines[i][from..upto] enums = append(enums, upper(name)) {from, upto} = found[3] params = lines[i][from..upto] code &= sprintf("procedure %s(sequence entity, sequence params)\n", {name}) {code, i} = manage_routine(code, lines, i) elements = append(elements, {name, PROC}) elsif begins("function", trimmed) then r = regex:new("function (.*)\\((.*)\\)$") found = regex:find(r, lines[i]) {from, upto} = found[2] name = lines[i][from..upto] enums = append(enums, upper(name)) {from, upto} = found[3] params = lines[i][from..upto] code &= sprintf("function %s(sequence entity, sequence params)\n", {name}) {code, i} = manage_routine(code, lines, i) elements = append(elements, {name, FUNC}) elsif begins("end procedure", trimmed) then class_definition= append(class_definition, "0") code &= lines[i] & "\n" code &= sprintf("%s[%s] = routine_id(\"%s\")\n", {class_name, upper(name), name}) elsif begins("end function", trimmed) then class_definition= append(class_definition, "0") code &= lines[i] & "\n" code &= sprintf("%s[%s] = routine_id(\"%s\")\n", {class_name, upper(name), name}) else puts(2, "Error: " & "Unexpected variable type!" & "\n") abort(1) end if i += 1 end while
To be continued ...