efene programming language

efene now JSON friendly - introducing structs

if I describe efene as “a javascript like syntax” then it should plain nice with JSON right?

well, from now I can say it does…

introducing structs:

>>> Me = {firstname: "mariano", lastname: "guerra", location: {city: "cordoba", country: "argentina"}}
{struct,[{firstname,"mariano"},
         {lastname,"guerra"},
         {location,{struct,[{city,"cordoba"},{country,"argentina"}]}}]}

>>> @Me.firstname
"mariano"

>>> @Me.location
{struct,[{city,"cordoba"},{country,"argentina"}]}

>>> @Me.location.city
"cordoba"

>>> @Me.firstname := "Mariano"
{struct,[{firstname,"Mariano"},
         {lastname,"guerra"},
         {location,{struct,[{city,"cordoba"},{country,"argentina"}]}}]}

>>> Me1 = @Me.firstname := "Mariano"
{struct,[{firstname,"Mariano"},
         {lastname,"guerra"},
         {location,{struct,[{city,"cordoba"},{country,"argentina"}]}}]}

>>> Me2 = @Me1.location.country := "Argentina"
{struct,[{firstname,"Mariano"},
         {lastname,"guerra"},
         {location,{struct,[{city,"cordoba"},{country,"Argentina"}]}}]}

>>> @Me.location.country
"argentina"

>>> @Me.email
exception throw: {attribute_error,"Me struct has no attribute 'email'"}
  in function  struct:get/3

>>> @Me.firstname.something
exception throw: {value_error,"firstname doesn't seem to be a struct"}
  in function  struct:get/3

>>> @Me.location.state
exception throw: {attribute_error,"location struct has no attribute 'state'"}
  in function  struct:get/3

>>> Me = {firstname: "mariano", lastname: "guerra", location: {city: "cordoba", country: "argentina"}}
{struct,[{firstname,"mariano"},
         {lastname,"guerra"},
         {location,{struct,[{city,"cordoba"},{country,"argentina"}]}}]}

>>> Json = mochijson.encode(Me)
[123,"\"firstname\"",58,"\"mariano\"",44,"\"lastname\"",58,"\"guerra\"",44,
 "\"location\"",58,
 [123,"\"city\"",58,"\"cordoba\"",44,"\"country\"",58,"\"argentina\"",125],
 125]

>>> io.format("~s~n", [Json])
{"firstname":"mariano","lastname":"guerra","location":{"city":"cordoba","country":"argentina"}}
ok

>>> Data = mochijson.decode(Json)
{struct,[{"firstname","mariano"},
         {"lastname","guerra"},
         {"location",{struct,[{"city","cordoba"},{"country","argentina"}]}}]}

>>> @Data.firstname
"mariano"

>>> io.format("~s~n", [mochijson.encode(@Data.firstname := "Mariano")])
{"firstname":"Mariano","lastname":"guerra","location":{"city":"cordoba","country":"argentina"}}
ok

from the example you can see that the object notation has the same syntax then JSON and python dictionaries.

you should also see that are serialized to and from json without any extra step using mochijson.

also the syntax to access and modify the “structs” is similar to an OO syntax

last but not least, check the descriptive error messages!

let me know what you think. My idea is to put efene “objects” to rest and focus on structs, since they are data structures (not closures), have a nice and friendly syntax and play really nice with JSON


To Tumblr, Love Metalab