Browse Source

Updates to ebb_run and word_split; working prototype\!

getty 13 years ago
parent
commit
1d93144ec5
5 changed files with 77 additions and 60 deletions
  1. 23 21
      compiler/compile
  2. 2 1
      compiler/scheme.erl
  3. 30 26
      compiler/scheme_prelude.erl
  4. 21 11
      sample/word_split.erl
  5. 1 1
      src/ebb_run.erl

+ 23 - 21
compiler/compile

@@ -68,17 +68,19 @@ parse_list(S) -> case parse(S) of
 % converts a Scheme character to an Erlang character for identifiers; converts
 % certain special characters to three-character equivalents.
 char_convert(C) -> case C of
-                       $* -> "_ml";
-                       $+ -> "_pl";
-                       $/ -> "_dv";
-                       $- -> "_mn";
-                       $_ -> "_us";
-                       $< -> "_lt";
-		       $> -> "_gt";
-		       $= -> "_eq";
-		       $! -> "_ex";
-		       $? -> "_qs";
-		       $: -> "_cn";
+                       $* -> "xml";
+                       $+ -> "xpl";
+                       $/ -> "xdv";
+                       $- -> "xmn";
+                       $_ -> "xus";
+                       $< -> "xlt";
+		       $> -> "xgt";
+		       $= -> "xeq";
+		       $! -> "xex";
+		       $? -> "xqs";
+		       $: -> "xcn";
+		       $x -> "xxx";
+		       $X -> "xXX";
 		       X -> [X]
                    end.
 
@@ -104,19 +106,19 @@ deep_string_append(L) ->
 % String -> String
 % Convert a string representing a Scheme identifier to a string representing
 % an Erlang function identifier or an atom, i.e. lower-case
-to_erlang_atom([S|XS]) ->
-  lists:foldr(fun(X, Y) -> X ++ Y end, "",
-    lists:map(fun(C) -> char_convert(C) end,
-              [string:to_lower(S)|XS]));
-to_erlang_atom({atom, X}) -> to_erlang_atom(X).
+to_erlang_atom({atom, X}) -> to_erlang_atom(X);
+to_erlang_atom(S) ->
+    string:to_lower(
+      lists:foldr(fun(X, Y) -> X ++ Y end, "",
+		  lists:map(fun(C) -> char_convert(C) end, S))).
 
 % Convert a string representing a Scheme identifier to a string representing
 % an Erlang variable, i.e. upper-case.
