pascal 0.1.4

A modern Pascal compiler with build/intepreter/package manager built with Rust
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/**
 * poscal-rs Runtime Library
 * 
 * This C library implements the runtime functions for the poscal-rs compiler.
 * It provides implementations for all external functions declared in the
 * standard library units (System, SysUtils, Classes, Math).
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <setjmp.h>

// Add setjmp/longjmp based exception handling
jmp_buf pas_exception_buf;

void pas_raise(int code) {
    longjmp(pas_exception_buf, code);
}

// At the top, after includes
extern int pas_argc;
extern char** pas_argv;

/* ============================================================================
 * String Type Definition
 * ============================================================================ */

typedef struct {
    int length;
    int capacity;
    char* data;
} PascalString;

/* ============================================================================
 * I/O Functions
 * ============================================================================ */

void pas_write(const char* s) {
    printf("%s", s);
    fflush(stdout);
}

void pas_writeln(const char* s) {
    printf("%s\n", s);
}

char* pas_readln() {
    char* line = NULL;
    size_t len = 0;
    getline(&line, &len, stdin);
    return line;
}

void pas_read(char* c) {
    *c = getchar();
}

void pas_readln(char* buffer, int maxlen) {
    if (fgets(buffer, maxlen, stdin) != NULL) {
        size_t len = strlen(buffer);
        if (len > 0 && buffer[len-1] == '\n') {
            buffer[len-1] = '\0';
        }
    }
}

/* ============================================================================
 * String Functions
 * ============================================================================ */

int pas_length(const char* s) {
    return s ? strlen(s) : 0;
}

void pas_copy(const char* s, int index, int count, char* result, int maxlen) {
    if (!s || index < 1 || count < 0) {
        result[0] = '\0';
        return;
    }
    
    int len = strlen(s);
    int start = index - 1; // Pascal is 1-indexed
    
    if (start >= len) {
        result[0] = '\0';
        return;
    }
    
    int actual_count = (start + count > len) ? (len - start) : count;
    if (actual_count > maxlen - 1) {
        actual_count = maxlen - 1;
    }
    
    strncpy(result, s + start, actual_count);
    result[actual_count] = '\0';
}

void pas_concat(const char* s1, const char* s2, char* result, int maxlen) {
    snprintf(result, maxlen, "%s%s", s1 ? s1 : "", s2 ? s2 : "");
}

int pas_pos(const char* substr, const char* s) {
    if (!substr || !s) return 0;
    
    const char* found = strstr(s, substr);
    return found ? (int)(found - s) + 1 : 0; // Pascal is 1-indexed
}

char pas_upcase(char c) {
    return toupper(c);
}

void pas_lowercase(const char* s, char* result, int maxlen) {
    if (!s) {
        result[0] = '\0';
        return;
    }
    
    int len = strlen(s);
    if (len >= maxlen) len = maxlen - 1;
    
    for (int i = 0; i < len; i++) {
        result[i] = tolower(s[i]);
    }
    result[len] = '\0';
}

void pas_uppercase(const char* s, char* result, int maxlen) {
    if (!s) {
        result[0] = '\0';
        return;
    }
    
    int len = strlen(s);
    if (len >= maxlen) len = maxlen - 1;
    
    for (int i = 0; i < len; i++) {
        result[i] = toupper(s[i]);
    }
    result[len] = '\0';
}

/* ============================================================================
 * Type Conversion Functions
 * ============================================================================ */

void pas_inttostr(int i, char* result, int maxlen) {
    snprintf(result, maxlen, "%d", i);
}

int pas_strtoint(const char* s) {
    return s ? atoi(s) : 0;
}

void pas_floattostr(double f, char* result, int maxlen) {
    snprintf(result, maxlen, "%.6f", f);
}

double pas_strtofloat(const char* s) {
    return s ? atof(s) : 0.0;
}

char pas_chr(int i) {
    return (char)i;
}

int pas_ord(char c) {
    return (int)c;
}

/* ============================================================================
 * Math Functions
 * ============================================================================ */

int pas_abs_int(int x) {
    return abs(x);
}

double pas_abs_real(double x) {
    return fabs(x);
}

int pas_sqr_int(int x) {
    return x * x;
}

double pas_sqr_real(double x) {
    return x * x;
}

