-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.c
146 lines (121 loc) · 3.11 KB
/
run.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "includes/common.h"
#include "includes/debug.h"
#include "includes/iota.h"
#include "includes/scanner.h"
#include "includes/vm.h"
#define VERSION "v0.0.3"
const char *get_filename_ext(const char *filename) {
const char *dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot + 1;
}
static void repl(VM *vm) {
char line[1024];
for( ; ; ) {
printf("> ");
if(!fgets(line, sizeof(line), stdin)) {
printf("\n");
break;
}
if(strcmp(line, "exit\n") == 0) {
printf("Thank you for using Pulse\n");
return;
}
interpret(vm, line);
}
}
static char* readFile(const char* path) {
FILE* file = fopen(path, "rb");
if(file == NULL) {
fprintf(stderr, "Could not open file \"%s\".\n", path);
exit(74);
}
fseek(file, 0L, SEEK_END);
size_t fileSize = ftell(file);
rewind(file);
char* buffer = (char *)malloc(fileSize + 1);
if(buffer == NULL) {
fprintf(stderr, "Not enough memory to read \"%s\".\n", path);
exit(74);
}
size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
if(bytesRead < fileSize) {
fprintf(stderr, "Could not read file \"%s\".\n", path);
exit(74);
}
buffer[bytesRead] = '\0';
fclose(file);
return buffer;
}
static void runFile(VM* vm, const char* path) {
char* source = readFile(path);
FILE* file = fopen("temp.txt", "w");
fprintf(file, "%s", path);
fclose(file);
InterpretResult result = interpret(vm, source);
remove("temp.txt");
free(source);
if(result == INTERPRET_COMPILE_ERROR) exit(65);
if(result == INTERPRET_RUNTIME_ERROR) exit(70);
}
int main(int argc, char* argv[]) {
VM vm;
initVM(&vm);
if(argc == 1) {
repl(&vm);
} else if(argc == 2) {
if(strcmp(argv[1], "version") == 0) {
printf("\nPulse version\n=============\n");
puts(VERSION);
printf("\n");
} else {
if(strcmp(get_filename_ext(argv[1]), "pls") != 0) {
printf("Invalid file extension!\n");
printf("Usage pulse [filename][author]\n");
exit(64);
}
char* temp = argv[1];
remove(strcat(temp, ".bc"));
int len = strlen(argv[1]);
argv[1][len - 1] = '\0';
argv[1][len - 2] = '\0';
argv[1][len - 3] = '\0';
runFile(&vm, argv[1]);
}
} else if(argc == 3) {
if(strcmp(argv[2], "author") != 0) {
printf("Invalid command syntax!\n");
printf("Usage pulse [filename][author]\n");
exit(64);
}
char *buffer = readFile(argv[1]);
int len = strlen(buffer);
int i, f = 0;
int pos = -1;
for(i = 0; i < len; i++) {
if(buffer[i] == '~') {
pos = i;
break;
}
}
if(pos == -1) {
printf("Cannot find author token in the pulse file!\n");
exit(64);
} else {
printf("\n**File**: %s\n\n", argv[1]);
printf("Author\n======\n");
for(i = pos+1; buffer[i] != '\n'; i++) {
printf("%c", buffer[i]);
}
printf("\n\n");
}
}else {
fprintf(stderr, "Usage: clox [path][author]\n");
exit(64);
}
freeVM(&vm);
return 0;
}