mirror of
https://github.com/Noratrieb/lambda-calculus.git
synced 2026-01-14 15:25:05 +01:00
some things
This commit is contained in:
parent
8f305045fe
commit
731974ef45
2 changed files with 83 additions and 1 deletions
82
src/lib.rs
82
src/lib.rs
|
|
@ -0,0 +1,82 @@
|
||||||
|
use logos::Logos;
|
||||||
|
|
||||||
|
mod lexer {
|
||||||
|
use logos::Logos;
|
||||||
|
|
||||||
|
#[derive(Logos, Clone, Eq, PartialEq, Hash)]
|
||||||
|
pub enum Token<'a> {
|
||||||
|
#[token("λ")]
|
||||||
|
Lambda,
|
||||||
|
|
||||||
|
#[token(".")]
|
||||||
|
Dot,
|
||||||
|
|
||||||
|
#[token(":=")]
|
||||||
|
Binding,
|
||||||
|
|
||||||
|
#[token("(")]
|
||||||
|
ParenO,
|
||||||
|
|
||||||
|
#[token(")")]
|
||||||
|
ParenC,
|
||||||
|
|
||||||
|
#[regex("[a-zA-Z]+")]
|
||||||
|
Ident(&'a str),
|
||||||
|
|
||||||
|
#[error]
|
||||||
|
#[regex(r"[ \t\r\n]+", logos::skip)]
|
||||||
|
Error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod parser {
|
||||||
|
use crate::lexer::Token;
|
||||||
|
use chumsky::prelude::*;
|
||||||
|
|
||||||
|
enum Expr {
|
||||||
|
Name(String),
|
||||||
|
Application {
|
||||||
|
function: Box<Expr>,
|
||||||
|
argument: Box<Expr>,
|
||||||
|
},
|
||||||
|
Abstraction {
|
||||||
|
args: Vec<String>,
|
||||||
|
body: Expr,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expr_parser<'a>() -> impl Parser<Token<'a>, Spanned<Expr>, Error = Simple<Token<'a>>> + Clone
|
||||||
|
{
|
||||||
|
recursive(|expr| {
|
||||||
|
let variable = filter_map(|span, token| match token {
|
||||||
|
Token::Ident(name) => Ok(Expr::Name(name.to_string())),
|
||||||
|
_ => Err(Simple::expected_input_found(span, [], Some(token))),
|
||||||
|
})
|
||||||
|
.labelled("variable");
|
||||||
|
|
||||||
|
let abstraction = just(Token::Lambda)
|
||||||
|
.ignore_then(variable)
|
||||||
|
.then(Token::Dot)
|
||||||
|
.then(expr.clone());
|
||||||
|
|
||||||
|
let application = just(expr).then(expr.clone());
|
||||||
|
|
||||||
|
let atom = variable
|
||||||
|
.or(expr.delimited_by(Token::ParenO, Token::ParenC))
|
||||||
|
.or(abstraction)
|
||||||
|
.or(application);
|
||||||
|
|
||||||
|
let binding = just(variable)
|
||||||
|
.then_ignore(Token::Binding)
|
||||||
|
.then(expr.clone());
|
||||||
|
|
||||||
|
let statement = just(expr.clone()).or(binding);
|
||||||
|
|
||||||
|
todo!()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(input: &str) {
|
||||||
|
let mut lex = lexer::Token::lexer(input);
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
lambda_calculus::run("hello");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue