Parser
This commit adds a parser, complete with tests. The parser implements an iterator which returns Datum. It wraps around a Lexer and uses the Lexer's iterator interfact to consume lexemes. It may return an error which may wrap around a LexError or a fully lexed lexeme. In the implementation of the Parser bugs were found in the lexer package. This resulted in the lexing tests being extended as well as several small logic updates. The number package has had slight tweaks to make number representations less cumbersome. Finally, the Datum display logic in the sexpr package has also been updated. Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
parent
a48fc52fab
commit
86f905ba1d
5 changed files with 632 additions and 29 deletions
|
|
@ -38,7 +38,7 @@ pub const E_SCIENTIFIC_MULTI_E: &str = "scientific notation implies only a s
|
|||
pub const E_SCIENTIFIC_OPERAND: &str = "couldnt parse 32 bit float operand";
|
||||
pub const E_SCIENTIFIC_POWER: &str = "couldnt parse integer power";
|
||||
|
||||
trait Numeric: Copy + Clone + Debug + FromStr + Into<String> {
|
||||
pub trait Numeric: Copy + Clone + Debug + FromStr + Into<String> {
|
||||
fn is_exact(&self) -> bool;
|
||||
fn make_inexact(&self) -> Float;
|
||||
fn make_exact(&self) -> Fraction;
|
||||
|
|
@ -46,7 +46,7 @@ trait Numeric: Copy + Clone + Debug + FromStr + Into<String> {
|
|||
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct ScientificNotation (f32, isize);
|
||||
pub struct ScientificNotation (pub f32, pub isize);
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub enum SymbolicNumber {
|
||||
|
|
@ -57,10 +57,10 @@ pub enum SymbolicNumber {
|
|||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct Fraction (isize, isize);
|
||||
pub struct Fraction (pub isize, pub isize);
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct Float (f64);
|
||||
pub struct Float (pub f64);
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum Number {
|
||||
|
|
@ -481,7 +481,7 @@ impl FromStr for Fraction {
|
|||
|
||||
impl Into<String> for Fraction {
|
||||
fn into(self) -> String {
|
||||
format!("#e{}/{}", self.0, self.1)
|
||||
format!("{}/{}", self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -508,7 +508,11 @@ impl FromStr for Float {
|
|||
|
||||
impl Into<String> for Float {
|
||||
fn into(self) -> String {
|
||||
format!("#i{}", self.0)
|
||||
if self.is_exact() {
|
||||
format!("{}", self.0)
|
||||
} else {
|
||||
format!("#i{}", self.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -560,7 +564,7 @@ impl FromStr for ScientificNotation {
|
|||
|
||||
impl Into<String> for ScientificNotation {
|
||||
fn into(self) -> String {
|
||||
format!("#{}e{}", self.0, self.1)
|
||||
format!("{}e{}", self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue