/* Oort * 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 #include #include #include #include "ast.h" ast_t * compile (const char *input, gboolean do_optimize, gboolean debug_spew) { token_t *tokens; ast_t *ast; if (!(tokens = scan (input))) return NULL; if (!(ast = parse (tokens))) return NULL; if (debug_spew) dump_program (&ast->program); 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 (debug_spew) dump_graph (&ast->program); if (!init_check (ast)) return NULL; if (!return_check (ast)) return NULL; if (!levels (ast)) return NULL; if (do_optimize) { if (!optimize (ast)) return NULL; if (debug_spew) { g_print ("-=-=-=-=- Graph after optimization -=-=-=-=-=- \n"); dump_graph (&ast->program); } } 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, debug_spew))) { interpret (ast); } } return 0; }