-to_erlang_variable([S|XS]) ->
-  lists:foldr(fun(X, Y) -> X ++ Y end, "",
-    lists:map(fun(C) -> char_convert(C) end,
-              [string:to_upper(S)|XS]));
-to_erlang_variable({atom, X}) -> to_erlang_variable(X).
+to_erlang_variable({atom, X}) -> to_erlang_variable(X);
+to_erlang_variable(S) ->
+    string:to_upper(
+      lists:foldr(fun(X, Y) -> X ++ Y end, "",
+		  lists:map(fun(C) -> char_convert(C) end, S))).
 
 % Heterogeneous List -> String
 generate([{atom, "define"} |

+ 2 - 1
compiler/scheme.erl

@@ -1,5 +1,5 @@
 -module(scheme).
--export([compile/1, lex/1, parse/1, is_string/1]).
+-export([compile/1, lex/1, parse/1]).
 
 %%% CONSTANTS %%%
 % Mostly used for built-in Scheme functions.
@@ -76,6 +76,7 @@ char_convert(C) -> case C of
 		       $= -> "_eq";
 		       $! -> "_ex";
 		       $? -> "_qs";
+		       $: -> "_cn";
 		       X -> [X]
                    end.
 

+ 30 - 26
compiler/scheme_prelude.erl

@@ -1,5 +1,5 @@
 -module(scheme_prelude).
--export([cons/2, car/1, cdr/1, list_p/1]).
+-export([cons/2, car/1, cdr/1, listxqs/1]).
 
 % Scheme prelude for programs compiled  from Scheme to Erlang.
 % A few conventions apply:
@@ -22,16 +22,16 @@
 % transparent enough.
 
 % variadic functions
-_pl([A]) -> A;
-_pl([A|B]) -> A + _pl(B).
-_st([A]) -> A;
-_st([A|B]) -> A * _st(B).
-_mn([A]) -> A;
-_mn([A|B]) -> A - _mn(B).
+xpl([A]) -> A;
+xpl([A|B]) -> A + xpl(B).
+xst([A]) -> A;
+xst([A|B]) -> A * xst(B).
+xmn([A]) -> A;
+xmn([A|B]) -> A - xmn(B).
 list(L) -> L.
 
 
-_eq(A, B) -> A == B.
+xeq(A, B) -> A == B.
 cons(A, B) -> [A|B].
 car([A|_]) -> A.
 cdr([_|B]) -> B.
@@ -51,34 +51,38 @@ reverse(L) -> lists:reverse(L).
 
 reduce(Proc, [X|L]) -> lists:foldl(Proc, X, L).
 
-list_qs([_|B]) -> scheme_prelude:list_qs(B);
-list_qs([]) -> true;
-list_qs(_) -> false.
+listxqs([_|B]) -> scheme_prelude:list_qs(B);
+listxqs([]) -> true;
+listxqs(_) -> false.
 
-null_qs([]) -> true;
-null_qs(_) -> false.
+nullxqs([]) -> true;
+nullxqs(_) -> false.
 
-pair_qs([_|_]) -> true;
-pair_qs(_) -> false.
+pairxqs([_|_]) -> true;
+pairxqs(_) -> false.
 
-number_qs(X) -> is_number(X).
+numberxqs(X) -> is_number(X).
 
-equals_qs(X, Y) -> X == Y.
+equalsxqs(X, Y) -> X == Y.
 
 length([X|Y]) -> 1 + scheme_prelude:length(Y);
 length([]) -> 0.
 
-symbol_qs(X) -> is_atom(X).
+symbolxqs(X) -> is_atom(X).
 
-boolean_qs(true) -> true.
-boolean_qs(false) -> true.
-boolean_qs(_) -> false.
+booleanxqs(true) -> true;
+booleanxqs(false) -> true;
+booleanxqs(_) -> false.
 
-and(L) -> all(scheme_prelude:is_true, L).
-or(L) -> any(scheme_prelude:is_true, L).
+scheme_and(L) -> all(fun(X) -> scheme_prelude:is_true end, L).
+scheme_or(L) -> any(fun(X) -> scheme_prelude:is_true end, L).
 
-not(false) -> true;
-not(_) -> false.
+all(Func, [X|XS]) ->
+    case Func(X) of
+	
+
+scheme_not(false) -> true;
+scheme_not(_) -> false.
 
 is_true(false) -> false;
-is_true(X) -> X.
+is_true(X) -> X.

+ 21 - 11
sample/word_split.erl

@@ -1,6 +1,7 @@
 -module(word_split).
 -export([words/1, to_list/1]).
 
+%%-include_lib("src/ebb_flow.erl").
 
 % data WordState = {chunk, S} | {segment, L, C, R}
 
@@ -25,27 +26,36 @@ maybe_word("") ->
 maybe_word(S) ->
     singleton(S).
 
-process_char($ ) ->
-    {segment, "", nil(), ""};
 process_char(C) ->
-    {chunk, [C]}.
+    case lists:member(C, " \t\n\f\l") of
+	true ->
+	    {segment, "", nil(), ""};
+	false ->
+	    {chunk, [C]}
+    end.
 
 combine({chunk, S1}, {chunk, S2}) ->
     {chunk, S1 ++ S2};
 combine({chunk, S}, {segment, L, C, R}) ->
-    {segment, L ++ S, C, R};
+    {segment, S ++ L, C, R};
 combine({segment, L, C, R}, {chunk, S}) ->
     {segment, L, C, R ++ S};
 combine({segment, L1, C1, R1}, {segment, L2, C2, R2}) ->
     {segment, L1, conc(C1, conc(maybe_word(R1 ++ L2), C2)), R2}.
 
-words([X|XS]) ->
-    T = lists:foldl(fun(A, B) -> combine(B, A) end, process_char(X),
-		    lists:map(fun(C) -> process_char(C) end, XS)),
-    case T of
-	{chunk, C} ->
-	    maybe_word(C);
+head([X|_]) ->
+    X.
+
+words(Str) ->
+    T = ebb_flow:map_reduce(fun process_char/1,
+			    fun combine/2,
+			    Str),
+    case head(ebb_run:run_linear(T)) of
+	{chunk, S} ->
+	    maybe_word(S);
 	{segment, L, C, R} ->
-	    conc(maybe_word(L), conc(C, maybe_word(R)))
+	    conc(maybe_word(L), conc(C, maybe_word(R)));
+	_ ->
+	    error
     end.
 

+ 1 - 1
src/ebb_run.erl

@@ -32,7 +32,7 @@ run_linear(Op, Args) ->
 do_run_linear(#value{value=X}, []) ->
     list_output(X);
 do_run_linear(#func{code=F}, Args) ->
-    list_output(apply(F, Args));
+    [apply(F, Args)];
 do_run_linear(#pipe{ops=Ops}, Args) ->
     lists:foldl(fun do_run_linear/2, Args, Ops);
 do_run_linear(#par{ops=Ops}, Args) ->