diff --git a/README.md b/README.md index eed290b..7e26c0f 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). --- @@ -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. --- diff --git a/docs/cpp-compiler-status.md b/docs/cpp-compiler-status.md index 9b37457..bdf5f06 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. @@ -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 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"<