Skip to content

Commit 880c3a2

Browse files
authored
Merge pull request #460 from GaneshPatil7517/fix/cpp-literal-parser
Fix C++ literal parser to match Python concore wire format
2 parents c1240d0 + 3708241 commit 880c3a2

5 files changed

Lines changed: 576 additions & 20 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ The CONTROL-CORE framework consists of the below projects.
2121

2222
_concore_ enables composing studies from programs developed in different languages. Currently supported languages are, Python, Matlab/Octave, Verilog, and C++. The studies are designed through the visual _concore_ Editor (DHGWorkflow) and interpreted into _concore_ through its parser. Neural control systems consist of loops (dicycles). Therefore, they cannot be represented by classic workflow standards (such as CWL or WDL). Therefore, _concore_ addresses a significant research gap to model closed-loop neuromodulation control systems. The _concore_ protocol shares data between the programs through file sharing, with no centralized entity (a broker or an orchestrator) to arbitrate communications between the programs. (In the distributed executions, the CONTROL-CORE Mediator enables connecting the disjoint pieces of the study through REST APIs).
2323

24+
## Wire Format
25+
26+
Concore payloads follow Python literal syntax compatible with `ast.literal_eval()`. The Python, C++, and Java implementations parse this shared format; the MATLAB and Verilog implementations currently support only flat numeric arrays derived from it. Supported value types include:
27+
28+
* **Numbers** — integers and floats, including scientific notation (e.g., `1e3`, `-2.5`)
29+
* **Booleans**`True` / `False` (converted to `1.0` / `0.0` in numeric contexts)
30+
* **Strings** — single- or double-quoted (e.g., `"start"`, `'label'`)
31+
* **Nested arrays**`[1, [2, 3]]`
32+
* **Tuples**`(1.0, 2.0)` (treated identically to arrays)
33+
2434

2535
# Installation and Getting Started Guide
2636

