scheme.erl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. -module(scheme).
  2. -export([compile/1, lex/1, parse/1, is_string/1]).
  3. %%% CONSTANTS %%%
  4. % Mostly used for built-in Scheme functions.
  5. -define(PRELUDE, ["+", "-", "*", "/", "=", "<", ">", "<=", ">=",
  6. "cons", "list", "append", "car", "cdr", "map",
  7. "filter", "member", "list-ref"]).
  8. %%% LEXING %%%
  9. % Lex a string representing Scheme code into a list.
  10. lex(S) -> lists:filter(fun(Match) -> (Match /= "") and
  11. (re:run(Match, "[ \n]+") == nomatch) end,
  12. re:split(S, "(\()|(\))|(')|([^ \n()']+)", [{return, list}])).
  13. %%% PARSING %%%
  14. % String -> Regex -> Bool
  15. % true if Regex matches the entirety of Str; false otherwise.
  16. string_matches(Str, Regex) -> case re:run(Str, Regex, [{capture, first, list}]) of
  17. {match, [S]} -> S == Str;
  18. _ -> false
  19. end.
  20. % String -> Bool
  21. % true if Str consists entirely of number characters; false otherwise.
  22. is_number_string(Str) -> string_matches(Str, "[0-9]+").
  23. % [String] -> Heterogeneous List
  24. % Returns the tuple { Parsed, Unparsed } consisting of a deep list of
  25. % parsed tokens and a flat list of unparsed tokens.
  26. parse([]) -> [];
  27. parse(["(" | Rest]) -> parse_list(Rest);
  28. parse([")" | _]) -> error;
  29. parse(["'" | Rest]) -> case parse(Rest) of
  30. {Dat, R} ->
  31. {[{atom, "quote"}, Dat], R};
  32. error ->
  33. error
  34. end;
  35. parse([S | Rest]) -> case is_number_string(S) of
  36. true ->
  37. {{number, S}, Rest};
  38. false ->
  39. {{atom, S}, Rest}
  40. end.
  41. parse_list([]) -> {[], []};
  42. parse_list([")" | Rest]) -> {[], Rest};
  43. parse_list(S) -> case parse(S) of
  44. {L, R} -> case parse_list(R) of
  45. {List, Rest} -> {lists:append([L], List),
  46. Rest};
  47. error -> error
  48. end;
  49. error -> error
  50. end.
  51. %%% COMPILATION %%%
  52. % Char -> String
  53. % converts a Scheme character to an Erlang character for identifiers; converts
  54. % certain special characters to three-character equivalents.
  55. char_convert(C) -> case C of
  56. $* -> "_ml";
  57. $+ -> "_pl";
  58. $/ -> "_dv";
  59. $- -> "_mn";
  60. $_ -> "_us";
  61. $< -> "_lt";
  62. $> -> "_gt";
  63. $= -> "_eq";
  64. $! -> "_ex";
  65. $? -> "_qs";
  66. X -> [X]
  67. end.
  68. % a -> Bool
  69. % Well; "string" is more properly "is a list of non-lists", which also (in this case)
  70. % includes strings and lists of integers indistinguishably. With that caveat--
  71. % recogizes whether something is a "string".
  72. is_string(S) ->
  73. is_list(S) and lists:foldl(fun(X, Y) -> X and Y end, true,
  74. lists:map(fun(X) -> not(is_list(X)) end, S)).
  75. % Heterogeneous List of Strings -> String
  76. deep_string_append(L) ->
  77. string:join(lists:map(fun(M) -> case is_string(M) of
  78. true -> M;
  79. false -> deep_string_append(M)
  80. end end, L),
  81. "").
  82. % String -> String
  83. % Convert a string representing a Scheme identifier to a string representing
  84. % an Erlang function identifier or an atom, i.e. lower-case
  85. to_erlang_atom([S|XS]) ->
  86. lists:foldr(fun(X, Y) -> X ++ Y end, "",
  87. lists:map(fun(C) -> char_convert(C) end,
  88. [string:to_lower(S)|XS]));
  89. to_erlang_atom({atom, X}) -> to_erlang_atom(X).
  90. % Convert a string representing a Scheme identifier to a string representing
  91. % an Erlang variable, i.e. upper-case.
  92. to_erlang_variable([S|XS]) ->
  93. lists:foldr(fun(X, Y) -> X ++ Y end, "",
  94. lists:map(fun(C) -> char_convert(C) end,
  95. [string:to_upper(S)|XS]));
  96. to_erlang_variable({atom, X}) -> to_erlang_variable(X).
  97. % Heterogeneous List -> String
  98. generate([{atom, "define"} |
  99. [ [Name | Args] | Body]], Vars, Variadics) ->
  100. [to_erlang_atom(Name),
  101. " ( ", string:join(
  102. lists:map(fun(X) -> to_erlang_variable(X) end, Args),
  103. " , "), " ) -> ",
  104. string:join(lists:map(fun(X) -> generate(X, Vars ++ Args, Variadics) end,
  105. Body), " ; "),
  106. " . "];
  107. generate([{atom, "lambda"} | [Args | Body]], Vars, Variadics) ->
  108. ["fun (",
  109. string:join(
  110. lists:map(fun(X) -> to_erlang_variable(X) end, Args),
  111. " , "),
  112. ") -> ",
  113. string:join(lists:map(fun(X) -> generate(X, Vars ++ Args, Variadics) end,
  114. Body), " ; "),
  115. "end"];
  116. generate([{atom, "if"}
  117. | [ Condition
  118. | [ Then_Case
  119. | [ Else_Case ]]]], Vars, Variadics) ->
  120. ["case scheme_prelude:is_true( ",
  121. generate(Condition, Vars, Variadics),
  122. ") of true -> ",
  123. generate(Then_Case, Vars, Variadics),
  124. " ; false -> ",
  125. generate(Else_Case, Vars, Variadics),
  126. " end "];
  127. generate([{atom, "quote"} | [Rest]], _, _) ->
  128. generate_quoted(Rest);
  129. generate([{atom, Func} | Args], Vars, Variadics) ->
  130. case lists:member(Func, Variadics) of
  131. true -> [generate({atom, Func}, Vars, Variadics),
  132. " ([ ",
  133. string:join(lists:map(fun(X) -> generate(X, Vars, Variadics) end, Args),
  134. [" , "]),
  135. " ]) "];
  136. false -> [generate({atom, Func}, Vars, Variadics),
  137. " ( ",
  138. string:join(lists:map(fun(X) -> generate(X, Vars, Variadics) end, Args),
  139. [" , "]),
  140. " ) "]
  141. end;
  142. generate({number, X}, _, _) -> [X];
  143. generate({atom, X}, Vars, _) ->
  144. [case lists:member(X, Vars) of
  145. true -> to_erlang_variable(X);
  146. false -> case lists:member(X, ?PRELUDE) of
  147. true -> ["scheme_prelude:", to_erlang_variable(X)];
  148. false -> to_erlang_variable(X)
  149. end
  150. end].
  151. generate_quoted({atom, X}) ->
  152. [to_erlang_atom(X)];
  153. generate_quoted({number, X}) ->
  154. [X];
  155. generate_quoted(List) ->
  156. [" [ ", string:join(lists:map(fun(X) -> generate_quoted(X) end,
  157. List), [" , "]),
  158. " ] "].
  159. % String -> String
  160. % Compiles a string representing Scheme code into a string representing an
  161. % Erlang module.
  162. compile(S) -> case parse_list(lex(S)) of
  163. {[X|_], _} -> deep_string_append(generate(X,
  164. [],
  165. ["-", "+", "*", "list"]));
  166. _ -> "NOTHING"
  167. end.