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 Environment;
|
||||||
class Evaluation;
|
class Evaluation;
|
||||||
class Function;
|
class Function;
|
||||||
|
class Condition;
|
||||||
struct Variable;
|
struct Variable;
|
||||||
using SharedEnvironment = std::shared_ptr<Environment>;
|
using SharedEnvironment = std::shared_ptr<Environment>;
|
||||||
using SharedVariable = std::shared_ptr<Variable>;
|
using SharedVariable = std::shared_ptr<Variable>;
|
||||||
|
@ -40,6 +41,7 @@ namespace Pilang3 {
|
||||||
id_reference,
|
id_reference,
|
||||||
id_evaluation,
|
id_evaluation,
|
||||||
id_function,
|
id_function,
|
||||||
|
id_condition [[maybe_unused]],
|
||||||
id_type [[maybe_unused]],
|
id_type [[maybe_unused]],
|
||||||
id_retval [[maybe_unused]],
|
id_retval [[maybe_unused]],
|
||||||
id_any [[maybe_unused]],
|
id_any [[maybe_unused]],
|
||||||
|
@ -95,4 +97,20 @@ namespace Pilang3 {
|
||||||
|
|
||||||
Variable execute(SharedEnvironment env, Cmdargs& args);
|
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_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_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_type: fres << std::get<Variable::Type>(arg.data); break;
|
||||||
|
case Variable::id_null: fres << "(null)"; break;
|
||||||
default: fres << "<ID" << arg.type << '>'; break;
|
default: fres << "<ID" << arg.type << '>'; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
35
pilang.cpp
35
pilang.cpp
|
@ -18,6 +18,16 @@ static inline bool is_sep(const char character) {
|
||||||
namespace Pilang3 {
|
namespace Pilang3 {
|
||||||
std::unordered_map<std::string, Cmddesc> builtinCmds;
|
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) {
|
Evaluation::Evaluation(SharedEnvironment env, const std::string& expression, const bool autoeval, const bool autoderef) {
|
||||||
// Count off trailing spaces
|
// Count off trailing spaces
|
||||||
ssize_t tspaces = 0;
|
ssize_t tspaces = 0;
|
||||||
|
@ -34,6 +44,7 @@ namespace Pilang3 {
|
||||||
if (not expression.empty() and static_cast<ssize_t>(expression.size()) != tspaces) {
|
if (not expression.empty() and static_cast<ssize_t>(expression.size()) != tspaces) {
|
||||||
Variable thisarg;
|
Variable thisarg;
|
||||||
bool in_command_name = true;
|
bool in_command_name = true;
|
||||||
|
bool in_string = false;
|
||||||
bool newarg = false;
|
bool newarg = false;
|
||||||
bool escapenext = false;
|
bool escapenext = false;
|
||||||
std::string cache;
|
std::string cache;
|
||||||
|
@ -67,6 +78,7 @@ namespace Pilang3 {
|
||||||
goto parsechar;
|
goto parsechar;
|
||||||
} else if (character == '"') {
|
} else if (character == '"') {
|
||||||
// String
|
// String
|
||||||
|
in_string = true;
|
||||||
thisarg.type = Variable::id_string;
|
thisarg.type = Variable::id_string;
|
||||||
} else if (character == '&') {
|
} else if (character == '&') {
|
||||||
// Expression
|
// Expression
|
||||||
|
@ -75,6 +87,10 @@ namespace Pilang3 {
|
||||||
// Function
|
// Function
|
||||||
thisarg.type = Variable::id_function;
|
thisarg.type = Variable::id_function;
|
||||||
goto parsechar;
|
goto parsechar;
|
||||||
|
} else if (character == '!') {
|
||||||
|
// Function
|
||||||
|
thisarg.type = Variable::id_condition;
|
||||||
|
goto parsechar;
|
||||||
} else {
|
} else {
|
||||||
// Reference
|
// Reference
|
||||||
thisarg.type = Variable::id_reference;
|
thisarg.type = Variable::id_reference;
|
||||||
|
@ -102,14 +118,16 @@ namespace Pilang3 {
|
||||||
} break;
|
} break;
|
||||||
case Variable::id_string: {
|
case Variable::id_string: {
|
||||||
// String
|
// String
|
||||||
if (character == '\\' and not escapenext) {
|
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;
|
escapenext = true;
|
||||||
} else if (character == '"' and not escapenext) {
|
} else if (in_string) {
|
||||||
thisarg.data = cache;
|
|
||||||
newarg = true;
|
|
||||||
character = *(++characterit);
|
|
||||||
} else {
|
|
||||||
cache.push_back(character);
|
cache.push_back(character);
|
||||||
|
} else if (maybe_end_of_arg) {
|
||||||
|
newarg = true;
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case Variable::id_reference: {
|
case Variable::id_reference: {
|
||||||
|
@ -204,7 +222,12 @@ namespace Pilang3 {
|
||||||
thisarg.data = thisfunc;
|
thisarg.data = thisfunc;
|
||||||
newarg = true;
|
newarg = true;
|
||||||
} break;
|
} break;
|
||||||
|
case Variable::id_condition: {
|
||||||
|
//TODO
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
|
// Check if argument might be a condition
|
||||||
|
//TODO
|
||||||
// Actually end argument if required
|
// Actually end argument if required
|
||||||
if (newarg) {
|
if (newarg) {
|
||||||
arguments.push_back(thisarg);
|
arguments.push_back(thisarg);
|
||||||
|
|
Loading…
Add table
Reference in a new issue