forked from solod-dev/solod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
183 lines (164 loc) · 5.01 KB
/
Copy pathmain.c
File metadata and controls
183 lines (164 loc) · 5.01 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
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
#include "main.h"
// -- Types --
typedef struct point point;
typedef struct point {
so_int x;
so_int y;
} point;
// -- Result types --
typedef struct pointResult {
point val;
so_Error err;
} pointResult;
// -- Forward declarations --
static main_FileResult makeFile(so_int size);
static so_R_ptr_err returnPtr(void);
static pointResult makePoint(so_int x, so_int y);
static sub_PointResult makeSubPoint(so_int x, so_int y);
static so_R_int_err divide(so_int a, so_int b);
static so_R_rune_err returnRune(void);
static so_R_str_err returnString(void);
static so_R_slice_err returnSlice(void);
static so_R_int_err forwardCall(void);
// -- Variables and constants --
static main_File file = {0};
// -- Implementation --
static main_FileResult makeFile(so_int size) {
return (main_FileResult){.val = (main_File){.size = size}, .err = (so_Error){0}};
}
so_R_int_err main_File_Read(void* self, so_int buf) {
main_File* f = self;
(void)buf;
return (so_R_int_err){.val = f->size, .err = (so_Error){0}};
}
static so_R_ptr_err returnPtr(void) {
return (so_R_ptr_err){.val = &file, .err = (so_Error){0}};
}
static pointResult makePoint(so_int x, so_int y) {
return (pointResult){.val = (point){.x = x, .y = y}, .err = (so_Error){0}};
}
static sub_PointResult makeSubPoint(so_int x, so_int y) {
return (sub_PointResult){.val = (sub_Point){.X = x, .Y = y}, .err = (so_Error){0}};
}
static so_R_int_err divide(so_int a, so_int b) {
return (so_R_int_err){.val = a / b, .err = (so_Error){0}};
}
static so_R_rune_err returnRune(void) {
return (so_R_rune_err){.val = U'x', .err = (so_Error){0}};
}
static so_R_str_err returnString(void) {
return (so_R_str_err){.val = so_str("hello"), .err = (so_Error){0}};
}
static so_R_slice_err returnSlice(void) {
return (so_R_slice_err){.val = (so_Slice){(so_int[3]){1, 2, 3}, 3, 3}, .err = (so_Error){0}};
}
static so_R_int_err forwardCall(void) {
return divide(10, 3);
}
int main(void) {
{
// Destructure into new variables.
so_R_int_err _res1 = divide(10, 3);
so_int q = _res1.val;
so_Error err = _res1.err;
(void)q;
(void)err;
// Blank identifier.
so_R_int_err _res2 = divide(10, 3);
so_Error err2 = _res2.err;
(void)err2;
so_R_int_err _res3 = divide(10, 3);
so_int r3 = _res3.val;
(void)r3;
// Partial reassignment.
so_R_int_err _res4 = divide(10, 3);
so_int r4 = _res4.val;
err2 = _res4.err;
(void)r4;
// Assign to existing variables.
q = 0;
err = (so_Error){0};
so_R_int_err _res5 = divide(20, 7);
q = _res5.val;
err = _res5.err;
}
{
// If-init with multi-return.
main_File f = (main_File){.size = 42};
{
so_R_int_err _res6 = main_File_Read(&f, 64);
so_int n = _res6.val;
so_Error err = _res6.err;
if (err.self != NULL) {
(void)n;
}
}
}
{
// Various return types.
so_Error err = {0};
(void)err;
so_R_rune_err _res7 = returnRune();
so_rune run = _res7.val;
err = _res7.err;
(void)run;
so_R_str_err _res8 = returnString();
so_String str = _res8.val;
err = _res8.err;
(void)str;
so_R_slice_err _res9 = returnSlice();
so_Slice slice = _res9.val;
err = _res9.err;
(void)slice;
// struc, err := returnStruct()
// _ = struc
so_R_ptr_err _res10 = returnPtr();
main_File* ptr = _res10.val;
err = _res10.err;
// iface, err := returnIface()
// _ = iface
(void)ptr;
}
{
// Forward call.
so_R_int_err _res11 = forwardCall();
so_int q = _res11.val;
so_Error err = _res11.err;
(void)q;
(void)err;
}
{
// Custom exported struct + error.
main_FileResult _res12 = makeFile(42);
main_File f = _res12.val;
so_Error err = _res12.err;
if (f.size != 42 || err.self != NULL) {
so_panic("Custom exported struct failed");
}
}
{
// Custom unexported struct + error.
pointResult _res13 = makePoint(1, 2);
point p = _res13.val;
so_Error err = _res13.err;
if (p.x != 1 || p.y != 2 || err.self != NULL) {
so_panic("Custom unexported struct failed");
}
}
{
// Custom struct from another package + error.
sub_PointResult _res14 = makeSubPoint(1, 2);
sub_Point sp1 = _res14.val;
so_Error err = _res14.err;
if (sp1.X != 1 || sp1.Y != 2 || err.self != NULL) {
so_panic("Custom struct from another package failed");
}
sub_PointResult _res15 = sub_MakePoint(3, 4);
sub_Point sp2 = _res15.val;
err = _res15.err;
if (sp2.X != 3 || sp2.Y != 4 || err.self != NULL) {
so_panic("Custom struct from another package failed");
}
}
return 0;
}