feat: start on encoding constants with Flat Encoder
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
use flat::en::{Encode, Encoder};
|
||||
|
||||
use crate::builtins::DefaultFunction;
|
||||
|
||||
const TERM_TAG_WIDTH: u32 = 4;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Program {
|
||||
pub version: String,
|
||||
@@ -32,6 +36,22 @@ pub enum Term {
|
||||
Builtin(DefaultFunction),
|
||||
}
|
||||
|
||||
pub fn encode_term_tag(tag: u8, e: &mut Encoder) -> Result<(), String> {
|
||||
safe_encode_bits(TERM_TAG_WIDTH, tag, e)
|
||||
}
|
||||
|
||||
pub fn safe_encode_bits(num_bits: u32, byte: u8, e: &mut Encoder) -> Result<(), String> {
|
||||
if 2_u8.pow(num_bits) < byte {
|
||||
Err(format!(
|
||||
"Overflow detected, cannot fit {} in {} bits.",
|
||||
byte, num_bits
|
||||
))
|
||||
} else {
|
||||
e.bits(num_bits as i64, byte);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Constant {
|
||||
// TODO: figure out the right size for this
|
||||
@@ -48,3 +68,43 @@ pub enum Constant {
|
||||
// tag: 5
|
||||
Bool(bool),
|
||||
}
|
||||
|
||||
impl Encode for Program {
|
||||
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||
self.version.encode(e)?;
|
||||
self.term.encode(e)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for Term {
|
||||
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||
match self {
|
||||
Term::Constant(constant) => {
|
||||
encode_term_tag(4, e)?;
|
||||
constant.encode(e)?;
|
||||
}
|
||||
rest => {
|
||||
todo!("Implement: {:?}", rest)
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for &Constant {
|
||||
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||
match self {
|
||||
Constant::Integer(_) => todo!(),
|
||||
Constant::ByteString(bytes) => bytes.encode(e)?,
|
||||
Constant::String(s) => s.encode(e)?,
|
||||
Constant::Char(c) => c.encode(e)?,
|
||||
Constant::Unit => todo!(),
|
||||
Constant::Bool(b) => b.encode(e)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user