Skip to content

Commit

Permalink
implemented OCamlPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
0xGlitchbyte committed Feb 13, 2024
1 parent 371aa79 commit 8f03ddf
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions src/ocaml.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::File;
use std::io::Read;
use syn;
// use syn;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum OCamlError {
Expand Down Expand Up @@ -30,9 +30,18 @@ impl std::fmt::Display for OCamlError {

#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
pub enum OCamlType {
Pointer{ ty: Box<OCamlType>, is_const: bool },
Array{ ty: Box<OCamlType>, size: Option<usize> },
Function{ args: Vec<OCamlType>, ret: Box<OCamlType> },
Pointer {
ty: Box<OCamlType>,
is_const: bool,
},
Array {
ty: Box<OCamlType>,
size: Option<usize>,
},
Function {
args: Vec<OCamlType>,
ret: Box<OCamlType>,
},
Path(Vec<String>),
Tuple(Vec<OCamlType>),
Never,
Expand Down Expand Up @@ -68,11 +77,11 @@ impl OCaml {
syn::parse_file(src).map_err(|e| OCamlError::Parse(format!("'{}': {}", src, e)))?;

// Rust AST
// dbg!(&syntax);
dbg!(&syntax);

let syntax_items: Vec<Self> = syntax.items.iter().map(|item| item.into()).collect();

print!("{:#?}", syntax_items);
//print!("{:#?}", syntax_items);

Ok(Self::Statements(syntax_items))
}
Expand All @@ -96,6 +105,22 @@ impl OCamlExpr {
}
}

#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
pub struct OCamlExprCast {
expr: OCamlExpr,
ty: OCamlType,
}

#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
pub enum OCamlUpcast {
As(),
}

#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
pub enum OCamlPtr {
Element(Vec<String>),
}

#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
pub enum OCamlLiteral {
Integer {
Expand Down Expand Up @@ -198,7 +223,7 @@ impl From<SynType<'_>> for OCamlType {
ty: Box::new(SynType(&ty.elem).into()),
size: len_expr.eval_to_int(),
}
},
}
syn::Type::BareFn(ty) => {
let args: Vec<OCamlType> = ty
.inputs
Expand All @@ -207,7 +232,7 @@ impl From<SynType<'_>> for OCamlType {
.collect();
let ret = Box::new(SynReturnType(&ty.output).into());
OCamlType::Function { args, ret }
},
}
syn::Type::Verbatim(ty) => OCamlType::Verbatim(ty.to_string()),
syn::Type::Slice(ty) => OCamlType::Array {
ty: Box::new(SynType(&ty.elem).into()),
Expand All @@ -217,12 +242,9 @@ impl From<SynType<'_>> for OCamlType {
ty: Box::new(SynType(&ty.elem).into()),
is_const: ty.mutability.is_none(),
},
syn::Type::Tuple(ty) => OCamlType::Tuple(
ty.elems
.iter()
.map(|elem| SynType(elem).into())
.collect(),
),
syn::Type::Tuple(ty) => {
OCamlType::Tuple(ty.elems.iter().map(|elem| SynType(elem).into()).collect())
}
syn::Type::Never(_) => OCamlType::Never,
syn::Type::Paren(ty) => OCamlType::Paren(Box::new(SynType(&*ty.elem).into())),
_ => todo!("{:#?} is not implemented", value.0),
Expand Down Expand Up @@ -277,7 +299,7 @@ impl From<&syn::Lit> for OCamlLiteral {

pub(crate) fn size_from_rust_basic_number_type(ty: &str) -> (u8, Option<usize>) {
let type_width = &ty[1..];
if ty.starts_with('f'){
if ty.starts_with('f') {
return (b'f', type_width.parse().ok());
}

Expand Down Expand Up @@ -317,19 +339,16 @@ impl From<&syn::LitInt> for OCamlLiteral {
};
}

return if prefix == b'f' {
OCamlLiteral::Float {
digits,
width,
}
return if prefix == b'f' {
OCamlLiteral::Float { digits, width }
} else {
OCamlLiteral::Integer {
digits,
width,
is_signed: Some(is_signed),
is_native: false,
}
}
};
}
}

Expand All @@ -349,10 +368,7 @@ impl From<&syn::LitFloat> for OCamlLiteral {
let (prefix, width) = size_from_rust_basic_number_type(suffix);

if prefix == b'f' && width.is_some() {
return OCamlLiteral::Float {
digits,
width,
};
return OCamlLiteral::Float { digits, width };
}

unreachable!("Unknown suffix: {}", suffix)
Expand Down Expand Up @@ -416,6 +432,15 @@ impl From<&syn::ExprBinary> for OCamlBinary {
}
}

impl From<&syn::ExprCast> for OCamlExprCast {
fn from(value: &syn::ExprCast) -> Self {
Self {
expr: value.expr.as_ref().into(),
ty: SynType(value.ty.as_ref()).into(),
}
}
}

#[cfg(test)]
mod tests {
use std::vec;
Expand Down

0 comments on commit 8f03ddf

Please sign in to comment.