double pas_sqrt(double x) {
    return sqrt(x);
}

double pas_sin(double x) {
    return sin(x);
}

double pas_cos(double x) {
    return cos(x);
}

double pas_tan(double x) {
    return tan(x);
}

double pas_arctan(double x) {
    return atan(x);
}

double pas_ln(double x) {
    return log(x);
}

double pas_exp(double x) {
    return exp(x);
}

int pas_round(double x) {
    return (int)round(x);
}

int pas_trunc(double x) {
    return (int)trunc(x);
}

double pas_frac(double x) {
    return x - trunc(x);
}

double pas_int(double x) {
    return trunc(x);
}

/* ============================================================================
 * Memory Management Functions
 * ============================================================================ */

void pas_new(void** p, size_t size) {
    *p = malloc(size);
}

void pas_dispose(void** p) {
    if (p && *p) {
        free(*p);
        *p = NULL;
    }
}

void pas_getmem(void** p, size_t size) {
    *p = malloc(size);
}

void pas_freemem(void** p) {
    if (p && *p) {
        free(*p);
        *p = NULL;
    }
}

/* ============================================================================
 * Program Control Functions
 * ============================================================================ */

void pas_halt(void) {
    exit(0);
}

void pas_halt_code(int exitCode) {
    exit(exitCode);
}

/* ============================================================================
 * Global Variables for argc/argv
 * ============================================================================ */

void pas_init_runtime(int argc, char** argv) {
    pas_argc = argc;
    pas_argv = argv;
}

int pas_paramcount(void) {
    return pas_argc - 1;
}

void pas_paramstr(int index, char* result, int maxlen) {
    if (index >= 0 && index < pas_argc) {
        snprintf(result, maxlen, "%s", pas_argv[index]);
    } else {
        result[0] = '\0';
    }
}

int pas_paramcount() {
    return pas_argc - 1;
}

char* pas_paramstr(int i) {
    if (i >= 0 && i < pas_argc) {
        return pas_argv[i];
    }
    return "";
}

void pas_halt() {
    exit(0);
}

/* ============================================================================
 * File Operations
 * ============================================================================ */

typedef struct {
    FILE* handle;
    char filename[256];
    int mode; // 0=closed, 1=read, 2=write
} PascalFile;

void pas_assign(PascalFile* f, const char* name) {
    strncpy(f->filename, name, sizeof(f->filename) - 1);
    f->filename[sizeof(f->filename) - 1] = '\0';
    f->handle = NULL;
    f->mode = 0;
}

void pas_reset(PascalFile* f) {
    f->handle = fopen(f->filename, "r");
    f->mode = f->handle ? 1 : 0;
}

void pas_rewrite(PascalFile* f) {
    f->handle = fopen(f->filename, "w");
    f->mode = f->handle ? 2 : 0;
}

void pas_close(PascalFile* f) {
    if (f->handle) {
        fclose(f->handle);
        f->handle = NULL;
        f->mode = 0;
    }
}

int pas_eof(PascalFile* f) {
    return f->handle ? feof(f->handle) : 1;
}

int pas_eoln(PascalFile* f) {
    if (!f->handle) return 1;
    int c = fgetc(f->handle);
    if (c == EOF) return 1;
    ungetc(c, f->handle);
    return c == '\n';
}

/* ============================================================================
 * Date/Time Functions
 * ============================================================================ */

double pas_now(void) {
    time_t t = time(NULL);
    return (double)t / 86400.0 + 25569.0; // Days since 1899-12-30
}

double pas_date(void) {
    return trunc(pas_now());
}

double pas_time(void) {
    return pas_frac(pas_now());
}

void pas_datetostr(double d, char* result, int maxlen) {
    time_t t = (time_t)((d - 25569.0) * 86400.0);
    struct tm* tm_info = localtime(&t);
    strftime(result, maxlen, "%Y-%m-%d", tm_info);
}

void pas_timetostr(double t, char* result, int maxlen) {
    int seconds = (int)(t * 86400.0);
    int hours = seconds / 3600;
    int minutes = (seconds % 3600) / 60;
    int secs = seconds % 60;
    snprintf(result, maxlen, "%02d:%02d:%02d", hours, minutes, secs);
}

