From 48d9d1e2e16da1a7c28584cf0b83f022dd196bb5 Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 23 Oct 2021 21:53:21 +0200 Subject: [PATCH] Read in AST takes Assignable instead of Ident --- ablescript/src/ast.rs | 2 +- ablescript/src/interpret.rs | 11 +++++++++-- ablescript/src/parser.rs | 14 ++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/ablescript/src/ast.rs b/ablescript/src/ast.rs index fa304205..a5f19055 100644 --- a/ablescript/src/ast.rs +++ b/ablescript/src/ast.rs @@ -144,7 +144,7 @@ pub enum StmtKind { args: Vec, }, Print(Expr), - Read(Ident), + Read(Assignable), Melo(Ident), Rlyeh, Rickroll, diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index 1c7f0963..356e9f3c 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -280,14 +280,21 @@ impl ExecEnv { .write_all(include_str!("rickroll").as_bytes()) .expect("Failed to write to stdout"); } - StmtKind::Read(ident) => { + StmtKind::Read(assignable) => { let mut value = 0; for _ in 0..READ_BITS { value <<= 1; value += self.get_bit()? as i32; } - self.get_var_mut(ident)?.value.replace(Value::Int(value)); + match assignable.kind { + AssignableKind::Variable => { + self.get_var_mut(&assignable.ident)? + .value + .replace(Value::Int(value)); + } + AssignableKind::Index { .. } => todo!(), + } } } diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index 1aa4876f..a397287b 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -370,12 +370,14 @@ impl<'source> Parser<'source> { // Read input Token::Read => { - if let Some(Expr { - kind: ExprKind::Variable(ident), - span, - }) = buf - { - break self.semi_terminated(StmtKind::Read(Ident::new(ident, span)))?; + if let Some(Ok(assignable)) = buf.take().map(Assignable::from_expr) { + self.require(Token::Semicolon)?; + break StmtKind::Read(assignable); + } else { + return Err(Error::new( + ErrorKind::UnexpectedToken(Token::Read), + self.lexer.span(), + )); } }