From 504913c719d8509382a14c377dba428fa176f2f3 Mon Sep 17 00:00:00 2001 From: Yehowshua Immanuel Date: Wed, 27 Jul 2022 09:35:44 -0400 Subject: [PATCH] still broken - but I need to save progress --- src/vcd/parse.rs | 73 ++++++++++++++++++++++++++++++++++++++++++------ src/vcd/types.rs | 10 +++++-- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/src/vcd/parse.rs b/src/vcd/parse.rs index d9a1d0b..45e3dc3 100644 --- a/src/vcd/parse.rs +++ b/src/vcd/parse.rs @@ -164,8 +164,9 @@ fn parse_events<'a>( } } else { + let (f, l )= (file!(), line!()); let value = BigInt::parse_bytes(value.as_bytes(), 10).ok_or( - format!("failed to parse {value} as BigInt at {cursor:?}").as_str())?; + format!("Error near {f}:{l}. Failed to parse {value} as BigInt at {cursor:?}").as_str())?; vcd.cursor = Value::BigInt(value); Ok(()) }; @@ -187,12 +188,14 @@ fn parse_events<'a>( // There could have been other parse errors... // Return Err below if there were. if e.kind() != &IntErrorKind::PosOverflow { - Err(format!("{e:?}"))?; + let (f, l )= (file!(), line!()); + Err(format!("Error near {f}:{l}. {e:?}"))?; } match value.parse::() { Ok(value) => { vcd.cursor = Value::u16(value); + println!("switching to u16"); Ok(()) } Err(e) => Err(e) @@ -215,12 +218,14 @@ fn parse_events<'a>( // There could have been other parse errors... // Return Err below if there were. if e.kind() != &IntErrorKind::PosOverflow { - Err(format!("{e:?}"))?; + let (f, l )= (file!(), line!()); + Err(format!("Error near {f}:{l}. {e:?}"))?; } match value.parse::() { Ok(value) => { vcd.cursor = Value::u32(value); + println!("switching to u32"); Ok(()) } Err(e) => Err(e) @@ -243,17 +248,66 @@ fn parse_events<'a>( // There could have been other parse errors... // Return Err below if there were. if e.kind() != &IntErrorKind::PosOverflow { - Err(format!("{e:?}"))?; + let (f, l )= (file!(), line!()); + Err(format!("Error near {f}:{l}. {e:?}"))?; } match value.parse::() { Ok(value) => { vcd.cursor = Value::u64(value); + println!("switching to u64"); Ok(()) } Err(e) => Err(e) } }; + + // If there was no parse error, we don't evaluate any more logic + // in this match arm and simply continue to the next iteration of + // the outer loop to evaluate the next word. + if parse_ok.is_ok() { + continue + } + + // Try parsing value as u64 since there was a previous + // PosOverflow error, and record if this parse attempt + // was Ok or Err in parse_ok. + let parse_ok = + { + let e = parse_ok.unwrap_err(); + // There could have been other parse errors... + // Return Err below if there were. + if e.kind() != &IntErrorKind::PosOverflow { + let (f, l )= (file!(), line!()); + Err(format!("Error near {f}:{l}. {e:?}"))?; + } + + match value.parse::() { + Ok(value) => { + vcd.cursor = Value::u64(value); + println!("switching to u64"); + Ok(()) + } + Err(e) => Err(e) + } + }; + + // Try parsing value as BigInt since there was a previous + // PosOverflow error and propagate any Result Errors. + let e = parse_ok.unwrap_err(); + // There could have been other parse errors... + // Return Err below if there were. + if e.kind() != &IntErrorKind::PosOverflow { + let (f, l )= (file!(), line!()); + Err(format!("Error near {f}:{l}. {e:?}"))?; + } + + let (f, l )= (file!(), line!()); + let value = BigInt::parse_bytes(value.as_bytes(), 10).ok_or( + format!("Error near {f}:{l}. Failed to parse {value} as BigInt at {cursor:?}").as_str())?; + vcd.cursor = Value::BigInt(value); + println!("switching to BigInt"); + } "0" => { // lokup signal idx @@ -278,14 +332,14 @@ fn parse_events<'a>( // after handling potential indirection, go ahead and update the timeline // of the signal signal_idx references - let signal = vcd.all_signals.get_mut(0usize).unwrap(); - // let signal = vcd.all_signals.get_mut(signal_idx).unwrap(); + let signal = vcd.all_signals.get_mut(signal_idx).unwrap(); match signal { Signal::Data {name, sig_type, num_bits, self_idx, timeline, scope_parent} => { - let value = 0.to_bigint().unwrap(); - let pair = (vcd.cursor.clone(), Value::u8(0)); - timeline.push(pair); + // let pair = (0.to_bigint(), Value::u8(0)); + let pair = (Value::u8(0), Value::u8(0)); + let t = 0u32.to_be_bytes(); + // timeline.push(pair); Ok(()) } Signal::Alias {..} => { @@ -358,6 +412,7 @@ pub fn parse_vcd(file : File) -> Result { let mut vcd = VCD{ metadata : header, cursor : Value::u8(0), + timeline : vec![], all_signals: vec![], all_scopes : vec![], scope_roots: vec![], diff --git a/src/vcd/types.rs b/src/vcd/types.rs index bb9bc2a..127d343 100644 --- a/src/vcd/types.rs +++ b/src/vcd/types.rs @@ -21,6 +21,9 @@ pub(super) struct Scope_Idx(pub(super) usize); #[derive(Debug, Copy, Clone)] pub(super) struct Signal_Idx(pub(super) usize); +#[derive(Debug, Copy, Clone)] +pub(super) struct TimelineIdx(pub(super) usize); + #[derive(Debug)] pub(super) enum Sig_Type {Integer, Parameter, Real, Reg, Str, Wire, Tri1, Time} @@ -42,6 +45,8 @@ pub(super) enum Value { BigInt(BigInt), } +pub type BigNum = Vec; + #[derive(Debug)] pub(super) enum Sig_Value { Numeric(u64), @@ -55,7 +60,7 @@ pub(super) enum Signal{ num_bits : Option, // TODO : may be able to remove self_idx self_idx : Signal_Idx, - timeline : Vec<(Value, Value)>, + timeline : Vec<(TimelineIdx, BigNum)>, scope_parent : Scope_Idx}, Alias{ name : String, @@ -77,7 +82,8 @@ pub(super) struct Scope { #[derive(Debug)] pub struct VCD { pub(super) metadata : Metadata, - pub (super) cursor : Value, + pub(super) cursor : Value, + pub(super) timeline : Vec, pub(super) all_signals : Vec, pub(super) all_scopes : Vec, pub(super) scope_roots : Vec}