Skip to content

Commit

Permalink
use logos for lexer and tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 13, 2024
1 parent e410d0a commit f578d2a
Show file tree
Hide file tree
Showing 31 changed files with 592 additions and 1,300 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ index_vec = "0.1.3"
inkwell = { version = "0.4.0", features = ["llvm17-0"] }
int-enum = "1.0.1"
itertools = "0.13.0"
logos = "0.14.1"
string-interner = "0.17.0"
thiserror = "1.0.58"

Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,19 @@ use inkwell::{
use repr::ty::Ty;
use stage::{
codegen::{ctx::LLVMCtx as _, llvm::FunctionGenerator},
lex::Lexer,
lower_ir::{self, IRCtx as _},
parse::parse,
};
use util::source::Source;

pub mod compile_pass;
pub mod repr;
pub mod stage;
pub mod util;

pub fn compile_and_run(source: &'static str, debug: bool) -> i64 {
let source = Source::new(source);

let mut ctx = CompilePass::default();

let program = parse(&mut ctx, &mut Lexer::new(source)).unwrap();
let program = parse(&mut ctx, source).unwrap();

let program = program.ty_solve(&mut ctx).unwrap();

Expand Down
22 changes: 11 additions & 11 deletions src/repr/ast/base/expression/infix.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use crate::{ast_node, repr::token::Token, util::source::Span};
use crate::{ast_node, repr::token::Token};

use super::Expression;

#[derive(Debug, Clone)]
pub enum InfixOperation {
Minus(Span),
Plus(Span),
Eq(Span),
NotEq(Span),
Minus,
Plus,
Eq,
NotEq,
}

impl InfixOperation {
pub fn plus() -> Self {
Self::Plus(Span::default())
Self::Plus
}

pub fn minus() -> Self {
Self::Minus(Span::default())
Self::Minus
}
}

Expand All @@ -25,10 +25,10 @@ impl TryFrom<Token> for InfixOperation {

fn try_from(token: Token) -> Result<Self, Self::Error> {
match token {
Token::Plus(token) => Ok(InfixOperation::Plus(token.span)),
Token::Minus(token) => Ok(InfixOperation::Minus(token.span)),
Token::Eq(token) => Ok(InfixOperation::Eq(token.span)),
Token::NotEq(token) => Ok(InfixOperation::NotEq(token.span)),
Token::Plus => Ok(InfixOperation::Plus),
Token::Minus => Ok(InfixOperation::Minus),
Token::DoubleEq => Ok(InfixOperation::Eq),
Token::NotEq => Ok(InfixOperation::NotEq),
_ => Err(()),
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/repr/ast/base/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ pub use if_else::*;
pub use infix::*;
pub use integer::*;

use crate::{
ast_node,
util::source::{Span, Spanned},
};
use crate::{ast_node, util::span::Span};

use super::Statement;

Expand All @@ -41,7 +38,7 @@ impl<TyInfo: Default, FnIdentifier, IdentIdentifier>
operation: InfixOperation,
right: Expression<TyInfo, FnIdentifier, IdentIdentifier>,
) -> Self {
let span = Spanned::span(&left).to(&right);
let span = left.span().start..right.span().end;
Self::Infix(Infix::<TyInfo, FnIdentifier, IdentIdentifier>::new(
Box::new(left),
operation,
Expand Down
18 changes: 5 additions & 13 deletions src/repr/ast/base/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ macro_rules! ast_node {
(common struct $struct_name:ident<$ty_info:ident $(, $($generic:ident),*)?> { $($name:ident: $ty:ty,)* }) => {
#[derive(Debug, Clone)]
pub struct $struct_name<$ty_info $(, $($generic),*)?> {
pub span: $crate::util::source::Span,
pub span: $crate::util::span::Span,
$(pub $name: $ty,)*
}

impl<$ty_info $(, $($generic),*)?> $crate::util::source::Spanned for $struct_name<$ty_info $(, $($generic),*)?> {
fn span(&self) -> &$crate::util::source::Span {
&self.span
}
}
};

// AST node that is typed
Expand All @@ -23,7 +17,7 @@ macro_rules! ast_node {
});

impl<$ty_info: Default $(, $($generic),*)?> $struct_name<$ty_info $(, $($generic),*)?> {
pub fn new($($name: $ty,)* span: $crate::util::source::Span) -> Self {
pub fn new($($name: $ty,)* span: $crate::util::span::Span) -> Self {
Self {
span,
ty_info: Default::default(),
Expand All @@ -40,7 +34,7 @@ macro_rules! ast_node {
});

impl<$ty_info $(, $($generic),*)?> $struct_name<$ty_info $(, $($generic),*)?> {
pub fn new($($name: $ty,)* span: $crate::util::source::Span) -> Self {
pub fn new($($name: $ty,)* span: $crate::util::span::Span) -> Self {
Self {
span,
$($name,)*
Expand All @@ -62,12 +56,10 @@ macro_rules! ast_node {
$(Self::$name(value) => &value.ty_info),*
}
}
}

impl<$ty_info $(, $($generic),*)?> $crate::util::source::Spanned for $enum_name<$ty_info $(, $($generic),*)?> {
fn span(&self) -> &$crate::util::source::Span {
pub fn span(&self) -> &$crate::util::span::Span {
match self {
$(Self::$name(value) => $crate::util::source::Spanned::span(value)),*
$(Self::$name(value) => &value.span),*
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/repr/ast/base/statement.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ast_node, util::source::Span};
use crate::{ast_node, util::span::Span};

use super::*;

Expand Down
8 changes: 4 additions & 4 deletions src/repr/ir/triple/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pub enum BinaryOp {
impl From<&ast::InfixOperation> for BinaryOp {
fn from(value: &ast::InfixOperation) -> Self {
match value {
ast::InfixOperation::Plus(_) => Self::Add,
ast::InfixOperation::Minus(_) => Self::Sub,
ast::InfixOperation::Eq(_) => Self::Eq,
ast::InfixOperation::NotEq(_) => Self::NotEq,
ast::InfixOperation::Plus => Self::Add,
ast::InfixOperation::Minus => Self::Sub,
ast::InfixOperation::Eq => Self::Eq,
ast::InfixOperation::NotEq => Self::NotEq,
}
}
}
Expand Down
Loading

0 comments on commit f578d2a

Please sign in to comment.