void pas_datetimetostr(double dt, char* result, int maxlen) {
    char date[32], time[32];
    pas_datetostr(dt, date, sizeof(date));
    pas_timetostr(pas_frac(dt), time, sizeof(time));
    snprintf(result, maxlen, "%s %s", date, time);
}

/* ============================================================================
 * Boolean Functions
 * ============================================================================ */

int pas_odd(int x) {
    return x % 2 != 0;
}

int pas_succ_int(int x) {
    return x + 1;
}

char pas_succ_char(char c) {
    return c + 1;
}

int pas_pred_int(int x) {
    return x - 1;
}

char pas_pred_char(char c) {
    return c - 1;
}

/* ============================================================================
 * Min/Max Functions
 * ============================================================================ */

int pas_min_int(int a, int b) {
    return a < b ? a : b;
}

double pas_min_real(double a, double b) {
    return a < b ? a : b;
}

int pas_max_int(int a, int b) {
    return a > b ? a : b;
}

double pas_max_real(double a, double b) {
    return a > b ? a : b;
}

/* ============================================================================
 * File System Functions (SysUtils)
 * ============================================================================ */

int pas_fileexists(const char* filename) {
    struct stat st;
    return stat(filename, &st) == 0 && S_ISREG(st.st_mode);
}

int pas_directoryexists(const char* dirname) {
    struct stat st;
    return stat(dirname, &st) == 0 && S_ISDIR(st.st_mode);
}

int pas_deletefile(const char* filename) {
    return unlink(filename) == 0;
}

int pas_renamefile(const char* oldname, const char* newname) {
    return rename(oldname, newname) == 0;
}

void pas_getcurrentdir(char* result, int maxlen) {
    if (getcwd(result, maxlen) == NULL) {
        result[0] = '\0';
    }
}

int pas_setcurrentdir(const char* dir) {
    return chdir(dir) == 0;
}

int pas_createdir(const char* dir) {
    return mkdir(dir, 0755) == 0;
}

int pas_removedir(const char* dir) {
    return rmdir(dir) == 0;
}

/* ============================================================================
 * Random Functions
 * ============================================================================ */

static int random_initialized = 0;

double pas_random(void) {
    if (!random_initialized) {
        srand(time(NULL));
        random_initialized = 1;
    }
    return (double)rand() / RAND_MAX;
}

int pas_random_range(int range) {
    if (!random_initialized) {
        srand(time(NULL));
        random_initialized = 1;
    }
    return rand() % range;
}

void pas_randomize(void) {
    srand(time(NULL));
    random_initialized = 1;
}

/* ============================================================================
 * Utility Functions
 * ============================================================================ */

void pas_sleep(int milliseconds) {
    usleep(milliseconds * 1000);
}

unsigned int pas_gettickcount(void) {
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (unsigned int)(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}

/* ============================================================================
 * Memory Utility Functions
 * ============================================================================ */

void pas_fillchar(void* x, int count, unsigned char value) {
    memset(x, value, count);
}

void pas_move(const void* source, void* dest, int count) {
    memmove(dest, source, count);
}

int pas_comparemem(const void* buf1, const void* buf2, int count) {
    return memcmp(buf1, buf2, count) == 0;
}

/* ============================================================================
 * Advanced Math Functions
 * ============================================================================ */

double pas_power(double base, double exponent) {
    return pow(base, exponent);
}

double pas_log10(double x) {
    return log10(x);
}

double pas_log2(double x) {
    return log2(x);
}

int pas_ceil(double x) {
    return (int)ceil(x);
}

int pas_floor(double x) {
    return (int)floor(x);
}

double pas_arcsin(double x) {
    return asin(x);
}

double pas_arccos(double x) {
    return acos(x);
}

double pas_arctan2(double y, double x) {
    return atan2(y, x);
}

/* ============================================================================
 * Global Variables for argc/argv
 * ============================================================================ */

int pas_strlen(const char* s) { return strlen(s); }

char* pas_strcopy(const char* s, int index, int count) {
    char* result = malloc(count + 1);
    strncpy(result, s + index - 1, count);
    result[count] = '\0';
    return result;
}

int pas_strpos(const char* sub, const char* s) {
    char* p = strstr(s, sub);
    return p ? (p - s) + 1 : 0;
}