-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtip_rtlib.c
More file actions
68 lines (58 loc) · 1.7 KB
/
tip_rtlib.c
File metadata and controls
68 lines (58 loc) · 1.7 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
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/*
* These are defined for each TIP program in the compiled code.
*/
int64_t _tip_main();
extern int64_t _tip_num_inputs;
extern int64_t _tip_input_array[];
/*
* runtime library functions for TIP IO expressions and statements
* x = input;
* output y;
* error y;
*/
int64_t _tip_input() {
int64_t x;
printf("Enter input: ");
scanf("%" SCNd64, &x);
return x;
}
void _tip_output(int64_t x) { printf("Program output: %" PRId64 "\n", x); }
void _tip_error(int64_t x) {
printf("[error] Error: Execution error, code: %" PRId64 "\n", x);
exit(-1);
}
/*
* If the compiled program has no "main" function then one is created
* that calls this function.
*/
void _tip_main_undefined() {
printf("Error: missing main function\n");
exit(-1);
}
/*
* Set up the arguments to be read by the TIP "main" function.
* The number of arguments is defined by the compiled TIP code
* and read here to perform error checking. All arguments to
* the TIP "main" are passed through the "_tip_input_array".
* Finally, the TIP "main" is renamed during compilation to "_tip_main",
* its arguments are removed, and code to read them from "_tip_input_array"
* is generated.
*/
int main(int argc, char *argv[]) {
// Throw an error if the wrong number of arguments are passed
if (argc != _tip_num_inputs + 1) {
printf("expected %" PRId64 " integer arguments\n", _tip_num_inputs);
exit(-1);
}
// required by strtoll, but discarded
char *eptr;
for (size_t i = 0; i < _tip_num_inputs; i++) {
_tip_input_array[i] = strtoll(argv[i + 1], &eptr, 10);
}
printf("Program output: %" PRId64 "\n", _tip_main());
return 0;
}