diff --git a/v4/ctx.py b/v4/ctx.py index 5ca624d..af8f36e 100644 --- a/v4/ctx.py +++ b/v4/ctx.py @@ -32,10 +32,7 @@ class ctx_Controller: self.callbacks.append(cb) def registerFieldCallback(self, fieldName, cb): - def execCB(c): - if c.recentField == fieldName: - cb(c) - self.callbacks.append(execCB) + self.callbacks.append(lambda c: cb(c) if c.recentField == fieldName else None) def registerFunction(self, f): self.functions.append(f) diff --git a/v4/ctx_test.cpp b/v4/ctx_test.cpp new file mode 100644 index 0000000..fc38a80 --- /dev/null +++ b/v4/ctx_test.cpp @@ -0,0 +1,86 @@ + +#include +#include +#include +#include "ctx.h" +#include "llm.h" +#include "memory_Context.h" +#include "main.h" + +std::string ctx_test_Controller_executeFunctions_set( +) { + auto c = memory_createContext(); + auto ctrl = ctx_Controller(c); + // Disable automatic invocation of executeFunctions. + ctrl.isProcessingQueue = true; + ctrl.set("input", "123"); + ctrl.registerFunction(__processInput); + // Apply 'input'. + ctrl.executeFunctions(); + // Apply 'outputHelp'. + ctrl.executeFunctions(); + if ( + c.input == "123" && + c.outputHelp == "Checked" + ) { + return "OK: ctx_Controller_executeFunctions_set"; + } + return "ERR: ctx_Controller_executeFunctions_set"; +} + +std::string ctx_test_Controller_processQueue( +) { + auto c = memory_createContext(); + auto ctrl = ctx_Controller(c); + ctrl.registerFunction(__processInput); + ctrl.set("input", "abc");; + if ( + c.input == "abc" && + c.outputHelp == "Checked" + ) { + return "OK: ctx_Controller_processQueue"; + } + return "ERR: ctx_Controller_processQueue"; +} + +std::string ctx_test_memoryContext_field( +) { + auto c = memory_createContext(); + c.input = "abc"; + if ( + c.field("input") == "abc" + ) { + return "OK: ctx_memoryContext_field"; + } + return "ERR: ctx_memoryContext_field"; +} + +std::string ctx_test_memoryContext_setField( +) { + auto c = memory_createContext(); + c.input = "abc"; + c.setField("input", "123"); + if ( + c.field("input") == "123" + ) { + return "OK: ctx_memoryContext_setField"; + } + return "ERR: ctx_memoryContext_setField"; +} + +// Auxiliary functions. + +memory_Context __processInput( + memory_Context c +) { + if ( + c.recentField == "input" + ) { + c.outputHelp = "Checked"; + c.recentField = "outputHelp"; + return c; + } + c.recentField = "none"; + return c; +} + diff --git a/v4/ctx_test.py b/v4/ctx_test.py index 2179714..f20836d 100644 --- a/v4/ctx_test.py +++ b/v4/ctx_test.py @@ -66,7 +66,9 @@ def ctx_test_memoryContext_setField( # Auxiliary functions. @llm_by_value -def __processInput(c): +def __processInput( + c: memory_Context +) -> memory_Context: if ( c.recentField == "input" ): diff --git a/v4/ctx_test2.py b/v4/ctx_test2.py new file mode 100644 index 0000000..1a2bc83 --- /dev/null +++ b/v4/ctx_test2.py @@ -0,0 +1,142 @@ +from ctx import * +from memory import * +from memory_Context import * + +def ctx_test_Controller_executeFunctions_set( +) -> str: + c = memory_createContext() + ctrl = ctx_Controller(c) + # Disable automatic invocation of executeFunctions. + ctrl.isProcessingQueue = True + ctrl.set("input", "123") + + @llm_by_value + def processInput(c): + if ( + c.recentField == "input" + ): + c.outputHelp = "Checked" + c.recentField = "outputHelp" + return c + #} + c.recentField = "none" + return c + #} + + ctrl.registerFunction(processInput) + # Apply 'input'. + ctrl.executeFunctions() + # Apply 'outputHelp'. + ctrl.executeFunctions() + if ( + c.input == "123" and + c.outputHelp == "Checked" + ): + return "OK: ctx_Controller_executeFunctions_set" + #} + return "ERR: ctx_Controller_executeFunctions_set" +#} + +def ctx_test_Controller_processQueue( +) -> str: + c = memory_createContext() + ctrl = ctx_Controller(c) + + @llm_by_value + def processInput(c): + if ( + c.recentField == "input" + ): + c.outputHelp = "Checked" + c.recentField = "outputHelp" + return c + #} + c.recentField = "none" + return c + #} + + ctrl.registerFunction(processInput) + ctrl.set("input", "abc"); + if ( + c.input == "abc" and + c.outputHelp == "Checked" + ): + return "OK: ctx_Controller_processQueue" + #} + return "ERR: ctx_Controller_processQueue" +#} + +def ctx_test_Controller_registerFieldCallback_match( +) -> str: + c = memory_createContext() + ctrl = ctx_Controller(c) + c.input = "123" + c.recentField = "input" + + globals()["callbackInput"] = "" + def setCallbackInput(c): + if ( + c.recentField == "input" + ): + globals()["callbackInput"] = c.input + + ctrl.registerFieldCallback("input", setCallbackInput) + ctrl.reportContext() + if ( + c.input == globals()["callbackInput"] + ): + return "OK: ctx_Controller_registerFieldCallback_match" + #} + return f"ERR: ctx_Controller_registerFieldCallback_match" +#} + +def ctx_test_Controller_registerFieldCallback_mismatch( +) -> str: + c = memory_createContext() + ctrl = ctx_Controller(c) + c.input = "123" + c.outputHelp = "you" + # A field other than 'input' is marked recent. + c.recentField = "outputHelp" + + globals()["callbackInput"] = "" + def setCallbackInput(c): + if ( + c.recentField == "input" + ): + globals()["callbackInput"] = c.input + + ctrl.registerFieldCallback("input", setCallbackInput) + ctrl.reportContext() + if ( + globals()["callbackInput"] == "" + ): + return "OK: ctx_Controller_registerFieldCallback_mismatch" + #} + return f"ERR: ctx_Controller_registerFieldCallback_mismatch" +#} + +def ctx_test_memoryContext_field( +) -> str: + c = memory_createContext() + c.input = "abc" + if ( + c.field("input") == "abc" + ): + return "OK: ctx_memoryContext_field" + #} + return "ERR: ctx_memoryContext_field" +#} + +def ctx_test_memoryContext_setField( +) -> str: + c = memory_createContext() + c.input = "abc" + c.setField("input", "123") + if ( + c.input == "123" + ): + return "OK: ctx_memoryContext_setField" + #} + return "ERR: ctx_memoryContext_setField" +#} diff --git a/v4/cxx-headers b/v4/cxx-headers index eb8d0fc..7a69094 100755 --- a/v4/cxx-headers +++ b/v4/cxx-headers @@ -7,6 +7,7 @@ grep '^memory_Context ' memory.cpp > $TMP grep '^std::string ' memory_test.cpp >> $TMP grep '^memory_Context ' cli.cpp >> $TMP grep '^std::string ' cli_test.cpp >> $TMP +grep '^std::string ' ctx_test.cpp >> $TMP grep '^std::string ' llm_test.cpp >> $TMP IN=$TMP diff --git a/v4/gen-C++ b/v4/gen-C++ index 4f3d614..23c8529 100755 --- a/v4/gen-C++ +++ b/v4/gen-C++ @@ -6,4 +6,5 @@ c++ -o test_memory_C++ -std=c++11 -I. \ llm_test.cpp \ cli.cpp \ cli_test.cpp \ + ctx_test2.cpp \ main.cpp diff --git a/v4/main.h b/v4/main.h index 3914930..3fd1d58 100644 --- a/v4/main.h +++ b/v4/main.h @@ -40,6 +40,10 @@ std::string cli_test_selectItem(); std::string cli_test_showHelp_h(); std::string cli_test_showHelp_help(); std::string cli_test_reportVictory(); +std::string ctx_test_Controller_executeFunctions_set(); +std::string ctx_test_Controller_processQueue(); +std::string ctx_test_memoryContext_field(); +std::string ctx_test_memoryContext_setField(); std::string llm_test_isDigit_digit(); std::string llm_test_isDigit_notDigit(); std::string llm_test_strToInt(); diff --git a/v4/main.py b/v4/main.py index d6fbc4f..4409ca9 100644 --- a/v4/main.py +++ b/v4/main.py @@ -1,8 +1,9 @@ from cli import * from cli_test import * from ctx import * -from ctx_test import * -from ctx_test_Python import * +from ctx_test2 import * +#from ctx_test import * +#from ctx_test_Python import * from llm_test import * from llm_test_Python import * from memory_test import * @@ -10,8 +11,8 @@ import sys print(ctx_test_Controller_executeFunctions_set()) print(ctx_test_Controller_processQueue()) -print(ctx_test_Python_Controller_registerFieldCallback_match()) -print(ctx_test_Python_Controller_registerFieldCallback_mismatch()) +print(ctx_test_Controller_registerFieldCallback_match()) +print(ctx_test_Controller_registerFieldCallback_mismatch()) print(ctx_test_memoryContext_field()) print(ctx_test_memoryContext_setField()) diff --git a/v4/tPythonC++/CPP.py b/v4/tPythonC++/CPP.py index 364f7cd..944c56e 100644 --- a/v4/tPythonC++/CPP.py +++ b/v4/tPythonC++/CPP.py @@ -55,6 +55,7 @@ def translateStatement(s, state): posColon = ss.find(": ") posComma = ss.find(", ") posComment = ss.find("# ") + posCtrl = ss.find("ctrl.") posCtx = ss.find("c.") posEqual = ss.find(" = ") posFor = ss.find("for ") @@ -97,6 +98,7 @@ def translateStatement(s, state): # name = value -> auto name = value if ( posCtx == -1 and + posCtrl == -1 and posColon == -1 and posOpenSquareBracket == -1 and posEqual >= 0 diff --git a/v4/translate-py-cxx b/v4/translate-py-cxx index 2b29e66..fa010a3 100755 --- a/v4/translate-py-cxx +++ b/v4/translate-py-cxx @@ -7,5 +7,6 @@ $TR $DIR/memory.py > $DIR/memory.cpp $TR $DIR/memory_test.py > $DIR/memory_test.cpp $TR $DIR/cli.py > $DIR/cli.cpp $TR $DIR/cli_test.py > $DIR/cli_test.cpp +$TR $DIR/ctx_test.py > $DIR/ctx_test.cpp $TR $DIR/llm_test.py > $DIR/llm_test.cpp $DIR/cxx-headers