-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (69 loc) · 1.89 KB
/
Copy pathmain.cpp
File metadata and controls
77 lines (69 loc) · 1.89 KB
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
//-----------------------------------------------------------------------------
// C++ implementation of a Forth interpreter
// Copyright (c) Paulo Custodio, 2020-2026
// License: GPL3 https://www.gnu.org/licenses/gpl-3.0.html
//-----------------------------------------------------------------------------
#include "environment.h"
#include "forth.h"
#include "vm.h"
#include <iostream>
#include <cstring>
const char* FORTH_ENV = "FORTH";
static void die_usage() {
std::cerr << "Usage: forth [-e forth] [-t] [source [args...]]" << std::endl;
exit(EXIT_FAILURE);
}
int main(int argc, char* argv[]) {
// parse env variable
const char* envp = getenv(FORTH_ENV);
if (envp != nullptr) {
vm.input.set_text(envp, static_cast<uint>(strlen(envp)));
f_execute(xtINTERPRET);
}
// parse command line
g_argc = argc;
g_argv = argv;
g_interactive = false;
bool did_forth = false;
g_argc--;
g_argv++;
while (g_argc > 0 && g_argv[0][0] == '-') {
switch (g_argv[0][1]) {
case 'e':
if (g_argc == 1) {
die_usage();
}
else {
g_argc--;
g_argv++;
vm.input.set_text(g_argv[0], static_cast<uint>(strlen(g_argv[0])));
f_execute(xtINTERPRET);
did_forth = true;
}
break;
case 't':
vm.user->TRACE = F_TRUE;
break;
default:
die_usage();
}
g_argc--;
g_argv++;
}
// get script, if any
if (g_argc == 0) {
if (!did_forth) {
vm.input.open_terminal();
g_interactive = true;
f_execute(xtQUIT);
}
}
else {
const char* source = g_argv[0];
g_argc--;
g_argv++;
vm.input.open_file(source);
f_execute(xtQUIT);
}
return EXIT_SUCCESS;
}