hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: a3d1caadf817d6867114c15bd2e33e8c538d6b69 (plain)
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
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "ast.h"
#include "eval.h"
#include "lexer.h"

#define VERSION "0.1.0"

extern struct Node* program_root;
int yyparse(void);

static int run_program(void)
{
    int status = yyparse();
    if (status != 0 || lexer_failed())
        status = 1;
    else
    {
        struct Machine machine;
        machine_init(&machine);
        status = execute(&machine, program_root) == RUN_FAILED;
    }

    node_free(program_root);
    program_root = NULL;
    return status;
}

static void print_usage(FILE* stream)
{
    (void)fprintf(stream,
                  "usage: sfinae <file.sfinae>\n"
                  "       sfinae [-v | -h]\n");
}

static void print_help(void)
{
    (void)printf("sfinae " VERSION " - substitution failure is not an error\n\n"
                 "a toy language inspired by c++ template meta programming\n\n");
    print_usage(stdout);
    (void)printf("\noptions:\n"
                 "  -v, --version    print version\n"
                 "  -h, --help       print this\n\n"
                 "by hachem <im@hachem.wtf>\n");
}

static bool is_flag(const char* argument, const char* brief, const char* full)
{
    return strcmp(argument, brief) == 0 || strcmp(argument, full) == 0;
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        print_usage(stderr);
        return 2;
    }

    if (is_flag(argv[1], "-h", "--help"))
    {
        print_help();
        return 0;
    }

    if (is_flag(argv[1], "-v", "--version"))
    {
        (void)printf("sfinae " VERSION "\n");
        return 0;
    }

    yyin = fopen(argv[1], "r");
    if (yyin == NULL)
    {
        (void)fprintf(stderr, "cannot open %s\n", argv[1]);
        return 1;
    }

    int status = run_program();
    (void)fclose(yyin);
    return status;
}