diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2021-10-24 00:58:49 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2021-10-24 00:58:49 -0700 |
commit | 5f501b3654d6b36e850044ba492dd7bc7ee56438 (patch) | |
tree | a3122d81196c4ef7d01edb015f0fe466e846fd57 /src/sexpr.rs | |
parent | 2ad36af41d237f3fb4be4d8ed0c78cf227612ea3 (diff) | |
download | casual-5f501b3654d6b36e850044ba492dd7bc7ee56438.tar.gz casual-5f501b3654d6b36e850044ba492dd7bc7ee56438.zip |
progress
Diffstat (limited to 'src/sexpr.rs')
-rw-r--r-- | src/sexpr.rs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/sexpr.rs b/src/sexpr.rs index ea07bf9..d761192 100644 --- a/src/sexpr.rs +++ b/src/sexpr.rs @@ -10,13 +10,14 @@ */ use std::str; +use std::fmt; use std::io::Read; use std::fs::File; use std::path::Path; //////////// Types and Constants -#[derive(Clone, PartialEq)] +#[derive(Clone, PartialEq, Debug)] pub enum SExpr { SNull, SBoolean(bool), @@ -238,3 +239,21 @@ pub fn sexpr_parse_file(fpath: &Path) -> Result<(), String> { let (_ast_list, _) = sexpr_parse(&tokens, 0)?; Ok(()) } + +impl SExpr { + pub fn from_str(raw: &str) -> Result<SExpr, String> { + let tokens = sexpr_tokenize(&raw)?; + let (mut ast, _) = sexpr_parse(&tokens, 0)?; + Ok(ast.remove(0)) + } +} + +impl fmt::Display for SExpr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match sexpr_repr(self) { + Ok(repr) => write!(f, "{}", repr), + Err(_) => Err(std::fmt::Error), + } + } +} + |