CHECKPOINT: Initial syntax arbitrary implementation.

This commit is contained in:
2024-06-16 20:59:32 -07:00
parent cf7eff7a93
commit 212ca6cc53
13 changed files with 867 additions and 279 deletions

View File

@@ -1,6 +1,6 @@
use internment::ArcIntern;
use logos::{Lexer, Logos};
use std::fmt;
use std::{fmt, str::FromStr};
use thiserror::Error;
/// A single token of the input stream; used to help the parsing go down
@@ -205,6 +205,43 @@ impl From<ConstantType> for cranelift_codegen::ir::Type {
}
}
pub struct StringNotConstantType();
impl FromStr for ConstantType {
type Err = StringNotConstantType;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"i8" => Ok(ConstantType::I8),
"i16" => Ok(ConstantType::I16),
"i32" => Ok(ConstantType::I32),
"i64" => Ok(ConstantType::I64),
"u8" => Ok(ConstantType::U8),
"u16" => Ok(ConstantType::U16),
"u32" => Ok(ConstantType::U32),
"u64" => Ok(ConstantType::U64),
"void" => Ok(ConstantType::Void),
_ => Err(StringNotConstantType()),
}
}
}
impl fmt::Display for ConstantType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConstantType::I8 => write!(f, "i8"),
ConstantType::I16 => write!(f, "i16"),
ConstantType::I32 => write!(f, "i32"),
ConstantType::I64 => write!(f, "i64"),
ConstantType::U8 => write!(f, "u8"),
ConstantType::U16 => write!(f, "u16"),
ConstantType::U32 => write!(f, "u32"),
ConstantType::U64 => write!(f, "u64"),
ConstantType::Void => write!(f, "void"),
}
}
}
impl ConstantType {
/// Return the set of types that can be safely casted into this type.
pub fn safe_casts_to(self) -> Vec<ConstantType> {
@@ -268,6 +305,32 @@ impl ConstantType {
ConstantType::U64 => "u64".to_string(),
}
}
/// Return the set of all primitives that can return this
/// type, along with the argument types for those primitives.
///
/// A "None" value as an argument type means that the argument
/// type is unconstrained by the return type.
pub fn primitives_for(&self) -> Vec<(crate::ir::Primitive, Vec<Option<ConstantType>>)> {
use crate::ir::Primitive::*;
match self {
ConstantType::Void => vec![(Print, vec![None])],
ConstantType::I8 | ConstantType::I16 | ConstantType::I32 | ConstantType::I64 => vec![
(Plus, vec![Some(*self), Some(*self)]),
(Minus, vec![Some(*self), Some(*self)]),
(Times, vec![Some(*self), Some(*self)]),
(Divide, vec![Some(*self), Some(*self)]),
(Negate, vec![Some(*self)]),
],
ConstantType::U8 | ConstantType::U16 | ConstantType::U32 | ConstantType::U64 => vec![
(Plus, vec![Some(*self), Some(*self)]),
(Minus, vec![Some(*self), Some(*self)]),
(Times, vec![Some(*self), Some(*self)]),
(Divide, vec![Some(*self), Some(*self)]),
],
}
}
}
#[derive(Debug, Error, PartialEq)]