Skip to content
Permalink
Browse files

(fix) REPL can now import

Signed-off-by: Jerome Simeon <jeromesimeon@me.com>
  • Loading branch information...
jeromesimeon committed Jul 14, 2018
1 parent b23bdb0 commit 6d63f34c3d4a4b69d4e72b9c5c5d1813547dab5f
@@ -1,2 +1,2 @@
#!/bin/bash
rlwrap "${BASH_SOURCE%/*}/ergotop.native"
rlwrap "${BASH_SOURCE%/*}/ergotop.native" "$@"
@@ -31,33 +31,13 @@ let args_list gconf =
"<lang> Indicates the language for the target (default: javascript) " ^ available_targets)
]

let anon_args gconf cto_files input_files f =
let extension = Filename.extension f in
if extension = ".ctoj"
then cto_files := (f, Util.string_of_file f) :: !cto_files
else if extension = ".ergo"
then input_files := f :: !input_files
else ergo_raise (ergo_system_error (f ^ " is not cto, ctoj or ergo file"))

let usage =
"Ergo Compiler\n"^
"Usage: "^Sys.argv.(0)^" [options] cto1 cto2 ... contract1 contract2 ..."

let parse fa args l f msg =
try
Arg.parse_argv (fa args) l f msg
with
| Arg.Bad msg -> Printf.eprintf "%s" msg; exit 2
| Arg.Help msg -> Printf.printf "%s" msg; exit 0

let parse_args fa args gconf =
let input_files = ref [] in
let cto_files = ref [] in
parse fa args (args_list gconf) (anon_args gconf cto_files input_files) usage;
(List.rev !cto_files, List.rev !input_files)

let main fa args =
let main args =
let gconf = ErgoConfig.default_config () in
let (cto_files,input_files) = parse_args fa args gconf in
batch_compile_top gconf cto_files input_files
let (cto_files,input_files) = ErgoUtil.parse_args args_list usage args gconf in
List.iter (ErgoConfig.add_cto_file gconf) cto_files;
List.iter (process_file (ErgoCompile.ergo_proc gconf)) input_files

@@ -22,12 +22,11 @@ let wrap_error e =
new%js Js.error_constr (Js.string (string_of_error (ergo_system_error (Printexc.to_string exn))))
end

let () =
let _ =
begin try
Ergoc.main (fun x -> x) Sys.argv
Ergoc.main Sys.argv
with
| e ->
Js.raise_js_error (wrap_error e)
end


@@ -14,20 +14,6 @@

open ErgoUtil

let patch_cto_extension f =
begin try
let extension = Filename.extension f in
if extension = ".cto"
then
(Filename.chop_suffix f ".cto") ^ ".ctoj"
else f
with
| _ -> f
end

let patch_argv argv =
Array.map patch_cto_extension argv

let wrap_error e =
begin match e with
| Ergo_Error error ->
@@ -36,9 +22,9 @@ let wrap_error e =
Printf.eprintf "%s\n" (string_of_error (ergo_system_error (Printexc.to_string exn))); exit 2
end

let () =
let _ =
begin try
Ergoc.main (fun x -> x) (patch_argv Sys.argv)
Ergoc.main (patch_argv Sys.argv)
with
| e ->
wrap_error e
@@ -14,48 +14,62 @@

open ErgoComp
open ParseUtil
open Unix

let get_stdlib () =
List.map (ParseUtil.parse_ergo_module_from_string "stdlib") (List.map snd ErgoStdlib.ergo_stdlib)

let get_ctos () =
ErgoConfig.get_ctos (ErgoConfig.default_config ())

let string_of_char_list cl = String.concat "" (List.map (String.make 1) cl)

let welcome () =
if isatty stdin
then print_string ("Welcome to ERGOTOP version " ^ (string_of_char_list ergo_version) ^ "\n")
else ()
if Unix.isatty Unix.stdin
then print_string ("Welcome to ERGOTOP version " ^ (Util.string_of_char_list ergo_version) ^ "\n")
else ()

let prompt () =
if isatty stdin then
print_string "ergotop$ "
else ()
if Unix.isatty Unix.stdin then
print_string "ergotop$ "
else ()

let rec read_nonempty_line () =
prompt () ;
let line = read_line () in
if line = "" then
read_nonempty_line ()
else
line ^ "\n"
prompt () ;
let line = read_line () in
if line = "" then
read_nonempty_line ()
else
line ^ "\n"

let rec repl (sctx, dctx) =
try
let decl = (ParseUtil.parse_ergo_declaration_from_string "stdin" (read_nonempty_line ())) in
let result = ergo_eval_decl_via_calculus sctx dctx decl in
let out = ergo_string_of_result dctx result in
print_string (string_of_char_list out);
repl (ergo_maybe_update_context (sctx, dctx) result)
with
| ErgoUtil.Ergo_Error e ->
print_string (ErgoUtil.string_of_error e);
print_string "\n" ;
repl (sctx, dctx)
| End_of_file -> None

let main =
welcome ();
repl (ergo_make_stdlib_ctxt (get_ctos ()) (get_stdlib ()), ErgoCompiler.ergo_empty_eval_context)
try
let decl = (ParseUtil.parse_ergo_declaration_from_string "stdin" (read_nonempty_line ())) in
let result = ergo_eval_decl_via_calculus sctx dctx decl in
let out = ergo_string_of_result dctx result in
print_string (Util.string_of_char_list out);
repl (ergo_maybe_update_context (sctx, dctx) result)
with
| ErgoUtil.Ergo_Error e ->
print_string (ErgoUtil.string_of_error e);
print_string "\n" ;
repl (sctx, dctx)
| End_of_file -> None

let args_list gconf =
Arg.align
[
("-version", Arg.Unit ErgoUtil.get_version,
" Prints the compiler version");
("--version", Arg.Unit ErgoUtil.get_version,
" Prints the compiler version");
]

let usage =
"Ergo REPL\n"^
"Usage: "^Sys.argv.(0)^" [options] cto1 cto2 ... contract1 contract2 ..."

let main args =
let gconf = ErgoConfig.default_config () in
let (cto_files,input_files) = ErgoUtil.parse_args args_list usage args gconf in
List.iter (ErgoConfig.add_cto_file gconf) cto_files;
let ctos = ErgoConfig.get_ctos gconf in
let modules = ErgoConfig.get_modules gconf in
let ctxt = ergo_make_stdlib_ctxt ctos modules in
welcome ();
repl (ctxt, ErgoCompiler.ergo_empty_eval_context)

let _ =
main (ErgoUtil.patch_argv Sys.argv)

@@ -58,6 +58,3 @@ let ergo_proc gconf (file_name,file_content) =
make_file file_res.res_file file_res.res_content
end

let batch_compile_top gconf cto_files input_files =
List.iter (ErgoConfig.add_cto_file gconf) cto_files;
List.iter (process_file (ergo_proc gconf)) input_files
@@ -101,25 +101,22 @@ main_decl:
{ p }

emodule:
| NAMESPACE qn = qname_prefix ims = imports ss = decls
| NAMESPACE qn = qname_prefix ds = decls
{ { module_annot = mk_provenance $startpos $endpos;
module_namespace = Util.char_list_of_string qn;
module_imports = ims;
module_declarations = ss; } }
module_declarations = ds; } }

imports:
| { [] }
| IMPORT qn = qname_prefix ims = imports
{ (ErgoUtil.cto_import_decl_of_import_namespace qn) :: ims }


decls:
|
{ [] }
| s = decl ss = decls
{ s :: ss }

