mirror of
https://gitlab.com/niansa/pilang3.git
synced 2025-03-06 20:49:20 +01:00
Minor fixups to get ready for implementing conditions
This commit is contained in:
parent
361e6528c0
commit
6acc61b26f
3 changed files with 68 additions and 26 deletions
|
@ -26,6 +26,7 @@ namespace Pilang3 {
|
|||
class Environment;
|
||||
class Evaluation;
|
||||
class Function;
|
||||
class Condition;
|
||||
struct Variable;
|
||||
using SharedEnvironment = std::shared_ptr<Environment>;
|
||||
using SharedVariable = std::shared_ptr<Variable>;
|
||||
|
@ -40,6 +41,7 @@ namespace Pilang3 {
|
|||
id_reference,
|
||||
id_evaluation,
|
||||
id_function,
|
||||
id_condition [[maybe_unused]],
|
||||
id_type [[maybe_unused]],
|
||||
id_retval [[maybe_unused]],
|
||||
id_any [[maybe_unused]],
|
||||
|
@ -95,4 +97,20 @@ namespace Pilang3 {
|
|||
|
||||
Variable execute(SharedEnvironment env, Cmdargs& args);
|
||||
};
|
||||
|
||||
class Condition {
|
||||
public:
|
||||
enum {
|
||||
// Condition special types
|
||||
id_equals,
|
||||
id_notequals,
|
||||
id_biggerthan,
|
||||
id_smallerthan
|
||||
};
|
||||
|
||||
ssize_t exprlen = -1;
|
||||
std::vector<std::string*> arr;
|
||||
|
||||
Condition(const std::string& expression);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -58,6 +58,7 @@ public:
|
|||
case Variable::id_string: fres << std::get<std::string>(arg.data); break;
|
||||
case Variable::id_reference: fres << "REF<" << std::get<std::string>(arg.data) << '>'; break;
|
||||
case Variable::id_type: fres << std::get<Variable::Type>(arg.data); break;
|
||||
case Variable::id_null: fres << "(null)"; break;
|
||||
default: fres << "<ID" << arg.type << '>'; break;
|
||||
}
|
||||
}
|
||||
|
|
75
pilang.cpp
75
pilang.cpp
|
@ -18,6 +18,16 @@ static inline bool is_sep(const char character) {
|
|||
namespace Pilang3 {
|
||||
std::unordered_map<std::string, Cmddesc> builtinCmds;
|
||||
|
||||
Condition::Condition(const std::string& expression) {
|
||||
|
||||
for (auto characterit = expression.begin(); characterit < expression.end(); characterit++) {
|
||||
if (*characterit == ',') {
|
||||
exprlen = std::distance(expression.begin(), characterit);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Evaluation::Evaluation(SharedEnvironment env, const std::string& expression, const bool autoeval, const bool autoderef) {
|
||||
// Count off trailing spaces
|
||||
ssize_t tspaces = 0;
|
||||
|
@ -34,6 +44,7 @@ namespace Pilang3 {
|
|||
if (not expression.empty() and static_cast<ssize_t>(expression.size()) != tspaces) {
|
||||
Variable thisarg;
|
||||
bool in_command_name = true;
|
||||
bool in_string = false;
|
||||
bool newarg = false;
|
||||
bool escapenext = false;
|
||||
std::string cache;
|
||||
|
@ -67,6 +78,7 @@ namespace Pilang3 {
|
|||
goto parsechar;
|
||||
} else if (character == '"') {
|
||||
// String
|
||||
in_string = true;
|
||||
thisarg.type = Variable::id_string;
|
||||
} else if (character == '&') {
|
||||
// Expression
|
||||
|
@ -75,6 +87,10 @@ namespace Pilang3 {
|
|||
// Function
|
||||
thisarg.type = Variable::id_function;
|
||||
goto parsechar;
|
||||
} else if (character == '!') {
|
||||
// Function
|
||||
thisarg.type = Variable::id_condition;
|
||||
goto parsechar;
|
||||
} else {
|
||||
// Reference
|
||||
thisarg.type = Variable::id_reference;
|
||||
|
@ -85,33 +101,35 @@ namespace Pilang3 {
|
|||
bool maybe_end_of_arg = character == ',' or character == ';';
|
||||
// Do whatever (depends on type)
|
||||
switch (thisarg.type) {
|
||||
case Variable::id_integer: {
|
||||
// Integer
|
||||
if (is_sep(character)) continue;
|
||||
else if (maybe_end_of_arg) {
|
||||
// End argument
|
||||
try {
|
||||
thisarg.data = integer_type(std::stoi(cache));
|
||||
} catch (std::invalid_argument&) {
|
||||
throw exceptions::BadInteger();
|
||||
}
|
||||
newarg = true;
|
||||
} else {
|
||||
cache.push_back(character);
|
||||
case Variable::id_integer: {
|
||||
// Integer
|
||||
if (is_sep(character)) continue;
|
||||
else if (maybe_end_of_arg) {
|
||||
// End argument
|
||||
try {
|
||||
thisarg.data = integer_type(std::stoi(cache));
|
||||
} catch (std::invalid_argument&) {
|
||||
throw exceptions::BadInteger();
|
||||
}
|
||||
} break;
|
||||
case Variable::id_string: {
|
||||
// String
|
||||
if (character == '\\' and not escapenext) {
|
||||
escapenext = true;
|
||||
} else if (character == '"' and not escapenext) {
|
||||
thisarg.data = cache;
|
||||
newarg = true;
|
||||
character = *(++characterit);
|
||||
} else {
|
||||
cache.push_back(character);
|
||||
}
|
||||
} break;
|
||||
newarg = true;
|
||||
} else {
|
||||
cache.push_back(character);
|
||||
}
|
||||
} break;
|
||||
case Variable::id_string: {
|
||||
// String
|
||||
if (character == '"' and not escapenext) {
|
||||
std::get<std::string>(thisarg.data).append(cache);
|
||||
cache.clear();
|
||||
in_string = not in_string;
|
||||
} else if (in_string and character == '\\' and not escapenext) {
|
||||
escapenext = true;
|
||||
} else if (in_string) {
|
||||
cache.push_back(character);
|
||||
} else if (maybe_end_of_arg) {
|
||||
newarg = true;
|
||||
}
|
||||
} break;
|
||||
case Variable::id_reference: {
|
||||
// Reference
|
||||
if (maybe_end_of_arg) {
|
||||
|
@ -204,7 +222,12 @@ namespace Pilang3 {
|
|||
thisarg.data = thisfunc;
|
||||
newarg = true;
|
||||
} break;
|
||||
case Variable::id_condition: {
|
||||
//TODO
|
||||
} break;
|
||||
}
|
||||
// Check if argument might be a condition
|
||||
//TODO
|
||||
// Actually end argument if required
|
||||
if (newarg) {
|
||||
arguments.push_back(thisarg);
|
||||
|
|
Loading…
Add table
Reference in a new issue