diff options
-rw-r--r-- | users/tazjin/rlox/src/interpreter.rs | 82 | ||||
-rw-r--r-- | users/tazjin/rlox/src/interpreter/tests.rs | 29 | ||||
-rw-r--r-- | users/tazjin/rlox/src/main.rs | 9 | ||||
-rw-r--r-- | users/tazjin/rlox/src/parser.rs | 178 | ||||
-rw-r--r-- | users/tazjin/rlox/src/scanner.rs | 10 |
5 files changed, 150 insertions, 158 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index f04d767dc5a9..5fb5283bc791 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -16,12 +16,12 @@ mod tests; // Representation of all callables, including builtins & user-defined // functions. #[derive(Clone, Debug)] -pub enum Callable<'a> { +pub enum Callable { Builtin(&'static dyn builtins::Builtin), - Function(Rc<parser::Function<'a>>), + Function(Rc<parser::Function>), } -impl<'a> Callable<'a> { +impl Callable { fn arity(&self) -> usize { match self { Callable::Builtin(builtin) => builtin.arity(), @@ -29,12 +29,12 @@ impl<'a> Callable<'a> { } } - fn call(&self, lox: &mut Interpreter<'a>, args: Vec<Value<'a>>) -> Result<Value<'a>, Error> { + fn call(&self, lox: &mut Interpreter, args: Vec<Value>) -> Result<Value, Error> { match self { Callable::Builtin(builtin) => builtin.call(args), Callable::Function(func) => { - let mut fn_env: Environment<'a> = Default::default(); + let mut fn_env: Environment = Default::default(); for (param, value) in func.params.iter().zip(args.into_iter()) { fn_env.define(param, value)?; @@ -48,12 +48,12 @@ impl<'a> Callable<'a> { // Representation of an in-language value. #[derive(Clone, Debug)] -pub enum Value<'a> { +pub enum Value { Literal(Literal), - Callable(Callable<'a>), + Callable(Callable), } -impl<'a> PartialEq for Value<'a> { +impl PartialEq for Value { fn eq(&self, other: &Self) -> bool { match (self, other) { (Value::Literal(lhs), Value::Literal(rhs)) => lhs == rhs, @@ -63,13 +63,13 @@ impl<'a> PartialEq for Value<'a> { } } -impl<'a> From<Literal> for Value<'a> { - fn from(lit: Literal) -> Value<'a> { +impl From<Literal> for Value { + fn from(lit: Literal) -> Value { Value::Literal(lit) } } -impl<'a> Value<'a> { +impl Value { fn expect_literal(self) -> Result<Literal, Error> { match self { Value::Literal(lit) => Ok(lit), @@ -79,19 +79,19 @@ impl<'a> Value<'a> { } #[derive(Debug, Default)] -struct Environment<'a> { - enclosing: Option<Rc<RwLock<Environment<'a>>>>, - values: HashMap<String, Value<'a>>, +struct Environment { + enclosing: Option<Rc<RwLock<Environment>>>, + values: HashMap<String, Value>, } -impl<'a> Environment<'a> { - fn define(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { +impl Environment { + fn define(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { let ident = identifier_str(name)?; self.values.insert(ident.into(), value); Ok(()) } - fn get(&self, name: &parser::Variable) -> Result<Value<'a>, Error> { + fn get(&self, name: &parser::Variable) -> Result<Value, Error> { let ident = identifier_str(&name.0)?; self.values @@ -110,7 +110,7 @@ impl<'a> Environment<'a> { }) } - fn assign(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { + fn assign(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { let ident = identifier_str(name)?; match self.values.get_mut(ident) { @@ -132,7 +132,7 @@ impl<'a> Environment<'a> { } } -fn identifier_str<'a>(name: &'a scanner::Token) -> Result<&'a str, Error> { +fn identifier_str(name: &scanner::Token) -> Result<&str, Error> { if let TokenKind::Identifier(ident) = &name.kind { Ok(ident) } else { @@ -144,11 +144,11 @@ fn identifier_str<'a>(name: &'a scanner::Token) -> Result<&'a str, Error> { } #[derive(Debug)] -pub struct Interpreter<'a> { - env: Rc<RwLock<Environment<'a>>>, +pub struct Interpreter { + env: Rc<RwLock<Environment>>, } -impl<'a> Interpreter<'a> { +impl Interpreter { /// Create a new interpreter and configure the initial global /// variable set. pub fn create() -> Self { @@ -168,28 +168,28 @@ impl<'a> Interpreter<'a> { } // Environment modification helpers - fn define_var(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { + fn define_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { self.env .write() .expect("environment lock is poisoned") .define(name, value) } - fn assign_var(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { + fn assign_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { self.env .write() .expect("environment lock is poisoned") .assign(name, value) } - fn get_var(&mut self, var: &parser::Variable) -> Result<Value<'a>, Error> { + fn get_var(&mut self, var: &parser::Variable) -> Result<Value, Error> { self.env .read() .expect("environment lock is poisoned") .get(var) } - fn set_enclosing(&mut self, parent: Rc<RwLock<Environment<'a>>>) { + fn set_enclosing(&mut self, parent: Rc<RwLock<Environment>>) { self.env .write() .expect("environment lock is poisoned") @@ -197,7 +197,7 @@ impl<'a> Interpreter<'a> { } // Interpreter itself - pub fn interpret(&mut self, program: &Block<'a>) -> Result<Value<'a>, Error> { + pub fn interpret(&mut self, program: &Block) -> Result<Value, Error> { let mut value = Value::Literal(Literal::Nil); for stmt in program { @@ -207,7 +207,7 @@ impl<'a> Interpreter<'a> { Ok(value) } - fn interpret_stmt(&mut self, stmt: &Statement<'a>) -> Result<Value<'a>, Error> { + fn interpret_stmt(&mut self, stmt: &Statement) -> Result<Value, Error> { let value = match stmt { Statement::Expr(expr) => self.eval(expr)?, Statement::Print(expr) => { @@ -226,7 +226,7 @@ impl<'a> Interpreter<'a> { Ok(value) } - fn interpret_var(&mut self, var: &parser::Var<'a>) -> Result<Value<'a>, Error> { + fn interpret_var(&mut self, var: &parser::Var) -> Result<Value, Error> { let init = var.initialiser.as_ref().ok_or_else(|| Error { line: var.name.line, kind: ErrorKind::InternalError("missing variable initialiser".into()), @@ -238,9 +238,9 @@ impl<'a> Interpreter<'a> { fn interpret_block( &mut self, - env: Rc<RwLock<Environment<'a>>>, - block: &parser::Block<'a>, - ) -> Result<Value<'a>, Error> { + env: Rc<RwLock<Environment>>, + block: &parser::Block, + ) -> Result<Value, Error> { // Initialise a new environment and point it at the parent // (this is a bit tedious because we need to wrap it in and // out of the Rc). @@ -257,7 +257,7 @@ impl<'a> Interpreter<'a> { return result; } - fn interpret_if(&mut self, if_stmt: &parser::If<'a>) -> Result<Value<'a>, Error> { + fn interpret_if(&mut self, if_stmt: &parser::If) -> Result<Value, Error> { let condition = self.eval(&if_stmt.condition)?; if eval_truthy(&condition) { @@ -269,7 +269,7 @@ impl<'a> Interpreter<'a> { } } - fn interpret_while(&mut self, stmt: &parser::While<'a>) -> Result<Value<'a>, Error> { + fn interpret_while(&mut self, stmt: &parser::While) -> Result<Value, Error> { let mut value = Value::Literal(Literal::Nil); while eval_truthy(&self.eval(&stmt.condition)?) { value = self.interpret_stmt(&stmt.body)?; @@ -278,14 +278,14 @@ impl<'a> Interpreter<'a> { Ok(value) } - fn interpret_function(&mut self, stmt: Rc<parser::Function<'a>>) -> Result<Value<'a>, Error> { + fn interpret_function(&mut self, stmt: Rc<parser::Function>) -> Result<Value, Error> { let name = stmt.name.clone(); let value = Value::Callable(Callable::Function(stmt)); self.define_var(&name, value.clone())?; Ok(value) } - fn eval(&mut self, expr: &Expr<'a>) -> Result<Value<'a>, Error> { + fn eval(&mut self, expr: &Expr) -> Result<Value, Error> { match expr { Expr::Assign(assign) => self.eval_assign(assign), Expr::Literal(lit) => Ok(lit.clone().into()), @@ -298,7 +298,7 @@ impl<'a> Interpreter<'a> { } } - fn eval_unary(&mut self, expr: &parser::Unary<'a>) -> Result<Value<'a>, Error> { + fn eval_unary(&mut self, expr: &parser::Unary) -> Result<Value, Error> { let right = self.eval(&*expr.right)?; match (&expr.operator.kind, right) { @@ -317,7 +317,7 @@ impl<'a> Interpreter<'a> { } } - fn eval_binary(&mut self, expr: &parser::Binary<'a>) -> Result<Value<'a>, Error> { + fn eval_binary(&mut self, expr: &parser::Binary) -> Result<Value, Error> { let left = self.eval(&*expr.left)?.expect_literal()?; let right = self.eval(&*expr.right)?.expect_literal()?; @@ -361,13 +361,13 @@ impl<'a> Interpreter<'a> { Ok(result.into()) } - fn eval_assign(&mut self, assign: &parser::Assign<'a>) -> Result<Value<'a>, Error> { + fn eval_assign(&mut self, assign: &parser::Assign) -> Result<Value, Error> { let value = self.eval(&assign.value)?; self.assign_var(&assign.name, value.clone())?; Ok(value) } - fn eval_logical(&mut self, logical: &parser::Logical<'a>) -> Result<Value<'a>, Error> { + fn eval_logical(&mut self, logical: &parser::Logical) -> Result<Value, Error> { let left = eval_truthy(&self.eval(&logical.left)?); let right = eval_truthy(&self.eval(&logical.right)?); @@ -381,7 +381,7 @@ impl<'a> Interpreter<'a> { } } - fn eval_call(&mut self, call: &parser::Call<'a>) -> Result<Value<'a>, Error> { + fn eval_call(&mut self, call: &parser::Call) -> Result<Value, Error> { let callable = match self.eval(&call.callee)? { Value::Callable(c) => c, Value::Literal(v) => { diff --git a/users/tazjin/rlox/src/interpreter/tests.rs b/users/tazjin/rlox/src/interpreter/tests.rs index bf2cf61b0a76..5bc9f0a0a475 100644 --- a/users/tazjin/rlox/src/interpreter/tests.rs +++ b/users/tazjin/rlox/src/interpreter/tests.rs @@ -1,11 +1,8 @@ use super::*; -fn code(code: &str) -> Vec<char> { - code.chars().collect() -} - /// Evaluate a code snippet, returning a value. -fn parse_eval<'a>(chars: &'a [char]) -> Value<'a> { +fn parse_eval(code: &str) -> Value { + let chars: Vec<char> = code.chars().collect(); let tokens = scanner::scan(&chars).expect("could not scan code"); let program = parser::parse(tokens).expect("could not parse code"); Interpreter::create() @@ -15,7 +12,7 @@ fn parse_eval<'a>(chars: &'a [char]) -> Value<'a> { #[test] fn test_if() { - let code = code( + let result = parse_eval( r#" if (42 > 23) "pass"; @@ -24,15 +21,12 @@ else "#, ); - assert_eq!( - Value::Literal(Literal::String("pass".into())), - parse_eval(&code) - ); + assert_eq!(Value::Literal(Literal::String("pass".into())), result,); } #[test] fn test_scope() { - let code = code( + let result = parse_eval( r#" var result = ""; @@ -54,26 +48,23 @@ var c = "global c"; assert_eq!( Value::Literal(Literal::String("inner a, outer b, global c".into())), - parse_eval(&code), + result, ); } #[test] fn test_binary_operators() { - assert_eq!( - Value::Literal(Literal::Number(42.0)), - parse_eval(&code("40 + 2;")) - ); + assert_eq!(Value::Literal(Literal::Number(42.0)), parse_eval("40 + 2;")); assert_eq!( Value::Literal(Literal::String("foobar".into())), - parse_eval(&code("\"foo\" + \"bar\";")) + parse_eval("\"foo\" + \"bar\";") ); } #[test] fn test_functions() { - let code = code( + let result = parse_eval( r#" fun add(a, b, c) { a + b + c; @@ -83,5 +74,5 @@ add(1, 2, 3); "#, ); - assert_eq!(Value::Literal(Literal::Number(6.0)), parse_eval(&code)); + assert_eq!(Value::Literal(Literal::Number(6.0)), result); } diff --git a/users/tazjin/rlox/src/main.rs b/users/tazjin/rlox/src/main.rs index 222eb4cd534f..8970349bfa69 100644 --- a/users/tazjin/rlox/src/main.rs +++ b/users/tazjin/rlox/src/main.rs @@ -25,12 +25,14 @@ fn main() { // Run Lox code from a file and print results to stdout fn run_file(file: &str) { let contents = fs::read_to_string(file).expect("failed to read the input file"); - run(&contents); + let mut lox = interpreter::Interpreter::create(); + run(&mut lox, &contents); } // Evaluate Lox code interactively in a shitty REPL. fn run_prompt() { let mut line = String::new(); + let mut lox = interpreter::Interpreter::create(); loop { print!("> "); @@ -38,14 +40,13 @@ fn run_prompt() { io::stdin() .read_line(&mut line) .expect("failed to read user input"); - run(&line); + run(&mut lox, &line); line.clear(); } } -fn run(code: &str) { +fn run(lox: &mut interpreter::Interpreter, code: &str) { let chars: Vec<char> = code.chars().collect(); - let mut lox = interpreter::Interpreter::create(); match scanner::scan(&chars) { Ok(tokens) => match parser::parse(tokens) { diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs index 78aaec105e32..ed5e670ecb79 100644 --- a/users/tazjin/rlox/src/parser.rs +++ b/users/tazjin/rlox/src/parser.rs @@ -12,27 +12,27 @@ use std::rc::Rc; // AST #[derive(Debug)] -pub struct Assign<'a> { - pub name: Token<'a>, - pub value: Box<Expr<'a>>, +pub struct Assign { + pub name: Token, + pub value: Box<Expr>, } #[derive(Debug)] -pub struct Binary<'a> { - pub left: Box<Expr<'a>>, - pub operator: Token<'a>, - pub right: Box<Expr<'a>>, +pub struct Binary { + pub left: Box<Expr>, + pub operator: Token, + pub right: Box<Expr>, } #[derive(Debug)] -pub struct Logical<'a> { - pub left: Box<Expr<'a>>, - pub operator: Token<'a>, - pub right: Box<Expr<'a>>, +pub struct Logical { + pub left: Box<Expr>, + pub operator: Token, + pub right: Box<Expr>, } #[derive(Debug)] -pub struct Grouping<'a>(pub Box<Expr<'a>>); +pub struct Grouping(pub Box<Expr>); #[derive(Debug, Clone, PartialEq)] pub enum Literal { @@ -43,73 +43,73 @@ pub enum Literal { } #[derive(Debug)] -pub struct Unary<'a> { - pub operator: Token<'a>, - pub right: Box<Expr<'a>>, +pub struct Unary { + pub operator: Token, + pub right: Box<Expr>, } #[derive(Debug)] -pub struct Call<'a> { - pub callee: Box<Expr<'a>>, - pub paren: Token<'a>, - pub args: Vec<Expr<'a>>, +pub struct Call { + pub callee: Box<Expr>, + pub paren: Token, + pub args: Vec<Expr>, } // Not to be confused with `Var`, which is for assignment. #[derive(Debug)] -pub struct Variable<'a>(pub Token<'a>); +pub struct Variable(pub Token); #[derive(Debug)] -pub enum Expr<'a> { - Assign(Assign<'a>), - Binary(Binary<'a>), - Grouping(Grouping<'a>), +pub enum Expr { + Assign(Assign), + Binary(Binary), + Grouping(Grouping), Literal(Literal), - Unary(Unary<'a>), - Call(Call<'a>), - Variable(Variable<'a>), - Logical(Logical<'a>), + Unary(Unary), + Call(Call), + Variable(Variable), + Logical(Logical), } // Variable assignment. Not to be confused with `Variable`, which is // for access. #[derive(Debug)] -pub struct Var<'a> { - pub name: Token<'a>, - pub initialiser: Option<Expr<'a>>, +pub struct Var { + pub name: Token, + pub initialiser: Option<Expr>, } #[derive(Debug)] -pub struct If<'a> { - pub condition: Expr<'a>, - pub then_branch: Box<Statement<'a>>, - pub else_branch: Option<Box<Statement<'a>>>, +pub struct If { + pub condition: Expr, + pub then_branch: Box<Statement>, + pub else_branch: Option<Box<Statement>>, } #[derive(Debug)] -pub struct While<'a> { - pub condition: Expr<'a>, - pub body: Box<Statement<'a>>, +pub struct While { + pub condition: Expr, + pub body: Box<Statement>, } -pub type Block<'a> = Vec<Statement<'a>>; +pub type Block = Vec<Statement>; #[derive(Debug)] -pub struct Function<'a> { - pub name: Token<'a>, - pub params: Vec<Token<'a>>, - pub body: Block<'a>, +pub struct Function { + pub name: Token, + pub params: Vec<Token>, + pub body: Block, } #[derive(Debug)] -pub enum Statement<'a> { - Expr(Expr<'a>), - Print(Expr<'a>), - Var(Var<'a>), - Block(Block<'a>), - If(If<'a>), - While(While<'a>), - Function(Rc<Function<'a>>), +pub enum Statement { + Expr(Expr), + Print(Expr), + Var(Var), + Block(Block), + If(If), + While(While), + Function(Rc<Function>), } // Parser @@ -162,18 +162,18 @@ primary → NUMBER | STRING | "true" | "false" | "nil" | "(" expression ")" ; */ -struct Parser<'a> { - tokens: Vec<Token<'a>>, +struct Parser { + tokens: Vec<Token>, current: usize, } -type ExprResult<'a> = Result<Expr<'a>, Error>; -type StmtResult<'a> = Result<Statement<'a>, Error>; +type ExprResult = Result<Expr, Error>; +type StmtResult = Result<Statement, Error>; -impl<'a> Parser<'a> { +impl Parser { // recursive-descent parser functions - fn declaration(&mut self) -> StmtResult<'a> { + fn declaration(&mut self) -> StmtResult { if self.match_token(&TokenKind::Fun) { return self.function(); } @@ -185,7 +185,7 @@ impl<'a> Parser<'a> { self.statement() } - fn function(&mut self) -> StmtResult<'a> { + fn function(&mut self) -> StmtResult { let name = self.identifier("Expected function name.")?; self.consume( @@ -229,7 +229,7 @@ impl<'a> Parser<'a> { }))) } - fn var_declaration(&mut self) -> StmtResult<'a> { + fn var_declaration(&mut self) -> StmtResult { // Since `TokenKind::Identifier` carries data, we can't use // `consume`. let mut var = Var { @@ -245,7 +245,7 @@ impl<'a> Parser<'a> { Ok(Statement::Var(var)) } - fn statement(&mut self) -> StmtResult<'a> { + fn statement(&mut self) -> StmtResult { if self.match_token(&TokenKind::Print) { self.print_statement() } else if self.match_token(&TokenKind::LeftBrace) { @@ -261,14 +261,14 @@ impl<'a> Parser<'a> { } } - fn print_statement(&mut self) -> StmtResult<'a> { + fn print_statement(&mut self) -> StmtResult { let expr = self.expression()?; self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?; Ok(Statement::Print(expr)) } - fn block_statement(&mut self) -> Result<Block<'a>, Error> { - let mut block: Block<'a> = vec![]; + fn block_statement(&mut self) -> Result<Block, Error> { + let mut block: Block = vec![]; while !self.check_token(&TokenKind::RightBrace) && !self.is_at_end() { block.push(self.declaration()?); @@ -279,7 +279,7 @@ impl<'a> Parser<'a> { Ok(block) } - fn if_statement(&mut self) -> StmtResult<'a> { + fn if_statement(&mut self) -> StmtResult { self.consume( &TokenKind::LeftParen, ErrorKind::ExpectedToken("Expected '(' after 'if'"), @@ -305,7 +305,7 @@ impl<'a> Parser<'a> { Ok(Statement::If(stmt)) } - fn while_statement(&mut self) -> StmtResult<'a> { + fn while_statement(&mut self) -> StmtResult { self.consume( &TokenKind::LeftParen, ErrorKind::ExpectedToken("Expected '(' after 'while'"), @@ -324,7 +324,7 @@ impl<'a> Parser<'a> { })) } - fn for_statement(&mut self) -> StmtResult<'a> { + fn for_statement(&mut self) -> StmtResult { // Parsing of clauses ... self.consume( &TokenKind::LeftParen, @@ -379,17 +379,17 @@ impl<'a> Parser<'a> { Ok(body) } - fn expr_statement(&mut self) -> StmtResult<'a> { + fn expr_statement(&mut self) -> StmtResult { let expr = self.expression()?; self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?; Ok(Statement::Expr(expr)) } - fn expression(&mut self) -> ExprResult<'a> { + fn expression(&mut self) -> ExprResult { self.assignment() } - fn assignment(&mut self) -> ExprResult<'a> { + fn assignment(&mut self) -> ExprResult { let expr = self.logic_or()?; if self.match_token(&TokenKind::Equal) { @@ -412,7 +412,7 @@ impl<'a> Parser<'a> { Ok(expr) } - fn logic_or(&mut self) -> ExprResult<'a> { + fn logic_or(&mut self) -> ExprResult { let mut expr = self.logic_and()?; while self.match_token(&TokenKind::Or) { @@ -426,7 +426,7 @@ impl<'a> Parser<'a> { Ok(expr) } - fn logic_and(&mut self) -> ExprResult<'a> { + fn logic_and(&mut self) -> ExprResult { let mut expr = self.equality()?; while self.match_token(&TokenKind::And) { @@ -440,14 +440,14 @@ impl<'a> Parser<'a> { Ok(expr) } - fn equality(&mut self) -> ExprResult<'a> { + fn equality(&mut self) -> ExprResult { self.binary_operator( &[TokenKind::BangEqual, TokenKind::EqualEqual], Self::comparison, ) } - fn comparison(&mut self) -> ExprResult<'a> { + fn comparison(&mut self) -> ExprResult { self.binary_operator( &[ TokenKind::Greater, @@ -459,15 +459,15 @@ impl<'a> Parser<'a> { ) } - fn term(&mut self) -> ExprResult<'a> { + fn term(&mut self) -> ExprResult { self.binary_operator(&[TokenKind::Minus, TokenKind::Plus], Self::factor) } - fn factor(&mut self) -> ExprResult<'a> { + fn factor(&mut self) -> ExprResult { self.binary_operator(&[TokenKind::Slash, TokenKind::Star], Self::unary) } - fn unary(&mut self) -> ExprResult<'a> { + fn unary(&mut self) -> ExprResult { if self.match_token(&TokenKind::Bang) || self.match_token(&TokenKind::Minus) { return Ok(Expr::Unary(Unary { operator: self.previous().clone(), @@ -478,7 +478,7 @@ impl<'a> Parser<'a> { return self.call(); } - fn call(&mut self) -> ExprResult<'a> { + fn call(&mut self) -> ExprResult { let mut expr = self.primary()?; loop { @@ -492,7 +492,7 @@ impl<'a> Parser<'a> { Ok(expr) } - fn finish_call(&mut self, callee: Expr<'a>) -> ExprResult<'a> { + fn finish_call(&mut self, callee: Expr) -> ExprResult { let mut args = vec![]; if !self.check_token(&TokenKind::RightParen) { @@ -517,7 +517,7 @@ impl<'a> Parser<'a> { })) } - fn primary(&mut self) -> ExprResult<'a> { + fn primary(&mut self) -> ExprResult { let next = self.advance(); let literal = match next.kind { TokenKind::True => Literal::Boolean(true), @@ -538,7 +538,7 @@ impl<'a> Parser<'a> { eprintln!("encountered {:?}", unexpected); return Err(Error { line: next.line, - kind: ErrorKind::ExpectedExpression(next.lexeme.into_iter().collect()), + kind: ErrorKind::ExpectedExpression(next.lexeme), }); } }; @@ -548,7 +548,7 @@ impl<'a> Parser<'a> { // internal helpers - fn identifier(&mut self, err: &'static str) -> Result<Token<'a>, Error> { + fn identifier(&mut self, err: &'static str) -> Result<Token, Error> { if let TokenKind::Identifier(_) = self.peek().kind { Ok(self.advance()) } else { @@ -570,7 +570,7 @@ impl<'a> Parser<'a> { } /// Return the next token and advance parser state. - fn advance(&mut self) -> Token<'a> { + fn advance(&mut self) -> Token { if !self.is_at_end() { self.current += 1; } @@ -587,15 +587,15 @@ impl<'a> Parser<'a> { self.peek().kind == *token } - fn peek(&self) -> &Token<'a> { + fn peek(&self) -> &Token { &self.tokens[self.current] } - fn previous(&self) -> &Token<'a> { + fn previous(&self) -> &Token { &self.tokens[self.current - 1] } - fn consume(&mut self, kind: &TokenKind, err: ErrorKind) -> Result<Token<'a>, Error> { + fn consume(&mut self, kind: &TokenKind, err: ErrorKind) -> Result<Token, Error> { if self.check_token(kind) { return Ok(self.advance()); } @@ -634,8 +634,8 @@ impl<'a> Parser<'a> { fn binary_operator( &mut self, oneof: &[TokenKind], - each: fn(&mut Parser<'a>) -> ExprResult<'a>, - ) -> ExprResult<'a> { + each: fn(&mut Parser) -> ExprResult, + ) -> ExprResult { let mut expr = each(self)?; while oneof.iter().any(|t| self.match_token(t)) { @@ -650,9 +650,9 @@ impl<'a> Parser<'a> { } } -pub fn parse<'a>(tokens: Vec<Token<'a>>) -> Result<Block<'a>, Vec<Error>> { +pub fn parse(tokens: Vec<Token>) -> Result<Block, Vec<Error>> { let mut parser = Parser { tokens, current: 0 }; - let mut program: Block<'a> = vec![]; + let mut program: Block = vec![]; let mut errors: Vec<Error> = vec![]; while !parser.is_at_end() { diff --git a/users/tazjin/rlox/src/scanner.rs b/users/tazjin/rlox/src/scanner.rs index fde43975316f..36c2d6a966ae 100644 --- a/users/tazjin/rlox/src/scanner.rs +++ b/users/tazjin/rlox/src/scanner.rs @@ -53,15 +53,15 @@ pub enum TokenKind { } #[derive(Clone, Debug)] -pub struct Token<'a> { +pub struct Token { pub kind: TokenKind, - pub lexeme: &'a [char], + pub lexeme: String, pub line: usize, } struct Scanner<'a> { source: &'a [char], - tokens: Vec<Token<'a>>, + tokens: Vec<Token>, errors: Vec<Error>, start: usize, // offset of first character in current lexeme current: usize, // current offset into source @@ -82,7 +82,7 @@ impl<'a> Scanner<'a> { let lexeme = &self.source[self.start..self.current]; self.tokens.push(Token { kind, - lexeme, + lexeme: lexeme.into_iter().collect(), line: self.line, }) } @@ -263,7 +263,7 @@ impl<'a> Scanner<'a> { } } -pub fn scan<'a>(input: &'a [char]) -> Result<Vec<Token<'a>>, Vec<Error>> { +pub fn scan<'a>(input: &'a [char]) -> Result<Vec<Token>, Vec<Error>> { let mut scanner = Scanner { source: &input, tokens: vec![], |