use crate::front::span::Span;
use crate::ir::BinaryOp;
use std::fmt;
pub struct Token {
pub span: Span,
pub kind: TokenKind,
}
impl Token {
pub fn new(span: Span, kind: TokenKind) -> Self {
Self { span, kind }
}
}
impl Default for Token {
fn default() -> Self {
Self {
span: Span::default(),
kind: TokenKind::End,
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum TokenKind {
Int(i64),
Symbol(String),
Keyword(Keyword),
BinaryOp(BinaryOp),
Other(char),
End,
}
impl fmt::Display for TokenKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TokenKind::Int(v) => write!(f, "integer '{}'", v),
TokenKind::Symbol(v) => write!(f, "symbol '{}'", v),
TokenKind::Keyword(v) => write!(f, "keyword '{}'", v),
TokenKind::BinaryOp(v) => write!(f, "binary operator '{}'", v),
TokenKind::Other(v) => write!(f, "character '{}'", v),
TokenKind::End => write!(f, "end of file"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Keyword {
I32,
Undef,
ZeroInit,
Global,
Alloc,
Load,
Store,
GetPtr,
GetElemPtr,
Br,
Jump,
Call,
Ret,
Fun,
Decl,
}
impl fmt::Display for Keyword {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Keyword::I32 => f.write_str("i32"),
Keyword::Undef => f.write_str("undef"),
Keyword::ZeroInit => f.write_str("zeroinit"),
Keyword::Global => f.write_str("global"),
Keyword::Alloc => f.write_str("alloc"),
Keyword::Load => f.write_str("load"),
Keyword::Store => f.write_str("store"),
Keyword::GetPtr => f.write_str("getptr"),
Keyword::GetElemPtr => f.write_str("getelemptr"),
Keyword::Br => f.write_str("br"),
Keyword::Jump => f.write_str("jump"),
Keyword::Call => f.write_str("call"),
Keyword::Ret => f.write_str("ret"),
Keyword::Fun => f.write_str("fun"),
Keyword::Decl => f.write_str("decl"),
}
}
}