cargo new

This commit is contained in:
nora 2022-10-16 21:48:09 +02:00
parent f86f37810c
commit f7964cf17e
No known key found for this signature in database
9 changed files with 306 additions and 0 deletions

30
src/ast.rs Normal file
View file

@ -0,0 +1,30 @@
use std::ops::Range;
type Span = Range<usize>;
type Ident = String;
pub enum Item {
Fn(Fn),
}
pub enum FnKind {
Fn,
Task,
}
struct Fn {
pub kind: FnKind,
pub span: Span,
}
pub enum Expr {
Int(u64),
Ident(Ident),
Binary(BinaryOp, Box<Expr>, Box<Expr>, Span),
Assign(Ident, Box<Expr>, Span),
}
pub enum BinaryOp {
Add,
Sub,
}

31
src/lexer.rs Normal file
View file

@ -0,0 +1,31 @@
#[derive(Debug, Clone, PartialEq, Eq, Hash, logos::Logos)]
pub enum Token {
#[token("+")]
Plus,
#[token("-")]
Minus,
#[token("=")]
Eq,
#[token(";")]
Semi,
#[token("(")]
POpen,
#[token(")")]
PClose,
#[token("{")]
BOpen,
#[token("}")]
BClose,
#[regex(r"\d+", |lex| lex.slice().parse())]
Int(u64),
#[regex(r"[a-zA-Z_]\w+", |lex| lex.slice().to_string())]
Ident(String),
#[token("fn")]
Fn,
#[token("let")]
Let,
#[token("task")]
Task,
#[error]
Error,
}

3
src/lib.rs Normal file
View file

@ -0,0 +1,3 @@
mod ast;
mod lexer;
mod parser;

3
src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

38
src/parser.rs Normal file
View file

@ -0,0 +1,38 @@
use chumsky::{prelude::*, Parser};
use crate::{
ast::{Expr, Item},
lexer::Token,
};
fn expr() -> impl Parser<Token, Expr, Error = Simple<Token>> {
recursive(|expr| {
let value = select! {
Token::Int(int) => Expr::Int(int),
Token::Ident(ident) => Expr::Ident(ident),
}
.boxed();
let ident = select! {
Token::Ident(ident) => ident
}
.boxed();
let let_expr = just(Token::Let)
.ignore_then(ident.clone())
.then_ignore(just(Token::Eq))
.then(expr.clone())
.then_ignore(just(Token::Semi))
.map_with_span(|(name, value): (String, Expr), span| {
Expr::Assign(name, Box::new(value), span)
})
.boxed();
value.or(let_expr)
})
.boxed()
}
fn item() -> impl Parser<Token, Item, Error = Simple<Token>> {
todo()
}