86 lines
2.0 KiB
C
86 lines
2.0 KiB
C
#include "../../parser.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define EMBED_FILE(name) \
|
|
void emit_##name(const char *filename) \
|
|
{ \
|
|
FILE *f = fopen(filename, "wb"); \
|
|
if(f) { \
|
|
fwrite(name, 1, name##_len, f); \
|
|
fclose(f); \
|
|
} \
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void
|
|
print_help()
|
|
{
|
|
printf("Usage: undar [options] file...\nOptions:\n\t-emit={c|u}\tEmitter "
|
|
"output, currently C source and Uxn binary.\n\n");
|
|
}
|
|
|
|
#define MEM_SIZE 68000
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
if(argc > 1 && (seq(argv[1], "--help") || seq(argv[1], "-h"))) {
|
|
print_help();
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
if(argc != 3) {
|
|
print_help();
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
Emitter e;
|
|
if(seq(argv[1], "-emit=c")) {
|
|
e = c_emitter();
|
|
} else if(seq(argv[1], "-emit=u")) {
|
|
e = uxn_emitter();
|
|
} else {
|
|
printf("unknown emitter type\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
u8 tape[MEM_SIZE];
|
|
Arena a = {tape, 0, MEM_SIZE};
|
|
|
|
char *source = readFile(argv[2]);
|
|
compile(&a, e, source);
|
|
free(source);
|
|
printf("\n");
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|