KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I would like to convert a string containing a valid Erlang expression to its abstract syntax tree representation, without any success so far. Below is an example of what I would like to do. After compiling, alling z:z(). generates module zed , which by calling zed:zed(). returns the result of applying lists:reverse on the given list. -module(z). -export([z/0]). z() -> ModuleAST = erl_syntax:attribute(erl_syntax:atom(module), [erl_syntax:atom("zed")]), ExportAST = erl_syntax:attribute(erl_syntax:atom(export), [erl_syntax:list( [erl_syntax:arity_qualifier( erl_syntax:atom("zed"), erl_syntax:integer(0))])]), %ListAST = ?(String), % This is where I would put my AST ListAST = erl_syntax:list([erl_syntax:integer(1), erl_syntax:integer(2)]), FunctionAST = erl_syntax:function(erl_syntax:atom("zed"), [erl_syntax:clause( [], none, [erl_syntax:application( erl_syntax:atom(lists), erl_syntax:atom(reverse), [ListAST] )])]), Forms = [erl_syntax:revert(AST) || AST <- [ModuleAST, ExportAST, FunctionAST]], case compile:forms(Forms) of {ok,ModuleName,Binary} -> code:load_binary(ModuleName, "z", Binary); {ok,ModuleName,Binary,_Warnings} -> code:load_binary(ModuleName, "z", Binary) end. String could be "[1,2,3]." , or "begin A=4, B=2+3, [A,B] end." , or anything alike. (Note that this is just an example of what I would like to do, so evaluating String is not an
Tags (comma-separated)
Save Edits
Cancel