1
0
Fork 0
mirror of https://gitlab.com/niansa/pilang3.git synced 2025-03-06 20:49:20 +01:00

Further progress in conditions

This commit is contained in:
niansa 2021-03-10 10:21:36 +01:00
parent dc472601bb
commit e4de76cb38
2 changed files with 31 additions and 28 deletions

View file

@ -66,6 +66,8 @@ namespace Pilang3 {
using Data = std::variant<std::string, integer_type, SharedVariable, SharedEvaluationWBool, SharedFunction, condition::array, Type>;
Data data;
static void derefer(SharedEnvironment env, Variable& var);
};
using Cmdargs = std::vector<Variable>;

View file

@ -82,6 +82,7 @@ namespace Pilang3 {
// String
in_string = true;
thisarg.type = Variable::id_string;
thisarg.data = std::string();
} else if (character == '&') {
// Expression
thisarg.type = Variable::id_evaluation;
@ -136,14 +137,9 @@ namespace Pilang3 {
case Variable::id_reference: {
// Reference
if (maybe_end_of_arg) {
if (autoderef) {
auto res = scope.find(cache);
if (res == scope.end()) {
throw exceptions::NoSuchVariable();
}
thisarg = *res->second;
} else {
thisarg.data = cache;
if (autoderef) {
Variable::derefer(env, thisarg);
}
newarg = true;
} else {
@ -333,30 +329,35 @@ namespace Pilang3 {
}
}
void Evaluation::derefer(SharedEnvironment env) {
for (auto& arg : arguments) {
switch (arg.type) {
void Variable::derefer(SharedEnvironment env, Variable& var) {
switch (var.type) {
case Variable::id_evaluation: {
SharedEvaluation evaluation;
bool exec;
std::tie(evaluation, exec) = std::get<SharedEvaluationWBool>(arg.data);
std::tie(evaluation, exec) = std::get<SharedEvaluationWBool>(var.data);
// Execute evaluation only if allowed (ie not &!)
if (exec) {
evaluation->derefer(env);
arg = evaluation->execute(env);
var = evaluation->execute(env);
}
} break;
case Variable::id_reference: {
auto &scope = env->currScope();
auto res = scope.find(std::get<std::string>(arg.data));
auto x = std::get<std::string>(var.data);
auto res = scope.find(std::get<std::string>(var.data));
if (res == scope.end()) {
throw exceptions::NoSuchVariable();
}
arg = *res->second;
var = *res->second;
} break;
default: continue;
default: return;
}
}
void Evaluation::derefer(SharedEnvironment env) {
for (auto& arg : arguments) {
Variable::derefer(env, arg);
}
}
Variable Evaluation::execute(SharedEnvironment env) {