-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathultimate.al
More file actions
217 lines (180 loc) · 4.75 KB
/
Copy pathultimate.al
File metadata and controls
217 lines (180 loc) · 4.75 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// --- DATA STRUCTURES ---
struct File {
int id;
int size;
str name;
str content;
}
struct System {
int file_count;
int max_files;
int boot_time;
}
// Global pointers
int[] disk;
System sys;
// --- HELPER FUNCTIONS ---
// Feature: Multiple Return Values (Tuple)
function get_file_meta(File f) : (int id, int size, str name) {
return f.id, f.size, f.name;
}
// Feature: Heap Allocation & Break
function create_file(int id, str name, str content) : (int success) {
if (sys.file_count >= sys.max_files) {
throw "Disk Full! Delete some files first.";
}
// Find empty slot using BREAK
int slot = -1;
for (int i = 0; i < sys.max_files; i++) {
if (disk[i] == 0) {
slot = i;
break; // <--- NEW FEATURE: Exit loop immediately
}
}
if (slot == -1) {
throw "System Error: Count mismatch (Fragmentation).";
}
// Manual Memory Allocation (Heap)
File f = new (File);
f.id = id;
f.name = name;
f.content = content;
f.size = 1024;
disk[slot] = f;
// Feature: Struct Field Increment
sys.file_count++;
print("File '%{str}' created at Index %{int}.", name, slot);
return 1;
}
// Feature: Manual Free & Decrement
function delete_file(int id) : (int success) {
int found = 0;
for (int i = 0; i < sys.max_files; i++) {
if (disk[i] == 0) {
continue; // <--- NEW FEATURE: Skip empty slots
}
File f = disk[i];
if (f.id == id) {
print("Deleting '%{str}'...", f.name);
free(f); // <--- Manual Memory Management
disk[i] = 0; // Nullify pointer
// Feature: Struct Field Decrement
sys.file_count--;
found = 1;
break; // Stop searching
}
}
if (found == 0) {
throw "File ID not found!";
}
return 1;
}
// Feature: Stack Allocation (Auto-Free)
function clipboard_demo() : (int result) {
print("--- Stack Memory Test ---");
// This allocates on the stack frame.
// It vanishes automatically when function returns.
File clip = stack(File);
clip.id = 999;
clip.name = "clipboard.tmp";
clip.content = "Copied Data";
print("Clipboard [Stack] Address: %{int}", clip);
print("Data: %{str}", clip.content);
return 1;
}
// --- MAIN KERNEL ---
void main() {
print("========================================");
print(" A B Y S S O S v 2 . 0 ");
print("========================================");
// Initialize System
sys = new (System);
sys.max_files = 5;
sys.file_count = 0;
sys.boot_time = clock();
// Initialize Disk (Array of pointers)
disk = new (int, 5);
// Init disk to 0
for (int i = 0; i < 5; i++) {
disk[i] = 0;
}
int running = 1;
int next_id = 100;
while (running) {
print("");
print("--- MENU [Files: %{int}/%{int}] ---", sys.file_count, sys.max_files);
print("1. New File");
print("2. List Files");
print("3. File Info (Unpacking)");
print("4. Delete File");
print("5. Clipboard (Stack)");
print("6. Memory Map (Abyss Eye)");
print("7. Shutdown");
print("Select > ");
int choice = input_int();
print("");
try {
if (choice == 1) {
create_file(next_id, "doc.txt", "Hello World");
// Feature: Variable Increment
next_id++;
} else if (choice == 2) {
print("--- FILE LIST ---");
int empty = 1;
for (int k = 0; k < sys.max_files; k++) {
if (disk[k] == 0) {
continue; // <--- Skip empty
}
File f = disk[k];
print("[%{int}] %{str}", f.id, f.name);
empty = 0;
}
if (empty) {
print("(Disk Empty)");
}
} else if (choice == 3) {
print("Enter File ID:");
int target = input_int();
int found = 0;
for (int i = 0; i < sys.max_files; i++) {
if (disk[i] == 0) {
continue;
}
File f = disk[i];
if (f.id == target) {
// Feature: Tuple Unpacking
int fid;
int fsize;
str fname;
fid, fsize, fname = get_file_meta(f);
print("ID: %{int} | Name: %{str} | Size: %{int}", fid, fname,
fsize);
found = 1;
break;
}
}
if (found == 0) {
throw "File not found.";
}
} else if (choice == 4) {
print("Enter File ID to delete:");
int target = input_int();
delete_file(target);
print("Deleted.");
} else if (choice == 5) {
clipboard_demo();
print("(Stack memory reclaimed)");
} else if (choice == 6) {
abyss_eye();
} else if (choice == 7) {
running = 0;
}
} catch (err) {
print("!!! ERROR: %{str} !!!", err);
}
}
print("Shutting down...");
free(disk);
free(sys);
print("Bye.");
}