From cabb6dac9890972bdaca5d004d2f3aebea31790b Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:48:36 +0000 Subject: [PATCH 01/10] Preserve embedded NUL strings and harden integer opcodes --- src/main.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 813de8b..0aae8f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,7 +35,7 @@ std::string c_quote(const std::string &s) { case '\n': out += "\\n"; break; case '\r': out += "\\r"; break; case '\t': out += "\\t"; break; - case '\0': out += "\\0"; break; + case '\0': out += "\\000"; break; default: if (c < 32 || c >= 127) { const char d[] = "01234567"; @@ -211,14 +211,14 @@ bool opcode(std::ostringstream &o,long long n) { case 31:o<<"push_value(log(pop_value()));\n";break;case 32:o<<"push_value(log10(pop_value()));\n";break; case 33:o<<"push_value(sin(pop_value()));\n";break;case 34:o<<"push_value(cos(pop_value()));\n";break;case 35:o<<"push_value(tan(pop_value()));\n";break; case 36:o<<"push_value(round(pop_value()));\n";break;case 37:o<<"push_value(trunc(pop_value()));\n";break; - case 38:o<<"{int b=(int)pop_value(),a=(int)pop_value();if(!b)fail_runtime(\"integer remainder by zero\");push_value(a%b);}\n";break; + case 38:o<<"{int32_t b=checked_i32(pop_value()),a=checked_i32(pop_value());if(!b)fail_runtime(\"integer remainder by zero\");if(a==INT32_MIN&&b==-1)fail_runtime(\"integer remainder overflow\");push_value(a%b);}\n";break; case 39:o<<"{double b=pop_value(),a=pop_value();push_value(a!=0&&b!=0);}\n";break; - case 40:o<<"{int b=(int)pop_value(),a=(int)pop_value();push_value(a&b);}\n";break; - case 41:o<<"{int b=(int)pop_value(),a=(int)pop_value();push_value(a|b);}\n";break; - case 42:o<<"{int b=(int)pop_value(),a=(int)pop_value();push_value(a^b);}\n";break; - case 43:o<<"push_value(~(int)pop_value());\n";break; - case 44:o<<"{int b=(int)pop_value(),a=(int)pop_value();if(b<0||b>=31)fail_runtime(\"invalid shift\");push_value(a<=31)fail_runtime(\"invalid shift\");push_value(a>>b);}\n";break; + case 40:o<<"{int32_t b=checked_i32(pop_value()),a=checked_i32(pop_value());push_value(bits_to_i32((uint32_t)a&(uint32_t)b));}\n";break; + case 41:o<<"{int32_t b=checked_i32(pop_value()),a=checked_i32(pop_value());push_value(bits_to_i32((uint32_t)a|(uint32_t)b));}\n";break; + case 42:o<<"{int32_t b=checked_i32(pop_value()),a=checked_i32(pop_value());push_value(bits_to_i32((uint32_t)a^(uint32_t)b));}\n";break; + case 43:o<<"push_value(bits_to_i32(~(uint32_t)checked_i32(pop_value())));\n";break; + case 44:o<<"{int32_t b=checked_i32(pop_value()),a=checked_i32(pop_value());if(b<0||b>=32)fail_runtime(\"invalid shift\");push_value(bits_to_i32((uint32_t)a<=32)fail_runtime(\"invalid shift\");push_value(a>=0?(a>>b):(b==0?a:-1-((-1-a)>>b)));}\n";break; case 46:o<<"push_value(asin(pop_value()));\n";break;case 47:o<<"push_value(acos(pop_value()));\n";break; case 48:o<<"push_value(atan(pop_value()));\n";break;case 49:o<<"{double x=pop_value(),y=pop_value();push_value(atan2(y,x));}\n";break; default:return false; @@ -228,7 +228,7 @@ bool opcode(std::ostringstream &o,long long n) { std::string compile_c(const std::vector &tokens,int stack_size) { std::ostringstream body; for(const auto &t:tokens) { - if(t.kind==Token::Kind::String) body<<"fputs("<=100)throw std::runtime_error("variable index must be 0-99 at line "+std::to_string(t.line)); @@ -254,9 +254,11 @@ std::string compile_c(const std::vector &tokens,int stack_size) { } } std::ostringstream c; - c<<"#include \n#include \n#include \n#include \n"; + c<<"#include \n#include \n#include \n#include \n#include \n"; c<<"#define STACK_CAPACITY "<(double)INT32_MAX)fail_runtime(\"integer conversion out of range\");return (int32_t)v;}\n"; + c<<"static int32_t bits_to_i32(uint32_t v){return v<=(uint32_t)INT32_MAX?(int32_t)v:(int32_t)((int64_t)v-INT64_C(4294967296));}\n"; c<<"static void push_value(double v){if(stack_pointer>=STACK_CAPACITY)fail_runtime(\"stack overflow\");stack_values[stack_pointer++]=v;}\n"; c<<"static double pop_value(void){if(!stack_pointer)fail_runtime(\"stack underflow\");return stack_values[--stack_pointer];}\n"; c<<"int main(void){\n"< Date: Mon, 26 Oct 2026 00:48:48 +0000 Subject: [PATCH 02/10] Test embedded NUL strings and safe integer opcode behavior --- tests/smoke_cpp.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/smoke_cpp.py b/tests/smoke_cpp.py index 854c09f..d9b0752 100644 --- a/tests/smoke_cpp.py +++ b/tests/smoke_cpp.py @@ -24,6 +24,9 @@ def main(): assert r.stdout == "Hello, World!\n", repr(r.stdout) r = run([compiler, str(root / "examples/hello.num"), "--run", "--cc", str(wrapper)]) assert r.stdout == "42\n", repr(r.stdout) + p = tmp / "nul-followed-by-digit.num"; p.write_text('"\\\\01" |', encoding="utf-8") + r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) + assert r.stdout == "\\x001\\n", repr(r.stdout) p = tmp / "hex.num"; p.write_text("0xA |", encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) assert r.stdout == "10\n", repr(r.stdout) @@ -37,6 +40,24 @@ def main(): p = tmp / "suffixed-integer.num"; p.write_text("42u |", encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) assert r.stdout == "42\n", repr(r.stdout) + p = tmp / "bitwise.num"; p.write_text("5 3 42 |", encoding="utf-8") + r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) + assert r.stdout == "6\n", repr(r.stdout) + p = tmp / "signed-right-shift.num"; p.write_text("0 8 - 1 45 |", encoding="utf-8") + r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) + assert r.stdout == "-4\n", repr(r.stdout) + p = tmp / "left-shift-wrap.num"; p.write_text("2147483647 1 44 |", encoding="utf-8") + r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) + assert r.stdout == "-2\n", repr(r.stdout) + p = tmp / "bitwise-range.num"; p.write_text("2147483648 1 40 |", encoding="utf-8") + r = run([compiler, str(p), "--run", "--cc", str(wrapper)], expected=1) + assert "integer conversion out of range" in r.stderr, repr(r.stderr) + p = tmp / "remainder-overflow.num"; p.write_text("0 2147483648 - 0 1 - 38 |", encoding="utf-8") + r = run([compiler, str(p), "--run", "--cc", str(wrapper)], expected=1) + assert "integer remainder overflow" in r.stderr, repr(r.stderr) + p = tmp / "invalid-shift.num"; p.write_text("1 32 44 |", encoding="utf-8") + r = run([compiler, str(p), "--run", "--cc", str(wrapper)], expected=1) + assert "invalid shift" in r.stderr, repr(r.stderr) p = tmp / "large-float.num"; p.write_text("1e100 |", encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) assert r.stdout == "1e+100\n", repr(r.stdout) From ca7da566a12e0c127e18f267b19c01aa5e07102d Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:49:04 +0000 Subject: [PATCH 03/10] Document safe bitwise operations and NUL-preserving strings --- docs/cpp-compiler-status.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cpp-compiler-status.md b/docs/cpp-compiler-status.md index 9b37457..41d5d6e 100644 --- a/docs/cpp-compiler-status.md +++ b/docs/cpp-compiler-status.md @@ -5,11 +5,11 @@ This document describes the C++ `numlangc` implementation. The historical Python ## Supported end-to-end subset - Numeric literals: decimal integers/floats plus basic hex, octal, and binary forms. -- String literals with common C escapes; single-byte character literals with basic escapes. +- String literals with common C escapes, including embedded NUL bytes preserved by length-aware output; single-byte character literals with basic escapes. - Stack operations: `16` DUP, `17` SWAP, `18` DROP. - Arithmetic: `+`, `-`, `*`, `/`, `%` with runtime zero checks. - Comparisons: `10`–`15`; logical OR/AND `19` and `39`; logical NOT `!`. -- Math and bitwise opcodes implemented by the C++ code generator: `21`–`27`, `29`, `31`–`49`. +- Math and bitwise opcodes implemented by the C++ code generator: `21`–`27`, `29`, `31`–`49`; integer bitwise operations use checked 32-bit conversions, explicit signed-shift behavior, and diagnostics for out-of-range values and remainder overflow. - Numbered variables: `|n` loads and `&` stores a value to the index on top of the stack. - Console I/O: `|` prints a number, `^` prints a character, `?` reads a number, and strings write directly to stdout. - CLI: `-o/--output`, `--run`, `--cc`, `--stack-size`, and `--support`. `--run` uses a unique temporary directory and shell-quotes executable and file paths. From f849e3741446006732284d61a15b6e8f6a1d23ff Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:49:09 +0000 Subject: [PATCH 04/10] Document string and integer opcode safety --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eed290b..5f636f4 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ cmake -S . -B build -DCMAKE_BUILD_TYPE=Release cmake --build build -j ``` -The binary is `build/numlangc`. The current C++ implementation supports a tested core subset: numeric, string and character literals; stack operations; arithmetic; comparisons; selected math and bitwise opcodes; numbered variables; and basic console I/O. It rejects recognized but unsupported structural opcodes rather than treating them as ordinary numbers. The full language reference below describes the intended language and retained Python reference compiler, not complete C++ parity. See [the C++ compiler status](docs/cpp-compiler-status.md). +The binary is `build/numlangc`. The current C++ implementation supports a tested core subset: numeric, string and character literals; stack operations; arithmetic; comparisons; selected math and bitwise opcodes; numbered variables; and basic console I/O. String output preserves embedded NUL bytes. Integer bitwise opcodes validate 32-bit input ranges and use explicit signed-shift behavior instead of relying on undefined or implementation-defined signed casts/shifts. It rejects recognized but unsupported structural opcodes rather than treating them as ordinary numbers. The full language reference below describes the intended language and retained Python reference compiler, not complete C++ parity. See [the C++ compiler status](docs/cpp-compiler-status.md). --- From 4cd47d1c59b43d80f60378955ffa78fd3eba3f6c Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:50:31 +0000 Subject: [PATCH 05/10] Correct Python escaping for embedded-NUL regression --- tests/smoke_cpp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/smoke_cpp.py b/tests/smoke_cpp.py index d9b0752..5c3fd8b 100644 --- a/tests/smoke_cpp.py +++ b/tests/smoke_cpp.py @@ -24,9 +24,9 @@ def main(): assert r.stdout == "Hello, World!\n", repr(r.stdout) r = run([compiler, str(root / "examples/hello.num"), "--run", "--cc", str(wrapper)]) assert r.stdout == "42\n", repr(r.stdout) - p = tmp / "nul-followed-by-digit.num"; p.write_text('"\\\\01" |', encoding="utf-8") + p = tmp / "nul-followed-by-digit.num"; p.write_text('"\\01" |', encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) - assert r.stdout == "\\x001\\n", repr(r.stdout) + assert r.stdout == "\x001\n", repr(r.stdout) p = tmp / "hex.num"; p.write_text("0xA |", encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) assert r.stdout == "10\n", repr(r.stdout) From 51c4ec611f0354e2f436a93be4e58cd8c2485ae1 Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:52:11 +0000 Subject: [PATCH 06/10] Remove numeric print opcode after direct string output --- tests/smoke_cpp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/smoke_cpp.py b/tests/smoke_cpp.py index 5c3fd8b..c1b7c10 100644 --- a/tests/smoke_cpp.py +++ b/tests/smoke_cpp.py @@ -24,7 +24,7 @@ def main(): assert r.stdout == "Hello, World!\n", repr(r.stdout) r = run([compiler, str(root / "examples/hello.num"), "--run", "--cc", str(wrapper)]) assert r.stdout == "42\n", repr(r.stdout) - p = tmp / "nul-followed-by-digit.num"; p.write_text('"\\01" |', encoding="utf-8") + p = tmp / "nul-followed-by-digit.num"; p.write_text('"\\01"', encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) assert r.stdout == "\x001\n", repr(r.stdout) p = tmp / "hex.num"; p.write_text("0xA |", encoding="utf-8") From fd9018d44d21bbdf5e9ccfd88a96ca23317a0b3d Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:53:03 +0000 Subject: [PATCH 07/10] Match embedded-NUL test to direct string output --- tests/smoke_cpp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/smoke_cpp.py b/tests/smoke_cpp.py index c1b7c10..b7354af 100644 --- a/tests/smoke_cpp.py +++ b/tests/smoke_cpp.py @@ -26,7 +26,7 @@ def main(): assert r.stdout == "42\n", repr(r.stdout) p = tmp / "nul-followed-by-digit.num"; p.write_text('"\\01"', encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) - assert r.stdout == "\x001\n", repr(r.stdout) + assert r.stdout == "\x001", repr(r.stdout) p = tmp / "hex.num"; p.write_text("0xA |", encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)]) assert r.stdout == "10\n", repr(r.stdout) From 59128f0e10c3936cd25452de0d3770e10adebf37 Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:54:06 +0000 Subject: [PATCH 08/10] Avoid opcode collision in invalid-shift regression --- tests/smoke_cpp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/smoke_cpp.py b/tests/smoke_cpp.py index b7354af..aee195b 100644 --- a/tests/smoke_cpp.py +++ b/tests/smoke_cpp.py @@ -55,7 +55,7 @@ def main(): p = tmp / "remainder-overflow.num"; p.write_text("0 2147483648 - 0 1 - 38 |", encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)], expected=1) assert "integer remainder overflow" in r.stderr, repr(r.stderr) - p = tmp / "invalid-shift.num"; p.write_text("1 32 44 |", encoding="utf-8") + p = tmp / "invalid-shift.num"; p.write_text("1 32.0 44 |", encoding="utf-8") r = run([compiler, str(p), "--run", "--cc", str(wrapper)], expected=1) assert "invalid shift" in r.stderr, repr(r.stderr) p = tmp / "large-float.num"; p.write_text("1e100 |", encoding="utf-8") From a5a189d8ca36d36022ac16f115b42d343a13ed31 Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:58:05 +0000 Subject: [PATCH 09/10] Clarify 32-bit bitwise operation semantics --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5f636f4..7e26c0f 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ Values are still held as doubles, so integer results are exact only up to 2^53. --- -### Bitwise operators *(operands truncated to `int` before the operation)* +### Bitwise operators *(operands truncated to signed 32-bit values when in range)* | Code | Name | Effect | |------|------|--------| @@ -140,7 +140,9 @@ Values are still held as doubles, so integer results are exact only up to 2^53. | `42` | BXOR | `(int)a ^ (int)b` | | `43` | BNOT | `~(int)a` (unary) | | `44` | BSHL | `(int)a << (int)b` | -| `45` | BSHR | `(int)a >> (int)b` (arithmetic) | +| `45` | BSHR | arithmetic signed 32-bit right shift | + +The C++ compiler rejects non-finite or out-of-range operands for these integer operations, rejects shift counts outside 0–31, defines left-shift wraparound as a 32-bit bit pattern, and diagnoses `INT32_MIN % -1` rather than invoking signed-overflow undefined behavior. --- From 24f2eff42efe026cf4286095a330e9838e51d68a Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 26 Oct 2026 00:58:09 +0000 Subject: [PATCH 10/10] Clarify remaining escape and integer suffix parity gaps --- docs/cpp-compiler-status.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cpp-compiler-status.md b/docs/cpp-compiler-status.md index 41d5d6e..bdf5f06 100644 --- a/docs/cpp-compiler-status.md +++ b/docs/cpp-compiler-status.md @@ -18,7 +18,7 @@ This document describes the C++ `numlangc` implementation. The historical Python Control-flow opcodes (`20`, `28`, `30`, `50`), array operations (`51`–`53`), integer division (`55`), miscellaneous runtime opcodes (`60`–`64`), type declarations/conversions, named functions and variables, FFI, pointers, structs/unions, and the broader C-like type system are not implemented in C++ yet. Known reserved opcodes fail with a compile-time diagnostic instead of being emitted as numeric pushes. -Escape decoding, integer suffixes, C type conversions, stack-depth introspection, and full error/semantic parity are also incomplete. Do not extend the capability matrix to `implemented` for these features until there are C++ end-to-end tests. +Full C escape-decoding parity, integer suffix/type semantics, C type conversions, stack-depth introspection, and full error/semantic parity are also incomplete. Do not extend the capability matrix to `implemented` for these features until there are C++ end-to-end tests. ## Verification