Jonathan Graveline pushed to branch graveline at Stefan / Typer
Commits: 0259aaff by Jonathan Graveline at 2018-05-26T21:10:19Z cleanup - - - - -
1 changed file:
- samples/do.typer
Changes:
===================================== samples/do.typer ===================================== --- a/samples/do.typer +++ b/samples/do.typer @@ -1,53 +1,43 @@ -% -% -% -% Here's an example : -% `` -% fun = action ( -% newline; -% tab; -% a <- return "Hello world"; -% b <- return "!"; -% print a; -% print b; -% newline; -% newline; -% return 0; -% ); -% `` -% `print` take a `String` as `a` and `b` are bind by macro. -% -% `action` may take `action` -% -% (Change : var are now binded, now using ; to separate command) -% - -no-var = Sexp_symbol "_"; -assign = Sexp_symbol "<-"; +%%% +%%% Macro : action +%%% + +%% +%% Here's an example : +%% +%% fun = action ( +%% str <- return "\n\tHello world\n\n"; +%% print str; +%% ); +%% +%% str is bind by macro, +%% +%% fun is a command, +%% +%% action may contain action because it returns a command. +%%
-ferr = (lambda _ -> Sexp_error); -ffollow = (lambda x y -> x); +assign = Sexp_symbol "<-";
get-sym : Sexp -> Sexp; get-sym sexp = let
- kno-var = (lambda _ -> no-var); + dflt-sym = (lambda _ -> Sexp_symbol "_");
in Sexp_dispatch sexp
( lambda s ss -> if_then_else_ (Sexp_eq (List_nth 0 ss Sexp_error) assign) (s) - (no-var) + (dflt-sym ()) )
- %% If there's no assign there should be an IO op - kno-var kno-var kno-var - kno-var kno-var; + dflt-sym dflt-sym dflt-sym + dflt-sym dflt-sym; % there must be a command
-get-op : Sexp -> Sexp -> Sexp; -get-op lsym sexp = let +get-op : Sexp -> Sexp; +get-op sexp = let
- follow = ffollow sexp; + as-is = lambda _ -> sexp;
helper = (lambda sexp -> Sexp_dispatch sexp
@@ -56,7 +46,7 @@ get-op lsym sexp = let (Sexp_node s ss) )
- follow follow follow follow follow + as-is as-is as-is as-is as-is );
op = (helper sexp); @@ -66,26 +56,29 @@ in if_then_else_ (Sexp_eq op (Sexp_symbol "")) Sexp_error op; get-decl : List Sexp -> List Sexp; get-decl args = let
- ferr = lambda _ -> cons Sexp_error nil; + err = lambda _ -> cons Sexp_error nil;
in Sexp_dispatch (List_nth 0 args Sexp_error)
(lambda s ss -> if_then_else_ (Sexp_eq (Sexp_symbol "_;_") s)
- % return declarations - (ss) + (ss) % return declarations
- % else assume there's only one declaration - (cons (Sexp_node s ss) nil) + (cons (Sexp_node s ss) nil) % assuming there's only one declaration ) (lambda s -> cons (Sexp_symbol s) nil) - ferr ferr ferr ferr; - -%% IO_bind a-op (lambda a-sym -> [next fun]) -%% IO_bind a-op (lambda a-sym -> (IO_bind b-op (lambda b-sym -> [next fun]))) + err err err err; + +%% +%% The idea of the macro is : +%% +%% IO_bind a-op (lambda a-sym -> [next command or return a-sym]) +%% IO_bind a-op (lambda a-sym -> (IO_bind b-op (lambda b-sym -> [next command or return a-sym]))) +%% %% this way a-sym is defined within b-op and so on %% a-sym is now just `a` and not `IO a` +%%
set-fun : List Sexp -> Sexp; set-fun args = let @@ -96,16 +89,11 @@ set-fun args = let
sym = get-sym s;
- op = get-op lsym s; - - % Simpler to directly bind command to variable - % also return last variable so you may `return` something + op = get-op s;
in (case ss - | cons _ _ => Sexp_node (Sexp_symbol "IO_bind") - (cons (op) - (cons (Sexp_node (Sexp_symbol "lambda_->_") (cons sym (cons (helper sym ss) nil))) nil) - ) + | cons _ _ => Sexp_node (Sexp_symbol "IO_bind") (cons (op) + (cons (Sexp_node (Sexp_symbol "lambda_->_") (cons sym (cons (helper sym ss) nil))) nil)) | nil => Sexp_node (Sexp_symbol "IO_return") (cons lsym nil) ) ) @@ -114,41 +102,38 @@ set-fun args = let
in helper (Sexp_symbol "()") args;
-% Serie of action -% you can use `<-` to affect something useful in subsequent arguments `(...)` +%% Serie of command
action = macro (lambda args -> (set-fun (get-decl args)) );
-% Next are just function and constant. -% We can define much more. - -print_ str = (File_write (File_stdout ()) str); - -newline_ = "\n"; +%% Next are example command
-tab_ = "\t"; +print str = (File_write (File_stdout ()) str);
return v = IO_return v;
-print v = print_ v; - -newline = print newline_; - -tab = print tab_; - -flt2str v = IO_return (Float->String v); - -Hello = action ( - newline; - tab; - a <- return "Hello world"; - b <- return "!"; - print a; - print b; - newline; - newline; - return 0; -); - +newline = print "\n"; + +tab = print "\t"; + +float2string v = IO_return (Float->String v); + +%% +%% example0 = action ( +%% str <- return "\n\tHello world!"; +%% print str; +%% return 0.0; +%% ); +%% +%% example1 = action ( +%% flt <- example0; +%% str <- float2string flt; +%% newline; +%% tab; +%% print str; +%% newline; +%% newline; +%% ); +%%
View it on GitLab: https://gitlab.com/monnier/typer/commit/0259aaff7c4c0b24dd0d08355ddb6247fff8...