From 588b3b5b2c258a3183943c0186edf330c765ec48 Mon Sep 17 00:00:00 2001 From: Daniel M Date: Thu, 3 Feb 2022 17:38:25 +0100 Subject: [PATCH] Autoformat --- src/interpreter.rs | 27 ++++++++++++++++------- src/lexer.rs | 2 +- src/main.rs | 12 +++++----- src/parser.rs | 55 +++++++++++++++++++++++++++------------------- 4 files changed, 59 insertions(+), 37 deletions(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index ddee443..f02e93e 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1,6 +1,10 @@ use std::{collections::HashMap, fmt::Display, rc::Rc}; -use crate::{ast::{Expression, BinOpType, UnOpType, Ast, Statement, If}, parser::parse, lexer::lex}; +use crate::{ + ast::{Ast, BinOpType, Expression, If, Statement, UnOpType}, + lexer::lex, + parser::parse, +}; #[derive(Debug, PartialEq, Eq, Clone)] pub enum Value { @@ -61,7 +65,11 @@ impl Interpreter { print!("{}", result); } - Statement::If(If {condition, body_true, body_false}) => { + Statement::If(If { + condition, + body_true, + body_false, + }) => { if matches!(self.resolve_expr(condition), Value::I64(0)) { self.run(body_false); } else { @@ -69,7 +77,6 @@ impl Interpreter { } } } - } } @@ -116,9 +123,9 @@ impl Interpreter { } return rhs; } - _ => () + _ => (), } - + let lhs = self.resolve_expr(lhs); match (lhs, rhs) { @@ -158,11 +165,10 @@ impl Display for Value { } } - #[cfg(test)] mod test { use super::{Interpreter, Value}; - use crate::ast::{Expression, BinOpType}; + use crate::ast::{BinOpType, Expression}; #[test] fn test_interpreter_expr() { @@ -173,7 +179,12 @@ mod test { Expression::BinOp( BinOpType::Add, Expression::I64(1).into(), - Expression::BinOp(BinOpType::Mul, Expression::I64(2).into(), Expression::I64(3).into()).into(), + Expression::BinOp( + BinOpType::Mul, + Expression::I64(2).into(), + Expression::I64(3).into(), + ) + .into(), ) .into(), Expression::I64(4).into(), diff --git a/src/lexer.rs b/src/lexer.rs index 4bca8e0..d145c04 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -126,7 +126,7 @@ impl<'a> Lexer<'a> { } /// Lex multiple characters as a number until encountering a non numeric digit. This includes - /// the first character + /// the first character fn lex_number(&mut self, first_char: char) -> Result { // String representation of the integer value let mut sval = String::from(first_char); diff --git a/src/main.rs b/src/main.rs index 12b382e..0efe66a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,11 @@ -use std::{env::args, fs, io::{stdout, Write, stdin}}; +use std::{ + env::args, + fs, + io::{stdin, stdout, Write}, +}; use nek_lang::interpreter::Interpreter; - #[derive(Debug, Default)] struct CliConfig { print_tokens: bool, @@ -12,7 +15,6 @@ struct CliConfig { } fn main() { - let mut conf = CliConfig::default(); // Go through all commandline arguments except the first (filename) @@ -42,14 +44,12 @@ fn main() { code.clear(); stdin().read_line(&mut code).unwrap(); - + if code.trim() == "exit" { break; } interpreter.run_str(&code, conf.print_tokens, conf.print_ast); } - } - } diff --git a/src/parser.rs b/src/parser.rs index a5f5641..f0f56b4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,7 +1,7 @@ use std::iter::Peekable; -use crate::token::Token; use crate::ast::*; +use crate::token::Token; /// Parse the given tokens into an abstract syntax tree pub fn parse, A: IntoIterator>(tokens: A) -> Ast { @@ -20,7 +20,7 @@ impl> Parser { Self { tokens } } - /// Parse tokens into an abstract syntax tree. This will continuously parse statements until + /// Parse tokens into an abstract syntax tree. This will continuously parse statements until /// encountering end-of-file or a block end '}' . fn parse(&mut self) -> Ast { let mut prog = Vec::new(); @@ -30,23 +30,17 @@ impl> Parser { Token::Semicolon => { self.next(); } - Token::EoF => break, - Token::RBraces => { - break; - } + Token::EoF | Token::RBraces => break, // By default try to lex a statement - _ => { - prog.push(self.parse_stmt()) - } + _ => prog.push(self.parse_stmt()), } } Ast { prog } - } - /// Parse a single statement from the tokens. + /// Parse a single statement from the tokens. fn parse_stmt(&mut self) -> Statement { match self.peek() { Token::Loop => Statement::Loop(self.parse_loop()), @@ -108,13 +102,17 @@ impl> Parser { } body_false = self.parse(); - + if !matches!(self.next(), Token::RBraces) { panic!("Error lexing if: Expected '}}'") } } - If { condition, body_true, body_false } + If { + condition, + body_true, + body_false, + } } /// Parse a loop statement from the tokens @@ -141,17 +139,20 @@ impl> Parser { } body = self.parse(); - }, + } - _ => panic!("Error lexing loop: Expected ';' or '{{'") + _ => panic!("Error lexing loop: Expected ';' or '{{'"), } if !matches!(self.next(), Token::RBraces) { panic!("Error lexing loop: Expected '}}'") } - Loop { condition, advancement, body } - + Loop { + condition, + advancement, + body, + } } /// Parse a single expression from the tokens @@ -222,7 +223,7 @@ impl> Parser { let operand = self.parse_primary(); Expression::UnOp(UnOpType::BNot, operand.into()) } - + // Unary logical not Token::LNot => { let operand = self.parse_primary(); @@ -246,8 +247,11 @@ impl> Parser { #[cfg(test)] mod tests { - use super::{parse, Expression, BinOpType}; - use crate::{token::Token, parser::{Statement, Ast}}; + use super::{parse, BinOpType, Expression}; + use crate::{ + parser::{Ast, Statement}, + token::Token, + }; #[test] fn test_parser() { @@ -269,13 +273,20 @@ mod tests { Expression::BinOp( BinOpType::Add, Expression::I64(1).into(), - Expression::BinOp(BinOpType::Mul, Expression::I64(2).into(), Expression::I64(3).into()).into(), + Expression::BinOp( + BinOpType::Mul, + Expression::I64(2).into(), + Expression::I64(3).into(), + ) + .into(), ) .into(), Expression::I64(4).into(), )); - let expected = Ast { prog: vec![expected] }; + let expected = Ast { + prog: vec![expected], + }; let actual = parse(tokens); assert_eq!(expected, actual);