ligo/src/main/compile/helpers.ml

172 lines
5.5 KiB
OCaml
Raw Normal View History

2019-09-10 15:19:15 +02:00
open Trace
type s_syntax = Syntax_name of string
2020-01-27 16:05:47 +01:00
type v_syntax = PascaLIGO | CameLIGO | ReasonLIGO
let syntax_to_variant (Syntax_name syntax) source =
match syntax, source with
"auto", Some sf ->
(match Filename.extension sf with
".ligo" | ".pligo" -> ok PascaLIGO
| ".mligo" -> ok CameLIGO
| ".religo" -> ok ReasonLIGO
| _ -> simple_fail "Cannot auto-detect the syntax.\n\
Hint: Use -s <name of syntax>\n")
| ("pascaligo" | "PascaLIGO"), _ -> ok PascaLIGO
| ("cameligo" | "CameLIGO"), _ -> ok CameLIGO
| ("reasonligo" | "ReasonLIGO"), _ -> ok ReasonLIGO
| _ -> simple_fail "Invalid syntax name.\n\
Hint: Use \"pascaligo\", \"cameligo\" \
or \"reasonligo\".\n"
2019-09-10 15:19:15 +02:00
2020-01-24 12:56:05 +01:00
let parsify_pascaligo source =
2019-09-10 15:19:15 +02:00
let%bind raw =
trace (simple_error "parsing") @@
Parser.Pascaligo.parse_file source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting") @@
Concrete_to_imperative.Pascaligo.compile_program raw
in ok imperative
2019-09-10 15:19:15 +02:00
2020-01-27 16:05:47 +01:00
let parsify_expression_pascaligo source =
2019-09-10 15:19:15 +02:00
let%bind raw =
trace (simple_error "parsing expression") @@
Parser.Pascaligo.parse_expression source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting expression") @@
Concrete_to_imperative.Pascaligo.compile_expression raw
in ok imperative
2019-09-10 15:19:15 +02:00
2020-01-27 16:05:47 +01:00
let parsify_cameligo source =
2019-09-10 15:19:15 +02:00
let%bind raw =
trace (simple_error "parsing") @@
Parser.Cameligo.parse_file source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting") @@
Concrete_to_imperative.Cameligo.compile_program raw
in ok imperative
2019-09-10 15:19:15 +02:00
2020-01-27 16:05:47 +01:00
let parsify_expression_cameligo source =
2019-09-10 15:19:15 +02:00
let%bind raw =
trace (simple_error "parsing expression") @@
Parser.Cameligo.parse_expression source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting expression") @@
Concrete_to_imperative.Cameligo.compile_expression raw
in ok imperative
2019-09-10 15:19:15 +02:00
2020-01-27 16:05:47 +01:00
let parsify_reasonligo source =
2019-12-10 13:47:31 +00:00
let%bind raw =
trace (simple_error "parsing") @@
Parser.Reasonligo.parse_file source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting") @@
Concrete_to_imperative.Cameligo.compile_program raw
in ok imperative
2019-12-10 13:47:31 +00:00
2020-01-27 16:05:47 +01:00
let parsify_expression_reasonligo source =
2019-12-10 13:47:31 +00:00
let%bind raw =
trace (simple_error "parsing expression") @@
Parser.Reasonligo.parse_expression source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting expression") @@
Concrete_to_imperative.Cameligo.compile_expression raw
in ok imperative
2019-12-10 13:47:31 +00:00
2020-01-27 16:05:47 +01:00
let parsify syntax source =
let%bind parsify =
match syntax with
PascaLIGO -> ok parsify_pascaligo
| CameLIGO -> ok parsify_cameligo
2020-01-24 12:56:05 +01:00
| ReasonLIGO -> ok parsify_reasonligo in
2020-01-27 16:05:47 +01:00
let%bind parsified = parsify source in
2020-03-12 23:20:39 +01:00
let%bind applied = Self_ast_imperative.all_program parsified
2020-01-27 16:05:47 +01:00
in ok applied
2019-09-10 15:19:15 +02:00
2020-01-27 16:05:47 +01:00
let parsify_expression syntax source =
2019-09-10 15:19:15 +02:00
let%bind parsify = match syntax with
2020-01-27 16:05:47 +01:00
PascaLIGO -> ok parsify_expression_pascaligo
| CameLIGO -> ok parsify_expression_cameligo
| ReasonLIGO -> ok parsify_expression_reasonligo in
2019-09-19 12:59:07 +02:00
let%bind parsified = parsify source in
2020-03-12 23:20:39 +01:00
let%bind applied = Self_ast_imperative.all_expression parsified
2020-01-27 16:05:47 +01:00
in ok applied
2019-12-23 15:18:32 +01:00
2020-01-27 16:05:47 +01:00
let parsify_string_reasonligo source =
2019-12-23 15:18:32 +01:00
let%bind raw =
trace (simple_error "parsing") @@
Parser.Reasonligo.parse_string source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting") @@
Concrete_to_imperative.Cameligo.compile_program raw
in ok imperative
2019-12-23 15:18:32 +01:00
2020-01-27 16:05:47 +01:00
let parsify_string_pascaligo source =
2019-12-23 15:18:32 +01:00
let%bind raw =
trace (simple_error "parsing") @@
Parser.Pascaligo.parse_string source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting") @@
Concrete_to_imperative.Pascaligo.compile_program raw
in ok imperative
2019-12-23 15:18:32 +01:00
2020-01-27 16:05:47 +01:00
let parsify_string_cameligo source =
2019-12-23 15:18:32 +01:00
let%bind raw =
trace (simple_error "parsing") @@
Parser.Cameligo.parse_string source in
let%bind imperative =
2020-03-12 23:20:39 +01:00
trace (simple_error "abstracting") @@
Concrete_to_imperative.Cameligo.compile_program raw
in ok imperative
2019-12-23 15:18:32 +01:00
2020-01-27 16:05:47 +01:00
let parsify_string syntax source =
let%bind parsify =
match syntax with
PascaLIGO -> ok parsify_string_pascaligo
| CameLIGO -> ok parsify_string_cameligo
| ReasonLIGO -> ok parsify_string_reasonligo in
let%bind parsified = parsify source in
2020-03-12 23:20:39 +01:00
let%bind applied = Self_ast_imperative.all_program parsified
2020-01-27 16:05:47 +01:00
in ok applied
let pretty_print_pascaligo source =
let%bind ast = Parser.Pascaligo.parse_file source in
let buffer = Buffer.create 59 in
2020-01-27 16:05:47 +01:00
let state =
Parser_pascaligo.ParserLog.mk_state
~offsets:true
~mode:`Byte
~buffer in
Parser_pascaligo.ParserLog.pp_ast state ast;
ok buffer
2020-01-27 16:05:47 +01:00
let pretty_print_cameligo source =
let%bind ast = Parser.Cameligo.parse_file source in
let buffer = Buffer.create 59 in
2020-01-27 16:05:47 +01:00
let state = (* TODO: Should flow from the CLI *)
Parser_cameligo.ParserLog.mk_state
~offsets:true
~mode:`Point
~buffer in
Parser.Cameligo.ParserLog.pp_ast state ast;
ok buffer
2020-01-27 16:05:47 +01:00
let pretty_print_reasonligo source =
let%bind ast = Parser.Reasonligo.parse_file source in
let buffer = Buffer.create 59 in
2020-01-27 16:05:47 +01:00
let state = (* TODO: Should flow from the CLI *)
Parser.Reasonligo.ParserLog.mk_state
~offsets:true
~mode:`Point
~buffer in
Parser.Reasonligo.ParserLog.pp_ast state ast;
ok buffer
2020-01-27 16:05:47 +01:00
let pretty_print syntax source =
let%bind v_syntax =
syntax_to_variant syntax (Some source) in
match v_syntax with
PascaLIGO -> pretty_print_pascaligo source
| CameLIGO -> pretty_print_cameligo source
| ReasonLIGO -> pretty_print_reasonligo source