Public API and docs

This commit is contained in:
2023-05-26 15:23:15 +01:00
parent 3c1a6e2be2
commit fdf18e6ff8
99 changed files with 503 additions and 406 deletions

14
src/stl/conv/mod.rs Normal file
View File

@@ -0,0 +1,14 @@
use crate::interner::Interner;
use crate::pipeline::ConstTree;
mod parse_float;
mod parse_uint;
mod to_string;
pub fn conv(i: &Interner) -> ConstTree {
ConstTree::tree([
(i.i("parse_float"), ConstTree::xfn(parse_float::ParseFloat1)),
(i.i("parse_uint"), ConstTree::xfn(parse_uint::ParseUint1)),
(i.i("to_string"), ConstTree::xfn(to_string::ToString1)),
])
}

View File

@@ -0,0 +1,43 @@
use std::fmt::Debug;
use chumsky::Parser;
use super::super::assertion_error::AssertionError;
use super::super::litconv::with_lit;
use crate::parse::float_parser;
use crate::representations::interpreted::ExprInst;
use crate::representations::Literal;
use crate::{atomic_impl, atomic_redirect, externfn_impl};
/// parse a number. Accepts the same syntax Orchid does
///
/// Next state: [ParseFloat0]
#[derive(Clone)]
pub struct ParseFloat1;
externfn_impl!(ParseFloat1, |_: &Self, x: ExprInst| Ok(ParseFloat0 { x }));
/// Prev state: [ParseFloat1]
#[derive(Debug, Clone)]
pub struct ParseFloat0 {
x: ExprInst,
}
atomic_redirect!(ParseFloat0, x);
atomic_impl!(ParseFloat0, |Self { x }: &Self, _| {
let number = with_lit(x, |l| {
Ok(match l {
Literal::Str(s) => {
let parser = float_parser();
parser.parse(s.as_str()).map_err(|_| {
AssertionError::ext(x.clone(), "cannot be parsed into a float")
})?
},
Literal::Num(n) => *n,
Literal::Uint(i) => (*i as u32).into(),
Literal::Char(char) => char
.to_digit(10)
.ok_or(AssertionError::ext(x.clone(), "is not a decimal digit"))?
.into(),
})
})?;
Ok(number.into())
});

View File

@@ -0,0 +1,47 @@
use std::fmt::Debug;
use chumsky::Parser;
use super::super::assertion_error::AssertionError;
use super::super::litconv::with_lit;
use crate::parse::int_parser;
use crate::representations::interpreted::ExprInst;
use crate::representations::Literal;
use crate::{atomic_impl, atomic_redirect, externfn_impl};
/// Parse an unsigned integer. Accepts the same formats Orchid does. If the
/// input is a number, floors it.
///
/// Next state: [ParseUint0]
#[derive(Clone)]
pub struct ParseUint1;
externfn_impl!(ParseUint1, |_: &Self, x: ExprInst| Ok(ParseUint0 { x }));
/// Prev state: [ParseUint1]
#[derive(Debug, Clone)]
pub struct ParseUint0 {
x: ExprInst,
}
atomic_redirect!(ParseUint0, x);
atomic_impl!(ParseUint0, |Self { x }: &Self, _| {
let uint = with_lit(x, |l| {
Ok(match l {
Literal::Str(s) => {
let parser = int_parser();
parser.parse(s.as_str()).map_err(|_| {
AssertionError::ext(
x.clone(),
"cannot be parsed into an unsigned int",
)
})?
},
Literal::Num(n) => n.floor() as u64,
Literal::Uint(i) => *i,
Literal::Char(char) => char
.to_digit(10)
.ok_or(AssertionError::ext(x.clone(), "is not a decimal digit"))?
.into(),
})
})?;
Ok(uint.into())
});

32
src/stl/conv/to_string.rs Normal file
View File

@@ -0,0 +1,32 @@
use std::fmt::Debug;
use super::super::litconv::with_lit;
use crate::representations::interpreted::ExprInst;
use crate::representations::Literal;
use crate::{atomic_impl, atomic_redirect, externfn_impl};
/// Convert a literal to a string using Rust's conversions for floats, chars and
/// uints respectively
///
/// Next state: [ToString0]
#[derive(Clone)]
pub struct ToString1;
externfn_impl!(ToString1, |_: &Self, x: ExprInst| Ok(ToString0 { x }));
/// Prev state: [ToString1]
#[derive(Debug, Clone)]
pub struct ToString0 {
x: ExprInst,
}
atomic_redirect!(ToString0, x);
atomic_impl!(ToString0, |Self { x }: &Self, _| {
let string = with_lit(x, |l| {
Ok(match l {
Literal::Char(c) => c.to_string(),
Literal::Uint(i) => i.to_string(),
Literal::Num(n) => n.to_string(),
Literal::Str(s) => s.clone(),
})
})?;
Ok(string.into())
});