start on blocks
This commit is contained in:
parent
02858a9ef7
commit
c5b045ebcb
2 changed files with 40 additions and 28 deletions
|
|
@ -4,7 +4,7 @@ this is a programming language maybe
|
||||||
perf log:
|
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
|
Fibonacci/Parse/programs/medium.jang
|
||||||
time: [13.715 ms 13.748 ms 13.782 ms]
|
time: [13.715 ms 13.748 ms 13.782 ms]
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use chumsky::{
|
||||||
input::{Stream, ValueInput},
|
input::{Stream, ValueInput},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use logos::{Lexer, Logos};
|
use logos::Logos;
|
||||||
use std::{collections::HashMap, rc::Rc};
|
use std::{collections::HashMap, rc::Rc};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
@ -101,6 +101,7 @@ pub fn eval<'src>(
|
||||||
vars.get_mut(name).unwrap().pop();
|
vars.get_mut(name).unwrap().pop();
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
Expr::Block(_) => panic!("unimplemented"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,6 +125,10 @@ pub enum Token<'src> {
|
||||||
LParen,
|
LParen,
|
||||||
#[token(")")]
|
#[token(")")]
|
||||||
RParen,
|
RParen,
|
||||||
|
#[token("{")]
|
||||||
|
LCurly,
|
||||||
|
#[token("}")]
|
||||||
|
RCurly,
|
||||||
#[token(";")]
|
#[token(";")]
|
||||||
Semicolon,
|
Semicolon,
|
||||||
#[token(",")]
|
#[token(",")]
|
||||||
|
|
@ -136,6 +141,32 @@ pub enum Token<'src> {
|
||||||
Ident(&'src str),
|
Ident(&'src str),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Expr<'src> {
|
||||||
|
Num(f64),
|
||||||
|
Var(&'src str),
|
||||||
|
|
||||||
|
Neg(Box<Expr<'src>>),
|
||||||
|
Add(Box<Expr<'src>>, Box<Expr<'src>>),
|
||||||
|
Sub(Box<Expr<'src>>, Box<Expr<'src>>),
|
||||||
|
Mul(Box<Expr<'src>>, Box<Expr<'src>>),
|
||||||
|
Div(Box<Expr<'src>>, Box<Expr<'src>>),
|
||||||
|
|
||||||
|
Block(Vec<Expr<'src>>),
|
||||||
|
Call(&'src str, Vec<Expr<'src>>),
|
||||||
|
Let {
|
||||||
|
name: &'src str,
|
||||||
|
rhs: Box<Expr<'src>>,
|
||||||
|
then: Box<Expr<'src>>,
|
||||||
|
},
|
||||||
|
Fn {
|
||||||
|
name: &'src str,
|
||||||
|
args: Vec<&'src str>,
|
||||||
|
body: Rc<Expr<'src>>,
|
||||||
|
then: Box<Expr<'src>>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
type ParseErrorContent<'src> = Rich<'src, Token<'src>>;
|
type ParseErrorContent<'src> = Rich<'src, Token<'src>>;
|
||||||
type ParseError<'src> = extra::Err<ParseErrorContent<'src>>;
|
type ParseError<'src> = extra::Err<ParseErrorContent<'src>>;
|
||||||
|
|
||||||
|
|
@ -167,31 +198,6 @@ pub fn parse<'src, const LOG: bool>(
|
||||||
create_parser().parse(token_stream).into_result()
|
create_parser().parse(token_stream).into_result()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Expr<'src> {
|
|
||||||
Num(f64),
|
|
||||||
Var(&'src str),
|
|
||||||
|
|
||||||
Neg(Box<Expr<'src>>),
|
|
||||||
Add(Box<Expr<'src>>, Box<Expr<'src>>),
|
|
||||||
Sub(Box<Expr<'src>>, Box<Expr<'src>>),
|
|
||||||
Mul(Box<Expr<'src>>, Box<Expr<'src>>),
|
|
||||||
Div(Box<Expr<'src>>, Box<Expr<'src>>),
|
|
||||||
|
|
||||||
Call(&'src str, Vec<Expr<'src>>),
|
|
||||||
Let {
|
|
||||||
name: &'src str,
|
|
||||||
rhs: Box<Expr<'src>>,
|
|
||||||
then: Box<Expr<'src>>,
|
|
||||||
},
|
|
||||||
Fn {
|
|
||||||
name: &'src str,
|
|
||||||
args: Vec<&'src str>,
|
|
||||||
body: Rc<Expr<'src>>,
|
|
||||||
then: Box<Expr<'src>>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_parser<'src, I: ValueInput<'src, Token = Token<'src>, Span = SimpleSpan>>()
|
pub fn create_parser<'src, I: ValueInput<'src, Token = Token<'src>, Span = SimpleSpan>>()
|
||||||
-> impl Parser<'src, I, Expr<'src>, ParseError<'src>> {
|
-> impl Parser<'src, I, Expr<'src>, ParseError<'src>> {
|
||||||
let ident = any()
|
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_ignore(just(Token::Equals))
|
||||||
.then(expr.clone())
|
.then(expr.clone())
|
||||||
.then_ignore(just(Token::Semicolon))
|
.then_ignore(just(Token::Semicolon))
|
||||||
.then(decl)
|
.then(decl.clone())
|
||||||
.map(|(((name, params), body), then)| Expr::Fn {
|
.map(|(((name, params), body), then)| Expr::Fn {
|
||||||
name,
|
name,
|
||||||
args: params,
|
args: params,
|
||||||
|
|
@ -280,6 +286,12 @@ pub fn create_parser<'src, I: ValueInput<'src, Token = Token<'src>, Span = Simpl
|
||||||
then: Box::new(then),
|
then: Box::new(then),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// let decl_block = decl
|
||||||
|
// .repeated()
|
||||||
|
// .collect::<Vec<_>>()
|
||||||
|
// .delimited_by(just(Token::LCurly), just(Token::RCurly))
|
||||||
|
// .map(Expr::Block);
|
||||||
|
|
||||||
// must parse 'let' keyword first so it doesn't become an identifier
|
// must parse 'let' keyword first so it doesn't become an identifier
|
||||||
decl_fn.or(decl_let).or(expr)
|
decl_fn.or(decl_let).or(expr)
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue