-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.c
88 lines (71 loc) · 1.58 KB
/
runtime.c
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
//
// runtime.c
// Minimalist "runtime" / startup support for SwiSwi
//
// Created by Arpad Goretity "H2CO3"
// for the Budapest Swift Meetup
//
// There's absolutely no warranty of any kind.
//
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
typedef double Double;
typedef int64_t Int;
typedef struct {
char *buf;
Int size;
} String;
String __string_literal(char *c_string) {
return (String){ strdup(c_string), strlen(c_string) };
}
void __string_destroy(String *str)
{
free(str->buf);
}
extern void swimain(Int);
String intToString(Int n) {
size_t len = snprintf(NULL, 0, "%" PRIi64, n);
char *buf = malloc(len + 1);
snprintf(buf, len + 1, "%" PRIi64, n);
return (String){ buf, len };
}
String doubleToString(Double x) {
size_t len = snprintf(NULL, 0, "%lf", x);
char *buf = malloc(len + 1);
snprintf(buf, len + 1, "%lf", x);
return (String){ buf, len };
}
String getLine() {
char buf[LINE_MAX] = { 0 };
fgets(buf, sizeof buf, stdin);
char *newline = strchr(buf, '\n');
if (newline) {
*newline = 0;
}
return (String){ strdup(buf), strlen(buf) };
}
Int stringToInt(String s) {
return strtoll(s.buf, NULL, 10);
}
Double stringToDouble(String s) {
return strtod(s.buf, NULL);
}
void print(String s) {
fprintf(stdout, "%.*s\n", (int)s.size, s.buf);
fflush(stdout);
}
void panic(String s) {
fprintf(stderr, "SwiSwi PANIC: %.*s\n", (int)s.size, s.buf);
fflush(stderr);
abort();
}
int main(int argc, char *argv[]) {
swimain(strtoll(argv[1], NULL, 10));
return 0;
}