-
Notifications
You must be signed in to change notification settings - Fork 0
/
compil_table.erl
137 lines (103 loc) · 4.03 KB
/
compil_table.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
%% -----------------------------------------------------------------------------
%% @author Yves Müller <myves.addAnAt.zedat.fu-berlin.de>
%% @copyright Simplified BSD License by Yves Müller, 2012
%% @title Compil.erl
%% @doc A table server, for supporting the Compil compiler
%% @version 0.0.1
%% -----------------------------------------------------------------------------
-module(compil_table).
-behaviour(gen_server).
-export([start_link/0, stop/0]).
-export([init/1, terminate/2, handle_call/3, handle_cast/2, handle_info/2, code_change/3]).
-define(RENAME_TABLE,compil_rename_table).
-define(ENV_TABLE,compil_env_table).
-define(TYPE_TABLE,compil_type_table).
% -- Entries -------------
-record(envEntry,{ ref, parent, members = [], free=[]}).
-record(typeEntry,{ name, guess = [], type }).
% -- API -----------
-export([newTemp/0]).
-export([newEnv/2, getParentEnv/1, addMemberEnv/2, getMembersEnv/1,
addFreeEnv/2, getFreeEnv/1, setType/2, getType/1]).
newTemp() ->
"%" ++ gen_server:call(?MODULE, newTemp).
newEnv(Ref, Parent) ->
gen_server:call(?MODULE, {store, ?ENV_TABLE, #envEntry{ref = Ref, parent = Parent}}),
gen_server:call(?MODULE, newPrefix).
getParentEnv(Ref) ->
#envEntry{ parent = Parent } = gen_server:call(?MODULE, {get, ?ENV_TABLE, Ref}),
Parent.
addMemberEnv(Ref,Member) ->
Bla = #envEntry { members = OldMembers} = gen_server:call(?MODULE, {get, ?ENV_TABLE, Ref}),
gen_server:call(?MODULE, {store, ?ENV_TABLE, Bla#envEntry{ members=[Member | OldMembers]}}).
getMembersEnv(Ref) ->
#envEntry{ members = Members } = gen_server:call(?MODULE, {get, ?ENV_TABLE, Ref}),
Members.
addFreeEnv(Ref, Name) ->
Bla = #envEntry { free = Free} = gen_server:call(?MODULE, {get, ?ENV_TABLE, Ref}),
gen_server:call(?MODULE, {store, ?ENV_TABLE, Bla#envEntry{ free=[ Name | Free]}}).
getFreeEnv(Ref) ->
#envEntry{ free = Free } = gen_server:call(?MODULE, {get, ?ENV_TABLE, Ref}),
Free.
setType(Name,Type) ->
gen_server:call(?MODULE, {store, ?TYPE_TABLE, #typeEntry{name=Name, type=Type}}).
getType(Name) ->
case gen_server:call(?MODULE, {get, ?TYPE_TABLE, Name}) of
#typeEntry{ type = Type} ->
Type;
_ ->
undefined
end.
% -- Internals -----
-record(state,{counter=0,rename,env_counter=0}).
start_link() ->
gen_server:start({local, ?MODULE} ,?MODULE ,[] ,[]).
% case whereis(?MODULE) of
% Pid when is_pid(Pid) ->
% {ok, Pid};
% undefined ->
% gen_server:start({local, ?MODULE} ,?MODULE ,[] ,[])
% end.
stop() ->
gen_server:cast(?MODULE, stop).
init(_Args)->
Table = ets:new(?RENAME_TABLE, [set,protected,named_table]),
ets:new(?ENV_TABLE, [set,protected,named_table,{keypos,2}]),
ets:new(?TYPE_TABLE, [set,protected,named_table,{keypos,2}]),
{ok,#state{rename=Table,counter=0}}.
handle_call(Msg, _From, State = #state{counter=C, env_counter=E, rename=RTable}) ->
case Msg of
newPrefix->
{reply, "env_" ++ integer_to_list(E), State#state{env_counter=E+1}};
newTemp ->
{reply, "tmp_" ++ integer_to_list(C), State#state{counter=C+1}};
{newUnique,Atom} ->
case ets:lookup(RTable, Atom) of
[{Atom, Counter}] ->
ets:insert(RTable,{Atom,Counter+1}),
{reply, atom_to_list(Atom) ++ integer_to_list(Counter) ++ "_", State}
end;
{store, Table, Entry} ->
ets:insert(Table, Entry),
{reply, ok, State};
{get, Table, Key} ->
Ret = case ets:lookup(Table, Key) of
[H|[]] ->
H;
[] ->
undefined
end,
{reply, Ret, State}
end.
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(Msg, State) ->
io:printf("Unexpected cast ~f",[Msg]),
{noreply, State}.
handle_info(Info, State) ->
io:printf("Unexpected info ~f",[Info]),
{noreply, State}.
code_change(_OldVs, State, _Extra) ->
{ok, State}.
terminate(normal, _State) ->
ok.