summaryrefslogtreecommitdiff
path: root/main.c
blob: 8ab9f25260e544ec672b20ca201bca1f3dbd9818 (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
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
/* Ninja
 * Copyright 2007, Soren Sandmann Pedersen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "ast.h"

ast_t *
compile (const char *input, gboolean do_optimize)
{
    token_t *tokens;
    ast_t *ast;

    if (!(tokens = scan (input)))
	return NULL;

    if (!(ast = parse (tokens)))
	return NULL;

    if (!prepare (ast))
	return NULL;

    if (!symbol (ast))
	return NULL;

    if (!type_check (ast))
	return NULL;

    if (!mark_constants (ast))
	return NULL;

    if (!switch_check (ast))
	return NULL;

    if (!graph (ast))
	return NULL;

    if (!init_check (ast))
	return NULL;

    if (!return_check (ast))
	return NULL;

    if (!levels (ast))
	return NULL;

    if (do_optimize && !optimize (ast))
	return NULL;

    if (!offsets (ast))
	return NULL;

    return ast;
}

static void
usage (const char *arg0)
{
    g_print ("Usage: %s [-O] [-d] filename\n", arg0);
    g_print ("\n");
    g_print ("-O: Optimize\n");
    g_print ("-d: Print debug spew\n");
    exit (1);
}

int
main (int argc, char **argv)
{
    gboolean optimize = FALSE;
    gboolean debug_spew = FALSE;
    gboolean run_tests = FALSE;
    const char *filename = NULL;
    char *contents;
    size_t size;
    int i;
    
    for (i = 1; i < argc; ++i)
    {
	char *arg = argv[i];

	if (strcmp (arg, "-O") == 0)
	{
	    optimize = TRUE;
	}
	else if (strcmp (arg, "-t") == 0)
	{
	    run_tests = TRUE;
	}
	else if (strcmp (arg, "-d") == 0)
	{
	    debug_spew = TRUE;
	}
	else if (!filename)
	{
	    filename = arg;
	}
	else
	{
	    usage (argv[0]);
	}
    }

    if (run_tests)
	run_internal_tests ();
    else if (!filename)
	usage (argv[0]);

    if (!g_file_get_contents (filename, &contents, &size, NULL))
    {
	g_print ("Could not read %s\n", filename);
	return 0;
    }
    else
    {
	ast_t *ast;

	if ((ast = compile (contents, optimize)))
	{
	    if (debug_spew)
	    {
		dump_program (&ast->program);
		dump_graph (&ast->program);
	    }
	    interpret (ast);
	}
    }

    return 0;
}