-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cc
203 lines (159 loc) · 5.76 KB
/
main.cc
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// This file is part of nuBASIC
// Copyright (c) Antonino Calderone ([email protected])
// All rights reserved.
// Licensed under the MIT License.
// See COPYING file in the project root for full license information.
//
/* -------------------------------------------------------------------------- */
#include "nu_builtin_help.h"
#include "nu_exception.h"
#include "nu_interpreter.h"
#include "nu_os_console.h"
#include "nu_os_std.h"
#include "nu_reserved_keywords.h"
#include "nu_terminal_frame.h"
#include <cassert>
#include <iostream>
#include <memory>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <variant>
#ifdef WIN32
#pragma message("Warning: Use winconsole project to build command line version of nuBASIC, which is compatible with Windows 11")
#include <Windows.h>
#endif
/* -------------------------------------------------------------------------- */
static nu::interpreter_t::exec_res_t exec_command(
nu::interpreter_t& basic, const std::string& command)
{
try {
const auto res = basic.exec_command(command);
if (basic.get_and_reset_break_event())
printf("Code execution has been interrupted by CTRL+C\n");
if (res == nu::interpreter_t::exec_res_t::BREAKPOINT)
printf("Execution stopped at breakpoint, line %i.\n"
"Type 'cont' to continue\n",
basic.get_cur_line_n());
else if (res == nu::interpreter_t::exec_res_t::STOP_REQ)
printf("Execution stopped at STOP instruction, line %i.\n"
"Type 'cont' to continue\n",
basic.get_cur_line_n());
return res;
}
// Print out Runtime Error Messages
catch (nu::runtime_error_t& e) {
int line = e.get_line_num();
line = line <= 0 ? basic.get_cur_line_n() : line;
printf(
"Runtime Error #%i at %i %s\n", e.get_error_code(), line, e.what());
}
// Print out Syntax Error Messages
catch (std::exception& e) {
if (basic.get_cur_line_n() > 0)
printf("At line %i: %s\n", basic.get_cur_line_n(), e.what());
else
printf("%s\n", e.what());
return nu::interpreter_t::exec_res_t::RT_ERROR;
}
catch (...) {
printf("Runtime Error\n");
return nu::interpreter_t::exec_res_t::RT_ERROR;
}
return nu::interpreter_t::exec_res_t::CMD_EXEC;
}
/* -------------------------------------------------------------------------- */
static int nuBASIC_console(int argc, char* argv[])
{
nu::_os_cls();
nu::interpreter_t nuBASIC;
std::string command_line;
bool first_command = false;
if (argc >= 2) {
int i = 1;
while (argc-- > 1) {
std::string param = argv[i];
if (param != NU_BASIC_XTERM_FRAME_SWITCH
&& param != NU_BASIC_XTERM_NOFRAME_SWITCH) {
if (argc > 1 && param.size() == 2 && param.c_str()[0] == '-') {
switch (param.c_str()[1]) {
case NU_BASIC_EXEC_MACRO:
param = "EXEC \"" + std::string(argv[++i]) + "\"";
--argc;
break;
case NU_BASIC_LOAD_MACRO:
param = "LOAD \"" + std::string(argv[++i]) + "\"";
--argc;
break;
#ifdef WIN32
case NU_BASIC_HELP_MACRO: {
::FreeConsole();
auto item = std::string(argv[++i]);
auto help
= nu::builtin_help_t::get_instance().help(item);
if (help.empty()) {
::MessageBox(0, "Item not found", item.c_str(),
MB_ICONEXCLAMATION);
}
else {
::MessageBox(0, help.c_str(), item.c_str(),
MB_ICONINFORMATION);
}
exit(0);
break;
}
#endif
}
}
command_line += param + " ";
}
++i;
}
}
std::string command;
if (command_line.empty()) {
const auto ver_str = nuBASIC.version();
printf("%s", ver_str.c_str());
printf(NU_BASIC_MSG_STR__READY NU_BASIC_PROMPT_NEWLINE);
}
else {
command = command_line;
first_command = true;
}
while (1) {
if (!first_command)
command = nu::_os_input(stdin);
else
first_command = false;
if (command.empty())
continue;
nuBASIC.get_and_reset_break_event();
const auto res = exec_command(nuBASIC, command);
switch (res) {
case nu::interpreter_t::exec_res_t::IO_ERROR:
printf(NU_BASIC_ERROR_STR__ERRORLOADING NU_BASIC_PROMPT_NEWLINE);
break;
case nu::interpreter_t::exec_res_t::SYNTAX_ERROR:
printf(NU_BASIC_ERROR_STR__SYNTAXERROR NU_BASIC_PROMPT_NEWLINE);
break;
case nu::interpreter_t::exec_res_t::CMD_EXEC:
printf(NU_BASIC_PROMPT_STR NU_BASIC_PROMPT_NEWLINE);
break;
case nu::interpreter_t::exec_res_t::NOP:
case nu::interpreter_t::exec_res_t::BREAKPOINT:
case nu::interpreter_t::exec_res_t::STOP_REQ:
case nu::interpreter_t::exec_res_t::RT_ERROR:
case nu::interpreter_t::exec_res_t::UPDATE_PROG:
break;
}
}
return 0;
}
/* -------------------------------------------------------------------------- */
int main(int argc, char* argv[])
{
nu::reserved_keywords_t::list();
nu::create_terminal_frame(argc, argv);
return nuBASIC_console(argc, argv);
}