cao_lang/
lib.rs

1//! ## Programs
2//!
3//! __TBA__
4//!
5
6#![recursion_limit = "256"]
7
8mod alloc;
9pub mod collections;
10pub mod compiled_program;
11pub mod compiler;
12pub mod instruction;
13pub mod prelude;
14pub mod procedures;
15pub mod stdlib;
16pub mod traits;
17pub mod value;
18pub mod vm;
19
20mod bytecode;
21
22pub mod version {
23    pub const VERSION_STR: &str = env!("CARGO_PKG_VERSION");
24}
25
26use std::{mem::size_of, str::FromStr};
27
28use bytemuck::{Pod, Zeroable};
29
30use crate::instruction::Instruction;
31
32#[derive(Pod, Zeroable, Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34#[repr(C)]
35pub struct VariableId(u32);
36
37impl FromStr for VariableId {
38    type Err = <u32 as FromStr>::Err;
39
40    fn from_str(s: &str) -> Result<Self, Self::Err> {
41        let inner = u32::from_str(s)?;
42        Ok(VariableId(inner))
43    }
44}
45
46#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
47#[repr(transparent)]
48pub struct StrPointer(pub *mut u8);
49
50impl StrPointer {
51    /// # Safety
52    ///
53    /// Must be called with ptr obtained from a `string_literal` instruction, before the last `clear`!
54    ///
55    /// # Return value
56    ///
57    /// Returns None if the underlying string is not valid utf8
58    pub unsafe fn get_str<'a>(self) -> Option<&'a str> {
59        let ptr = self.0;
60        if ptr.is_null() {
61            return None;
62        }
63        let len = *(ptr as *const u32);
64        let ptr = ptr.add(size_of::<u32>());
65        std::str::from_utf8(std::slice::from_raw_parts(ptr, len as usize)).ok()
66    }
67}
68
69pub type InputString = String;
70pub type VarName = String;