Skip to content

Add Y-lang prime number calculator: parser <=, assignment coercion, stdlib/primes#3

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/add-prime-number-calculator
Draft

Add Y-lang prime number calculator: parser <=, assignment coercion, stdlib/primes#3
Copilot wants to merge 2 commits intomasterfrom
copilot/add-prime-number-calculator

Conversation

Copy link
Contributor

Copilot AI commented Feb 24, 2026

Implements a Y-lang program that computes and prints the first 1000 prime numbers to stdout, compiled to a native Linux binary and verified against a Go-computed reference. Three blocking issues required fixes before the program could compile and run correctly.

Compiler / Parser Fixes

  • parser/parser.go: <= (TokenTypeLessThanEqual) was lexed and codegen-handled but never registered as an infix operator — added to precedence table (LESSGREATER) and infix dispatch. Without this, while (i * i <= n) failed to parse.
  • compiler/generator/assignment.go: Stores to typed pointer targets (e.g. i8* from mmap) now truncate or sign-extend the RHS to match the element type before emitting store. Enables byte-buffer writes:
    let buf = syscall(9, 0, 8, 3, 34, -1, 0);
    buf[0] = d + 48;  // i32 truncated to i8 before store

New stdlib Module

  • lib/stdlib/primes/primes.y: Pure Y-lang stdlib module using Linux x86-64 syscalls only (no C runtime):
    • isPrime(n) — trial division; modulo emulated as n - (n/i)*i
    • print_digit(d) — single digit via mmap scratch buffer + SYS_write + munmap
    • print_int(n) — recursive decimal printer
    • print_primes(count) — iterates candidates, prints first count primes

Integration Test

  • primes_test.go: Lexes → parses → compiles to LLVM IR → clang → runs binary → verifies all 1000 output lines against firstNPrimes(1000) computed in Go (first: 2, 1000th: 7919).

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: andrewjc <444861+andrewjc@users.noreply.github.com>
Copilot AI changed the title [WIP] Add console program for calculating 1000 prime numbers Add Y-lang prime number calculator: parser <=, assignment coercion, stdlib/primes Feb 24, 2026
Copilot AI requested a review from andrewjc February 24, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants