From e4de76cb382aad6b4cfc088a64445dcbd4dc08d8 Mon Sep 17 00:00:00 2001 From: niansa Date: Wed, 10 Mar 2021 10:21:36 +0100 Subject: [PATCH] Further progress in conditions --- include/pilang.hpp | 2 ++ pilang.cpp | 57 +++++++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/include/pilang.hpp b/include/pilang.hpp index f5b71a0..c8fe8e4 100644 --- a/include/pilang.hpp +++ b/include/pilang.hpp @@ -66,6 +66,8 @@ namespace Pilang3 { using Data = std::variant; Data data; + + static void derefer(SharedEnvironment env, Variable& var); }; using Cmdargs = std::vector; diff --git a/pilang.cpp b/pilang.cpp index 49f2076..8b2afa8 100644 --- a/pilang.cpp +++ b/pilang.cpp @@ -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) { + thisarg.data = cache; if (autoderef) { - auto res = scope.find(cache); - if (res == scope.end()) { - throw exceptions::NoSuchVariable(); - } - thisarg = *res->second; - } else { - thisarg.data = cache; + Variable::derefer(env, thisarg); } newarg = true; } else { @@ -333,29 +329,34 @@ namespace Pilang3 { } } + void Variable::derefer(SharedEnvironment env, Variable& var) { + switch (var.type) { + case Variable::id_evaluation: { + SharedEvaluation evaluation; + bool exec; + std::tie(evaluation, exec) = std::get(var.data); + // Execute evaluation only if allowed (ie not &!) + if (exec) { + evaluation->derefer(env); + var = evaluation->execute(env); + } + } break; + case Variable::id_reference: { + auto &scope = env->currScope(); + auto x = std::get(var.data); + auto res = scope.find(std::get(var.data)); + if (res == scope.end()) { + throw exceptions::NoSuchVariable(); + } + var = *res->second; + } break; + default: return; + } + } + void Evaluation::derefer(SharedEnvironment env) { for (auto& arg : arguments) { - switch (arg.type) { - case Variable::id_evaluation: { - SharedEvaluation evaluation; - bool exec; - std::tie(evaluation, exec) = std::get(arg.data); - // Execute evaluation only if allowed (ie not &!) - if (exec) { - evaluation->derefer(env); - arg = evaluation->execute(env); - } - } break; - case Variable::id_reference: { - auto &scope = env->currScope(); - auto res = scope.find(std::get(arg.data)); - if (res == scope.end()) { - throw exceptions::NoSuchVariable(); - } - arg = *res->second; - } break; - default: continue; - } + Variable::derefer(env, arg); } }