VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
Interpreter.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "SubComponent.h"
16#include "Command.h"
17#include "Exception.h"
18#include "Error.h"
19#include "Parser.h"
20
21namespace vc64 {
22
23struct TooFewArgumentsError : public util::ParseError {
24 using ParseError::ParseError;
25};
26
27struct TooManyArgumentsError : public util::ParseError {
28 using ParseError::ParseError;
29};
30
31struct ScriptInterruption: util::Exception {
32 using Exception::Exception;
33};
34
35class Interpreter: public SubComponent
36{
37 Descriptions descriptions = {{
38
39 .name = "Interpreter",
40 .description = "Command Interpreter"
41 }};
42
43 enum class Shell { Command, Debug };
44
45 // The currently active shell
46 Shell shell = Shell::Command;
47
48 // Commands of the command shell
49 Command commandShellRoot;
50
51 // Commands of the debug shell
52 Command debugShellRoot;
53
54
55 //
56 // Methods
57 //
58
59public:
60
61 Interpreter(C64 &ref);
62 Interpreter& operator= (const Interpreter& other) { return *this; }
63
64
65 //
66 // Methods from Serializable
67 //
68
69public:
70
71 template <class T> void serialize(T& worker) { } SERIALIZERS(serialize);
72
73
74 //
75 // Methods from CoreComponent
76 //
77
78public:
79
80 const Descriptions &getDescriptions() const override { return descriptions; }
81
82private:
83
84 void _dump(Category category, std::ostream& os) const override { }
85 void _initialize() override;
86
87 void initCommons(Command &root);
88 void initCommandShell(Command &root);
89 void initDebugShell(Command &root);
90
91public:
92
93 static string shellName(const CoreObject &object);
94
95
96 //
97 // Parsing input
98 //
99
100public:
101
102 // Auto-completes a user command
103 string autoComplete(const string& userInput);
104
105private:
106
107 // Splits an input string into an argument list
108 Arguments split(const string& userInput);
109
110 // Auto-completes an argument list
111 void autoComplete(Arguments &argv);
112
113 // Parses an argument of a certain type
114 bool isBool(const string &argv);
115 bool parseBool(const string &argv);
116 bool parseBool(const string &argv, bool fallback);
117 bool parseBool(const Arguments &argv, long nr, long fallback);
118
119 bool isOnOff(const string &argv);
120 bool parseOnOff(const string &argv);
121 bool parseOnOff(const string &argv, bool fallback);
122 bool parseOnOff(const Arguments &argv, long nr, long fallback);
123
124 long isNum(const string &argv);
125 long parseNum(const string &argv);
126 long parseNum(const string &argv, long fallback);
127 long parseNum(const Arguments &argv, long nr, long fallback);
128
129 u16 parseAddr(const string &argv) { return (u16)parseNum(argv); }
130 u16 parseAddr(const string &argv, long fallback) { return (u16)parseNum(argv, fallback); }
131 u16 parseAddr(const Arguments &argv, long nr, long fallback) { return (u16)parseNum(argv, nr, fallback); }
132
133 string parseSeq(const string &argv);
134 string parseSeq(const string &argv, const string &fallback);
135
136 template <typename T> long parseEnum(const string &argv) {
137 return util::parseEnum<T>(argv);
138 }
139 template <typename T> long parseEnum(const string &argv, long fallback) {
140 try { return util::parseEnum<T>(argv); } catch(...) { return fallback; }
141 }
142 template <typename E, typename T> E parseEnum(const string &argv) {
143 return (E)util::parseEnum<T>(argv);
144 }
145 template <typename E, typename T> long parseEnum(const string &argv, E fallback) {
146 try { return (E)util::parseEnum<T>(argv); } catch(...) { return fallback; }
147 }
148
149
150 //
151 // Managing the interpreter
152 //
153
154public:
155
156 // Returns the root node of the currently active instruction tree
157 Command &getRoot();
158
159 // Toggles between the command shell and the debug shell
160 void switchInterpreter();
161
162 bool inCommandShell() { return shell == Shell::Command; }
163 bool inDebugShell() { return shell == Shell::Debug; }
164
165
166
167 //
168 // Executing commands
169 //
170
171public:
172
173 // Executes a single command
174 void exec(const string& userInput, bool verbose = false) throws;
175 void exec(const Arguments &argv, bool verbose = false) throws;
176
177 // Prints a usage string for a command
178 void usage(const Command &command);
179
180 // Displays a help text for a (partially typed in) command
181 void help(const string &userInput);
182 void help(const Arguments &argv);
183 void help(const Command &command);
184
185
186 //
187 // Wrappers
188 //
189
190 void configure(Option opt, i64 value) throws;
191 void configure(Option opt, i64 value, isize id) throws;
192};
193
194}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16