2022-09-06 13:52:29 +00:00
|
|
|
use std::{fmt, rc::Rc};
|
|
|
|
|
|
|
|
use crate::Position;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Token {
|
|
|
|
pub ty: TokenType,
|
|
|
|
pub text: String,
|
|
|
|
pub pos: Position
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Token {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{:?} @ {},{}", self.ty, self.pos.line, self.pos.col)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum TokenType {
|
2022-09-07 22:10:43 +00:00
|
|
|
Int(i64), Float(f64), ImFloat(f64), String(Rc<str>), Char(char),
|
2022-09-06 13:52:29 +00:00
|
|
|
Ident(Rc<str>),
|
|
|
|
|
|
|
|
Plus, Minus, Star, Slash, Percent, DoubleSlash, Caret,
|
|
|
|
Bang, Amper, Pipe, DoubleAmper, DoublePipe,
|
|
|
|
|
|
|
|
Equal, PlusEqual, MinusEqual, StarEqual, SlashEqual, PercentEqual, DoubleSlashEqual, CaretEqual,
|
2022-09-09 20:08:40 +00:00
|
|
|
DoubleEqual, BangEqual, Greater, GreaterEqual, Less, LessEqual, Spaceship,
|
2022-09-06 13:52:29 +00:00
|
|
|
|
|
|
|
Arrow, PipeColon, PipePoint, PipeQuestion, PipeAmper,
|
|
|
|
|
2022-09-07 22:10:43 +00:00
|
|
|
Comma, Semicolon, Colon,
|
2022-09-06 13:52:29 +00:00
|
|
|
|
|
|
|
LParen, RParen, LBrack, RBrack, LBrace, RBrace,
|
|
|
|
|
2022-09-11 05:01:53 +00:00
|
|
|
True, False, Nil,
|
|
|
|
If, Elif, Else, For, While,
|
|
|
|
Let, Break, Continue, Return
|
2022-09-06 13:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenType {
|
|
|
|
pub fn get_op_type(&self) -> Option<OpType> {
|
|
|
|
match self {
|
|
|
|
Self::Plus | Self::Minus => Some(OpType::Additive),
|
|
|
|
|
|
|
|
Self::Star | Self::Slash | Self::DoubleSlash
|
|
|
|
| Self::Percent => Some(OpType::Multiplicative),
|
|
|
|
|
|
|
|
Self::Caret => Some(OpType::Exponential),
|
|
|
|
|
|
|
|
Self::PipeColon | Self::PipeAmper | Self::PipePoint
|
|
|
|
| Self::PipeQuestion => Some(OpType::Pipeline),
|
|
|
|
|
2022-09-09 20:08:40 +00:00
|
|
|
Self::Greater | Self::GreaterEqual | Self::Less | Self::LessEqual
|
|
|
|
| Self::DoubleEqual | Self::BangEqual | Self::Spaceship => Some(OpType::Comparison),
|
|
|
|
|
2022-09-06 13:52:29 +00:00
|
|
|
Self::Equal | Self::PlusEqual | Self::MinusEqual
|
|
|
|
| Self::StarEqual | Self::SlashEqual | Self::DoubleSlashEqual
|
|
|
|
| Self::CaretEqual | Self::PercentEqual => Some(OpType::Assignment),
|
|
|
|
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone,Copy,Debug,PartialEq)]
|
|
|
|
pub enum OpType {
|
2022-09-09 20:08:40 +00:00
|
|
|
Assignment, Comparison, Pipeline, Additive, Multiplicative, Exponential
|
2022-09-06 13:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OpType {
|
|
|
|
pub fn is_right_associative(&self) -> bool {
|
|
|
|
matches!(self, OpType::Exponential | OpType::Assignment)
|
|
|
|
}
|
|
|
|
}
|