Guest User

Untitled

a guest
Nov 30th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 5.14 KB | None | 0 0
  1.  
  2. -module(lang0).
  3.  
  4. -compile(export_all).
  5.  
  6. % About erlang language.
  7.  
  8. function_name(AnyArg, [HeadOfList | TailOfList], {T, U, P, L, E}) ->
  9.            % NO VARIABLES - only bindings. One bound with '=' could nevar change
  10.     X = 1, % read: we make a predicate on X: X must be 1
  11.            % X becomes bound to value 1
  12.     X = 1, % X is still (and forever in this context) 1,
  13.     1 = X, % all is ok, 1 is equal to (matches) X
  14.     X = 2, % oh, the erlang interpreter will loudly cry here:
  15.            % X doesn't match 2, its 1!
  16.     Y = 2 + X, % Y is 3 now
  17.  
  18.     Fun = fun (X) -> X + 1 end, % Fun is an incrementer now
  19.  
  20.     Y = Fun(2), % lol, erlang will eat this - both left and right are bound to 3
  21.  
  22.     some_atomic_shit, % it's an... atom. Like a string-constant, w/o bracets.
  23.  
  24.     module:some_function(X, HeadOfList), % qualified call
  25.  
  26.     [1, 2, 3], % list of three elems
  27.     {1, 2, 3} = {T, U, P} % T, U and P must be 1, 2 and 3, or the program will
  28.                           % fall - otherwise it just accepts this clause.
  29.     $a,    % 'a' character
  30.     $,,    % ',' character
  31.     $ ,    % ' ' character
  32.  
  33.     Fun. % returns Fun
  34.  
  35. % Okay, parsers. I made them functions, returning string-eating mechanism.
  36. % parser resuls is
  37. %   {ok, {Result, Context}},
  38. %   {error, {Reason, Where}}
  39. % basic parser is char()
  40.  
  41. test() ->
  42.     Char = parser:char($a),
  43.     {ok, {$a, "bc"}} = Char("abc"),
  44.     {error, {{exprected, $a, but_found}, "dbc"}} = Char("dbc").
  45.  
  46. % parser:any(List) tries to run any parser from list unless it succeeds.
  47.  
  48. test_any() ->
  49.     Any = parser:any([
  50.         parser:char($a),
  51.         parser:char($b)
  52.     ]),
  53.  
  54.     {ok, {$a, "b"}} = Any("ab"),
  55.     {error, {{exprected, $b, but_found}, "dbc"}} = Any("dbc").
  56.  
  57. test_misc() ->
  58.     Marked = parser:many(        % many
  59.         parser:marked(char,      % - marked as char
  60.             parser:any([         %   - any of
  61.                 parser:char($a), %     - a
  62.                 parser:char($b), %     - b
  63.                 parser:char($c), %     - c
  64.             ])
  65.         )
  66.     ),
  67.  
  68.     {ok, {[{char, $a},   % returned list of {char, Char}
  69.            {char, $b},
  70.            {char, $a},
  71.            {char, $c}],
  72.           "def"}} = Marked("abacdef").
  73.    
  74. % expression parser tries one of [lambda, application] parsers
  75. % and returns the first one matched
  76. expression() ->
  77.     parser:any([
  78.         lambda(),
  79.         application()
  80.     ]).
  81.  
  82. % lambda parser
  83. lambda() ->
  84.     parser:marked(lambda,                         % marks result as lambda
  85.         parser:entuple([                          % produces tuple containing:
  86.             parser:many1_tokens(parser:name()),   % - many space-separated names
  87.  
  88.             parser:token("->"),                   % - token(...) drops anything
  89.                                                   %   it matches
  90.             parser:enlist([                       % - list, containing
  91.                 parser:return([]),                %   - it is here to prevent
  92.                                                   %     RASPIDORASEELO
  93.                 parser:recuring(                  %   - expression
  94.                     fun (_) ->                    % Just a recursion. Damn.
  95.                         expression()              
  96.                     end)                          
  97.             ])                                    
  98.         ])
  99.     ).
  100.  
  101. % application parser
  102. application() ->
  103.     parser:marked(app,        % mark the result as app[lication] "f x" - f to x
  104.         parser:many1_tokens(  % 1 or more
  105.             terminal()        % - terminals
  106.         )
  107.     ).
  108.  
  109. % parser of terminals
  110. terminal() ->
  111.     parser:any([                               % terminal is one of:
  112.         parser:marked(var,   parser:name()),   % - variable, marked 'var'
  113.         parser:marked(const, parser:number()), % - const int, marked 'const'
  114.         parser:inside(                         % - braceted...
  115.             parser:token("("),
  116.             parser:recuring(
  117.                 fun (_) -> expression() end),  %   - ...subexpression
  118.             parser:token(")")
  119.         )
  120.     ]).
  121.  
  122. compile(Program) ->
  123.     E = expression(),           % Build the parser - which is function
  124.     {ok, {PC, _}} = E(Program), % Run it on program text and
  125.                                 % match to structure. This code also ensures
  126.                                 % the first element is 'ok'... or loudly falls.
  127.     dispatch([], PC).           % Compile into form the interpreter knows
  128.  
  129. print(Program) ->
  130.     P = compile(Program),
  131.     lc:print(P).                % lc is a interpreter module, it can print
  132.                                 % prettily
  133.  
  134. eval(Program) ->
  135.     P = compile(Program),
  136.     lc:eval(P).
  137.  
  138. dispatch(Stack, {app, List}) ->
  139.     lists:map(fun (X) -> dispatch(Stack, X) end, List);
  140.  
  141. dispatch(Stack, {lambda, Args, "->", [Body]}) ->
  142.     Head = lists:map(fun (_) -> l end, Args),
  143.     Head ++ dispatch(Args ++ Stack, Body);
  144.  
  145. dispatch(Stack, {var, Name}) ->
  146.     N = number_of(Stack, Name),
  147.     [var, N];
  148.  
  149. dispatch(Stack, {const, List}) ->
  150.     list_to_integer(List).
  151.  
  152. number_of([X | List], X) -> 0;
  153.  
  154. number_of([_ | List], X) -> 1 + number_of(List, X).
Add Comment
Please, Sign In to add comment