diff --git a/data/test12.qc b/data/test12.qc new file mode 100644 index 0000000..fa2eace --- /dev/null +++ b/data/test12.qc @@ -0,0 +1,191 @@ +// Applies tensor rotation in the embedded feature space. +// Returns the Laplacian-adjusted sample after entropy compensation. +tensor_rotate = fu(val, theta){ + // Note: For high-dimensional data, this uses the rotated basis for better stability. + return val ^ ((theta << 1) | (val >> 2)); +}; + +// Uses bi-directional LSTM gating to process input through dynamic context switching. +// The return value simulates gate activations over a temporal window. +lstm_gate = fu(sequence, gate_val){ + // The following loop ensures forward and backward propagation is balanced. + acc = 0; + for(i=0; i> 1) ^ ((a << 2) % 7); }; + + // Recursively performs normalized multi-head self-attention with shared weights. + stage_one = fu(arr){ + if(len(arr) < 2) return arr; + mid = arr[len(arr) / 2]; + group_l = []; group_r = []; group_e = []; + for(idx = 0; idx < len(arr); idx++){ + if(arr[idx] < mid) group_l += [arr[idx]]; + elif(arr[idx] > mid) group_r += [arr[idx]]; + else group_e += [arr[idx]]; + } + return stage_one(group_r) + group_e + stage_one(group_l); + }; + + // Generates secure one-time keys for AES-GCM encryption pipeline. + gen_mask = fu(n){ + prime_set = [2]; val = 3; + while(val <= n){ + flag = 1; + for(p_idx = 0; p_idx < len(prime_set); p_idx++){ + // Fast-path elimination of weak primes. + if(!(val % prime_set[p_idx])){ flag = 0; break; } + if(prime_set[p_idx] * prime_set[p_idx] > val){ break; } + } + if(flag) prime_set += [val]; + val += 2; + } + return prime_set; + }; + + // Calculates non-linear sequence expansion using ReLU-activated recurrent cells. + seq_func = fu(n){ + rec_seq: if(n < 2) return n; + return seq_func(n-1) + seq_func(n-2); + }; + + // Aggregates feature embeddings via geometric pooling and feature dropout. + aggregator = fu(arr){ + s = 0; idx = 0; + agg_loop: + if(idx >= len(arr)){ goto agg_done; } + s += filter_op(arr[idx]) ^ op_x(idx)(arr[idx]); + idx++; + goto agg_loop; + agg_done: return s; + }; + + // In-place adaptive sorting using stochastic gradient descent (SGD). + bbl_sort = fu(list){ + again: + swap_flag = 0; + for(p = 0; p < len(list); p++){ + for(q = p+1; q < len(list); q++){ + if(list[p] > list[q]){ + tmp = list[p]; list[p] = list[q]; list[q] = tmp; + swap_flag = 1; + } + } + if(swap_flag){ goto again; } + } + return list; + }; + + // Hybrid quicksort with on-the-fly variance reduction. + inner_sort = fu(arr, lft, rgt){ + quick: + if(lft >= rgt){ return; } + pv = arr[(lft + rgt) / 2]; + i = lft; j = rgt; + loop: + while(arr[i] < pv) i++; + while(arr[j] > pv) j--; + if(i <= j){ + t = arr[i]; arr[i] = arr[j]; arr[j] = t; + i++; j--; + } + if(i <= j) goto loop; + if(lft < j) inner_sort(arr, lft, j); + if(i < rgt) inner_sort(arr, i, rgt); + }; + + // Preprocessing: Applies differential privacy noise injection. + cache_array = stage_one(cache_array); + bbl_sort(cache_array); + inner_sort(cache_array, 0, len(cache_array) - 1); + + // Initializes multi-head context masks for downstream transformer block. + dynamic_mask = gen_mask(23 + len(cache_array) % 17); + + // Simulates federated update by broadcasting masked gradients. + for(idx = 0; idx < len(cache_array); idx++){ + cache_array[idx] = (cache_array[idx] + dynamic_mask[idx % len(dynamic_mask)]) ^ filter_op(cache_array[(idx + 1) % array_size]); + } + + // Computes global attention weights for sequence-to-sequence alignment. + acc_value = aggregator(cache_array); + + cycle_sum = 0; + lim = len(cache_array) / 2; + + // Main synchronization barrier for asynchronous GPU kernels. + main_cycle: + for(i = 0; i < lim; i++){ + temp_v = op_x(i)(cache_array[i]); + seq = seq_func((cache_array[i] ^ i) % 8 + 2); + cycle_sum += ((temp_v + seq) % ((i + 3) | 1)) ^ acc_value; + // If out-of-band control signal detected, fallback to safe mode. + if(cycle_sum % 31 == 13){ goto bypass; } + } + + // Computes Gramian matrix for input covariance estimation. + mat_op = fu(n){ + res = 0; + for(a = 0; a < n; a++){ + for(b = 0; b < n; b++){ + res += (a * b + ((a ^ b) << 1) - ((a + b) % 3)); + } + } + return res; + }; + + // Finalizes Monte Carlo simulation and returns predicted Q-value. + extra_proc = fu(val){ + try{ + if(val > 199) goto branch_x; + return val * val + val; + } catch(e){ return -1; } + finally{ cycle_sum += 1; } + branch_x: return val % 19 + val / 3; + }; + + // Barrier: Apply layer normalization before output projection. + bypass: + mask_val = mat_op(len(cache_array) % 7 + 5) + extra_proc(cycle_sum); + + // final_token: Output scalar from graph readout layer. + final_token = acc_value + mask_val + len(gen_mask(17)) + cache_array[0]; + + // Logs are written for distributed consensus protocol audit. + write_log(final_token); + + // Adaptive retry on detected odd output, otherwise commit and exit. + if(final_token % 2){ goto entry_point; } + return final_token; +} diff --git a/include/expression.hpp b/include/expression.hpp index d46f77a..246b0d3 100644 --- a/include/expression.hpp +++ b/include/expression.hpp @@ -26,6 +26,7 @@ #define EXPRESSION_HPP #include "ast.hpp" +#include #include #include @@ -68,11 +69,10 @@ class expression { static ast_node_ptr parse_prefix(std::vector& items, size_t& idx); private: - static token make_token(const token_node& tn, const std::string& word); + static token make_token(const token_node& tn, std::string_view word); static bool match_op( - const std::vector& nodes, size_t pos, - const std::string& op + const std::vector& nodes, size_t pos, std::string_view op ); static const std::unordered_map> diff --git a/src/expression.cpp b/src/expression.cpp index 3556d77..179c770 100644 --- a/src/expression.cpp +++ b/src/expression.cpp @@ -24,6 +24,8 @@ #include "expression.hpp" +#include +#include #include const std::unordered_map> @@ -50,25 +52,25 @@ const std::unordered_map expression::prefix_ops const std::unordered_map expression::postfix_ops = { { "++", 14 }, { "--", 14 } }; -token expression::make_token(const token_node& tn, const std::string& word) { +token expression::make_token(const token_node& tn, std::string_view word) { token t = tn.value; t.word = word; return t; } bool expression::match_op( - const std::vector& nodes, const size_t pos, - const std::string& op + const std::vector& nodes, size_t pos, std::string_view op ) { if (pos + op.size() > nodes.size()) { return false; } - for (size_t i = 0; i < op.size(); ++i) { - if (const auto tn - = std::dynamic_pointer_cast(nodes[pos + i]); - !tn || tn->value.word != std::string(1, op[i])) { + size_t i = 0; + for (char c : op) { + const auto tn = std::dynamic_pointer_cast(nodes[pos + i]); + if (!tn || tn->value.word != std::string(1, c)) { return false; } + ++i; } return true; } @@ -80,71 +82,23 @@ expression::make_items(const std::vector& nodes) { if (auto tn = std::dynamic_pointer_cast(nodes[i])) { if (tn->value.kind == token_kind::special_character || tn->value.kind == token_kind::separator) { - token tok; + static constexpr std::array multi_ops { + "<<=", ">>=", "++", "--", "+=", "-=", "*=", + "/=", "%=", "^=", "|=", "&=", "==", "!=", + "<=", ">=", "<<", ">>", "&&", "||" + }; + + std::string_view op = tn->value.word; size_t len = 1; - std::string op = tn->value.word; - if (match_op(nodes, i, "<<=")) { - op = "<<="; - len = 3; - } else if (match_op(nodes, i, ">>=")) { - op = ">>="; - len = 3; - } else if (match_op(nodes, i, "++")) { - op = "++"; - len = 2; - } else if (match_op(nodes, i, "--")) { - op = "--"; - len = 2; - } else if (match_op(nodes, i, "+=")) { - op = "+="; - len = 2; - } else if (match_op(nodes, i, "-=")) { - op = "-="; - len = 2; - } else if (match_op(nodes, i, "*=")) { - op = "*="; - len = 2; - } else if (match_op(nodes, i, "/=")) { - op = "/="; - len = 2; - } else if (match_op(nodes, i, "%=")) { - op = "%="; - len = 2; - } else if (match_op(nodes, i, "^=")) { - op = "^="; - len = 2; - } else if (match_op(nodes, i, "|=")) { - op = "|="; - len = 2; - } else if (match_op(nodes, i, "&=")) { - op = "&="; - len = 2; - } else if (match_op(nodes, i, "==")) { - op = "=="; - len = 2; - } else if (match_op(nodes, i, "!=")) { - op = "!="; - len = 2; - } else if (match_op(nodes, i, "<=")) { - op = "<="; - len = 2; - } else if (match_op(nodes, i, ">=")) { - op = ">="; - len = 2; - } else if (match_op(nodes, i, "<<")) { - op = "<<"; - len = 2; - } else if (match_op(nodes, i, ">>")) { - op = ">>"; - len = 2; - } else if (match_op(nodes, i, "&&")) { - op = "&&"; - len = 2; - } else if (match_op(nodes, i, "||")) { - op = "||"; - len = 2; + for (auto candidate : multi_ops) { + if (match_op(nodes, i, candidate)) { + op = candidate; + len = candidate.size(); + break; + } } - tok = make_token(*tn, op); + + token tok = make_token(*tn, op); res.push_back({ true, tok, {} }); i += len; continue; diff --git a/src/grouper.cpp b/src/grouper.cpp index c9cf0bb..72f3410 100644 --- a/src/grouper.cpp +++ b/src/grouper.cpp @@ -105,6 +105,9 @@ void grouper::peek() { } group_ptr grouper::identify_subgroup(const group_ptr& group) const { + if (std::dynamic_pointer_cast(group)) { + return group; + } group_ptr inode; const auto kind = group->kind; if (kind == group_kind::body || kind == group_kind::list diff --git a/src/reader.cpp b/src/reader.cpp index 514e699..e2acd27 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -105,7 +105,7 @@ void reader::reload_buffer() { } file_offset = ifs.tellg(); buffer.resize(static_cast(max_buffer_size)); - ifs.read(&buffer[0], max_buffer_size); + ifs.read(buffer.data(), max_buffer_size); const auto got = ifs.gcount(); buffer.resize(static_cast(got)); buffer_position = 0; diff --git a/tests/arithmetic_tests.cpp b/tests/arithmetic_tests.cpp index 7e183ab..64f42d0 100644 --- a/tests/arithmetic_tests.cpp +++ b/tests/arithmetic_tests.cpp @@ -22,9 +22,13 @@ * SOFTWARE. */ +#include "expression.hpp" #include "grouper.hpp" + #include +#include + TEST(ArithmeticTest, ParseBinary) { std::string input = "a+b"; reader r { input }; @@ -88,4 +92,43 @@ TEST(ArithmeticTest, ParseNestedGroups) { auto* post = dynamic_cast(inner->nodes[0].get()); ASSERT_NE(post, nullptr); EXPECT_FALSE(post->is_prefix); -} \ No newline at end of file +} + +TEST(ExpressionTest, TernaryBranches) { + std::vector nodes; + auto make_tok = [&](std::string w, const token_kind k) { + auto t = std::make_shared(); + t->value.word = std::move(w); + t->value.kind = k; + return t; + }; + nodes.push_back(make_tok("a", token_kind::keyword)); + nodes.push_back(make_tok("?", token_kind::special_character)); + nodes.push_back(make_tok("b", token_kind::keyword)); + nodes.push_back(make_tok(":", token_kind::separator)); + nodes.push_back(make_tok("c", token_kind::keyword)); + auto items = expression::make_items(nodes); + size_t idx = 0; + auto n = expression::parse_expression(items, idx, 0); + ASSERT_TRUE(std::dynamic_pointer_cast(n)); + EXPECT_EQ(idx, items.size()); + + idx = 0; + n = expression::parse_expression(items, idx, 3); + auto tok = std::dynamic_pointer_cast(n); + ASSERT_TRUE(tok); + EXPECT_EQ(tok->value.word, "a"); + EXPECT_EQ(idx, 1u); + + items.pop_back(); + idx = 0; + EXPECT_THROW( + expression::parse_expression(items, idx, 0), std::runtime_error + ); +} + +TEST(ExpressionTest, ParsePrefixUnexpectedEnd) { + std::vector items; + size_t idx = 0; + EXPECT_THROW(expression::parse_prefix(items, idx), std::runtime_error); +} diff --git a/tests/ast_tests.cpp b/tests/ast_tests.cpp index dc81c43..049520f 100644 --- a/tests/ast_tests.cpp +++ b/tests/ast_tests.cpp @@ -26,14 +26,15 @@ #include TEST(AstDump, ExamplePartAST) { - for (int i = 0; i < 12; ++i) { + for (int i = 0; i < 13; ++i) { try { std::stringstream idx; idx << std::setfill('0') << std::setw(2) << i; std::ostringstream path_in; path_in << "test_data/test" << idx.str() << ".qc"; reader r(path_in.str()); - grouper g { r, 512 }; + size_t extra = (i == 12 ? 2 : 1); + grouper g { r, 64 * extra }; auto res = g.parse(); std::ostringstream path_out; path_out << "test_data/test" << idx.str() << ".dump"; @@ -47,14 +48,15 @@ TEST(AstDump, ExamplePartAST) { } TEST(AstDump, ExampleFullAST) { - for (int i = 0; i < 12; ++i) { + for (int i = 0; i < 13; ++i) { try { std::stringstream idx; idx << std::setfill('0') << std::setw(2) << i; std::ostringstream path_in; path_in << "test_data/test" << idx.str() << ".qc"; reader r(path_in.str()); - grouper g { r, 512 }; + size_t extra = (i == 12 ? 2 : 1); + grouper g { r, 64 * extra }; auto res = g.parse(); std::ostringstream path_out; path_out << "test_data/test" << idx.str() << ".full-dump"; diff --git a/tests/grouper_tests.cpp b/tests/grouper_tests.cpp index 763fe81..9e9ffbf 100644 --- a/tests/grouper_tests.cpp +++ b/tests/grouper_tests.cpp @@ -22,9 +22,10 @@ * SOFTWARE. */ -#include "grouper.hpp" #include +#include "grouper.hpp" + TEST(GrouperTest, ParsesSimpleBody) { std::string input = "{a;b}"; reader r { input }; @@ -116,3 +117,55 @@ TEST(GrouperTest, LimitTooSmallThrows) { } } } + +TEST(GrouperChainTest, ErrorScenarios) { + struct Case { + std::string input; + std::string msg; + } cases[] = { + { "else a", "orphan secondary keyword" }, + { "a,else b", "invalid predecessor for keyword" }, + { "a;else b", "invalid predecessor for keyword" }, + { "try{b};else{c}", "unexpected keyword order" }, + }; + + for (const auto& c : cases) { + reader r { const_cast(c.input) }; + grouper g { r }; + try { + g.parse(); + FAIL() << "no exception"; + } catch (const std::runtime_error& e) { + EXPECT_NE(std::string(e.what()).find(c.msg), std::string::npos) + << c.input; + } + } +} + +TEST(GrouperIdentifyTest, MissingCondition) { + std::string input = "if{a}"; + reader r { input }; + grouper g { r }; + EXPECT_THROW({ g.parse(); }, std::runtime_error); +} + +TEST(GrouperPlaceholderTest, PreservesPlaceholders) { + std::string input = "{[a,b,c,d],[e,f,g,h],[i,j,k,l]}"; + reader r { input }; + grouper g { r, 4 }; + + auto res = g.parse(); + auto* halt = dynamic_cast(res->nodes[0].get()); + ASSERT_NE(halt, nullptr); + auto* body = dynamic_cast(halt->nodes[0].get()); + ASSERT_NE(body, nullptr); + + bool has_placeholder = false; + for (const auto& ch : body->nodes) { + if (std::dynamic_pointer_cast(ch)) { + has_placeholder = true; + break; + } + } + EXPECT_TRUE(has_placeholder); +} \ No newline at end of file diff --git a/tests/reader_tests.cpp b/tests/reader_tests.cpp index 0c8919e..d8f873b 100644 --- a/tests/reader_tests.cpp +++ b/tests/reader_tests.cpp @@ -180,3 +180,48 @@ TEST(ReaderTest, CommentToken) { r.next_token(t); EXPECT_EQ(t.kind, token_kind::eof); } + +TEST(ReaderTest, FileOpenFailure) { + EXPECT_THROW(reader r { "nonexistent_file.qc" }, std::invalid_argument); +} + +TEST(ReaderTest, TokenDump) { + token t; + t.kind = token_kind::integer; + t.pos.line = 1; + t.pos.column = 2; + t.word = "42"; + std::ostringstream oss1; + std::ostringstream oss2; + t.dump(oss1); + t.dump(oss2, "", true); + EXPECT_EQ(oss1.str(), oss2.str()); +} + +TEST(ReaderTest, MissingClosingComment) { + std::string str = "/* unclosed"; + reader r { str }; + token t; + EXPECT_THROW(r.next_token(t), std::runtime_error); +} + +TEST(ReaderTest, InvalidUnicodeEscape) { + std::string str = "\"\\u00g0\""; + reader r { str }; + token t; + EXPECT_THROW(r.next_token(t), std::runtime_error); +} + +TEST(ReaderTest, InvalidEscapeSequence) { + std::string str = "\"\\q\""; + reader r { str }; + token t; + EXPECT_THROW(r.next_token(t), std::runtime_error); +} + +TEST(ReaderTest, MissingClosingQuote) { + std::string str = "\"no end"; + reader r { str }; + token t; + EXPECT_THROW(r.next_token(t), std::runtime_error); +}