Current section

Files

Jump to
oaspec src oaspec@codegen@ir_render.erl
Raw

src/oaspec@codegen@ir_render.erl

-module(oaspec@codegen@ir_render).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/codegen/ir_render.gleam").
-export([render/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/oaspec/codegen/ir_render.gleam", 40).
?DOC(" Render a type definition to Gleam source.\n").
-spec render_type_def(
gleam@string_tree:string_tree(),
oaspec@codegen@ir:type_def()
) -> gleam@string_tree:string_tree().
render_type_def(Sb, Type_def) ->
case Type_def of
{type_alias, Name, Target} ->
_pipe = Sb,
_pipe@1 = oaspec@util@string_extra:line(
_pipe,
<<<<<<"pub type "/utf8, Name/binary>>/binary, " = "/utf8>>/binary,
Target/binary>>
),
oaspec@util@string_extra:blank_line(_pipe@1);
{record_type, Name@1, Fields} ->
Sb@1 = begin
_pipe@2 = Sb,
oaspec@util@string_extra:line(
_pipe@2,
<<<<"pub type "/utf8, Name@1/binary>>/binary, " {"/utf8>>
)
end,
Field_strs = gleam@list:map(
Fields,
fun(F) ->
<<<<(erlang:element(2, F))/binary, ": "/utf8>>/binary,
(erlang:element(3, F))/binary>>
end
),
Sb@2 = begin
_pipe@3 = Sb@1,
oaspec@util@string_extra:indent(
_pipe@3,
1,
<<<<<<Name@1/binary, "("/utf8>>/binary,
(gleam@string:join(Field_strs, <<", "/utf8>>))/binary>>/binary,
")"/utf8>>
)
end,
_pipe@4 = Sb@2,
_pipe@5 = oaspec@util@string_extra:line(_pipe@4, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@5);
{union_type, Name@2, Variants} ->
Sb@3 = begin
_pipe@6 = Sb,
oaspec@util@string_extra:line(
_pipe@6,
<<<<"pub type "/utf8, Name@2/binary>>/binary, " {"/utf8>>
)
end,
Sb@5 = gleam@list:fold(
Variants,
Sb@3,
fun(Sb@4, Variant) -> case Variant of
{variant_with_type, Vname, Inner_type} ->
_pipe@7 = Sb@4,
oaspec@util@string_extra:indent(
_pipe@7,
1,
<<<<<<Vname/binary, "("/utf8>>/binary,
Inner_type/binary>>/binary,
")"/utf8>>
);
{variant_empty, Vname@1} ->
_pipe@8 = Sb@4,
oaspec@util@string_extra:indent(_pipe@8, 1, Vname@1)
end end
),
_pipe@9 = Sb@5,
_pipe@10 = oaspec@util@string_extra:line(_pipe@9, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@10);
{enum_type, Name@3, Variants@1} ->
Sb@6 = begin
_pipe@11 = Sb,
oaspec@util@string_extra:line(
_pipe@11,
<<<<"pub type "/utf8, Name@3/binary>>/binary, " {"/utf8>>
)
end,
Sb@8 = gleam@list:fold(
Variants@1,
Sb@6,
fun(Sb@7, Variant_name) -> _pipe@12 = Sb@7,
oaspec@util@string_extra:indent(_pipe@12, 1, Variant_name) end
),
_pipe@13 = Sb@8,
_pipe@14 = oaspec@util@string_extra:line(_pipe@13, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@14)
end.
-file("src/oaspec/codegen/ir_render.gleam", 28).
?DOC(" Render a single declaration (doc comment + type definition).\n").
-spec render_declaration(
gleam@string_tree:string_tree(),
oaspec@codegen@ir:declaration()
) -> gleam@string_tree:string_tree().
render_declaration(Sb, Decl) ->
Sb@1 = case erlang:element(2, Decl) of
{some, Doc} ->
_pipe = Sb,
oaspec@util@string_extra:doc_comment(_pipe, Doc);
none ->
Sb
end,
render_type_def(Sb@1, erlang:element(3, Decl)).
-file("src/oaspec/codegen/ir_render.gleam", 14).
?DOC(" Render a complete IR Module to a Gleam source string.\n").
-spec render(oaspec@codegen@ir:module_()) -> binary().
render(Module) ->
Sb = begin
_pipe = oaspec@util@string_extra:file_header(<<"0.12.0"/utf8>>),
oaspec@util@string_extra:imports(_pipe, erlang:element(3, Module))
end,
Sb@2 = gleam@list:fold(
erlang:element(4, Module),
Sb,
fun(Sb@1, Decl) -> render_declaration(Sb@1, Decl) end
),
oaspec@util@string_extra:to_string(Sb@2).