From 987118be8b432333261249b30f7d42eeee4fb668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Fri, 30 Dec 2022 16:47:30 +0100 Subject: [PATCH] Token-related definitions --- src/main.rs | 14 ++++++++++--- src/tokens.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/tokens.rs diff --git a/src/main.rs b/src/main.rs index 98cb9fa..637f4ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,16 @@ -use std::{env, fs, io::{self, Write}, process::ExitCode}; +mod tokens; + +use std::{ + env, fs, + io::{self, Write}, + process::ExitCode, +}; /// Error handler. Can be used to print error messages; will also retain the /// current error status. #[derive(Default, Debug)] struct ErrorHandler { - had_error: bool + had_error: bool, } impl ErrorHandler { @@ -45,7 +51,9 @@ fn run_prompt() { loop { print!("slox> "); stdout.flush().unwrap(); - let n_read = stdin.read_line(&mut buffer).expect("Failed to read from stdin"); + let n_read = stdin + .read_line(&mut buffer) + .expect("Failed to read from stdin"); let _ = match n_read { 0 => return, _ => run(buffer.clone()), diff --git a/src/tokens.rs b/src/tokens.rs new file mode 100644 index 0000000..f9a91ea --- /dev/null +++ b/src/tokens.rs @@ -0,0 +1,55 @@ +/// The type of a token. May also contain its litteral value. +#[derive(Clone, PartialEq, Debug)] +pub enum TokenType { + LeftParen, + RightParen, + LeftBrace, + RightBrace, + Comma, + Dot, + Minus, + Plus, + Semicolon, + Slash, + Star, + + Bang, + BangEqual, + Equal, + EqualEqual, + Greater, + GreaterEqual, + Less, + LessEqual, + + Identifier(String), + String(String), + Number(f64), + + And, + Class, + Else, + False, + Fun, + For, + If, + Nil, + Or, + Print, + Return, + Super, + This, + True, + Var, + While, + + EOF, +} + +/// Full information about a token. +#[derive(Clone, Debug)] +pub struct Token { + pub token_type: TokenType, + pub lexeme: String, + pub line: usize, +}