From 71c5738a6ef9a2f93cbf55309f6b5b2b13b72eb3 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 4 Oct 2021 23:00:18 +0200 Subject: [PATCH] Renamed `Iden` to `Ident` --- ablescript/src/ast.rs | 32 ++++++++--------- ablescript/src/interpret.rs | 70 ++++++++++++++++++------------------- ablescript/src/lexer.rs | 4 +-- ablescript/src/parser.rs | 48 ++++++++++++------------- 4 files changed, 77 insertions(+), 77 deletions(-) diff --git a/ablescript/src/ast.rs b/ablescript/src/ast.rs index 61fe5230..8e654ed2 100644 --- a/ablescript/src/ast.rs +++ b/ablescript/src/ast.rs @@ -15,26 +15,26 @@ use crate::variables::Value; type Span = std::ops::Range; #[derive(Debug, Clone)] -pub struct Iden { - pub iden: String, +pub struct Ident { + pub ident: String, pub span: Span, } -impl Iden { - pub fn new(iden: String, span: Span) -> Self { - Self { iden, span } +impl Ident { + pub fn new(ident: String, span: Span) -> Self { + Self { ident, span } } } -impl PartialEq for Iden { +impl PartialEq for Ident { fn eq(&self, other: &Self) -> bool { - self.iden == other.iden + self.ident == other.ident } } -impl Hash for Iden { +impl Hash for Ident { fn hash(&self, state: &mut H) { - self.iden.hash(state) + self.ident.hash(state) } } @@ -76,21 +76,21 @@ pub enum StmtKind { HopBack, Var { - iden: Iden, + ident: Ident, init: Option, }, Assign { - iden: Iden, + ident: Ident, value: Expr, }, Functio { - iden: Iden, - params: Vec, + ident: Ident, + params: Vec, body: Block, }, BfFunctio { - iden: Iden, + ident: Ident, tape_len: Option, code: Vec, }, @@ -99,8 +99,8 @@ pub enum StmtKind { args: Vec, }, Print(Expr), - Read(Iden), - Melo(Iden), + Read(Ident), + Melo(Ident), Rlyeh, Rickroll, } diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index 516b681f..ab58a7e2 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -19,7 +19,7 @@ use std::{ use rand::random; use crate::{ - ast::{Expr, ExprKind, Iden, Stmt, StmtKind}, + ast::{Expr, ExprKind, Ident, Stmt, StmtKind}, base_55, consts::ablescript_consts, error::{Error, ErrorKind}, @@ -175,9 +175,9 @@ impl ExecEnv { } // TODO: not too happy with constructing an artificial - // Iden here. - Variable(name) => self.get_var(&Iden { - iden: name.to_owned(), + // Ident here. + Variable(name) => self.get_var(&Ident { + ident: name.to_owned(), span: expr.span.clone(), })?, }) @@ -189,30 +189,30 @@ impl ExecEnv { StmtKind::Print(expr) => { println!("{}", self.eval_expr(expr)?); } - StmtKind::Var { iden, init } => { + StmtKind::Var { ident, init } => { let init = match init { Some(e) => self.eval_expr(e)?, None => Value::Nul, }; - self.decl_var(&iden.iden, init); + self.decl_var(&ident.ident, init); } - StmtKind::Functio { iden, params, body } => { + StmtKind::Functio { ident, params, body } => { self.decl_var( - &iden.iden, + &ident.ident, Value::Functio(Functio::AbleFunctio { - params: params.iter().map(|iden| iden.iden.to_owned()).collect(), + params: params.iter().map(|ident| ident.ident.to_owned()).collect(), body: body.block.to_owned(), }), ); } StmtKind::BfFunctio { - iden, + ident, tape_len, code, } => { self.decl_var( - &iden.iden, + &ident.ident, Value::Functio(Functio::BfFunctio { instructions: code.to_owned(), tape_len: tape_len @@ -240,9 +240,9 @@ impl ExecEnv { HaltStatus::Hopback(_) => continue, } }, - StmtKind::Assign { iden, value } => { + StmtKind::Assign { ident, value } => { let value = self.eval_expr(value)?; - self.get_var_mut(iden)?.value.replace(value); + self.get_var_mut(ident)?.value.replace(value); } StmtKind::Break => { return Ok(HaltStatus::Break(stmt.span.clone())); @@ -250,8 +250,8 @@ impl ExecEnv { StmtKind::HopBack => { return Ok(HaltStatus::Hopback(stmt.span.clone())); } - StmtKind::Melo(iden) => { - self.get_var_mut(iden)?.melo = true; + StmtKind::Melo(ident) => { + self.get_var_mut(ident)?.melo = true; } StmtKind::Rlyeh => { // Maybe print a creepy error message or something @@ -263,14 +263,14 @@ impl ExecEnv { .write_all(include_str!("rickroll").as_bytes()) .expect("Failed to write to stdout"); } - StmtKind::Read(iden) => { + StmtKind::Read(ident) => { let mut value = 0; for _ in 0..READ_BITS { value <<= 1; value += self.get_bit()? as i32; } - self.get_var_mut(iden)?.value.replace(Value::Int(value)); + self.get_var_mut(ident)?.value.replace(Value::Int(value)); } } @@ -287,8 +287,8 @@ impl ExecEnv { .iter() .map(|arg| { if let ExprKind::Variable(name) = &arg.kind { - self.get_var_rc(&Iden { - iden: name.to_owned(), + self.get_var_rc(&Ident { + ident: name.to_owned(), span: arg.span.clone(), }) } else { @@ -380,9 +380,9 @@ impl ExecEnv { /// Get the value of a variable. Throw an error if the variable is /// inaccessible or banned. - fn get_var(&self, name: &Iden) -> Result { + fn get_var(&self, name: &Ident) -> Result { // One-letter names are reserved as base55 numbers. - let mut chars = name.iden.chars(); + let mut chars = name.ident.chars(); if let (Some(first), None) = (chars.next(), chars.next()) { return Ok(Value::Int(base_55::char2num(first))); } @@ -393,20 +393,20 @@ impl ExecEnv { .stack .iter() .rev() - .find_map(|scope| scope.variables.get(&name.iden)) + .find_map(|scope| scope.variables.get(&name.ident)) { Some(var) => { if !var.melo { Ok(var.value.borrow().clone()) } else { Err(Error { - kind: ErrorKind::MeloVariable(name.iden.to_owned()), + kind: ErrorKind::MeloVariable(name.ident.to_owned()), span: name.span.clone(), }) } } None => Err(Error { - kind: ErrorKind::UnknownVariable(name.iden.to_owned()), + kind: ErrorKind::UnknownVariable(name.ident.to_owned()), span: name.span.clone(), }), } @@ -414,27 +414,27 @@ impl ExecEnv { /// Get a mutable reference to a variable. Throw an error if the /// variable is inaccessible or banned. - fn get_var_mut(&mut self, name: &Iden) -> Result<&mut Variable, Error> { + fn get_var_mut(&mut self, name: &Ident) -> Result<&mut Variable, Error> { // This function has a lot of duplicated code with `get_var`, // which I feel like is a bad sign... match self .stack .iter_mut() .rev() - .find_map(|scope| scope.variables.get_mut(&name.iden)) + .find_map(|scope| scope.variables.get_mut(&name.ident)) { Some(var) => { if !var.melo { Ok(var) } else { Err(Error { - kind: ErrorKind::MeloVariable(name.iden.to_owned()), + kind: ErrorKind::MeloVariable(name.ident.to_owned()), span: name.span.clone(), }) } } None => Err(Error { - kind: ErrorKind::UnknownVariable(name.iden.to_owned()), + kind: ErrorKind::UnknownVariable(name.ident.to_owned()), span: name.span.clone(), }), } @@ -442,7 +442,7 @@ impl ExecEnv { /// Get an Rc'd pointer to the value of a variable. Throw an error /// if the variable is inaccessible or banned. - fn get_var_rc(&mut self, name: &Iden) -> Result>, Error> { + fn get_var_rc(&mut self, name: &Ident) -> Result>, Error> { Ok(self.get_var_mut(name)?.value.clone()) } @@ -584,8 +584,8 @@ mod tests { // Declaring and reading from a variable. eval(&mut env, "var foo = 32; var bar = foo + 1;").unwrap(); assert_eq!( - env.get_var(&Iden { - iden: "bar".to_owned(), + env.get_var(&Ident { + ident: "bar".to_owned(), span: 1..1, }) .unwrap(), @@ -595,8 +595,8 @@ mod tests { // Assigning an existing variable. eval(&mut env, "foo = \"hi\";").unwrap(); assert_eq!( - env.get_var(&Iden { - iden: "foo".to_owned(), + env.get_var(&Ident { + ident: "foo".to_owned(), span: 1..1, }) .unwrap(), @@ -621,8 +621,8 @@ mod tests { .unwrap(); assert_eq!( - env.get_var(&Iden { - iden: "foo".to_owned(), + env.get_var(&Ident { + ident: "foo".to_owned(), span: 1..1, }) .unwrap(), diff --git a/ablescript/src/lexer.rs b/ablescript/src/lexer.rs index e75414d9..24ee265b 100644 --- a/ablescript/src/lexer.rs +++ b/ablescript/src/lexer.rs @@ -137,7 +137,7 @@ pub enum Token { Integer(i32), /// A C-complaint identifier - #[regex(r"[a-zA-Z_][a-zA-Z_0-9]*", get_iden)] + #[regex(r"[a-zA-Z_][a-zA-Z_0-9]*", get_ident)] Identifier(String), #[regex(r"owo .*")] @@ -172,7 +172,7 @@ fn get_abool(lexer: &mut Lexer) -> Option { } } -fn get_iden(lexer: &mut Lexer) -> String { +fn get_ident(lexer: &mut Lexer) -> String { lexer.slice().to_owned() } diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index e0debe22..f07039d0 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -131,13 +131,13 @@ impl<'source> Parser<'source> { } /// Get an Identifier - fn get_iden(&mut self) -> Result { + fn get_ident(&mut self) -> Result { match self.checked_next()? { - Token::Identifier(iden) => Ok(Iden { - iden: if self.tdark { - iden.replace("lang", "script") + Token::Identifier(ident) => Ok(Ident { + ident: if self.tdark { + ident.replace("lang", "script") } else { - iden + ident }, span: self.lexer.span(), }), @@ -356,12 +356,12 @@ impl<'source> Parser<'source> { // Variable Assignment Token::Equal => { if let Some(Expr { - kind: ExprKind::Variable(iden), + kind: ExprKind::Variable(ident), span, }) = buf { break StmtKind::Assign { - iden: Iden::new(iden, span), + ident: Ident::new(ident, span), value: self.expr_flow(Token::Semicolon)?, }; } @@ -370,11 +370,11 @@ impl<'source> Parser<'source> { // Read input Token::Read => { if let Some(Expr { - kind: ExprKind::Variable(iden), + kind: ExprKind::Variable(ident), span, }) = buf { - break self.semi_terminated(StmtKind::Read(Iden::new(iden, span)))?; + break self.semi_terminated(StmtKind::Read(Ident::new(ident, span)))?; } } @@ -400,9 +400,9 @@ impl<'source> Parser<'source> { /// Parse functio flow /// - /// functio $iden (a, b, c) { ... } + /// functio $ident (a, b, c) { ... } fn functio_flow(&mut self) -> Result { - let iden = self.get_iden()?; + let ident = self.get_ident()?; self.require(Token::LeftParen)?; @@ -411,7 +411,7 @@ impl<'source> Parser<'source> { match self.checked_next()? { Token::RightParen => break, Token::Identifier(i) => { - params.push(Iden::new(i, self.lexer.span())); + params.push(Ident::new(i, self.lexer.span())); // Require comma (next) or right paren (end) after identifier match self.checked_next()? { @@ -431,14 +431,14 @@ impl<'source> Parser<'source> { let body = self.get_block()?; - Ok(StmtKind::Functio { iden, params, body }) + Ok(StmtKind::Functio { ident, params, body }) } /// Parse BF function declaration /// - /// `bff $iden ([tapelen]) { ... }` + /// `bff $ident ([tapelen]) { ... }` fn bff_flow(&mut self) -> Result { - let iden = self.get_iden()?; + let ident = self.get_ident()?; let tape_len = match self.checked_next()? { Token::LeftParen => { @@ -472,7 +472,7 @@ impl<'source> Parser<'source> { } Ok(StmtKind::BfFunctio { - iden, + ident, tape_len, code, }) @@ -513,20 +513,20 @@ impl<'source> Parser<'source> { /// Parse variable declaration fn var_flow(&mut self) -> Result { - let iden = self.get_iden()?; + let ident = self.get_ident()?; let init = match self.checked_next()? { Token::Equal => Some(self.expr_flow(Token::Semicolon)?), Token::Semicolon => None, t => return Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())), }; - Ok(StmtKind::Var { iden, init }) + Ok(StmtKind::Var { ident, init }) } /// Parse Melo flow fn melo_flow(&mut self) -> Result { - let iden = self.get_iden()?; - self.semi_terminated(StmtKind::Melo(iden)) + let ident = self.get_ident()?; + self.semi_terminated(StmtKind::Melo(ident)) } /// Parse loop flow @@ -593,8 +593,8 @@ mod tests { let code = r#"var a = 42;"#; let expected = &[Stmt { kind: StmtKind::Var { - iden: Iden { - iden: "a".to_owned(), + ident: Ident { + ident: "a".to_owned(), span: 4..5, }, init: Some(Expr { @@ -650,8 +650,8 @@ mod tests { let code = r#"T-Dark { var lang = "lang" + lang; }"#; let expected = &[Stmt { kind: StmtKind::Var { - iden: Iden { - iden: "script".to_owned(), + ident: Ident { + ident: "script".to_owned(), span: 13..17, }, init: Some(Expr {