decl:
| IMPORT qn = qname_prefix
{ ErgoCompiler.dimport
(mk_provenance $startpos $endpos)
(ErgoUtil.cto_import_decl_of_import_namespace qn) }
| DEFINE CONCEPT cn = ident dt = ergo_type_class_decl
{ let (oe,ctype) = dt in
ErgoCompiler.dtype (mk_provenance $startpos $endpos)
@@ -96,7 +96,7 @@ let string_of_error error =
let ergo_version = string_of_char_list ergo_version

let get_version () =
print_endline ("Ergo compiler version " ^ ergo_version);
print_endline ("Ergo version " ^ ergo_version);
exit 0

(** Additional utility functions *)
@@ -140,3 +140,39 @@ let cto_import_decl_of_import_namespace ns =
end
end

(** Command line args *)
let patch_cto_extension f =
begin try
let extension = Filename.extension f in
if extension = ".cto"
then
(Filename.chop_suffix f ".cto") ^ ".ctoj"
else f
with
| _ -> f
end

let patch_argv argv =
Array.map patch_cto_extension argv

let anon_args gconf cto_files input_files f =
let extension = Filename.extension f in
if extension = ".ctoj"
then cto_files := (f, Util.string_of_file f) :: !cto_files
else if extension = ".ergo"
then input_files := f :: !input_files
else ergo_raise (ergo_system_error (f ^ " is not cto, ctoj or ergo file"))

let parse_args args_list usage args gconf =
let parse args l f msg =
try
Arg.parse_argv args l f msg
with
| Arg.Bad msg -> Printf.eprintf "%s" msg; exit 2
| Arg.Help msg -> Printf.printf "%s" msg; exit 0
in
let input_files = ref [] in
let cto_files = ref [] in
parse args (args_list gconf) (anon_args gconf cto_files input_files) usage;
(List.rev !cto_files, List.rev !input_files)

@@ -27,6 +27,13 @@ Section EAstUtil.
| ImportSelf : A -> namespace_name -> import_decl
| ImportName : A -> namespace_name -> local_name -> import_decl.

Definition import_annot (i:import_decl) :=
match i with
| ImportAll a _ => a
| ImportSelf a _ => a
| ImportName a _ _ => a
end.

Definition extends : Set := option N.

End Ast.
@@ -121,6 +121,8 @@ Section EResult.
(** Call errors *)
Definition function_not_found_error {A} prov (fname:string) : eresult A :=
efailure (CompilationError prov ("Function '" ++ fname ++ "' not found")).
Definition eval_function_not_found_error {A} prov (fname:string) : eresult A :=
efailure (RuntimeError prov ("Function '" ++ fname ++ "' not found during eval")).
Definition clause_not_found_error {A} prov (fname:string) : eresult A :=
efailure (CompilationError prov ("Clause '" ++ fname ++ "' not found")).
Definition call_params_error {A} prov (fname:string) : eresult A :=
@@ -238,6 +238,8 @@ Module ErgoCompiler.
ErgoSugar.EFunReturnEmpty.

(** Declarations *)
Definition dimport prov id : ergo_declaration
:= Ergo.DImport prov id.
Definition dtype prov etd : ergo_declaration
:= Ergo.DType prov etd.
Definition dstmt prov s : ergo_declaration
@@ -55,16 +55,6 @@ Section ErgoDriver.
(namespace_ctxt_of_compilation_ctxt ctxt)
ns).

Definition import_namespaces_in_compilation_ctxt
(ctxt:compilation_ctxt)
(ims:list limport_decl) : eresult compilation_ctxt :=
let ns_ctxt := namespace_ctxt_of_compilation_ctxt ctxt in
let rns_ctxt := resolve_ergo_imports ns_ctxt ims in
elift
(update_namespace_ctxt
ctxt)
rns_ctxt.

(* Initialize compilation context *)
Definition compilation_ctxt_from_inputs
(ctos:list lrcto_package)
@@ -73,7 +63,7 @@ Section ErgoDriver.
let imls := map InputErgo mls in
let ctxt := init_namespace_ctxt in
resolve_ergo_inputs ctxt (ictos ++ imls).

Definition ergo_make_stdlib_ctxt
(ctos:list lrcto_package)
(mls:list lrergo_module)
@@ -128,6 +128,7 @@ Section Ergo.

(** Declaration *)
Inductive ergo_declaration :=
| DImport : A -> @import_decl A -> ergo_declaration
| DType : A -> @ergo_type_declaration A N -> ergo_declaration
| DStmt : A -> ergo_stmt -> ergo_declaration
| DConstant : A -> absolute_name -> ergo_expr -> ergo_declaration
@@ -137,6 +138,7 @@ Section Ergo.

Definition decl_annot (d:ergo_declaration) : A :=
match d with
| DImport a _ => a
| DType a _ => a
| DStmt a _ => a
| DConstant a _ _ => a
@@ -149,7 +151,6 @@ Section Ergo.
mkModule
{ module_annot : A;
module_namespace : namespace_name;
module_imports : list (@import_decl A);
module_declarations : list ergo_declaration; }.

Inductive ergo_input :=
@@ -183,6 +183,7 @@ Section ErgoExpand.
(namespace:string)
(d:laergo_declaration) : eresult laergo_declaration :=
match d with
| DImport _ _ => esuccess d
| DType _ _ => esuccess d
| DStmt _ _ => esuccess d
| DConstant _ _ _ => esuccess d
@@ -204,7 +205,6 @@ Section ErgoExpand.
(fun ds => mkModule
p.(module_annot)
p.(module_namespace)
p.(module_imports)
ds)
(add_main_init_clauses_to_declarations p.(module_namespace) p.(module_declarations)).

0 comments on commit 6d63f34

Please sign in to comment.
You can’t perform that action at this time.