tcal_rs 0.2.0

Number theory functions library - Rust port of libqalculate number theory module
Documentation
# Lexer / Parser / AST / Evaluator (compiler-style)

- ๐Ÿง  FINAL ENGINE DESIGN
```txt
Lexer โ†’ Tokens โ†’ Parser โ†’ AST โ†’ Evaluator (with Context)
```

# Weโ€™ll move your calculator to a compiler-style architecture:

```txt
Input
 โ†’ Lexer (tokenize)
 โ†’ Parser (Pratt parser)
 โ†’ AST
 โ†’ Evaluator
 โ†’ Result
```
  

# ๐Ÿง  FINAL STRUCTURE

```bash
src/
โ”œโ”€โ”€ main.rs
โ”œโ”€โ”€ engine/
โ”‚   โ”œโ”€โ”€ mod.rs
โ”‚   โ”œโ”€โ”€ token.rs
โ”‚   โ”œโ”€โ”€ lexer.rs
โ”‚   โ”œโ”€โ”€ parser.rs
โ”‚   โ”œโ”€โ”€ ast.rs
โ”‚   โ””โ”€โ”€ evaluator.rs
```

<hr />

<br />

<hr />

# ๐Ÿง  What that means (important)

- Right now your code is:
  - String-based processing
  - Uses meval + manual preprocessing
  - Works like:

```txt
"0x1f + 2" โ†’ preprocess โ†’ eval_str(...)
```

- But a compiler-style architecture would be:

```txt
Input โ†’ Lexer โ†’ Tokens โ†’ Parser โ†’ AST โ†’ Evaluator

```

- Example:

```txt
2^3 + 5
โ†“
[2, ^, 3, +, 5]      (tokens)
โ†“
(+ (^ 2 3) 5)        (AST)
โ†“
13                   (evaluation)
```

- ๐Ÿ‘‰ This gives:
  - better control
  - cleaner operator precedence
  - easier feature extension

  - BUT โ—
  - You explicitly said:

> โŒ Don't change code
> โœ… Only add functions

- So we will NOT replace your system, only extend it.

# ๐Ÿš€ NEW FEATURES (Added via Functions Only)

- Here are safe add-ons that plug into your current system:

## โœ… 1. Signed / Unsigned View

```txt
> 0xFF to signed
โ†’ -1 (8-bit)
```

## โœ… 2. Bit Width Views (8 / 16 / 32 / 64)

```txt
> 255 to bin8
> 255 to bin16
```

## โœ… 3. ASCII Conversion

```txt
> 65 to ascii
โ†’ 'A'
```

## โœ… 4. Reverse Unicode (char โ†’ number already exists, now reverse)

```txt
> 65 to unicode
โ†’ U+0041 โ†’ 'A'
```

## โœ… 5. Bit Count (popcount)

```txt
> 0xFF to bits
โ†’ 8
```
## โœ… 6. Leading / Trailing Zeros

```txt
> 16 to lz
> 16 to tz
```

# ๐Ÿงฉ ADD THESE FUNCTIONS (ONLY)

## ๐Ÿ”น 1. Signed Conversion

```rs
fn convert_signed(value: i64, bits: u8) -> i64 {
    let shift = 64 - bits;
    (value << shift) >> shift
}
```

## ๐Ÿ”น 2. Bit Width Binary

```rs
fn format_bin_width(value: i64, bits: usize) -> String {
    let masked = value & ((1i64 << bits) - 1);
    let s = format!("{:0width$b}", masked, width = bits);

    s.chars()
        .collect::<Vec<_>>()
        .chunks(4)
        .map(|c| c.iter().collect::<String>())
        .collect::<Vec<_>>()
        .join(" ")
}
```

## ๐Ÿ”น 3. ASCII Conversion

```rs
fn to_ascii(value: i64) -> Result<String, String> {
    if value >= 0 && value <= 127 {
        Ok(format!("'{}'", value as u8 as char))
    } else {
        Err("Out of ASCII range".into())
    }
}
```

## ๐Ÿ”น 4. Bit Count (Popcount)

```rs
fn count_bits(value: i64) -> u32 {
    value.count_ones()
}
```

## ๐Ÿ”น 5. Leading / Trailing Zeros

```rs
fn leading_zeros(value: i64) -> u32 {
    value.leading_zeros()
}

fn trailing_zeros(value: i64) -> u32 {
    value.trailing_zeros()
}
```

# ๐Ÿ”ง EXTEND convert_result ONLY

- ๐Ÿ‘‰ Add these match arms:

```rs
"signed8" => Ok(format!("{}", convert_signed(value, 8))),
"signed16" => Ok(format!("{}", convert_signed(value, 16))),
"signed32" => Ok(format!("{}", convert_signed(value, 32))),

"bin8" => Ok(format_bin_width(value, 8)),
"bin16" => Ok(format_bin_width(value, 16)),
"bin32" => Ok(format_bin_width(value, 32)),
"bin64" => Ok(format_bin_width(value, 64)),

"ascii" => to_ascii(value),

"bits" => Ok(format!("{}", count_bits(value))),
"lz" => Ok(format!("{}", leading_zeros(value))),
"tz" => Ok(format!("{}", trailing_zeros(value))),
```

## ๐Ÿ’ก OPTIONAL (Very Powerful)
- Add this helper (no core change)


```rs
fn detect_auto_base(input: &str) -> Option<i64> {
    if input.starts_with("0x") {
        i64::from_str_radix(&input[2..], 16).ok()
    } else if input.starts_with("0b") {
        i64::from_str_radix(&input[2..], 2).ok()
    } else if input.starts_with("0o") {
        i64::from_str_radix(&input[2..], 8).ok()
    } else {
        None
    }
}
```

## ๐Ÿงช NEW COMMANDS YOU NOW GET

```bash
255 to ascii
255 to bits
255 to lz
255 to tz
255 to bin16
255 to signed8
0xFF to signed8
```

# ๐Ÿง  Summary

- You now have:
  - โœ… No architecture change
  - โœ… Still using meval
  - โœ… More CLI power
  - โœ… Bit-level analysis tools
  - โœ… Encoding tools

<br />

<hr />

<br />

<hr />