CommandsInterpreter variables
This commit is contained in:
@@ -222,9 +222,11 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
dynamic::Value fetchOrigin(Argument* arg) {
|
dynamic::Value fetchOrigin(CommandsInterpreter* interpreter, Argument* arg) {
|
||||||
if (dynamic::is_numeric(arg->origin)) {
|
if (dynamic::is_numeric(arg->origin)) {
|
||||||
return arg->origin;
|
return arg->origin;
|
||||||
|
} else if (auto string = std::get_if<std::string>(&arg->origin)) {
|
||||||
|
return (*interpreter)[*string];
|
||||||
}
|
}
|
||||||
return dynamic::NONE;
|
return dynamic::NONE;
|
||||||
}
|
}
|
||||||
@@ -248,12 +250,12 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dynamic::Value parseRelativeValue(Argument* arg) {
|
dynamic::Value parseRelativeValue(CommandsInterpreter* interpreter, Argument* arg) {
|
||||||
if (arg->type != ArgType::number && arg->type != ArgType::integer) {
|
if (arg->type != ArgType::number && arg->type != ArgType::integer) {
|
||||||
throw error("'~' operator is only allowed for numeric arguments");
|
throw error("'~' operator is only allowed for numeric arguments");
|
||||||
}
|
}
|
||||||
nextChar();
|
nextChar();
|
||||||
auto origin = fetchOrigin(arg);
|
auto origin = fetchOrigin(interpreter, arg);
|
||||||
if (peekNoJump() == ' ' || !hasNext()) {
|
if (peekNoJump() == ' ' || !hasNext()) {
|
||||||
return origin;
|
return origin;
|
||||||
}
|
}
|
||||||
@@ -265,11 +267,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline dynamic::Value performKeywordArg(
|
inline dynamic::Value performKeywordArg(
|
||||||
Command* command, const std::string& key
|
CommandsInterpreter* interpreter, Command* command, const std::string& key
|
||||||
) {
|
) {
|
||||||
if (auto arg = command->getArgument(key)) {
|
if (auto arg = command->getArgument(key)) {
|
||||||
nextChar();
|
nextChar();
|
||||||
auto value = peek() == '~' ? parseRelativeValue(arg) : parseValue();
|
auto value = peek() == '~'
|
||||||
|
? parseRelativeValue(interpreter, arg)
|
||||||
|
: parseValue();
|
||||||
typeCheck(arg, value);
|
typeCheck(arg, value);
|
||||||
return value;
|
return value;
|
||||||
} else {
|
} else {
|
||||||
@@ -277,7 +281,8 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Prompt parsePrompt(CommandsRepository* repo) {
|
Prompt parsePrompt(CommandsInterpreter* interpreter) {
|
||||||
|
auto repo = interpreter->getRepository();
|
||||||
std::string name = parseIdentifier();
|
std::string name = parseIdentifier();
|
||||||
auto command = repo->get(name);
|
auto command = repo->get(name);
|
||||||
if (command == nullptr) {
|
if (command == nullptr) {
|
||||||
@@ -301,7 +306,7 @@ public:
|
|||||||
// keyword argument
|
// keyword argument
|
||||||
if (!relative && hasNext() && peek() == '=') {
|
if (!relative && hasNext() && peek() == '=') {
|
||||||
auto key = std::get<std::string>(value);
|
auto key = std::get<std::string>(value);
|
||||||
kwargs->put(key, performKeywordArg(command, key));
|
kwargs->put(key, performKeywordArg(interpreter, command, key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,7 +320,7 @@ public:
|
|||||||
} while (!typeCheck(arg, value));
|
} while (!typeCheck(arg, value));
|
||||||
|
|
||||||
if (relative) {
|
if (relative) {
|
||||||
value = applyRelative(arg, value, fetchOrigin(arg));
|
value = applyRelative(arg, value, fetchOrigin(interpreter, arg));
|
||||||
}
|
}
|
||||||
std::cout << "argument value: " << value << std::endl;
|
std::cout << "argument value: " << value << std::endl;
|
||||||
args->put(value);
|
args->put(value);
|
||||||
@@ -348,5 +353,5 @@ Command* CommandsRepository::get(const std::string& name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Prompt CommandsInterpreter::parse(std::string_view text) {
|
Prompt CommandsInterpreter::parse(std::string_view text) {
|
||||||
return CommandParser("<string>", text).parsePrompt(repository.get());
|
return CommandParser("<string>", text).parsePrompt(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,12 +87,17 @@ namespace cmd {
|
|||||||
|
|
||||||
class CommandsInterpreter {
|
class CommandsInterpreter {
|
||||||
std::unique_ptr<CommandsRepository> repository;
|
std::unique_ptr<CommandsRepository> repository;
|
||||||
|
std::unordered_map<std::string, dynamic::Value> variables;
|
||||||
public:
|
public:
|
||||||
CommandsInterpreter(std::unique_ptr<CommandsRepository> repository)
|
CommandsInterpreter(std::unique_ptr<CommandsRepository> repository)
|
||||||
: repository(std::move(repository)){}
|
: repository(std::move(repository)){}
|
||||||
|
|
||||||
Prompt parse(std::string_view text);
|
Prompt parse(std::string_view text);
|
||||||
|
|
||||||
|
dynamic::Value& operator[](const std::string& name) {
|
||||||
|
return variables[name];
|
||||||
|
}
|
||||||
|
|
||||||
CommandsRepository* getRepository() const {
|
CommandsRepository* getRepository() const {
|
||||||
return repository.get();
|
return repository.get();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user