Jonathan Graveline pushed to branch graveline at Stefan / Typer
Commits: 00a41105 by Jonathan Graveline at 2018-05-26T15:17:41Z Syntax now using ";" and better use of IO_bind - - - - -
1 changed file:
- samples/do.typer
Changes:
===================================== samples/do.typer ===================================== --- a/samples/do.typer +++ b/samples/do.typer @@ -3,24 +3,24 @@ % % Here's an example : % `` -% code = action -% (newline) (tab) (a <- return "Hello world") -% (b <- return "!") (print a) (print b) -% (newline) (newline); +% fun = action ( +% newline; +% tab; +% a <- return "Hello world"; +% b <- return "!"; +% print a; +% print b; +% newline; +% newline; +% return 0; +% ); % `` -% `print` take a `IO String` as `a` and `b` are in the above example. +% `print` take a `String` as `a` and `b` are bind by macro. % -% You can replace `action` by `do` to execute it at the end of the statement -% or call `do` on `code` later like this : -% `` -% do code; -% `` % `action` may take `action` -% `do` may take `action` -% `do` can't take `do` % - -nop = Sexp_node (Sexp_symbol "IO_return") (cons (Sexp_symbol "unit") nil); +% (Change : var are now binded, now using ; to separate command) +%
no-var = Sexp_symbol "_"; assign = Sexp_symbol "<-"; @@ -32,8 +32,6 @@ get-sym : Sexp -> Sexp; get-sym sexp = let
kno-var = (lambda _ -> no-var); - - return = (lambda v -> IO_return v);
in Sexp_dispatch sexp
@@ -46,8 +44,8 @@ get-sym sexp = let kno-var kno-var kno-var kno-var kno-var;
-get-op : Sexp -> Sexp; -get-op sexp = let +get-op : Sexp -> Sexp -> Sexp; +get-op lsym sexp = let
follow = ffollow sexp;
@@ -60,55 +58,67 @@ get-op sexp = let
follow follow follow follow follow ); + + op = (helper sexp); + +in if_then_else_ (Sexp_eq op (Sexp_symbol "")) Sexp_error op; + +get-decl : List Sexp -> List Sexp; +get-decl args = let
-in (helper sexp); + ferr = lambda _ -> cons Sexp_error nil;
-unit-wrap : Sexp -> Sexp; -unit-wrap sexp = Sexp_node (Sexp_symbol "lambda_->_") (cons (Sexp_symbol "_") (cons sexp nil)); +in Sexp_dispatch (List_nth 0 args Sexp_error) + + (lambda s ss -> if_then_else_ + (Sexp_eq (Sexp_symbol "_;_") s) + + % return declarations + (ss) + + % else assume there's only one declaration + (cons (Sexp_node s ss) nil) + ) + (lambda s -> cons (Sexp_symbol s) nil) + ferr ferr ferr ferr;
-%% (lambda a-sym -> [next fun]) a-op -%% (lambda a-sym -> ((lambda b-sym -> [next fun]) b-op)) a-op +%% 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]))) %% 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
- helper : List Sexp -> Sexp; - helper args = case args + helper : Sexp -> List Sexp -> Sexp; + helper lsym args = case args | cons s ss => (let
- sym = get-sym s; + sym = get-sym s;
- op = get-op s; + op = get-op lsym s;
- % function - in Sexp_node (Sexp_node (Sexp_symbol "lambda_->_") - - % arg - (cons sym + % Simpler to directly bind command to variable + % also return last variable so you may `return` something
- % body - (cons (Sexp_node (Sexp_symbol "IO_bind") (cons sym (cons (unit-wrap (helper ss)) nil))) nil))) - - % call - (cons op nil) - ) + 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) + ) + | nil => Sexp_node (Sexp_symbol "IO_return") (cons lsym nil) + ) + )
- | nil => nop; + | nil => Sexp_error;
-in helper args; +in helper (Sexp_symbol "()") args;
-% Serie of action that will be executed at the end +% Serie of action % you can use `<-` to affect something useful in subsequent arguments `(...)`
-do = macro (lambda args -> - Sexp_node (Sexp_node (Sexp_symbol "IO_run") (cons (set-fun args) nil)) (cons (Sexp_symbol "unit") nil) -); - -% `action` create a serie of IO but do not execute it - action = macro (lambda args -> - (set-fun args) + (set-fun (get-decl args)) );
% Next are just function and constant. @@ -116,23 +126,29 @@ action = macro (lambda args ->
print_ str = (File_write (File_stdout ()) str);
-newline_ = IO_return "\n"; +newline_ = "\n";
-tab_ = IO_return "\t"; +tab_ = "\t";
return v = IO_return v;
-print v = IO_bind v print_; +print v = print_ v;
newline = print newline_;
tab = print tab_;
-%flt2str : IO Float -> IO String; - -%flt2str flt = IO_bind flt (lambda v -> IO_return (Float->String v)); +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; +);
-HelloWorld = do (h <- action - (newline) (tab) (a <- return "Hello world") - (b <- return "!") (print a) (print b) - (newline) (newline));
View it on GitLab: https://gitlab.com/monnier/typer/commit/00a41105b38c88525ce5aa48c50f71b12b1d...
Afficher les réponses par date
% % Here's an example : % `` -% code = action -% (newline) (tab) (a <- return "Hello world") -% (b <- return "!") (print a) (print b) -% (newline) (newline); +% fun = action ( +% newline;
Au, fait, SVP utilise "%%" pour les commentaires en début de ligne. "%" tout court c'est pour les commentaires qui vont en fin de ligne (généralement avec du code avant eux sur la même ligne). "%%%" (et plus long) c'est pour les titres de (sous)sections.
Autre chose: les commentaires peuvent utiliser un sous-ensemble de markdown, donc si tu veux donner du code en exemple, comme ci-dessus, indente-le de 4 colonnes par rapport au text environnant plutôt que de mettre ``.
Stefan