diff --git a/README.md b/README.md index 82b3158..5ac4e54 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ this is a programming language maybe perf log: -- switching to logos for lexing +- switching to logos for lexing (13.71 MB/s parsing... still pretty bad) ``` Fibonacci/Parse/programs/medium.jang time: [13.715 ms 13.748 ms 13.782 ms] diff --git a/crates/jang/src/lib.rs b/crates/jang/src/lib.rs index 83ce3c1..6d45eb3 100644 --- a/crates/jang/src/lib.rs +++ b/crates/jang/src/lib.rs @@ -2,7 +2,7 @@ use chumsky::{ input::{Stream, ValueInput}, prelude::*, }; -use logos::{Lexer, Logos}; +use logos::Logos; use std::{collections::HashMap, rc::Rc}; #[derive(Clone)] @@ -101,6 +101,7 @@ pub fn eval<'src>( vars.get_mut(name).unwrap().pop(); output } + Expr::Block(_) => panic!("unimplemented"), } } @@ -124,6 +125,10 @@ pub enum Token<'src> { LParen, #[token(")")] RParen, + #[token("{")] + LCurly, + #[token("}")] + RCurly, #[token(";")] Semicolon, #[token(",")] @@ -136,6 +141,32 @@ pub enum Token<'src> { Ident(&'src str), } +#[derive(Debug)] +pub enum Expr<'src> { + Num(f64), + Var(&'src str), + + Neg(Box>), + Add(Box>, Box>), + Sub(Box>, Box>), + Mul(Box>, Box>), + Div(Box>, Box>), + + Block(Vec>), + Call(&'src str, Vec>), + Let { + name: &'src str, + rhs: Box>, + then: Box>, + }, + Fn { + name: &'src str, + args: Vec<&'src str>, + body: Rc>, + then: Box>, + }, +} + type ParseErrorContent<'src> = Rich<'src, Token<'src>>; type ParseError<'src> = extra::Err>; @@ -167,31 +198,6 @@ pub fn parse<'src, const LOG: bool>( create_parser().parse(token_stream).into_result() } -#[derive(Debug)] -pub enum Expr<'src> { - Num(f64), - Var(&'src str), - - Neg(Box>), - Add(Box>, Box>), - Sub(Box>, Box>), - Mul(Box>, Box>), - Div(Box>, Box>), - - Call(&'src str, Vec>), - Let { - name: &'src str, - rhs: Box>, - then: Box>, - }, - Fn { - name: &'src str, - args: Vec<&'src str>, - body: Rc>, - then: Box>, - }, -} - pub fn create_parser<'src, I: ValueInput<'src, Token = Token<'src>, Span = SimpleSpan>>() -> impl Parser<'src, I, Expr<'src>, ParseError<'src>> { let ident = any() @@ -272,7 +278,7 @@ pub fn create_parser<'src, I: ValueInput<'src, Token = Token<'src>, Span = Simpl .then_ignore(just(Token::Equals)) .then(expr.clone()) .then_ignore(just(Token::Semicolon)) - .then(decl) + .then(decl.clone()) .map(|(((name, params), body), then)| Expr::Fn { name, args: params, @@ -280,6 +286,12 @@ pub fn create_parser<'src, I: ValueInput<'src, Token = Token<'src>, Span = Simpl then: Box::new(then), }); + // let decl_block = decl + // .repeated() + // .collect::>() + // .delimited_by(just(Token::LCurly), just(Token::RCurly)) + // .map(Expr::Block); + // must parse 'let' keyword first so it doesn't become an identifier decl_fn.or(decl_let).or(expr) });