1. I have a structure/object syntax idea

I'm writing a preprocessor to translate Euphoria code using my struct syntax to commom Euphoria code. For now i provide a demonstration, i hope well explained. Please, give me your two cents.

/* I had thinkered how create a undestandable structure feature in Euphoria, besides the non-oficial memstruct 

construct. 
 My desire it's a feature that seems C structures but with much more power. 
 Something generic and doable for using with dll function declarations, that follow the commom code 
style, with a minimal syntax change. 
 It need to be usable with legacy code without causing big headaches. "Enter and drive away NOW!". 
 
 My great idea was create a new type of function, that is alike the type-functions. 
 
 Let's go to a illustrated explanation. 
*/ 
 
/* Imagine that i want to write OO-like code in Euphoria. 

 The following it's a JavaScript-like "object constructor" function. 
*/ 
 
function new_point (integer x, integer y) 
   if x>0 and y>0 then 
      return {x, y} 
   end if 
   return 0 
end function 
 
/* Now i want to access the object's values using meaningful names, 

i want named fields without using a Euphoria's map. 
 We have the enum to help this. 
*/ 
 
enum X, Y 
 
/* And i want to use a type-function to validate the new created object. 

(to permit me to implement a instanceOf like function later :)). 
 This is the "new_point" sister function: 
*/ 
 
type point (sequence p) 
   if length(p) > 0 then 
   --** correspond to the "new_point()" body. 
      if p[X]>=0 and p[Y]>=0 then 
         return length(p) -- seems the most util to me 
      end if 
   --** 
   end if 
   return 0 
end type 
 
/* And now, we can do this: 

*/ 
 
object a = new_point(1,2) 
point b = new_point(3,4) 
? a[X] 
 
/* What i shown here it's the semantic base to support the new Euphoria structure syntax i'm 

proposing. 
 
 First, you declare a structure, providing its name, some code and field's names. This is the 
"new_point()" role. 
  
 Then, you want to access the object's values with its field's names, that's the purpose of the 
enum declaration. 
  
 To use the correct field's names, and to identifiate the structure of a object you took from 
somewhere, you use the object's struct variable-type identifier, preferably with same name you 
had created. 
 That's the role of "point()" function. 
  
 Now comes my syntax proposal. 
 I pretend to use a struct-function declarer, like the type-functions. 
 To access the fields using names, a dot notation. 
 
 Every struct begins with a struct-function. 
 A struct-function changes the "type" word from type-functions, by the "struct" word. 
 The fields names are given inside a parentesis couple, that appear the arguments parentesis 
couple. 
 This fields names parentesis follow immediatelly the arguments parentesis couple. 
 Think about it, it's as if we have "input and output" parameters. But the "output" parameters 
are the fields names. 
  
 The fields names parameters do not create variables in the struct-function scope. They are 
used only outside the function, with the dot notation. 
  
 A non-sense example, just to show the syntax: 
*/ 
 
procedure task_accept_clients () 
   while sock:listen(server, 10) = sock:OK do 
      object client = sock:accept(server) 
      if sequence(client) then 
         handle_request( network_request (client) ) -- creating a object with a structure 
         sock:close(client[1]) 
      end if 
      task_yield() 
   end while 
end procedure 
-- restricting the input with a type-like function 
procedure handle_request (network_request newcli) 
   if not atom(newcli) then 
      if newcli.in_buffer[0] > 1 then -- using the dot notation to access a value with a name 
         newcli = receive_new_bytes (newcli) 
      end if 
   end if 
end procedure 
 
-- The star of the show: a struct-function. 
/* My preprocessor will take it, create a "new_network_request()" func, a "network_request()" 

type-function, and a "enum network_request__client network_request__in_buffer" declaration. 
*/ 
 
struct network_request (socket client) -- the input parameters, as we are accoustumed 
                       -- the fields names, that go only to our enum 
                       (socket client, sequence in_buffer)  
   object _ = sock:receive (client) 
   if atom(_) then 
      return 0 
   else 
      return {client, _} -- simple, huh? 
   end if 
end struct 
 
function receive_more_bytes (network_request cli) 
   if atom(cli) then return 0 end if 
   object _ = sock:receive(cli.client) -- again, dot notation 
   if atom(_) then 
      return 0 
   else 
      -- this will be translated to: 
      --    cli[network_request__in_buffer] &= _ 
      cli.in_buffer &= _  
   end if 
   return cli 
end function 
 
/* To make my syntax proposal very clear: 

*/ 
 
sequence animal_species = { 
   "horse", 
   "dog" 
} 
 
struct animal_ (sequence species, sequence name) 
               -- the fields definition does not create any variable in the 
               -- struct-function internal scope 
               (integer species_type, sequence name) 
   integer a = find (species, animal_species) 
   if a > 0 then 
      -- there's not a "species_type" variable declared in this scope, so this is a error 
      species_type = a   
      name = name       -- and this simply don't make sense at all 
   else 
      return 0 
   end if 
end struct 
 
/* There is not a problem if your return has less elements that the defined fields IMHO. 

 Here, the "nick" field is by default left empty. 
 My "animal()" type-function will return the object length for you, then it's easy to verify 
this situation. 
*/ 
 
struct animal (sequence species, sequence name) 
              (integer species_type, sequence name, sequence nick) 
   integer a = find (species, animal_species) 
   if a > 0 then 
      return {a, name} -- this is the correct manner to return the struct 
   else 
      return 0 
   end if 
end struct 
new topic     » topic index » view message » categorize

2. Re: I have a structure/object syntax idea

It will be very interesting to see if you can make a pre-processor demo to illustrate your ideas.

My guess is that if you provide dot notation and structures you will end up inventing classes (attributes and methods) and instances of entities (OOP-objects).

A pre-processor that gives us the option of writing in an OOP-style (while not a favorite with some classic Euphoria users) should have great appeal to new Euphoria converts. Dot notation, for example, has a certain attraction.

The trick is to keep the pre-processor from becoming a complete programming language itself and while still keeping familiar Euphoria syntax.

Go ahead and invent. I, and many others, will be interested in what you can create.

_tom

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu