Getty Ritter 2 years ago
parent
commit
018b46d3fe
4 changed files with 23 additions and 38 deletions
  1. 0 22
      src/ast.rs
  2. 20 13
      src/interp.rs
  3. 2 2
      src/lexer.rs
  4. 1 1
      src/main.rs

+ 0 - 22
src/ast.rs

@@ -54,25 +54,3 @@ pub enum Literal {
     Atom(String),
     Num(i64),
 }
-
-impl Literal {
-    pub fn from_str_literal(s: &str) -> Literal {
-        // strip the outer pieces from the string
-        let mut buf = String::new();
-        let mut src = s[1..s.len() - 1].chars().into_iter();
-        while let Some(c) = src.next() {
-            if c == '\\' {
-                match src.next() {
-                    Some('n') => buf.push('\n'),
-                    Some('t') => buf.push('\t'),
-                    Some('r') => buf.push('\r'),
-                    Some(c) => buf.push(c),
-                    None => panic!("bad escape"),
-                }
-            } else {
-                buf.push(c);
-            }
-        }
-        Literal::Str(buf)
-    }
-}

+ 20 - 13
src/interp.rs

@@ -30,29 +30,31 @@ impl Value {
         }
     }
 
-    fn with_str<U>(&self, f: impl Fn(&str) -> U) -> U {
+    fn with_str<U>(&self, f: impl FnOnce(&str) -> U) -> U {
         match self {
-            Value::Lit(Literal::Str(s)) => f(&s),
-            Value::Lit(Literal::Atom(s)) => f(&s),
+            Value::Lit(Literal::Str(s)) => f(s),
+            Value::Lit(Literal::Atom(s)) => f(s),
             Value::Lit(Literal::Num(n)) => f(&format!("{}", n)),
             Value::Tup(values) => {
                 let mut buf = String::new();
-                buf.push_str("<");
+                buf.push('<');
                 for (i, val) in values.iter().enumerate() {
                     if i > 0 {
                         buf.push_str(", ");
                     }
                     buf.push_str(&val.to_string());
                 }
-                buf.push_str(">");
+                buf.push('>');
                 f(&buf)
             }
             Value::Builtin(func) => f(&format!("#<builtin {}>", func.name)),
         }
     }
+}
 
-    fn to_string(&self) -> String {
-        self.with_str(|s| s.to_string())
+impl fmt::Display for Value {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        self.with_str(|s| write!(f, "{}", s))
     }
 }
 
@@ -132,6 +134,12 @@ pub struct State {
     rand: rand::rngs::ThreadRng,
 }
 
+impl Default for State {
+    fn default() -> State {
+        Self::new()
+    }
+}
+
 impl State {
     pub fn new() -> State {
         let mut s = State {
@@ -152,16 +160,14 @@ impl State {
                 possibilities.push(name.clone());
             }
         }
-        if at_beginning {
-            if "puts".starts_with(fragment) {
-                possibilities.push("puts ".to_owned());
-            }
+        if at_beginning && "puts".starts_with(fragment) {
+            possibilities.push("puts ".to_owned());
         }
         possibilities
     }
 
     pub fn execute(&mut self, stmt: &Stmt) -> Result<(), Error> {
-        Ok(match stmt {
+        match stmt {
             Stmt::Puts(expr) => {
                 let val = self.eval(expr)?;
                 println!("{}", val.to_string());
@@ -182,7 +188,8 @@ impl State {
                     .insert(name.to_string(), NamedItem::Expr(Expr::Chc(choices)));
             }
             _ => bail!("unimplemented"),
-        })
+        }
+        Ok(())
     }
 
     fn eval(&mut self, expr: &Expr) -> Result<Value, Error> {

+ 2 - 2
src/lexer.rs

@@ -2,13 +2,13 @@ use logos::{Lexer, Logos};
 
 fn parse_num<'a>(lex: &mut Lexer<'a, Token<'a>>) -> Option<i64> {
     let slice = lex.slice();
-    Some(slice.parse().ok()?)
+    slice.parse().ok()
 }
 
 fn parse_str<'a>(lex: &mut Lexer<'a, Token<'a>>) -> Option<String> {
     let mut buf = String::new();
     let s = lex.slice();
-    let mut src = s[1..s.len() - 1].chars().into_iter();
+    let mut src = s[1..s.len() - 1].chars();
     while let Some(c) = src.next() {
         if c == '\\' {
             match src.next() {

+ 1 - 1
src/main.rs

@@ -3,7 +3,7 @@ use matzo::interp::State;
 use matzo::lexer::tokens;
 
 fn run(src: &str) {
-    let lexed = tokens(&src);
+    let lexed = tokens(src);
     let stmts = StmtsParser::new().parse(lexed).unwrap();
     let mut state = State::new();
     for stmt in stmts {