Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Crocker <dcrocker@eschertech.com>2021-03-07 17:07:51 +0300
committerDavid Crocker <dcrocker@eschertech.com>2021-03-07 17:07:51 +0300
commitf91ad24934aca0c14279a5ed86020e802ad3e8de (patch)
tree2dff232fc6126f776f163dcd83a7c0d2a67bd048 /src/GCodes/GCodeBuffer
parentd40f1bcb0248c280975d13d57bfdb0d0c16c9252 (diff)
More work on variables and parameters
Diffstat (limited to 'src/GCodes/GCodeBuffer')
-rw-r--r--src/GCodes/GCodeBuffer/ExpressionParser.cpp29
-rw-r--r--src/GCodes/GCodeBuffer/ExpressionParser.h3
-rw-r--r--src/GCodes/GCodeBuffer/GCodeBuffer.cpp10
-rw-r--r--src/GCodes/GCodeBuffer/GCodeBuffer.h3
4 files changed, 44 insertions, 1 deletions
diff --git a/src/GCodes/GCodeBuffer/ExpressionParser.cpp b/src/GCodes/GCodeBuffer/ExpressionParser.cpp
index 344810e3..48f5c027 100644
--- a/src/GCodes/GCodeBuffer/ExpressionParser.cpp
+++ b/src/GCodes/GCodeBuffer/ExpressionParser.cpp
@@ -1031,6 +1031,23 @@ ExpressionValue ExpressionParser::ParseIdentifierExpression(bool evaluate, bool
// If we are not evaluating then the object expression doesn't have to exist, so don't retrieve it because that might throw an error
if (evaluate)
{
+ // Check for a parameter, local or global variable
+ if (StringStartsWith(id.c_str(), "param."))
+ {
+ return GetVariableValue(gb.GetVariables(), id.c_str() + strlen("param."), true);
+ }
+
+ if (StringStartsWith(id.c_str(), "global."))
+ {
+ return GetVariableValue(gb.GetVariables(), id.c_str() + strlen("global."), false);
+ }
+
+ if (StringStartsWith(id.c_str(), "user."))
+ {
+ return GetVariableValue(reprap.globalVariables, id.c_str() + strlen("user."), false);
+ }
+
+ // Else assume an object model value
ExpressionValue value = reprap.GetObjectValue(context, nullptr, id.c_str());
if (context.ObsoleteFieldQueried() && obsoleteField.IsEmpty())
{
@@ -1041,6 +1058,18 @@ ExpressionValue ExpressionParser::ParseIdentifierExpression(bool evaluate, bool
return ExpressionValue(nullptr);
}
+// Get the value of a variable
+ExpressionValue ExpressionParser::GetVariableValue(VariableSet& vars, const char *name, bool parameter) THROWS(GCodeException)
+{
+ const Variable* var = vars.Lookup(name);
+ if (var != nullptr && (!parameter || var->GetScope() < 0))
+ {
+ return var->GetValue();
+ }
+
+ throw ConstructParseException("Unknown variable");
+}
+
// Parse a quoted string, given that the current character is double-quote
// This is almost a copy of InternalGetQuotedString in class StringParser
ExpressionValue ExpressionParser::ParseQuotedString() THROWS(GCodeException)
diff --git a/src/GCodes/GCodeBuffer/ExpressionParser.h b/src/GCodes/GCodeBuffer/ExpressionParser.h
index f48679fc..3dd5f236 100644
--- a/src/GCodes/GCodeBuffer/ExpressionParser.h
+++ b/src/GCodes/GCodeBuffer/ExpressionParser.h
@@ -13,6 +13,8 @@
#include <ObjectModel/ObjectModel.h>
#include <GCodes/GCodeException.h>
+class VariableSet;
+
class ExpressionParser
{
public:
@@ -41,6 +43,7 @@ private:
ExpressionValue ParseIdentifierExpression(bool evaluate, bool applyLengthOperator) THROWS(GCodeException)
pre(readPointer >= 0; isalpha(gb.buffer[readPointer]));
ExpressionValue ParseQuotedString() THROWS(GCodeException);
+ ExpressionValue GetVariableValue(VariableSet& vars, const char *name, bool parameter) THROWS(GCodeException);
void ConvertToFloat(ExpressionValue& val, bool evaluate) const THROWS(GCodeException);
void ConvertToBool(ExpressionValue& val, bool evaluate) const THROWS(GCodeException);
diff --git a/src/GCodes/GCodeBuffer/GCodeBuffer.cpp b/src/GCodes/GCodeBuffer/GCodeBuffer.cpp
index 21e6c1f2..1feac192 100644
--- a/src/GCodes/GCodeBuffer/GCodeBuffer.cpp
+++ b/src/GCodes/GCodeBuffer/GCodeBuffer.cpp
@@ -1016,4 +1016,14 @@ void GCodeBuffer::SetParameters(int codeRunning) noexcept
PARSER_OPERATION(SetParameters(machineState, codeRunning));
}
+VariableSet& GCodeBuffer::GetVariables() const noexcept
+{
+ GCodeMachineState *mc = machineState;
+ while (mc->localPush && mc->GetPrevious() != nullptr)
+ {
+ mc = mc->GetPrevious();
+ }
+ return mc->variables;
+}
+
// End
diff --git a/src/GCodes/GCodeBuffer/GCodeBuffer.h b/src/GCodes/GCodeBuffer/GCodeBuffer.h
index 25931cc4..65acf25f 100644
--- a/src/GCodes/GCodeBuffer/GCodeBuffer.h
+++ b/src/GCodes/GCodeBuffer/GCodeBuffer.h
@@ -49,7 +49,7 @@ public:
void Init() noexcept; // Set it up to parse another G-code
void Diagnostics(MessageType mtype) noexcept; // Write some debug info
- bool Put(char c) noexcept SPEED_CRITICAL; // Add a character to the end
+ bool Put(char c) noexcept SPEED_CRITICAL; // Add a character to the end
#if HAS_LINUX_INTERFACE
void PutBinary(const uint32_t *data, size_t len) noexcept; // Add an entire binary G-Code, overwriting any existing content
#endif
@@ -224,6 +224,7 @@ public:
bool WasMotionCommanded() const noexcept { return motionCommanded; }
void SetParameters(int codeRunning) noexcept;
+ VariableSet& GetVariables() const noexcept;
Mutex mutex;