Renamed Iden to Ident

This commit is contained in:
Erin 2021-10-04 23:00:18 +02:00 committed by ondra05
parent e8349bf0d9
commit 71c5738a6e
4 changed files with 77 additions and 77 deletions

View file

@ -15,26 +15,26 @@ use crate::variables::Value;
type Span = std::ops::Range<usize>; type Span = std::ops::Range<usize>;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Iden { pub struct Ident {
pub iden: String, pub ident: String,
pub span: Span, pub span: Span,
} }
impl Iden { impl Ident {
pub fn new(iden: String, span: Span) -> Self { pub fn new(ident: String, span: Span) -> Self {
Self { iden, span } Self { ident, span }
} }
} }
impl PartialEq for Iden { impl PartialEq for Ident {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.iden == other.iden self.ident == other.ident
} }
} }
impl Hash for Iden { impl Hash for Ident {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.iden.hash(state) self.ident.hash(state)
} }
} }
@ -76,21 +76,21 @@ pub enum StmtKind {
HopBack, HopBack,
Var { Var {
iden: Iden, ident: Ident,
init: Option<Expr>, init: Option<Expr>,
}, },
Assign { Assign {
iden: Iden, ident: Ident,
value: Expr, value: Expr,
}, },
Functio { Functio {
iden: Iden, ident: Ident,
params: Vec<Iden>, params: Vec<Ident>,
body: Block, body: Block,
}, },
BfFunctio { BfFunctio {
iden: Iden, ident: Ident,
tape_len: Option<Expr>, tape_len: Option<Expr>,
code: Vec<u8>, code: Vec<u8>,
}, },
@ -99,8 +99,8 @@ pub enum StmtKind {
args: Vec<Expr>, args: Vec<Expr>,
}, },
Print(Expr), Print(Expr),
Read(Iden), Read(Ident),
Melo(Iden), Melo(Ident),
Rlyeh, Rlyeh,
Rickroll, Rickroll,
} }

View file

