-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmilone.h
185 lines (136 loc) · 5.75 KB
/
milone.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Runtime code for C programs generated by the milone-lang compiler.
// Interface is still unstable.
#ifndef MILONE_H_INCLUDED
#define MILONE_H_INCLUDED
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
void milone_start(int argc, char **argv);
// -----------------------------------------------
// memory management
// -----------------------------------------------
void milone_region_enter(void);
void milone_region_leave(void);
void *milone_region_alloc(int32_t count, int32_t size);
void milone_region_defer(void (*fun)(void const *env), void const *env);
// -----------------------------------------------
// integers
// -----------------------------------------------
int milone_int32_compare(int32_t l, int32_t r);
int milone_int64_compare(int64_t l, int64_t r);
int milone_uint64_compare(uint64_t l, uint64_t r);
// -----------------------------------------------
// String
// -----------------------------------------------
// This assumes some invariants, see documentation about string type.
struct String {
char const *ptr;
int32_t len;
};
// Convert a null-terminated string to a string object by computing its length.
//
// SAFETY: The resulting string is valid until the pointer is valid.
struct String string_borrow(char const *c_str);
// Compare two strings in lexicographical order.
int string_compare(struct String l, struct String r);
// Create a copy of native C string.
struct String string_of_raw_parts(char const *ptr, int32_t len);
// Create a copy of native null-terminated C string.
struct String string_of_c_str(char const *s);
// Create a concatenation of two string.
struct String string_add(struct String l, struct String r);
// Create a slice of string.
struct String string_slice(struct String s, int32_t l, int32_t r);
/// Implementation of `s.[l..r]`; `r` is inclusive.
static struct String string_get_slice(int32_t l, int32_t r, struct String s) {
return string_slice(s, l, r + 1);
}
struct String string_ensure_null_terminated(struct String s);
// Ensure null-terminated.
char const *string_to_c_str(struct String s);
// Conversion:
int8_t string_to_int8(struct String s);
int16_t string_to_int16(struct String s);
int32_t string_to_int32(struct String s);
int64_t string_to_int64(struct String s);
intptr_t string_to_nativeint(struct String s);
uint8_t string_to_uint8(struct String s);
uint16_t string_to_uint16(struct String s);
uint32_t string_to_uint32(struct String s);
uint64_t string_to_uint64(struct String s);
uintptr_t string_to_unativeint(struct String s);
struct String string_of_int64(int64_t value);
struct String string_of_uint64(uint64_t value);
double string_to_float64(struct String s);
struct String string_of_float64(double value);
struct String string_of_bool(bool value);
char string_to_char(struct String s);
struct String string_of_char(char value);
// Actual name of string cons-cell.
struct StringCons;
struct String string_concat(struct String sep,
struct StringCons const *strings);
// Low level operations.
inline char const *string_as_ptr(struct String s) { return s.ptr; }
// -----------------------------------------------
// OsString
// -----------------------------------------------
// #milone_os_string
// HACK: This section should be written in an appropriate module
// but there's no good way.
// On Windows, this should include Windows.h and tchar.h if necessary.
#if defined(_MSC_VER) // On Windows MSVC
// UTF-16 code unit
typedef uint16_t OsChar;
#else
// UTF-8 code unit
typedef char OsChar;
#endif
// Pointer to a NUL-terminated string of OS-dependent encoding.
// LPCTSTR on Windows, char const * on Linux.
typedef OsChar const *OsStringPtr;
// String of OS-dependent encoding.
//
// This holds the same invariants as String.
struct MiloneOsString {
OsStringPtr ptr;
int32_t len;
};
// Wraps a pointer with a string object.
//
// SAFETY: Tha result is valid as long as the given pointer is valid.
struct MiloneOsString milone_os_string_borrow(OsStringPtr ptr);
// Creates an OS-encode string by copying from a NUL-terminated string.
// The result is guaranteed to be NUL-terminated.
struct MiloneOsString milone_os_string_of_native(OsStringPtr ptr);
// Creates an OS-encode string by converting from a UTF-8 string.
// The result is guaranteed to be NUL-terminated.
struct MiloneOsString milone_os_string_of(struct String s);
// Creates a UTF-8 string by converting from an OS-encode string.
// The result is guaranteed to be NUL-terminated.
struct String milone_os_string_to(struct MiloneOsString s);
// -----------------------------------------------
// runtime error
// -----------------------------------------------
// Invoke a runtime error with an error message to terminate the process.
_Noreturn void milone_abort(char const *name, char const *filename, int32_t row,
int32_t column);
// Invoked when asserted condition was evaluated to false.
_Noreturn static void milone_assert_error(char const *filename, int32_t row,
int32_t column) {
milone_abort("Assertion Error", filename, row, column);
}
// Invoked when match expression didn't have any arm to match.
_Noreturn static void milone_exhaust_error(char const *filename, int32_t row,
int32_t column) {
milone_abort("Exhaustion Error", filename, row, column);
}
// Invoked when never-returning function actually returned to caller.
_Noreturn static void milone_never_error(char const *filename, int32_t row,
int32_t column) {
milone_abort("Never Error", filename, row, column);
}
// Invoke a runtime error, commonly used by runtime libraries.
_Noreturn void milone_failwith(char const *msg);
_Noreturn void milone_failwithf(char const *fmt, ...);
#endif