-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.erl
More file actions
30 lines (27 loc) · 797 Bytes
/
parse.erl
File metadata and controls
30 lines (27 loc) · 797 Bytes
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
-module(parse).
-compile(export_all).
lex(Program) ->
Prog_Replace = re:replace(re:replace(Program, "[(]", " ( ", [global]), "[)]", " ) ", [global, {return, list}]),
string:tokens(Prog_Replace, " ").
read(["("|Tail]) ->
case read(Tail) of
{Done, ok} -> {list, Done};
{Done, Remaining} ->
{A, B} = read(Remaining),
{[{list, Done}] ++ A, B}
end;
read([")"]) ->
{[], ok};
read([")"|Tail]) ->
{[], Tail};
read([H|Tail]) ->
Token =
try list_to_integer(H) of X -> {num, X}
catch Error:badarg ->
try list_to_float(H) of Y -> {num, Y}
catch Error:badarg -> {str, H}
end
end,
{A, B} = read(Tail),
{[Token] ++ A, B}.
parse(Program) -> read(lex(Program)).