From 3edbca51d5429416fd9b0cc8716e2db58fdb5b20 Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 18 Jun 2021 20:28:53 +0200 Subject: [PATCH] Implemented read in Parser --- src/ast.rs | 1 + src/interpret.rs | 1 + src/lexer.rs | 4 ++++ src/parser.rs | 12 ++++++++++++ 4 files changed, 18 insertions(+) diff --git a/src/ast.rs b/src/ast.rs index 835e679..f84c47a 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -73,6 +73,7 @@ pub enum StmtKind { args: Vec, }, Print(Expr), + Read(Iden), Melo(Iden), Rlyeh, Rickroll, diff --git a/src/interpret.rs b/src/interpret.rs index 43d48d9..29466ec 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -280,6 +280,7 @@ impl ExecEnv { .write_all(include_str!("rickroll").as_bytes()) .expect("Failed to write to stdout"); } + StmtKind::Read(_) => todo!(), } Ok(HaltStatus::Finished) diff --git a/src/lexer.rs b/src/lexer.rs index 3f686c0..7632386 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -86,6 +86,10 @@ pub enum Token { #[token("print")] Print, + /// Read input into preceding variable + #[token("read")] + Read, + /// Ban the following variable from ever being used again #[token("melo")] Melo, diff --git a/src/parser.rs b/src/parser.rs index 893f44b..90d86bc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -318,6 +318,18 @@ impl<'source> Parser<'source> { }; } } + + // Read input + Token::Read => { + if let Some(Expr { + kind: ExprKind::Variable(iden), + span, + }) = buf + { + break self.semi_terminated(StmtKind::Read(Iden::new(iden, span)))?; + } + } + t => buf = Some(self.parse_expr(t, &mut buf)?), } };