Namdak Tonpa (maxim) wrote,
Namdak Tonpa
maxim

IO PROTOCOL


1.

-record(error, {code=[] :: [] | binary()}).
-record(ok,    {code=[] :: [] | binary()}).
-record(io,    {code=[] :: [] | #ok{} | #error{},
                data=[] :: [] | <<>> | {atom(),binary()|integer()}}).

2.

Form: {error,
          [{typed_record_field,
               {record_field,15,{atom,15,code},{nil,15}},
               {type,15,union,[{type,15,nil,[]},{type,15,binary,[]}]}}]}

Form: {ok,[{typed_record_field,{record_field,16,{atom,16,code},{nil,16}},
                               {type,16,union,
                                     [{type,16,nil,[]},
                                      {type,16,binary,[]}]}}]}

Form: {io,[{typed_record_field,
               {record_field,17,{atom,17,code},{nil,17}},
               {type,17,union,
                   [{type,17,nil,[]},
                    {type,17,record,[{atom,17,ok}]},
                    {type,17,record,[{atom,17,error}]}]}},
           {typed_record_field,
               {record_field,18,{atom,18,data},{nil,18}},
               {type,18,union,
                   [{type,18,nil,[]},
                    {type,18,binary,[{integer,18,0},{integer,18,0}]},
                    {type,18,tuple,
                        [{type,18,atom,[]},
                         {type,18,union,
                             [{type,18,binary,[]},
                              {type,18,integer,[]}]}]}]}}]}

3.

-module(swift).
-compile({parse_transform, swift}).
-export([parse_transform/2]).
-compile(export_all).

parse_transform(Forms, _Options) ->
    Directives = directives(Forms),
    Result = lists:flatten([directives(Forms)]),
    file:write_file("model.swift", list_to_binary(Result)),
    Forms.

directives(Forms) -> lists:flatten([ form(F) || F <- Forms ]).

form({attribute,_,record,{List,T}})  ->
   [X|Rest]=atom_to_list(List),
   case X >= $A andalso X =< $Z orelse List =:= io
                                orelse List == error
                                orelse List == ok of true -> class(List,T); _ -> [] end;
form(Form) ->  [].

class(List,T) ->
   case lists:concat([ io_lib:format("\n    var ~s",
                [ infer(Name,Args,nitro:to_list(Field))])
               || {_,{_,_,{atom,_,Field},Value},{type,_,Name,Args}} <- T ]) of
               [] -> [];
               Fields -> lists:concat(["\nclass ",List," {", Fields, "\n}"]) end.

keyword(list)    -> "String?";
keyword(term)    -> "AnyObject?";
keyword(integer) -> "Int?";
keyword(boolean) -> "Bool?";
keyword(atom)    -> "String?";
keyword(binary)  -> "String?";
keyword(union)   -> "AnyObject?";
keyword(nil)     -> "AnyObject?".

infer(union,Args,Field) -> Field ++ ": " ++ simple(Args);
infer(Name,Args,Field)  -> Field ++ ": " ++ keyword(Name).

simple([{type,_,nil,_},{type,_,Name,_}]) -> keyword(Name);
simple([{type,_,Name,_},{type,_,nil,_}]) -> keyword(Name);
simple(_) -> "AnyObject?".

4.

$ cat roster.swift

class error {
    var code: String?
}
class ok {
    var code: String?
}
class io {
    var code: AnyObject?
    var data: AnyObject?
}
Tags: #error, #ok, n2o
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

  • 0 comments