Skip to content

Commit

Permalink
refactor(ir): seperate tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniMay committed Feb 21, 2024
1 parent 5df46a1 commit 27fea9b
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 209 deletions.
202 changes: 1 addition & 201 deletions src/ir/frontend.rs
Original file line number Diff line number Diff line change
@@ -1,204 +1,4 @@
use std::fmt;

use super::values::{BinaryOp, UnaryOp};

pub(self) mod ast;
pub(self) mod lexer;
pub mod parser;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(self) struct Pos {
row: usize,
col: usize,
}

impl Default for Pos {
fn default() -> Self {
Self { row: 1, col: 0 }
}
}

impl Pos {
pub fn new() -> Self {
Self::default()
}

pub fn update(&mut self, c: char) {
if c == '\n' {
self.row += 1;
self.col = 0;
} else {
self.col += 1;
}
}
}

impl fmt::Display for Pos {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.row, self.col)
}
}

#[derive(Clone, PartialEq, Eq)]
pub(self) struct Span {
start: Pos,
end: Pos,
}

impl Span {
pub fn new(start: Pos) -> Self {
let end = start.clone();
Self { start, end }
}

pub fn update(&mut self, end: Pos) {
self.end = end;
}
}

impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.start, self.end)
}
}

impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.start, self.end)
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(self) struct Token {
span: Span,
kind: TokenKind,
}

impl Token {
pub fn new(span: Span, kind: TokenKind) -> Self {
Self { span, kind }
}
}

/// Kinds of tokens.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(self) enum TokenKind {
/// A label identifier
///
/// A label starts with a `^` and followed by a sequence of alphanumeric characters.
LabelIdent(String),

/// A global identifier
///
/// A global identifier starts with a `@` and followed by a sequence of alphanumeric characters.
GlobalIdent(String),

/// A local identifier
///
/// A local identifier starts with a `%` and followed by a sequence of alphanumeric characters.
LocalIdent(String),

/// A type identifier
///
/// A type identifier starts with a `$` and followed by a sequence of alphanumeric characters.
TypeIdent(String),

/// A number
Bytes(Vec<u8>),

/// A keyword
Keyword(Keyword),

/// An instruction operator
Inst(Inst),

LeftParen,
RightParen,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Comma,
Colon,
Semicolon,
Arrow,
Equal,

/// End of file
Eof,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(self) enum Keyword {
/// `fn`
Fn,

/// `decl`
Decl,

/// `iX`
Int(usize),

/// `half`
Half,

/// `float`
Float,

/// `double`
Double,

/// `ptr`
Ptr,

/// `void`
Void,

/// `undef`
Undef,

/// `zero`
Zero,

/// `global`
Global,

/// `const`
Const,

/// `Type`
Type,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(self) enum Inst {
/// A binary operator
Binary(BinaryOp),

/// A unary operator
Unary(UnaryOp),

/// `store`
Store,

/// `load`
Load,

/// `alloc`
Alloc,

/// `jump`
Jump,

/// `br`
Branch,

/// `ret`
Return,

/// `call`
Call,

/// `getelemptr`
GetElemPtr,
}
pub(self) mod tokens;
4 changes: 2 additions & 2 deletions src/ir/frontend/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::io;

use crate::ir::{
frontend::{Span, TokenKind},
frontend::tokens::{Span, TokenKind},
values::{BinaryOp, FCmpCond, ICmpCond, UnaryOp},
};

use super::{Inst, Keyword, Pos, Token};
use super::tokens::{Inst, Keyword, Pos, Token};

pub struct Lexer<'a, T>
where
Expand Down
9 changes: 3 additions & 6 deletions src/ir/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ use std::{
io::{self},
};

use crate::ir::{
frontend::ast::{self, InstKind},
types::Type,
};
use crate::ir::types::Type;

use super::{
ast::{Array, Ast, AstNode, AstNodeBox, Global, Operand, Struct, TypeDef},
ast::{self, Array, Ast, AstNode, AstNodeBox, Global, InstKind, Operand, Struct, TypeDef},
lexer::Lexer,
Inst, Keyword, Pos, Span, Token, TokenKind,
tokens::{Inst, Keyword, Pos, Span, Token, TokenKind},
};

pub struct Parser<'a, T>
Expand Down
Loading

0 comments on commit 27fea9b

Please sign in to comment.