@ -19,7 +19,7 @@ use std::{
use rand::random; use rand::random;
use crate::{ use crate::{
ast::{Expr, ExprKind, Iden, Stmt, StmtKind}, ast::{Expr, ExprKind, Ident, Stmt, StmtKind},
base_55, base_55,
consts::ablescript_consts, consts::ablescript_consts,
error::{Error, ErrorKind}, error::{Error, ErrorKind},
@ -175,9 +175,9 @@ impl ExecEnv {
} }
// TODO: not too happy with constructing an artificial // TODO: not too happy with constructing an artificial
// Iden here. // Ident here.
Variable(name) => self.get_var(&Iden { Variable(name) => self.get_var(&Ident {
iden: name.to_owned(), ident: name.to_owned(),
span: expr.span.clone(), span: expr.span.clone(),
})?, })?,
}) })
@ -189,30 +189,30 @@ impl ExecEnv {
StmtKind::Print(expr) => { StmtKind::Print(expr) => {
println!("{}", self.eval_expr(expr)?); println!("{}", self.eval_expr(expr)?);
} }
StmtKind::Var { iden, init } => { StmtKind::Var { ident, init } => {
let init = match init { let init = match init {
Some(e) => self.eval_expr(e)?, Some(e) => self.eval_expr(e)?,
None => Value::Nul, 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( self.decl_var(
&iden.iden, &ident.ident,
Value::Functio(Functio::AbleFunctio { 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(), body: body.block.to_owned(),
}), }),
); );
} }
StmtKind::BfFunctio { StmtKind::BfFunctio {
iden, ident,
tape_len, tape_len,
code, code,
} => { } => {
self.decl_var( self.decl_var(
&iden.iden, &ident.ident,
Value::Functio(Functio::BfFunctio { Value::Functio(Functio::BfFunctio {
instructions: code.to_owned(), instructions: code.to_owned(),
tape_len: tape_len tape_len: tape_len
@ -240,9 +240,9 @@ impl ExecEnv {
HaltStatus::Hopback(_) => continue, HaltStatus::Hopback(_) => continue,
} }
}, },
StmtKind::Assign { iden, value } => { StmtKind::Assign { ident, value } => {
let value = self.eval_expr(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 => { StmtKind::Break => {
return Ok(HaltStatus::Break(stmt.span.clone())); return Ok(HaltStatus::Break(stmt.span.clone()));
@ -250,8 +250,8 @@ impl ExecEnv {
StmtKind::HopBack => { StmtKind::HopBack => {
return Ok(HaltStatus::Hopback(stmt.span.clone())); return Ok(HaltStatus::Hopback(stmt.span.clone()));
} }
StmtKind::Melo(iden) => { StmtKind::Melo(ident) => {
self.get_var_mut(iden)?.melo = true; self.get_var_mut(ident)?.melo = true;
} }
StmtKind::Rlyeh => { StmtKind::Rlyeh => {
// Maybe print a creepy error message or something // Maybe print a creepy error message or something
@ -263,14 +263,14 @@ impl ExecEnv {
.write_all(include_str!("rickroll").as_bytes()) .write_all(include_str!("rickroll").as_bytes())
.expect("Failed to write to stdout"); .expect("Failed to write to stdout");
} }
StmtKind::Read(iden) => { StmtKind::Read(ident) => {
let mut value = 0; let mut value = 0;
for _ in 0..READ_BITS { for _ in 0..READ_BITS {
value <<= 1; value <<= 1;
value += self.get_bit()? as i32; 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() .iter()
.map(|arg| { .map(|arg| {
if let ExprKind::Variable(name) = &arg.kind { if let ExprKind::Variable(name) = &arg.kind {
self.get_var_rc(&Iden { self.get_var_rc(&Ident {
iden: name.to_owned(), ident: name.to_owned(),
span: arg.span.clone(), span: arg.span.clone(),
}) })
} else { } else {
@ -380,9 +380,9 @@ impl ExecEnv {
/// Get the value of a variable. Throw an error if the variable is /// Get the value of a variable. Throw an error if the variable is
/// inaccessible or banned. /// inaccessible or banned.
fn get_var(&self, name: &Iden) -> Result<Value, Error> { fn get_var(&self, name: &Ident) -> Result<Value, Error> {
// One-letter names are reserved as base55 numbers. // 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()) { if let (Some(first), None) = (chars.next(), chars.next()) {
return Ok(Value::Int(base_55::char2num(first))); return Ok(Value::Int(base_55::char2num(first)));
} }
@ -393,20 +393,20 @@ impl ExecEnv {
.stack .stack
.iter() .iter()
.rev() .rev()
.find_map(|scope| scope.variables.get(&name.iden)) .find_map(|scope| scope.variables.get(&name.ident))
{ {
Some(var) => { Some(var) => {
if !var.melo { if !var.melo {
Ok(var.value.borrow().clone()) Ok(var.value.borrow().clone())
} else { } else {
Err(Error { Err(Error {
kind: ErrorKind::MeloVariable(name.iden.to_owned()), kind: ErrorKind::MeloVariable(name.ident.to_owned()),
span: name.span.clone(), span: name.span.clone(),
}) })
} }
} }
None => Err(Error { None => Err(Error {
kind: ErrorKind::UnknownVariable(name.iden.to_owned()), kind: ErrorKind::UnknownVariable(name.ident.to_owned()),
span: name.span.clone(), span: name.span.clone(),
}), }),
} }
@ -414,27 +414,27 @@ impl ExecEnv {
/// Get a mutable reference to a variable. Throw an error if the /// Get a mutable reference to a variable. Throw an error if the
/// variable is inaccessible or banned. /// 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`, // This function has a lot of duplicated code with `get_var`,
// which I feel like is a bad sign... // which I feel like is a bad sign...
match self match self
.stack .stack
.iter_mut() .iter_mut()
.rev() .rev()
.find_map(|scope| scope.variables.get_mut(&name.iden)) .find_map(|scope| scope.variables.get_mut(&name.ident))
{ {
Some(var) => { Some(var) => {
if !var.melo { if !var.melo {
Ok(var) Ok(var)
} else { } else {
Err(Error { Err(Error {
kind: ErrorKind::MeloVariable(name.iden.to_owned()), kind: ErrorKind::MeloVariable(name.ident.to_owned()),
span: name.span.clone(), span: name.span.clone(),
}) })
} }
} }
None => Err(Error { None => Err(Error {
kind: ErrorKind::UnknownVariable(name.iden.to_owned()), kind: ErrorKind::UnknownVariable(name.ident.to_owned()),
span: name.span.clone(), span: name.span.clone(),
}), }),
} }
@ -442,7 +442,7 @@ impl ExecEnv {
/// Get an Rc'd pointer to the value of a variable. Throw an error /// Get an Rc'd pointer to the value of a variable. Throw an error
/// if the variable is inaccessible or banned. /// if the variable is inaccessible or banned.
fn get_var_rc(&mut self, name: &Iden) -> Result<Rc<RefCell<Value>>, Error> { fn get_var_rc(&mut self, name: &Ident) -> Result<Rc<RefCell<Value>>, Error> {
Ok(self.get_var_mut(name)?.value.clone()) Ok(self.get_var_mut(name)?.value.clone())
} }
@ -584,8 +584,8 @@ mod tests {
// Declaring and reading from a variable. // Declaring and reading from a variable.
eval(&mut env, "var foo = 32; var bar = foo + 1;").unwrap(); eval(&mut env, "var foo = 32; var bar = foo + 1;").unwrap();
assert_eq!( assert_eq!(
env.get_var(&Iden { env.get_var(&Ident {
iden: "bar".to_owned(), ident: "bar".to_owned(),
span: 1..1, span: 1..1,
}) })
.unwrap(), .unwrap(),
@ -595,8 +595,8 @@ mod tests {
// Assigning an existing variable. // Assigning an existing variable.
eval(&mut env, "foo = \"hi\";").unwrap(); eval(&mut env, "foo = \"hi\";").unwrap();
assert_eq!( assert_eq!(
env.get_var(&Iden { env.get_var(&Ident {
iden: "foo".to_owned(), ident: "foo".to_owned(),
span: 1..1, span: 1..1,
}) })
.unwrap(), .unwrap(),
@ -621,8 +621,8 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
env.get_var(&Iden { env.get_var(&Ident {
iden: "foo".to_owned(), ident: "foo".to_owned(),
span: 1..1, span: 1..1,
}) })
.unwrap(), .unwrap(),

View file

@ -137,7 +137,7 @@ pub enum Token {
Integer(i32), Integer(i32),
/// A C-complaint identifier /// 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), Identifier(String),
#[regex(r"owo .*")] #[regex(r"owo .*")]
@ -172,7 +172,7 @@ fn get_abool(lexer: &mut Lexer<Token>) -> Option<Abool> {
} }
} }
fn get_iden(lexer: &mut Lexer<Token>) -> String { fn get_ident(lexer: &mut Lexer<Token>) -> String {
lexer.slice().to_owned() lexer.slice().to_owned()
} }

View file

@ -131,13 +131,13 @@ impl<'source> Parser<'source> {
} }
/// Get an Identifier /// Get an Identifier
fn get_iden(&mut self) -> Result<Iden, Error> { fn get_ident(&mut self) -> Result<Ident, Error> {
match self.checked_next()? { match self.checked_next()? {
Token::Identifier(iden) => Ok(Iden { Token::Identifier(ident) => Ok(Ident {
iden: if self.tdark { ident: if self.tdark {
iden.replace("lang", "script") ident.replace("lang", "script")
} else { } else {
iden ident
}, },
span: self.lexer.span(), span: self.lexer.span(),
}), }),
@ -356,12 +356,12 @@ impl<'source> Parser<'source> {
// Variable Assignment // Variable Assignment
Token::Equal => { Token::Equal => {
if let Some(Expr { if let Some(Expr {
kind: ExprKind::Variable(iden), kind: ExprKind::Variable(ident),
span, span,
}) = buf }) = buf
{ {
break StmtKind::Assign { break StmtKind::Assign {
iden: Iden::new(iden, span), ident: Ident::new(ident, span),
value: self.expr_flow(Token::Semicolon)?, value: self.expr_flow(Token::Semicolon)?,
}; };
} }
@ -370,11 +370,11 @@ impl<'source> Parser<'source> {
// Read input // Read input
Token::Read => { Token::Read => {
if let Some(Expr { if let Some(Expr {
kind: ExprKind::Variable(iden), kind: ExprKind::Variable(ident),
span, span,
}) = buf }) = 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 /// Parse functio flow
/// ///
/// functio $iden (a, b, c) { ... } /// functio $ident (a, b, c) { ... }
fn functio_flow(&mut self) -> Result<StmtKind, Error> { fn functio_flow(&mut self) -> Result<StmtKind, Error> {
let iden = self.get_iden()?; let ident = self.get_ident()?;
self.require(Token::LeftParen)?; self.require(Token::LeftParen)?;
@ -411,7 +411,7 @@ impl<'source> Parser<'source> {
match self.checked_next()? { match self.checked_next()? {
Token::RightParen => break, Token::RightParen => break,
Token::Identifier(i) => { 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 // Require comma (next) or right paren (end) after identifier
match self.checked_next()? { match self.checked_next()? {
@ -431,14 +431,14 @@ impl<'source> Parser<'source> {
let body = self.get_block()?; let body = self.get_block()?;
Ok(StmtKind::Functio { iden, params, body }) Ok(StmtKind::Functio { ident, params, body })
} }
/// Parse BF function declaration /// Parse BF function declaration
/// ///
/// `bff $iden ([tapelen]) { ... }` /// `bff $ident ([tapelen]) { ... }`
fn bff_flow(&mut self) -> Result<StmtKind, Error> { fn bff_flow(&mut self) -> Result<StmtKind, Error> {
let iden = self.get_iden()?; let ident = self.get_ident()?;
let tape_len = match self.checked_next()? { let tape_len = match self.checked_next()? {
Token::LeftParen => { Token::LeftParen => {
@ -472,7 +472,7 @@ impl<'source> Parser<'source> {
} }
Ok(StmtKind::BfFunctio { Ok(StmtKind::BfFunctio {
iden, ident,
tape_len, tape_len,
code, code,
}) })
@ -513,20 +513,20 @@ impl<'source> Parser<'source> {
/// Parse variable declaration /// Parse variable declaration
fn var_flow(&mut self) -> Result<StmtKind, Error> { fn var_flow(&mut self) -> Result<StmtKind, Error> {
let iden = self.get_iden()?; let ident = self.get_ident()?;
let init = match self.checked_next()? { let init = match self.checked_next()? {
Token::Equal => Some(self.expr_flow(Token::Semicolon)?), Token::Equal => Some(self.expr_flow(Token::Semicolon)?),
Token::Semicolon => None, Token::Semicolon => None,
t => return Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())), t => return Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),
}; };
Ok(StmtKind::Var { iden, init }) Ok(StmtKind::Var { ident, init })
} }
/// Parse Melo flow /// Parse Melo flow
fn melo_flow(&mut self) -> Result<StmtKind, Error> { fn melo_flow(&mut self) -> Result<StmtKind, Error> {
let iden = self.get_iden()?; let ident = self.get_ident()?;
self.semi_terminated(StmtKind::Melo(iden)) self.semi_terminated(StmtKind::Melo(ident))
} }
/// Parse loop flow /// Parse loop flow
@ -593,8 +593,8 @@ mod tests {
let code = r#"var a = 42;"#; let code = r#"var a = 42;"#;
let expected = &[Stmt { let expected = &[Stmt {
kind: StmtKind::Var { kind: StmtKind::Var {
iden: Iden { ident: Ident {
iden: "a".to_owned(), ident: "a".to_owned(),
span: 4..5, span: 4..5,
}, },
init: Some(Expr { init: Some(Expr {
@ -650,8 +650,8 @@ mod tests {
let code = r#"T-Dark { var lang = "lang" + lang; }"#; let code = r#"T-Dark { var lang = "lang" + lang; }"#;
let expected = &[Stmt { let expected = &[Stmt {
kind: StmtKind::Var { kind: StmtKind::Var {
iden: Iden { ident: Ident {
iden: "script".to_owned(), ident: "script".to_owned(),
span: 13..17, span: 13..17,
}, },
init: Some(Expr { init: Some(Expr {