TestLiteralEvalCpp.cpp

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
/**
2+
* TestLiteralEvalCpp.cpp
3+
*
4+
* Test suite for the C++ Python-literal-compatible parser in concore_base.hpp.
5+
* Validates Issue #389 fix: C++ parser must accept all valid concore payloads
6+
* that Python's ast.literal_eval() accepts.
7+
*
8+
* Compile: g++ -std=c++11 -o TestLiteralEvalCpp TestLiteralEvalCpp.cpp
9+
* Run: ./TestLiteralEvalCpp (Linux/macOS)
10+
* TestLiteralEvalCpp.exe (Windows)
11+
*/
12+
13+
#include <iostream>
14+
#include <string>
15+
#include <vector>
16+
#include <cmath>
17+
#include <cstdlib>
18+
#include <stdexcept>
19+
20+
#include "concore_base.hpp"
21+
22+
using namespace concore_base;
23+
24+
static int passed = 0;
25+
static int failed = 0;
26+
27+
// ------------- helpers -------------------------------------------------
28+
29+
static void check(const std::string& testName, bool condition) {
30+
if (condition) {
31+
std::cout << "PASS: " << testName << std::endl;
32+
++passed;
33+
} else {
34+
std::cout << "FAIL: " << testName << std::endl;
35+
++failed;
36+
}
37+
}
38+
39+
static bool approx(double a, double b, double eps = 1e-9) {
40+
return std::fabs(a - b) < eps;
41+
}
42+
43+
// ------------- backward-compatibility tests ----------------------------
44+
45+
static void test_flat_numeric_list() {
46+
std::vector<double> v = parselist_double("[10.0, 0.5, 2.3]");
47+
check("flat_numeric size==3", v.size() == 3);
48+
check("flat_numeric[0]==10.0", approx(v[0], 10.0));
49+
check("flat_numeric[1]==0.5", approx(v[1], 0.5));
50+
check("flat_numeric[2]==2.3", approx(v[2], 2.3));
51+
}
52+
53+
static void test_empty_list() {
54+
std::vector<double> v = parselist_double("[]");
55+
check("empty_list size==0", v.size() == 0);
56+
}
57+
58+
static void test_single_element() {
59+
std::vector<double> v = parselist_double("[42.0]");
60+
check("single_element size==1", v.size() == 1);
61+
check("single_element[0]==42", approx(v[0], 42.0));
62+
}
63+
64+
static void test_negative_numbers() {
65+
std::vector<double> v = parselist_double("[-1.5, -3.0, 2.0]");
66+
check("negative size==3", v.size() == 3);
67+
check("negative[0]==-1.5", approx(v[0], -1.5));
68+
check("negative[1]==-3.0", approx(v[1], -3.0));
69+
}
70+
71+
static void test_scientific_notation() {
72+
std::vector<double> v = parselist_double("[1e3, 2.5E-2, -1.0e+1]");
73+
check("sci size==3", v.size() == 3);
74+
check("sci[0]==1000", approx(v[0], 1000.0));
75+
check("sci[1]==0.025", approx(v[1], 0.025));
76+
check("sci[2]==-10", approx(v[2], -10.0));
77+
}
78+
79+
static void test_integer_values() {
80+
std::vector<double> v = parselist_double("[1, 2, 3]");
81+
check("int size==3", v.size() == 3);
82+
check("int[0]==1", approx(v[0], 1.0));
83+
check("int[2]==3", approx(v[2], 3.0));
84+
}
85+
86+
// ------------- mixed-type payload tests (Issue #389 core) --------------
87+
88+
static void test_string_element() {
89+
// [10.0, "start", 0.5] – string should be skipped in numeric flatten
90+
std::vector<double> v = parselist_double("[10.0, \"start\", 0.5]");
91+
check("string_elem size==2", v.size() == 2);
92+
check("string_elem[0]==10.0", approx(v[0], 10.0));
93+
check("string_elem[1]==0.5", approx(v[1], 0.5));
94+
}
95+
96+
static void test_boolean_element() {
97+
// [10.0, True, 0.5]
98+
std::vector<double> v = parselist_double("[10.0, True, 0.5]");
99+
check("bool_elem size==3", v.size() == 3);
100+
check("bool_elem[0]==10.0", approx(v[0], 10.0));
101+
check("bool_elem[1]==1.0 (True)", approx(v[1], 1.0));
102+
check("bool_elem[2]==0.5", approx(v[2], 0.5));
103+
}
104+
105+
static void test_bool_false() {
106+
std::vector<double> v = parselist_double("[False, 5.0]");
107+
check("bool_false size==2", v.size() == 2);
108+
check("bool_false[0]==0.0", approx(v[0], 0.0));
109+
}
110+
111+
static void test_nested_list() {
112+
// [10.0, [0.5, 0.3], 0.1] – nested list flattened to [10.0, 0.5, 0.3, 0.1]
113+
std::vector<double> v = parselist_double("[10.0, [0.5, 0.3], 0.1]");
114+
check("nested size==4", v.size() == 4);
115+
check("nested[0]==10.0", approx(v[0], 10.0));
116+
check("nested[1]==0.5", approx(v[1], 0.5));
117+
check("nested[2]==0.3", approx(v[2], 0.3));
118+
check("nested[3]==0.1", approx(v[3], 0.1));
119+
}
120+
121+
static void test_tuple_payload() {
122+
// (10.0, 0.3) – tuple treated as array
123+
std::vector<double> v = parselist_double("(10.0, 0.3)");
124+
check("tuple size==2", v.size() == 2);
125+
check("tuple[0]==10.0", approx(v[0], 10.0));
126+
check("tuple[1]==0.3", approx(v[1], 0.3));
127+
}
128+
129+
static void test_nested_tuple() {
130+
// [10.0, (0.5, 0.3)]
131+
std::vector<double> v = parselist_double("[10.0, (0.5, 0.3)]");
132+
check("nested_tuple size==3", v.size() == 3);
133+
check("nested_tuple[0]==10.0", approx(v[0], 10.0));
134+
check("nested_tuple[1]==0.5", approx(v[1], 0.5));
135+
check("nested_tuple[2]==0.3", approx(v[2], 0.3));
136+
}
137+
138+
static void test_mixed_types() {
139+
// [10.0, "label", True, [1, 2], (3,), False, "end"]
140+
std::vector<double> v = parselist_double("[10.0, \"label\", True, [1, 2], (3,), False, \"end\"]");
141+
// numeric values: 10.0, 1.0(True), 1, 2, 3, 0.0(False) = 6 values
142+
check("mixed size==6", v.size() == 6);
143+
check("mixed[0]==10.0", approx(v[0], 10.0));
144+
check("mixed[1]==1.0", approx(v[1], 1.0)); // True
145+
check("mixed[2]==1.0", approx(v[2], 1.0)); // nested [1,...]
146+
check("mixed[3]==2.0", approx(v[3], 2.0)); // nested [...,2]
147+
check("mixed[4]==3.0", approx(v[4], 3.0)); // tuple (3,)
148+
check("mixed[5]==0.0", approx(v[5], 0.0)); // False
149+
}
150+
151+
// ------------- full ConcoreValue parse tests ---------------------------
152+
153+
static void test_parse_literal_string() {
154+
ConcoreValue v = parse_literal("[10.0, \"start\", 0.5]");
155+
check("literal_string is ARRAY", v.type == ConcoreValueType::ARRAY);
156+
check("literal_string len==3", v.array.size() == 3);
157+
check("literal_string[0] NUMBER", v.array[0].type == ConcoreValueType::NUMBER);
158+
check("literal_string[1] STRING", v.array[1].type == ConcoreValueType::STRING);
159+
check("literal_string[1]==\"start\"", v.array[1].str == "start");
160+
check("literal_string[2] NUMBER", v.array[2].type == ConcoreValueType::NUMBER);
161+
}
162+
163+
static void test_parse_literal_bool() {
164+
ConcoreValue v = parse_literal("[True, False]");
165+
check("literal_bool is ARRAY", v.type == ConcoreValueType::ARRAY);
166+
check("literal_bool[0] BOOL", v.array[0].type == ConcoreValueType::BOOL);
167+
check("literal_bool[0]==true", v.array[0].boolean == true);
168+
check("literal_bool[1]==false", v.array[1].boolean == false);
169+
}
170+
171+
static void test_parse_literal_nested() {
172+
ConcoreValue v = parse_literal("[1, [2, [3]]]");
173+
check("literal_nested outer ARRAY", v.type == ConcoreValueType::ARRAY);
174+
check("literal_nested[1] ARRAY", v.array[1].type == ConcoreValueType::ARRAY);
175+
check("literal_nested[1][1] ARRAY", v.array[1].array[1].type == ConcoreValueType::ARRAY);
176+
check("literal_nested[1][1][0]==3", approx(v.array[1].array[1].array[0].number, 3.0));
177+
}
178+
179+
static void test_parse_single_quoted_string() {
180+
ConcoreValue v = parse_literal("['hello']");
181+
check("single_quote ARRAY", v.type == ConcoreValueType::ARRAY);
182+
check("single_quote[0] STRING", v.array[0].type == ConcoreValueType::STRING);
183+
check("single_quote[0]=='hello'", v.array[0].str == "hello");
184+
}
185+
186+
static void test_parse_escape_sequences() {
187+
ConcoreValue v = parse_literal("[\"line\\none\"]");
188+
check("escape STRING", v.array[0].type == ConcoreValueType::STRING);
189+
check("escape has newline", v.array[0].str == "line\none");
190+
}
191+
192+
static void test_parse_none() {
193+
ConcoreValue v = parse_literal("[None, 1]");
194+
check("none[0] STRING", v.array[0].type == ConcoreValueType::STRING);
195+
check("none[0]==\"None\"", v.array[0].str == "None");
196+
}
197+
198+
static void test_trailing_comma() {
199+
// Python allows trailing comma: [1, 2,]
200+
std::vector<double> v = parselist_double("[1, 2,]");
201+
check("trailing_comma size==2", v.size() == 2);
202+
check("trailing_comma[1]==2", approx(v[1], 2.0));
203+
}
204+
205+
// ------------- error / failure case tests ------------------------------
206+
207+
static void test_malformed_bracket() {
208+
bool caught = false;
209+
try {
210+
parse_literal("[1, 2");
211+
} catch (const std::runtime_error&) {
212+
caught = true;
213+
}
214+
check("malformed_bracket throws", caught);
215+
}
216+
217+
static void test_malformed_string() {
218+
bool caught = false;
219+
try {
220+
parse_literal("[\"unterminated]");
221+
} catch (const std::runtime_error&) {
222+
caught = true;
223+
}
224+
check("malformed_string throws", caught);
225+
}
226+
227+
static void test_unsupported_object() {
228+
bool caught = false;
229+
try {
230+
parse_literal("{1: 2}");
231+
} catch (const std::runtime_error&) {
232+
caught = true;
233+
}
234+
check("unsupported_object throws", caught);
235+
}
236+
237+
static void test_empty_string_input() {
238+
std::vector<double> v = parselist_double("");
239+
check("empty_input size==0", v.size() == 0);
240+
}
241+
242+
// ------------- cross-language round-trip tests -------------------------
243+
244+
static void test_python_write_cpp_read_flat() {
245+
// Simulate Python write: "[5.0, 1.0, 2.0]"
246+
std::vector<double> v = parselist_double("[5.0, 1.0, 2.0]");
247+
check("py2cpp_flat size==3", v.size() == 3);
248+
check("py2cpp_flat[0]==5.0", approx(v[0], 5.0));
249+
}
250+
251+
static void test_python_write_cpp_read_mixed() {
252+
// Simulate Python write: "[5.0, 'sensor_a', True, [0.1, 0.2]]"
253+
std::vector<double> v = parselist_double("[5.0, 'sensor_a', True, [0.1, 0.2]]");
254+
// numeric: 5.0, 1.0(True), 0.1, 0.2 = 4
255+
check("py2cpp_mixed size==4", v.size() == 4);
256+
check("py2cpp_mixed[0]==5.0", approx(v[0], 5.0));
257+
check("py2cpp_mixed[1]==1.0", approx(v[1], 1.0));
258+
check("py2cpp_mixed[2]==0.1", approx(v[2], 0.1));
259+
check("py2cpp_mixed[3]==0.2", approx(v[3], 0.2));
260+
}
261+
262+
// ------------- main ----------------------------------------------------
263+
264+
int main() {
265+
std::cout << "===== C++ Literal Parser Tests (Issue #389) =====\n\n";
266+
267+
// Backward compatibility
268+
test_flat_numeric_list();
269+
test_empty_list();
270+
test_single_element();
271+
test_negative_numbers();
272+
test_scientific_notation();
273+
test_integer_values();
274+
275+
// Mixed-type payloads (core of Issue #389)
276+
test_string_element();
277+
test_boolean_element();
278+
test_bool_false();
279+
test_nested_list();
280+
test_tuple_payload();
281+
test_nested_tuple();
282+
test_mixed_types();
283+
284+
// Full ConcoreValue structure tests
285+
test_parse_literal_string();
286+
test_parse_literal_bool();
287+
test_parse_literal_nested();
288+
test_parse_single_quoted_string();
289+
test_parse_escape_sequences();
290+
test_parse_none();
291+
test_trailing_comma();
292+
293+
// Error / failure cases
294+
test_malformed_bracket();
295+
test_malformed_string();
296+
test_unsupported_object();
297+
test_empty_string_input();
298+
299+
// Cross-language round-trip
300+
test_python_write_cpp_read_flat();
301+
test_python_write_cpp_read_mixed();
302+
303+
std::cout << "\n=== Results: " << passed << " passed, " << failed
304+
<< " failed out of " << (passed + failed) << " tests ===\n";
305+
306+
return (failed > 0) ? 1 : 0;
307+
}

concore.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,24 @@ class Concore{
337337
return concore_base::parselist_double(f);
338338
}
339339

340+
/**
341+
* @brief Parses a literal string into a ConcoreValue representation.
342+
* @param f The input string to parse.
343+
* @return A ConcoreValue obtained by parsing the input string.
344+
*/
345+
concore_base::ConcoreValue parse_literal(string f){
346+
return concore_base::parse_literal(f);
347+
}
348+
349+
/**
350+
* @brief Flattens a ConcoreValue into a vector of numeric (double) values.
351+
* @param v The ConcoreValue to flatten.
352+
* @return A vector of double values obtained by flattening the input.
353+
*/
354+
vector<double> flatten_numeric(const concore_base::ConcoreValue& v){
355+
return concore_base::flatten_numeric(v);
356+
}
357+
340358
/**
341359
* @brief deviate the read to either the SM (Shared Memory) or FM (File Method) communication protocol based on iport and oport.
342360
* @param port The port number.

0 commit comments

Comments
 (0)