Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

prolog: move context implementation to separate files

  • Loading branch information...
commit ee25662c52febd4fb70372249ba4c95b773c12c5 1 parent 3950cac
@iley authored
View
2  prolog/Makefile
@@ -1,7 +1,7 @@
.PHONY: library
TARGETLIBNAME = libiprolog.a
-LIBSOURCES = plgutils.cpp plgexceptions.cpp plgdata.cpp plgengine.cpp plgpackage.cpp
+LIBSOURCES = plgutils.cpp plgexceptions.cpp plgdata.cpp plgengine.cpp plgpackage.cpp plgcontext.cpp
$(TARGETDIRFP)/$(TARGETLIBNAME): library
cd library && $(MAKE)
View
40 prolog/plgcontext.cpp
@@ -0,0 +1,40 @@
+#include "plgcontext.hpp"
+
+PlgContext::PlgContext() : values(), top(0)
+{
+ frameVars[0] = *(GetEmptyList());
+}
+
+void PlgContext::Set(const PlgReference &var, const PlgReference &value)
+{
+ values->AddItem(var, value);
+ frameVars[top] = var ^ frameVars[top];
+}
+
+PlgReference PlgContext::Get(const PlgReference &var) const
+{
+ return values->FindItem(var, PlgUnbound);
+}
+
+void PlgContext::ReturnTo(int pos, bool merge)
+{
+ INTELIB_ASSERT(pos >= 0, IntelibX_bug());
+ if (!merge) {
+ // destroy bindings which will be invalid after return
+ for (int i = top; i > pos; --i) {
+ for (SReference p = frameVars[i]; !p.IsEmptyList(); p = p.Cdr()) {
+ PlgReference var = p.Car();
+ values->RemoveItem(var);
+ }
+ }
+ }
+
+ top = pos;
+}
+
+int PlgContext::NextFrame()
+{
+ top++;
+ frameVars[top] = *(GetEmptyList());
+ return top - 1;
+}
View
31 prolog/plgcontext.hpp
@@ -0,0 +1,31 @@
+#ifndef INTELIB_PROLOG_PLGCONTEXT_HPP_SENTRY
+#define INTELIB_PROLOG_PLGCONTEXT_HPP_SENTRY
+
+#include "../sexpress/svector.hpp"
+#include "../sexpress/shashtbl.hpp"
+
+#include "plgdata.hpp"
+
+class PlgContext
+{
+public:
+ PlgContext();
+
+ void Set(const PlgReference &var, const PlgReference &value);
+ PlgReference Get(const PlgReference &var) const;
+
+ void ReturnTo(int pos, bool merge = false);
+ int NextFrame();
+
+ int Top() const { return top; }
+ const SHashTable &ValueTable() const { return values; }
+
+private:
+ SHashTable values;
+ SVector frameVars;
+ int top;
+
+ PlgContext(const PlgContext&);
+};
+
+#endif
View
18 prolog/plgengine.cpp
@@ -49,24 +49,6 @@ class PlgSentenceMark : public PlgSentenceMark_Super
PlgSentenceMark(const SReference &sref) : PlgSentenceMark_Super(sref) {}
};
-// Context
-
-void PlgContext::ReturnTo(int pos, bool merge)
-{
- INTELIB_ASSERT(pos >= 0, IntelibX_bug());
- if (!merge) {
- // destroy bindings which will be invalid after return
- for (int i = top; i > pos; --i) {
- for (SReference p = frameVars[i]; !p.IsEmptyList(); p = p.Cdr()) {
- PlgReference var = p.Car();
- values->RemoveItem(var);
- }
- }
- }
-
- top = pos;
-}
-
// Continuation
IntelibTypeId PlgExpressionContinuation::TypeId(&SExpression::TypeId, true);
View
44 prolog/plgengine.hpp
@@ -8,54 +8,12 @@
#include "../sexpress/shashtbl.hpp"
#include "plgdata.hpp"
+#include "plgcontext.hpp"
void Assert(const PlgReference &clause);
void AssertA(const PlgReference &clause);
void AssertWithoutExpansion(const PlgReference &clause);
-class PlgContext
-{
-public:
- PlgContext() : values(), top(0)
- {
- frameVars[0] = *(GetEmptyList());
- }
-
- void Set(const PlgReference &var, const PlgReference &value)
- {
- values->AddItem(var, value);
- frameVars[top] = var ^ frameVars[top];
- }
-
- PlgReference Get(const PlgReference &var) const
- {
- return values->FindItem(var, PlgUnbound);
- }
-
- void ReturnTo(int pos, bool merge = false);
-
- int Top() const { return top; }
-
- int NextFrame()
- {
- top++;
- frameVars[top] = *(GetEmptyList());
- return top - 1;
- }
-
- const SHashTable &ValueTable() const
- {
- return values;
- }
-
-private:
- SHashTable values;
- SVector frameVars;
- int top;
-
- PlgContext(const PlgContext&);
-};
-
class PlgExpressionContinuation : public SExpression
{
friend class PlgExpressionChoicePoint;
Please sign in to comment.
Something went wrong with that request. Please try again.