From d934eed23af49a7d9f9928c3f70012584f5551b1 Mon Sep 17 00:00:00 2001 From: codex Date: Tue, 17 Mar 2026 19:36:22 +0530 Subject: [PATCH] added codex --- .gitignore | 3 + CLAUDE.md | 1 + agents.md | 198 +++++++++++++ compiler/src/tests/variables.rey | 2 +- compiler/v1/src/interpreter/evaluator.rs | 2 +- compiler/v1/test_syntax.rey | 138 +++++++++ syntax.md | 348 +++++++++++++++++++++++ 7 files changed, 690 insertions(+), 2 deletions(-) create mode 100644 agents.md create mode 100644 compiler/v1/test_syntax.rey create mode 100644 syntax.md diff --git a/.gitignore b/.gitignore index 7afacd3..8838ecf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ target/ .DS_Store *.lock + +test_all.rey +resumes.txt diff --git a/CLAUDE.md b/CLAUDE.md index 340a133..2a1349c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -72,3 +72,4 @@ From the codebase and CONTRIBUTING.md: - I never add dependencies without asking Misbah first. - I never rewrite entire files for small fixes. - I never delete files without asking. +- At the Start of every session, instead of going through the code, i'll go through CLAUDE.md, primer.md and other readme files to get context diff --git a/agents.md b/agents.md new file mode 100644 index 0000000..9623c84 --- /dev/null +++ b/agents.md @@ -0,0 +1,198 @@ +# AGENTS.md — Codex contributor setup + +You are an autonomous contributor on this project. Read this entire file before +doing anything. Then read CLAUDE.md, primer.md, and CHANGELOG.md in that order. + +--- + +# Your identity + +Before doing anything else, set your git identity: +```bash +git config user.name "codex" +git config user.email "codex@users.noreply.github.com" +``` + +This ensures commits are attributed correctly without exposing personal emails. + +--- + +# Critical rules — never break these + +- Never touch `main` or `master` directly. Ever. +- Never commit or push to main. Ever. +- Never delete files without explicitly telling Misbah first. +- Never rewrite an entire file when a small fix is needed. Surgical changes only. +- Never add dependencies without telling Misbah first. +- Never push without running verification first. + +--- + +# Who you are working with + +- Misbah is the owner. He codes on `main`/`master`. +- You are a contributor. You code on the `codex` branch. +- You maintain `primer.md` — rewrite it at the start and end of every session. +- You update `CHANGELOG.md` whenever you merge, fix conflicts, or resolve issues. +- You update `CLAUDE.md` when the project meaningfully evolves. + +--- + +# Session start — do this every time + +1. Set your git identity (see above) +2. Check which branch you're on: +```bash +git branch --show-current +``` +3. If not on `codex` branch, switch to it: +```bash +git checkout codex 2>/dev/null || git checkout -b codex +git push -u origin codex 2>/dev/null || true +``` +4. Read `primer.md` — this is where you left off last session +5. Read `CLAUDE.md` — project context and conventions +6. Run git log to understand recent history: +```bash +git log --oneline -10 +git log main..codex --oneline +``` +7. Rewrite `primer.md` with current state before starting work +8. Ask Misbah what to work on — or if he says "go ahead", check primer.md + for the next unimplemented feature and start there + +--- + +# How to work + +- State what you're about to do before doing it +- Make small, targeted changes. Don't refactor unless asked. +- Match existing code style — camelCase, compact, minimal comments +- Comments only when non-obvious. Style: `// bad workaround - does x` +- Explain the WHY behind decisions, not just what you changed +- If something is wrong or Misbah has a bad approach, say so directly + +--- + +# Git workflow + +## Committing +After meaningful work: +```bash +git add -A +git commit -m "type(scope): description" +git push origin codex +``` + +Conventional commit format: +- `feat(lexer): add comment tokenization` +- `fix(parser): handle empty array literal` +- `chore(codex): update primer` + +Commit frequently. Do not batch large changes unnecessarily. + +## Pull Requests +- ONLY open a PR when a feature is **fully implemented and verified** +- Do NOT open PRs for partial, incomplete, or work-in-progress features + +When a feature is complete: +```bash +gh pr create --base main --head codex \ + --title "feat: your feature title" \ + --body "## What +brief description of what was done + +## Why +reasoning behind the approach + +## Notes +anything Misbah should pay attention to when reviewing" +``` + +--- + +# Never do this + +```bash +git push origin main # NEVER +git checkout main # NEVER (read only if needed) +git merge main # use rebase instead +``` + +--- + +# Autonomous mode + +If Misbah says "go ahead" or "keep going": + +1. Check primer.md for the next unimplemented feature +2. Implement it fully +3. Verify it works (compile, run tests if they exist) +4. Commit and push to codex branch +5. Open a PR ONLY if the feature is fully complete +6. Pick the next feature from the list and repeat +7. Don't stop until the session ends or you're genuinely stuck +8. If stuck — document exactly what's blocking in primer.md, commit, stop + +--- + +# Verification — always do this before saying done + +- Rust project → `cargo build`, check warnings, `cargo test` if tests exist +- .rey files → `cargo run -- .rey`, check output +- Never say done without verifying + +--- + +# Code style + +- camelCase for everything +- Compact formatting, no excessive blank lines +- Comments minimal and lowercase +- No abstractions that weren't asked for +- No premature optimization +- Match whatever style already exists in the file + +--- + +# Session end — do this before stopping + +1. Commit any uncommitted work +2. Push to codex branch +3. Open a PR ONLY if a feature is fully complete +4. Rewrite primer.md with: + - what was done this session + - current state of the project + - what's next + - anything that's blocked or broken +5. Commit the updated primer: +```bash +git add primer.md CHANGELOG.md +git commit -m "chore(codex): update primer and changelog" +git push origin codex +``` + +--- + +# Conflict resolution + +If merging causes conflicts: +1. Read both sides, understand what each was trying to do +2. Resolve correctly — don't just pick one side +3. Log the conflict in CHANGELOG.md: +``` +## [sync] — date +### Conflicts resolved +- path/to/file — what conflicted and how it was resolved +``` + +--- + +# Critical rules (repeated — read this last) + +- Never touch main. Ever. +- Never delete files without asking. +- Never rewrite entire files for small fixes. +- Never add dependencies without asking. +- Always verify before saying done. + diff --git a/compiler/src/tests/variables.rey b/compiler/src/tests/variables.rey index fcbff3a..9a26d53 100644 --- a/compiler/src/tests/variables.rey +++ b/compiler/src/tests/variables.rey @@ -13,4 +13,4 @@ func main(): Void { var flag = true; println(flag); -} +}x diff --git a/compiler/v1/src/interpreter/evaluator.rs b/compiler/v1/src/interpreter/evaluator.rs index 4553cc0..cc380d1 100644 --- a/compiler/v1/src/interpreter/evaluator.rs +++ b/compiler/v1/src/interpreter/evaluator.rs @@ -79,7 +79,7 @@ impl Evaluator { match (op, right) { (Minus, Value::Number(n)) => Ok(Value::Number(-n)), - (Bang, Value::Bool(b)) => Ok(Value::Bool(!b)), + (Not, Value::Bool(b)) => Ok(Value::Bool(!b)), _ => Err("Invalid unary operation".to_string()), } } diff --git a/compiler/v1/test_syntax.rey b/compiler/v1/test_syntax.rey new file mode 100644 index 0000000..3342935 --- /dev/null +++ b/compiler/v1/test_syntax.rey @@ -0,0 +1,138 @@ +func add(a: int, b: int): int { + return a + b; +} + +func sub(a: int, b: int): int { + return a - b; +} + +func mul(a: int, b: int): int { + return a * b; +} + +func div(a: int, b: int): int { + return a / b; +} + +func fib(n: int): int { + if (n < 2) { + return n; + } + return fib(n - 1) + fib(n - 2); +} + +func echo(x) { + return x; +} + +func greet(name: String): String { + return name; +} + +func compute(): Void { + var sum = 0; + var i = 0; + while (i < 10) { + sum = sum + i; + i = i + 1; + } + println(sum); +} + +func main(): Void { + var x = 10; + x = 20; + println(x); + + var name = "Rey"; + println(name); + + var flag = true; + println(flag); + + var f = 3.14; + println(f); + + var i: int = 42; + println(i); + + var fs: float = 2.71; + println(fs); + + var s: String = "hello"; + println(s); + + var b: bool = false; + println(b); + + println(add(10, 5)); + println(sub(10, 5)); + println(mul(10, 5)); + println(div(10, 5)); + + var neg = -42; + println(neg); + + var notflag = !true; + println(notflag); + + println(10 == 10); + println(10 != 5); + println(3 < 5); + println(5 > 3); + + if (x > 10) { + println("x is big"); + } else { + println("x is small"); + } + + var count = 0; + while (count < 5) { + println(count); + count = count + 1; + } + + var k = 0; + while (true) { + if (k == 3) { + break; + } + k = k + 1; + } + println(k); + + var j = 0; + while (j < 6) { + j = j + 1; + if (j == 3) { + continue; + } + println(j); + } + + for n in range(0, 5) { + println(n); + } + + compute(); + println(echo("untyped param works")); + println(greet("Misbah")); + + var fi = 0; + while (fi < 8) { + println(fib(fi)); + fi = fi + 1; + } + + var a = 5; + var c = 15; + if (a < 10) { + if (c > 10) { + println("nested if works"); + } + } + + var result = add(mul(2, 3), sub(10, 4)); + println(result); +} \ No newline at end of file diff --git a/syntax.md b/syntax.md new file mode 100644 index 0000000..2ebf9fe --- /dev/null +++ b/syntax.md @@ -0,0 +1,348 @@ +# Rey Language Syntax — v0 + +Reference documentation for implemented syntax and features in the Rey v0 interpreter. + +--- + +## Table of Contents + +1. [Variables](#variables) +2. [Data Types](#data-types) +3. [Operators](#operators) +4. [Control Flow](#control-flow) +5. [Functions](#functions) +6. [Builtins](#builtins) +7. [Program Structure](#program-structure) + +--- + +## Variables + +### Declaration + +Variables are declared using the `var` keyword. All variables must be declared before use. + +```rey +var x = 10; // untyped (type inferred) +var name = "Rey"; // untyped +var flag = true; // untyped +``` + +### Type Annotations + +Variables MAY declare a type using `: type`. Typed variables MUST receive compatible values on reassignment. + +```rey +var i: int = 5; // typed as int +var f: float = 3.14; // typed as float +var s: String = "hello"; // typed as String +var b: bool = false; // typed as bool +``` + +### Reassignment + +Variables can be reassigned using `=`. The new value MUST match the declared type if one was specified. + +```rey +var x = 10; +x = 20; // OK - same type + +var typed: int = 42; +typed = 100; // OK - int matches int +``` + +--- + +## Data Types + +### Core Types + +| Type | Example | Description | +|------|---------|-------------| +| `int` | `42`, `-10` | Integer numbers | +| `float` | `3.14`, `-0.5` | Floating-point numbers | +| `String` | `"hello"` | String literals | +| `bool` | `true`, `false` | Boolean values | +| `null` | `null` | Null value | +| `Void` | `Void` | Function return type (no value) | + +### Type Inference + +When no type annotation is provided, the type is inferred from the initializer: + +```rey +var x = 10; // inferred as int +var f = 3.14; // inferred as float +var s = "text"; // inferred as String +var b = true; // inferred as bool +``` + +--- + +## Operators + +### Arithmetic Operators + +| Operator | Description | Example | +|----------|-------------|---------| +| `+` | Addition | `a + b` | +| `-` | Subtraction | `a - b` | +| `*` | Multiplication | `a * b` | +| `/` | Division | `a / b` | + +### Comparison Operators + +| Operator | Description | Example | +|----------|-------------|---------| +| `==` | Equal | `a == b` | +| `!=` | Not equal | `a != b` | +| `<` | Less than | `a < b` | +| `>` | Greater than | `a > b` | + +Note: `<=` and `>=` are NOT implemented. + +### Logical Operators + +| Operator | Description | Example | +|----------|-------------|---------| +| `&&` | Logical AND | `a && b` | +| `||` | Logical OR | `a || b` | + +### Unary Operators + +| Operator | Description | Example | +|----------|-------------|---------| +| `-` | Negation | `-x` | +| `!` | Logical NOT | `!flag` | + +### Assignment Operator + +| Operator | Description | Example | +|----------|-------------|---------| +| `=` | Simple assignment | `x = 5` | + +--- + +## Control Flow + +### If/Else + +Conditional execution using `if` and `else`. Parentheses are **required** around the condition. + +```rey +if (condition) { + // executed if condition is true +} else { + // executed if condition is false +} +``` + +Example: + +```rey +if (x > 10) { + println("big"); +} else { + println("small"); +} +``` + +### While Loops + +While loops repeat while the condition is true. Parentheses are **required** around the condition. + +```rey +while (condition) { + // loop body +} +``` + +Example: + +```rey +var i = 0; +while (i < 10) { + println(i); + i = i + 1; +} +``` + +### For Loans + +For loops iterate over a range: + +```rey +for x in range(start, end) { + // loop body +} +``` + +The `range` function produces values from `start` (inclusive) to `end` (exclusive). + +### Break and Continue + +```rey +while (true) { + if (done) { + break; // exit loop + } + if (skip) { + continue; // next iteration + } +} +``` + +--- + +## Functions + +### Declaration + +Functions are declared using the `func` keyword. + +```rey +func name(parameters) : returnType { + body +} +``` + +### Parameters + +Parameters MAY be typed. Untyped parameters accept any value. + +```rey +// untyped parameter +func echo(x) { + return x; +} + +// typed parameters +func add(a: int, b: int): int { + return a + b; +} +``` + +### Return Types + +Functions MAY declare a return type. If declared, all return paths MUST return a compatible value. + +```rey +func greet(name: String): String { + return name; +} + +func compute(): Void { + return; // or omit return entirely +} +``` + +### Calling Functions + +```rey +func main(): Void { + var result = add(2, 3); + println(result); +} +``` + +--- + +## Builtins + +### `println` + +Print a value to stdout. + +```rey +println("Hello, World!"); +println(42); +println(true); +``` + +--- + +## Program Structure + +### Entry Point + +Programs start executing from the `main` function: + +```rey +func main(): Void { + // program entry point +} +``` + +### Complete Example + +```rey +// Variable and function example + +func add(a: int, b: int): int { + return a + b; +} + +func main(): Void { + var x = 10; + var y = 20; + var sum = add(x, y); + println(sum); // prints 30 +} +``` + +### Fibonacci Example + +```rey +func fib(n: int): int { + if (n < 2) { + return n; + } + return fib(n - 1) + fib(n - 2); +} + +func main(): Void { + var i: int = 0; + while (i < 10) { + println(fib(i)); + i = i + 1; + } +} +``` + +--- + +## Not Yet Implemented + +The following features are NOT implemented: + +| Feature | Status | +|---------|--------| +| Arrays (`[1, 2, 3]`) | Not implemented | +| Dictionaries (`{key: value}`) | Not implemented | +| Index access (`arr[i]`, `dict["key"]`) | Not implemented | +| Type enforcement at compile time | Parsed but not enforced | +| `input()` builtin | Not implemented | +| String methods (`.length()`) | Not implemented | +| Comments (`// ...`) | Lexer does not tokenize comments | +| Modulo operator (`%`) | Lexer token exists but parser doesn't use it | +| Compound assignment (`+=`, `-=`, etc) | Not implemented | +| Increment/decrement (`++`, `--`) | Not implemented | + +--- + +## Running Programs + +Build and run using Cargo: + +```bash +cd compiler/v1 +cargo build --release +./target/release/rey-v0 +``` + +Or use `cargo run`: + +```bash +cd compiler/v1 +cargo run -- ../src/tests/variables.rey +```