-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
91 lines (77 loc) · 2.31 KB
/
main.c
File metadata and controls
91 lines (77 loc) · 2.31 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
CLI interface for roblox-java.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "roblox-java/transpiler.h"
/* Strings. */
#define NO_ARGUMENTS_STRING "roblox-java: A full-on transpiler for converting Java code into Luau code.\nPass --help for commands or --info for more information.\n"
#define HELP_STRING "--version (--v) -- Version string.\n--compile (--c) <source> <out> -- Compiles all the <source> contents into the <out> directory.\n"
#define VERSION_STRING "roblox-java version %s. (C) All rights reserved.\n"
/* String utilities. */
static int string_size(const char* x) {
int sz = -1;
while (x[++sz] != '\0');
return sz;
}
static int string_compare(const char* x, const char* y) {
int i = -1;
while(x[++i] != '\0' && x[i] == y[i]);
return string_size(y) == i;
}
/* Prints, then exits program. */
#define printf_stop(...) printf(__VA_ARGS__); exit(EXIT_FAILURE)
/*}==========================================*/
/* Commands. */
static inline void help() {
printf_stop(HELP_STRING);
}
static inline void version() {
printf_stop(VERSION_STRING, RBXJ_VERSION);
}
static inline void compile(int argc, char* argv[]) {
/* Get arguments. */
const char* src_dir = argv[2];
const char* out_dir = argv[3];
struct stat* d1;
struct stat* d2;
/* Sanity check. */
/*
! doesnt work atm
if (stat(src_dir, d1) > 0 || stat(out_dir, d2) > 0 ||
(d1->st_mode & S_IFDIR) || (d2->st_mode & S_IFDIR)) {
fprintf(stderr, "Either path is invalid.\n");
exit(EXIT_FAILURE);
}
*/
/* Success! */
RBXJ_Compile_Dirs(src_dir, out_dir);
exit(EXIT_SUCCESS);
}
/*}==========================================*/
/*
====================================================================
MAIN
====================================================================
*/
int main(int argc, char* argv[]) {
if (argc < 2) {
/* No arguments. */
printf_stop(NO_ARGUMENTS_STRING);
}
const char* cmd = argv[1];
/* There is an argument. */
if (string_compare(cmd, "--help")) {
help();
}
if (string_compare(cmd, "--version") || string_compare(cmd, "--v")) {
version();
}
if (string_compare(cmd, "--compile") || string_compare(cmd, "--c")) {
compile(argc, argv);
}
/* Invalid command. */
fprintf(stderr, "Invalid command %s.\n", cmd);
return EXIT_FAILURE;
}