forum-msg-id-136399-edit

Original date:2021-10-24 04:28:39 Edited by: jmduro Subject: Re: Class programming with Euphoria

procedure convertOOP(sequence inputFile, sequence outputFile) 
  integer from, upto 
  object found, found2 
  sequence class_name = "", name, params, elements = {} 
  integer start = 0 
   
  sequence lines = read_lines(inputFile) 
  integer f_out = open(outputFile, "w") 
  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 
    lines[i] = trim(lines[i]) 
    if match("end class", lines[i]) then 
      start = i 
      exit 
    end if 
    if begins("integer", lines[i]) 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", lines[i]) 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", lines[i]) 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", lines[i]) 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", lines[i]) 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", lines[i]) 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", lines[i]) 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", lines[i]) 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 ...

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu