-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwrite_interface.m
More file actions
83 lines (65 loc) · 2.86 KB
/
write_interface.m
File metadata and controls
83 lines (65 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
%-----------------------------------------------------------------------%
% Write a Plasma interface file
% vim: ts=4 sw=4 et
%
% Copyright (C) Plasma Team
% Distributed under the terms of the MIT License see ../LICENSE.code
%
% This module provides the code for writing out an interface file.
%
%-----------------------------------------------------------------------%
:- module write_interface.
%-----------------------------------------------------------------------%
:- interface.
:- import_module io.
:- import_module maybe.
:- import_module string.
:- import_module core.
:- pred write_interface(string::in, core::in, maybe_error::out,
io::di, io::uo) is det.
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
:- implementation.
:- import_module cord.
:- import_module list.
:- import_module pair.
:- import_module common_types.
:- import_module core.function.
:- import_module core.pretty.
:- import_module core.types.
:- import_module core.resource.
:- import_module q_name.
:- import_module util.
:- import_module util.my_io.
:- import_module util.pretty.
%-----------------------------------------------------------------------%
write_interface(Filename, Core, Result, !IO) :-
PrettyStr = pretty_str([pretty_interface(Core)]),
write_temp_and_move(open_output, close_output,
(pred(File::in, ok::out, IO0::di, IO::uo) is det :-
io.write_string(File, PrettyStr, IO0, IO)
), Filename, Result, !IO).
:- func pretty_interface(core) = pretty.
pretty_interface(Core) = Pretty :-
ModuleName = q_name_to_string(module_name(Core)),
ExportedResources = core_all_exported_resources(Core),
ExportedTypes = core_all_exported_types(Core),
ExportedFuncs = core_all_exported_functions(Core),
Pretty = p_list([
p_str("// Plasma interface file"), p_nl_hard,
p_str("module"), p_spc, p_str(ModuleName), p_nl_double] ++
condense(map(pretty_resource_interface(Core), ExportedResources)) ++
condense(map(pretty_type_interface(Core), ExportedTypes)) ++
condense(map(pretty_func_interface(Core), ExportedFuncs))).
:- func pretty_resource_interface(core, pair(resource_id, resource)) =
list(pretty).
pretty_resource_interface(Core, _ - R) =
[resource_interface_pretty(Core, R), p_nl_double].
:- func pretty_type_interface(core, pair(type_id, user_type)) = list(pretty).
pretty_type_interface(Core, _ - Type) = Pretty :-
Pretty = [type_interface_pretty(Core, Type), p_nl_double].
:- func pretty_func_interface(core, pair(func_id, function)) = list(pretty).
pretty_func_interface(Core, _ - Func) = Pretty :-
Pretty = [p_expr(func_decl_pretty(Core, Func)), p_nl_double].
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%