Changeset 71


Ignore:
Timestamp:
07/15/09 06:21:16 (3 years ago)
Author:
dom
Message:

Refactoring.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/compiler.erl

    r70 r71  
    33 
    44init (Notify, Dir) -> 
    5     Watcher = spawn_link (directory_watcher, init_recursive, [Dir, notify_me ()]), 
    6     loop (Notify, Watcher, dict: new (), [], []). 
     5    Args = [Dir, notify_me ()], 
     6    Watcher = spawn_link (directory_watcher, init_recursive, Args), 
     7    State = {dict: new (), [], []}, 
     8    loop (Notify, Watcher, State). 
    79 
    8 loop (Notify, Watcher, Modules, Binaries, Removed) -> 
     10loop (Notify, Watcher, State) -> 
    911    receive 
    1012        check -> 
    1113            Watcher ! check, 
    12             New_modules = receive_files (Watcher, Modules), 
    13             compile (Notify, Watcher, Modules, New_modules, Binaries, Removed); 
     14            {Modules, Binaries, Removed} = State, 
     15            case receive_files (Watcher, Modules) of 
     16                Modules -> 
     17                    loop (Notify, Watcher, State); 
     18                Changed -> 
     19                    New_state = {Changed, Binaries, Removed}, 
     20                    compile (Notify, Watcher, New_state) 
     21            end; 
    1422        {Pid, stop} -> 
    1523            Pid ! {self (), bye} 
     
    2634    end. 
    2735 
    28 compile (Notify, Watcher, Modules, Modules, Binaries, Removed) -> 
    29     loop (Notify, Watcher, Modules, Binaries, Removed); 
    30 compile (Notify, Watcher, _, Modules, Binaries, Removed) -> 
    31     notify_totals (Modules, Notify), 
    32     Init = {Modules, Notify, Binaries, Removed}, 
    33     {Processed, _, New_binaries, New_removed} = dict: fold (fun process/3, Init, Modules), 
    34     {Final_binaries, Final_removed} = notify_end (totals (Processed), New_binaries, New_removed, Notify), 
    35     loop (Notify, Watcher, Processed, Final_binaries, Final_removed). 
     36compile (Notify, Watcher, State) -> 
     37    {Modules, _, _} = State, 
     38    Notify (totals (Modules)), 
     39    Process = process_fun (Notify), 
     40    Processed = dict: fold (Process, State, Modules), 
     41    Notified = notify_end (Notify, Processed), 
     42    loop (Notify, Watcher, Notified). 
    3643 
    37 process (File, Event, Acc) when Event == found; Event == changed -> 
    38     {Modules, Notify, Binaries, Removed} = Acc, 
    39     Result = modules: compile (File), 
    40     notify_result (Result, Notify), 
    41     New_modules = dict: store (File, Result, Modules), 
    42     notify_totals (New_modules, Notify), 
    43     New_binaries = binaries (Result, Binaries), 
    44     {New_modules, Notify, New_binaries, Removed}; 
    45 process (File, Event, Acc) when Event == lost -> 
    46     {Modules, Notify, Binaries, Removed} = Acc, 
    47     New_modules = dict: erase (File, Modules), 
    48     New_removed = [modules: module_name (File) | Removed], 
    49     {New_modules, Notify, Binaries, New_removed}; 
    50 process (_, _, Acc) -> 
    51     Acc. 
     44process_fun (Notify) -> 
     45    fun (File, Event, Acc) when Event == found; Event == changed -> 
     46            {Modules, Binaries, Removed} = Acc, 
     47            Result = modules: compile (File), 
     48            notify_result (Result, Notify), 
     49            New_modules = dict: store (File, Result, Modules), 
     50            Notify (totals (New_modules)), 
     51            New_binaries = binaries (Result, Binaries), 
     52            {New_modules, New_binaries, Removed}; 
     53        (File, Event, Acc) when Event == lost -> 
     54            {Modules, Binaries, Removed} = Acc, 
     55            New_modules = dict: erase (File, Modules), 
     56            New_removed = [modules: module_name (File) | Removed], 
     57            {New_modules, Binaries, New_removed}; 
     58        (_, _, Acc) -> 
     59            Acc 
     60    end. 
    5261 
    5362notify_me () -> 
     
    5564    fun (E) -> Self ! {self (), E} end. 
    5665             
    57 notify_totals (Modules, Notify) -> 
    58     Notify (totals (Modules)). 
    59  
    6066totals (Modules) -> 
    6167    dict: fold (fun count/3, {0, 0, 0}, Modules). 
     
    8288    Bs. 
    8389 
    84 notify_end ({N, N, N}, Binaries, Removed, Notify) -> 
     90notify_end (Notify, State) -> 
     91    {Modules, _, _} = State, 
     92    notify_end (Notify, totals (Modules), State). 
     93 
     94notify_end (Notify, {N, N, N}, {Modules, Binaries, Removed}) -> 
    8595    Notify ({{binaries, Binaries}, {removed, Removed}}), 
    86     {[], []}; 
    87 notify_end (_, Binaries, Removed, _) -> 
    88     {Binaries, Removed}. 
     96    {Modules, [], []}; 
     97notify_end (_, _, State) -> 
     98    State. 
  • trunk/src/compiler_test.erl

    r69 r71  
    7171    Compiler = spawn_link (compiler, init, [notify_me (), Root]), 
    7272    Compiler ! check, 
    73     [{2, 0, 0}, {_, _}, {2, 1, 0}, {2, 2, 1}] = receive_all (), 
     73    [{2, 0, 0}, _, _, {2, 2, 1}] = receive_all (), 
    7474    {file, Filename, _} = File, 
    75     Code = "-module(gdbye). -export([run/0]). run()->gdbye.", 
    76     file: write_file (filename: join (Root, Filename), Code), 
     75    Bin = list_to_binary ("-module(gdbye). -export([run/0]). run()->gdbye."), 
     76    ok = file: write_file (filename: join (Root, Filename), Bin), 
    7777    Compiler ! check, 
    7878    [{2, 1, 1}, {2, 2, 2}, {{binaries, [_, _]}, _}] = receive_all (), 
     
    8484    receive_all (), 
    8585    File = filename: join (Root, "hello.erl"), 
    86     Bin = list_to_binary ("-module(hello). -export([run/0]). run()->hello_howdy."), 
     86    Code = "-module(hello). -export([run/0]). run()->hello_howdy.", 
     87    Bin = list_to_binary (Code), 
    8788    ok = file: write_file (File, Bin), 
    8889    Compiler ! check, 
Note: See TracChangeset for help on using the changeset viewer.