Compile a Lean Level to an Ixon Univ.
( level: &Level, univ_params: &[Name], cache: &mut BlockCache, )
| 377 | if let Some(mut entry) = self.env.named.get_mut(name) { |
| 378 | entry.value_mut().set_original(orig_addr, orig_meta); |
| 379 | } |
| 380 | Ok(()) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // =========================================================================== |
| 385 | // Helper functions |
| 386 | // =========================================================================== |
| 387 | |
| 388 | /// Convert a Nat to u64, returning an error if the value is too large. |
| 389 | fn nat_to_u64(n: &Nat, context: &'static str) -> Result<u64, CompileError> { |
| 390 | n.to_u64().ok_or(CompileError::UnsupportedExpr { desc: context.into() }) |
| 391 | } |
| 392 | |
| 393 | // =========================================================================== |
| 394 | // Name compilation |
| 395 | // =========================================================================== |
| 396 | |
| 397 | /// Store a string as a blob and return its address. |
| 398 | pub fn store_string(s: &str, stt: &CompileState) -> Address { |
| 399 | stt.env.store_blob(s.as_bytes().to_vec()) |
| 400 | } |
| 401 | |
| 402 | /// Store a Nat as a blob and return its address. |
| 403 | pub fn store_nat(n: &Nat, stt: &CompileState) -> Address { |
| 404 | stt.env.store_blob(n.to_le_bytes()) |
| 405 | } |
| 406 | |
| 407 | /// Compile a Lean Name to an address (stored in env.names). |
| 408 | /// Uses the Name's internal hash as the address. |
| 409 | /// String components are stored in blobs. |
| 410 | pub fn compile_name(name: &Name, stt: &CompileState) -> Address { |
| 411 | // Use the Name's internal hash as the address |
| 412 | let addr = Address::from_blake3_hash(*name.get_hash()); |
| 413 | |
| 414 | // Check if already stored |
| 415 | if stt.env.names.contains_key(&addr) { |
| 416 | return addr; |
| 417 | } |
| 418 | |
| 419 | // Recurse on parent first (ensures parent is stored) |
| 420 | match name.as_data() { |
| 421 | NameData::Anonymous(_) => {}, |
| 422 | NameData::Str(parent, s, _) => { |
| 423 | compile_name(parent, stt); |
| 424 | store_string(s, stt); // string data in blobs |
| 425 | }, |
| 426 | NameData::Num(parent, _, _) => { |