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>; using Data = std::variant<std::string, integer_type, SharedVariable, SharedEvaluationWBool, SharedFunction, condition::array, Type>;
Data data; Data data;
static void derefer(SharedEnvironment env, Variable& var);
}; };
using Cmdargs = std::vector<Variable>; using Cmdargs = std::vector<Variable>;

View file

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