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:
parent
dc472601bb
commit
e4de76cb38
2 changed files with 31 additions and 28 deletions
|
@ -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>;
|
||||||
|
|
57
pilang.cpp
57
pilang.cpp
|
@ -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) {
|
||||||
|
thisarg.data = cache;
|
||||||
if (autoderef) {
|
if (autoderef) {
|
||||||
auto res = scope.find(cache);
|
Variable::derefer(env, thisarg);
|
||||||
if (res == scope.end()) {
|
|
||||||
throw exceptions::NoSuchVariable();
|
|
||||||
}
|
|
||||||
thisarg = *res->second;
|
|
||||||
} else {
|
|
||||||
thisarg.data = cache;
|
|
||||||
}
|
}
|
||||||
newarg = true;
|
newarg = true;
|
||||||
} else {
|
} 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<SharedEvaluationWBool>(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<std::string>(var.data);
|
||||||
|
auto res = scope.find(std::get<std::string>(var.data));
|
||||||
|
if (res == scope.end()) {
|
||||||
|
throw exceptions::NoSuchVariable();
|
||||||
|
}
|
||||||
|
var = *res->second;
|
||||||
|
} break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Evaluation::derefer(SharedEnvironment env) {
|
void Evaluation::derefer(SharedEnvironment env) {
|
||||||
for (auto& arg : arguments) {
|
for (auto& arg : arguments) {
|
||||||
switch (arg.type) {
|
Variable::derefer(env, arg);
|
||||||
case Variable::id_evaluation: {
|
|
||||||
SharedEvaluation evaluation;
|
|
||||||
bool exec;
|
|
||||||
std::tie(evaluation, exec) = std::get<SharedEvaluationWBool>(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<std::string>(arg.data));
|
|
||||||
if (res == scope.end()) {
|
|
||||||
throw exceptions::NoSuchVariable();
|
|
||||||
}
|
|
||||||
arg = *res->second;
|
|
||||||
} break;
|
|
||||||
default: continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue