From 87e5562f74e6b9906ec2f28f2fffffb6e304a6bc Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Fri, 25 Jan 2019 14:49:18 +0100 Subject: [PATCH] Super simple standalone version of the compiler --- rebar.config | 5 +++ rebar.lock | 8 ++++- src/aeso_compiler.erl | 2 +- src/aesophia.app.src | 1 + src/aesophia.erl | 71 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/aesophia.erl diff --git a/rebar.config b/rebar.config index 66e9732..03d2d8b 100644 --- a/rebar.config +++ b/rebar.config @@ -1,8 +1,13 @@ {erl_opts, [debug_info]}. +{escript_name, aesophia}. + +{provider_hooks, [{post, [{compile, escriptize}]}]}. + %% NOTE: When possible deps are referenced by Git ref to ensure consistency between builds. {deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {ref,"99bf097"}}} + , {getopt, "1.0.1"} ]}. diff --git a/rebar.lock b/rebar.lock index 34aa736..6f391f3 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1,4 +1,10 @@ +{"1.1.0", [{<<"aebytecode">>, {git,"https://github.com/aeternity/aebytecode.git", {ref,"99bf097759dedbe7553f87a796bc7e1c7322e64b"}}, - 0}]. + 0}, + {<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},0}]}. +[ +{pkg_hash,[ + {<<"getopt">>, <<"C73A9FA687B217F2FF79F68A3B637711BB1936E712B521D8CE466B29CBF7808A">>}]} +]. diff --git a/src/aeso_compiler.erl b/src/aeso_compiler.erl index bbe8e10..ca496ab 100644 --- a/src/aeso_compiler.erl +++ b/src/aeso_compiler.erl @@ -252,7 +252,7 @@ parse_error({Line,Pos}, ErrorString) -> error({parse_errors,[Error]}). read_contract(Name) -> - {ok, Bin} = file:read_file(filename:join(contract_path(), lists:concat([Name, ".aes"]))), + {ok, Bin} = file:read_file(Name), binary_to_list(Bin). contract_path() -> diff --git a/src/aesophia.app.src b/src/aesophia.app.src index 37b056d..6a47608 100644 --- a/src/aesophia.app.src +++ b/src/aesophia.app.src @@ -6,6 +6,7 @@ [kernel, stdlib, syntax_tools, + getopt, aebytecode ]}, {env,[]}, diff --git a/src/aesophia.erl b/src/aesophia.erl new file mode 100644 index 0000000..c7c57d9 --- /dev/null +++ b/src/aesophia.erl @@ -0,0 +1,71 @@ +-module(aesophia). + +-export([main/1]). + +-define(OPT_SPEC, + [ {src_file, undefined, undefined, string, "Sophia source code file"} + , {verbose, $v, "verbose", undefined, "Verbose output"} + , {help, $h, "help", undefined, "Show this message"} + , {outfile, $o, "out", string, "Output file (experimental)"} ]). + +usage() -> + getopt:usage(?OPT_SPEC, "aesophia"). + +main(Args) -> + case getopt:parse(?OPT_SPEC, Args) of + {ok, {Opts, []}} -> + case proplists:get_value(help, Opts, false) of + false -> + compile(Opts); + true -> + usage() + end; + + {ok, {_, NonOpts}} -> + io:format("Can't understand ~p\n\n", [NonOpts]), + usage(); + + {error, {Reason, Data}} -> + io:format("Error: ~s ~p\n\n", [Reason, Data]), + usage() + end. + + +compile(Opts) -> + case proplists:get_value(src_file, Opts, undefined) of + undefined -> + io:format("Error: no input source file\n\n"), + usage(); + File -> + compile(File, Opts) + end. + +compile(File, Opts) -> + Verbose = proplists:get_value(verbose, Opts, false), + OutFile = proplists:get_value(outfile, Opts, undefined), + + try + Res = aeso_compiler:file(File, [pp_ast || Verbose]), + write_outfile(OutFile, Res), + io:format("\nCompiled successfully!\n") + catch + %% The compiler errors. + error:{type_errors, Errors} -> + io:format("\n~s\n", [string:join(["** Type errors\n" | Errors], "\n")]); + error:{parse_errors, Errors} -> + io:format("\n~s\n", [string:join(["** Parse errors\n" | Errors], "\n")]); + error:{code_errors, Errors} -> + ErrorStrings = [ io_lib:format("~p", [E]) || E <- Errors ], + io:format("\n~s\n", [string:join(["** Code errors\n" | ErrorStrings], "\n")]); + %% General programming errors in the compiler. + error:Error -> + Where = hd(erlang:get_stacktrace()), + ErrorString = io_lib:format("Error: ~p in\n ~p", [Error,Where]), + io:format("\n~s\n", [ErrorString]) + end. + +write_outfile(undefined, _) -> ok; +write_outfile(Out, ResMap) -> + %% Lazy approach + file:write_file(Out, term_to_binary(ResMap)), + io:format("Output written to: ~s\n", [Out]).