aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp99
1 files changed, 63 insertions, 36 deletions
diff --git a/main.cpp b/main.cpp
index 28bd27d..e751dd3 100644
--- a/main.cpp
+++ b/main.cpp
@@ -3,64 +3,91 @@
#include "parser.h"
#include "eval.h"
#include "exc.h"
-#include <cstdio>
-int main(int argc, char **argv) {
- //freopen("in.scm", "r", stdin);
- Tokenizor *tk = new Tokenizor();
- ASTGenerator *ast = new ASTGenerator();
- Evaluator *eval = new Evaluator();
+#include <cstdio>
+#include <cstdlib>
- bool interactive = false;
- bool preload = false;
- char *fname;
+Tokenizor *tk = new Tokenizor();
+ASTGenerator *ast = new ASTGenerator();
+Evaluator *eval = new Evaluator();
- int rcnt = 0;
- for (int i = 1; i < argc; i++)
- if (strcmp(argv[i], "-i") == 0)
- interactive = true;
- else if (strcmp(argv[i], "-l") == 0 && i < argc - 1)
+void load_file(const char *fname) {
+ FILE *f = fopen(fname, "r");
+ if (!f)
+ {
+ printf("Can not open file: %s\n", fname);
+ exit(0);
+ }
+ tk->set_stream(f);
+ while (1)
+ {
+ try
{
- preload = true;
- fname = argv[i + 1];
+ Pair *tree = ast->absorb(tk);
+ if (!tree) break;
+ eval->run_expr(tree);
}
-
- if (preload)
- {
- FILE *f = fopen(fname, "r");
- if (!f)
+ catch (GeneralError &e)
{
- printf("Can not open file: %s\n", fname);
- return 0;
+ fprintf(stderr, "An error occured: %s\n", e.get_msg().c_str());
}
- tk->set_stream(f);
- while (1)
+ }
+}
+
+void print_help(const char *cmd) {
+ fprintf(stderr,
+ "Sonsi: Stupid and Obvious Scheme Interpreter\n"
+ "Usage: %s OPTION ...\n"
+ "Evaluate Scheme code, interactively or from a script.\n\n"
+ " FILE \t\tload Scheme source code from FILE, and exit\n"
+ "The above switches stop argument processing\n\n"
+ " -l FILE \tload Scheme source code from FILE\n"
+ " -h display \tthis help and exit\n", cmd);
+ exit(0);
+}
+
+int main(int argc, char **argv) {
+
+ for (int i = 1; i < argc; i++)
+ {
+ if (*argv[i] == '-') // parsing options
{
- try
+ if (strcmp(argv[i], "-l") == 0)
{
- Pair *tree = ast->absorb(tk);
- if (!tree) break;
- eval->run_expr(tree)->ext_repr().c_str();
+ if (i + 1 < argc)
+ load_file(argv[++i]);
+ else
+ {
+ puts("missing argument to `-l` switch");
+ print_help(*argv);
+ }
}
- catch (GeneralError &e)
+ else if (strcmp(argv[i], "-h") == 0)
+ print_help(*argv);
+ else
{
- fprintf(stderr, "An error occured: %s\n", e.get_msg().c_str());
+ printf("unrecognized switch `%s`\n", argv[i]);
+ print_help(*argv);
}
}
- interactive = true;
+ else
+ {
+ load_file(argv[i]);
+ exit(0);
+ }
}
- tk->set_stream(stdin);
+
+ int rcnt = 0;
+ tk->set_stream(stdin); // interactive mode
while (1)
{
- if (interactive)
fprintf(stderr, "Sonsi> ");
try
{
Pair *tree = ast->absorb(tk);
if (!tree) break;
string output = eval->run_expr(tree)->ext_repr();
- if (interactive)
- fprintf(stderr, "Ret> $%d = %s\n", rcnt++, output.c_str());
+ fprintf(stderr, "Ret> $%d = %s\n", rcnt++, output.c_str());
}
catch (GeneralError &e)
{