Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
froth committed Dec 12, 2023
1 parent 415447b commit 0533c83
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ pub struct Args {
#[arg()]
pub file: Option<String>,
}

4 changes: 2 additions & 2 deletions src/error_reporter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[derive(Default)]
pub struct ErrorReporter{
pub struct ErrorReporter {
had_error: bool,
}

Expand All @@ -19,4 +19,4 @@ impl ErrorReporter {
pub fn had_error(&self) -> bool {
self.had_error
}
}
}
8 changes: 2 additions & 6 deletions src/lox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ use std::{

use crate::{error_reporter::ErrorReporter, scanner::Scanner};


#[derive(Default)]
pub struct Lox {
error_reporter: ErrorReporter
error_reporter: ErrorReporter,
}



impl Lox {
fn run(&mut self, source: String) {
let mut scanner = Scanner::new(source, & mut self.error_reporter);
let mut scanner = Scanner::new(source, &mut self.error_reporter);
let tokens = scanner.scan_tokens();
tokens.iter().for_each(|x| println!("{:?}", x))
}
Expand All @@ -40,5 +37,4 @@ impl Lox {
}
}
}

}
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@

use clap::Parser;
use lox::Lox;

use args::Args;

mod args;
mod error_reporter;
mod lox;
mod scanner;
mod error_reporter;
mod token;

fn main() {
let args = Args::parse();
let mut lox = Lox::default();
match args.file {
Some(file) => if !lox.run_file(file) {
std::process::exit(65)
},
Some(file) => {
if !lox.run_file(file) {
std::process::exit(65)
}
}
None => lox.run_prompt(),
}
}

7 changes: 5 additions & 2 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'a> Scanner<'a> {
start: 0,
current: 0,
line: 1,
error_reporter: e
error_reporter: e,
}
}

Expand Down Expand Up @@ -191,7 +191,10 @@ impl<'a> Scanner<'a> {
}

fn read_identifier(&mut self) -> ScanResult {
while self.peek().is_some_and(|c| c.is_ascii_alphanumeric() || c == '_') {
while self
.peek()
.is_some_and(|c| c.is_ascii_alphanumeric() || c == '_')
{
self.current += 1;
}

Expand Down
54 changes: 47 additions & 7 deletions src/token.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
#[derive(Debug, Clone)]
pub enum TokenType {
//single character tokens.
LeftParen, RightParen, LeftBrace, RightBrace, Comma, Dot, Minus, Plus, Semicolon, Slash, Star,
LeftParen,
RightParen,
LeftBrace,
RightBrace,
Comma,
Dot,
Minus,
Plus,
Semicolon,
Slash,
Star,

// One or two character tokens.
Bang, BangEqual, Equal, EqualEqual, Greater, GreaterEqual, Less, LessEqual,
Bang,
BangEqual,
Equal,
EqualEqual,
Greater,
GreaterEqual,
Less,
LessEqual,

// Literals
Identifier, String(String), Number(f32),
Identifier,
String(String),
Number(f32),

// Keywords.
And, Class, Else, False, Fun, For, If, Nil, Or, Print, Return, Super, This, True, Var, While,
And,
Class,
Else,
False,
Fun,
For,
If,
Nil,
Or,
Print,
Return,
Super,
This,
True,
Var,
While,
// Eof
Eof
Eof,
}

#[derive(Debug, Clone)]
Expand All @@ -23,5 +57,11 @@ pub struct Token {
}

impl Token {
pub fn new(token_type: TokenType, lexeme: String, line: usize) -> Self { Self { token_type, lexeme, line } }
}
pub fn new(token_type: TokenType, lexeme: String, line: usize) -> Self {
Self {
token_type,
lexeme,
line,
}
}
}

0 comments on commit 0533c83

Please sign in to comment.