aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 3faf5d56603ca6992e780b83dc0c1481b85849d3 (plain) (blame)
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
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

/**
 * @file main.c
 *
 * Compiler main file, controls compilation and command line
 * handling.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <ek/debug.h>
#include <ek/imports.h>
#include <ek/compiler.h>

/**
 * String describing compiler usage.
 * @todo I suspect backends might want more flags, come up with
 * some way to make flag handling more generic
 */
static const char *cmdline_usage =
	"ek compiler usage:\n"
	" ek [-I <dir>...] [-D <var>...] infile...\n"
	"	-h       Show usage (this)\n"
	"	-I <dir> Add directory to import path\n"
	"	-D <var> Add predefined variable\n"
	"	-o       Name of output\n"
	"	infile   Top file(s) to compile\n"
;

/** Print usage of compiler. */
static void usage()
{
	fprintf(stderr, cmdline_usage);
}

/**
 * Main entry to compiler.
 * Checks command line parameters and drives the rest of the compiler.
 * Feels kind of weird documenting main, but doxygen warns about not
 * doing it so whatever.
 *
 * @param argc Number of command line arguments.
 * @param argv Array of command line arguments.
 * @return \c 0 when succesful, non-zero otherwise.
 */
int main(int argc, char *argv[])
{
	int opt;
	while ((opt = getopt(argc, argv, "hI:D:o:")) != -1) {
		switch (opt) {
		case 'o':
			error("not yet implemented");
			break;

		case 'I':
			add_import_path(optarg);
			break;

		case 'D':
			/* TODO */
			error("not yet implemented");
			break;

		case 'h':
			usage();
			exit(EXIT_SUCCESS);
		default:
			usage();
			exit(EXIT_FAILURE);
		}
	}

	if (optind >= argc) {
		error("no input files");
		usage();
		exit(EXIT_FAILURE);
	}

	for (int i = optind; i < argc; ++i) {
		debug("starting compilation of '%s'", argv[i]);
		if (compile(argv[i]))
			return -1;
	}

	return 0;
}