Skip to content
Merged
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

---

Expand Down Expand Up @@ -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 |
|------|------|--------|
Expand All @@ -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.

---

Expand Down
6 changes: 3 additions & 3 deletions docs/cpp-compiler-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
22 changes: 12 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<<b);}\n";break;
case 45:o<<"{int b=(int)pop_value(),a=(int)pop_value();if(b<0||b>=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<<b));}\n";break;
case 45:o<<"{int32_t b=checked_i32(pop_value()),a=checked_i32(pop_value());if(b<0||b>=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;
Expand All @@ -228,7 +228,7 @@ bool opcode(std::ostringstream &o,long long n) {
std::string compile_c(const std::vector<Token> &tokens,int stack_size) {
std::ostringstream body;
for(const auto &t:tokens) {
if(t.kind==Token::Kind::String) body<<"fputs("<<c_quote(t.text)<<",stdout);\n";
if(t.kind==Token::Kind::String) body<<"fwrite("<<c_quote(t.text)<<",1,"<<t.text.size()<<",stdout);\n";
else if(t.kind==Token::Kind::Character) body<<"push_value("<<t.integer<<");\n";
else if(t.kind==Token::Kind::Variable) {
if(t.integer<0||t.integer>=100)throw std::runtime_error("variable index must be 0-99 at line "+std::to_string(t.line));
Expand All @@ -254,9 +254,11 @@ std::string compile_c(const std::vector<Token> &tokens,int stack_size) {
}
}
std::ostringstream c;
c<<"#include <math.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <stddef.h>\n";
c<<"#include <math.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <stddef.h>\n#include <stdint.h>\n";
c<<"#define STACK_CAPACITY "<<stack_size<<"\nstatic double stack_values[STACK_CAPACITY];\nstatic size_t stack_pointer=0;\nstatic double variables[100]={0};\n";
c<<"static void fail_runtime(const char *m){fprintf(stderr,\"numlang runtime error: %s\\n\",m);exit(1);}\n";
c<<"static int32_t checked_i32(double v){if(!isfinite(v)||v<(double)INT32_MIN||v>(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"<<body.str()<<"return 0;\n}\n";return c.str();
Expand Down
21 changes: 21 additions & 0 deletions tests/smoke_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", 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)
Expand All @@ -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.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")
r = run([compiler, str(p), "--run", "--cc", str(wrapper)])
assert r.stdout == "1e+100\n", repr(r.stdout)
Expand Down
Loading