formatted
This commit is contained in:
parent
89c554abdb
commit
50526960ba
6 changed files with 101 additions and 80 deletions
6
justfile
6
justfile
|
|
@ -3,3 +3,9 @@ run:
|
||||||
|
|
||||||
test:
|
test:
|
||||||
cargo test --verbose
|
cargo test --verbose
|
||||||
|
|
||||||
|
doc:
|
||||||
|
cargo doc --no-deps
|
||||||
|
|
||||||
|
fmt:
|
||||||
|
cargo fmt
|
||||||
|
|
@ -7,10 +7,9 @@ pub type BlockStatement = Vec<Statement>;
|
||||||
|
|
||||||
pub type Span = Range<usize>;
|
pub type Span = Range<usize>;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Var(Ident, Option<Type>)
|
Var(Ident, Option<Type>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
|
@ -79,7 +78,6 @@ impl Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------
|
//---------------------------------------
|
||||||
// Structs
|
// Structs
|
||||||
//---------------------------------------
|
//---------------------------------------
|
||||||
|
|
@ -99,7 +97,6 @@ pub struct Condition {
|
||||||
pub else_body: Option<BlockStatement>,
|
pub else_body: Option<BlockStatement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Example: `x = 5`
|
/// Example: `x = 5`
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Assignment {
|
pub struct Assignment {
|
||||||
|
|
@ -112,7 +109,7 @@ pub struct Assignment {
|
||||||
pub struct Var {
|
pub struct Var {
|
||||||
pub ty: Option<Type>,
|
pub ty: Option<Type>,
|
||||||
pub ident: String,
|
pub ident: String,
|
||||||
pub value: Box<Expr>
|
pub value: Box<Expr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Example: `x++`
|
/// Example: `x++`
|
||||||
|
|
@ -151,10 +148,9 @@ pub struct Function {
|
||||||
pub params: Vec<(Ident, Type)>,
|
pub params: Vec<(Ident, Type)>,
|
||||||
pub return_type: Option<Type>,
|
pub return_type: Option<Type>,
|
||||||
pub body: Vec<Statement>,
|
pub body: Vec<Statement>,
|
||||||
pub body_expr: Option<Type>
|
pub body_expr: Option<Type>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Call {
|
pub struct Call {
|
||||||
pub callee: Box<Expr>,
|
pub callee: Box<Expr>,
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,45 @@
|
||||||
use chumsky::{
|
use chumsky::{
|
||||||
combinator::Or, error::Rich, extra, input::{Input, Stream, ValueInput}, prelude::{choice, end, just, nested_delimiters, recursive, skip_then_retry_until, via_parser}, primitive::select, recursive, select, select_ref, span::{self, SimpleSpan}, text::{self, ascii::{ident, keyword}, newline, whitespace}, Boxed, ConfigIterParser, IterParser, ParseResult, Parser
|
Boxed, ConfigIterParser, IterParser, ParseResult, Parser,
|
||||||
|
combinator::Or,
|
||||||
|
error::Rich,
|
||||||
|
extra,
|
||||||
|
input::{Input, Stream, ValueInput},
|
||||||
|
prelude::{choice, end, just, nested_delimiters, recursive, skip_then_retry_until, via_parser},
|
||||||
|
primitive::select,
|
||||||
|
recursive, select, select_ref,
|
||||||
|
span::{self, SimpleSpan},
|
||||||
|
text::{
|
||||||
|
self,
|
||||||
|
ascii::{ident, keyword},
|
||||||
|
newline, whitespace,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use logos::{source, Logos};
|
use logos::{Logos, source};
|
||||||
|
|
||||||
use crate::language_frontend::{abstract_syntax_tree::{ast::Expr, definitions::*}, lexer::tokens::Token};
|
use crate::language_frontend::{
|
||||||
|
abstract_syntax_tree::{ast::Expr, definitions::*},
|
||||||
|
lexer::tokens::Token,
|
||||||
|
};
|
||||||
|
|
||||||
// goal of parsing is to construct an abstract syntax tree
|
// goal of parsing is to construct an abstract syntax tree
|
||||||
|
|
||||||
pub fn parse(source: &str) -> Result<Vec<Expr>, Vec<Rich<'_, Token>>> {
|
pub fn parse(source: &str) -> Result<Vec<Expr>, Vec<Rich<'_, Token>>> {
|
||||||
let token_iter = Token::lexer(source).spanned().map(|(token, span)| (token.unwrap_or(Token::Error), span.into()));
|
let token_iter = Token::lexer(source)
|
||||||
|
.spanned()
|
||||||
|
.map(|(token, span)| (token.unwrap_or(Token::Error), span.into()));
|
||||||
let end_of_input: SimpleSpan = (0..source.len()).into();
|
let end_of_input: SimpleSpan = (0..source.len()).into();
|
||||||
let token_stream = Stream::from_iter(token_iter)
|
let token_stream = Stream::from_iter(token_iter)
|
||||||
// Tell chumsky to split the (Token, SimpleSpan) stream into its parts so that it can handle the spans for us
|
// Tell chumsky to split the (Token, SimpleSpan) stream into its parts so that it can handle the spans for us
|
||||||
// This involves giving chumsky an 'end of input' span: we just use a zero-width span at the end of the string
|
// This involves giving chumsky an 'end of input' span: we just use a zero-width span at the end of the string
|
||||||
.map((0..end_of_input.into_iter().len()).into(), |(t, s): (_, _)| (t, s));
|
.map(
|
||||||
|
(0..end_of_input.into_iter().len()).into(),
|
||||||
|
|(t, s): (_, _)| (t, s),
|
||||||
|
);
|
||||||
|
|
||||||
parser().parse(token_stream).into_result()
|
parser().parse(token_stream).into_result()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parser<'src, I>() -> impl Parser<'src, I, Vec<Expr>, extra::Err<Rich<'src, Token>>>
|
||||||
fn parser<'src, I>()
|
|
||||||
-> impl Parser<'src, I, Vec<Expr>, extra::Err<Rich<'src, Token>>>
|
|
||||||
where
|
where
|
||||||
I: ValueInput<'src, Token = Token, Span = SimpleSpan>,
|
I: ValueInput<'src, Token = Token, Span = SimpleSpan>,
|
||||||
{
|
{
|
||||||
|
|
@ -41,8 +60,9 @@ where
|
||||||
Token::FloatLiteral(x) => Expr::FloatLiteral(x),
|
Token::FloatLiteral(x) => Expr::FloatLiteral(x),
|
||||||
Token::IntLiteral(x) => Expr::IntLiteral(x),
|
Token::IntLiteral(x) => Expr::IntLiteral(x),
|
||||||
}
|
}
|
||||||
.or(expr.clone().delimited_by(just(Token::LParen), just(Token::RParen)));
|
.or(expr
|
||||||
|
.clone()
|
||||||
|
.delimited_by(just(Token::LParen), just(Token::RParen)));
|
||||||
|
|
||||||
let mul_div = atom.clone().foldl(
|
let mul_div = atom.clone().foldl(
|
||||||
choice((
|
choice((
|
||||||
|
|
@ -52,12 +72,14 @@ where
|
||||||
.then(atom)
|
.then(atom)
|
||||||
.then_ignore(just(Token::NewLine).or_not())
|
.then_ignore(just(Token::NewLine).or_not())
|
||||||
.repeated(),
|
.repeated(),
|
||||||
|lhs, (op, rhs)| Expr::BinaryExpr ( Binary {
|
|lhs, (op, rhs)| {
|
||||||
|
Expr::BinaryExpr(Binary {
|
||||||
lhs: Box::new(lhs),
|
lhs: Box::new(lhs),
|
||||||
operator: op,
|
operator: op,
|
||||||
rhs: Box::new(rhs),
|
rhs: Box::new(rhs),
|
||||||
|
})
|
||||||
},
|
},
|
||||||
));
|
);
|
||||||
|
|
||||||
let add_sub = mul_div.clone().foldl(
|
let add_sub = mul_div.clone().foldl(
|
||||||
choice((
|
choice((
|
||||||
|
|
@ -67,12 +89,14 @@ where
|
||||||
.then(mul_div)
|
.then(mul_div)
|
||||||
.then_ignore(just(Token::NewLine).or_not())
|
.then_ignore(just(Token::NewLine).or_not())
|
||||||
.repeated(),
|
.repeated(),
|
||||||
|lhs, (op, rhs)| Expr::BinaryExpr ( Binary {
|
|lhs, (op, rhs)| {
|
||||||
|
Expr::BinaryExpr(Binary {
|
||||||
lhs: Box::new(lhs),
|
lhs: Box::new(lhs),
|
||||||
operator: op,
|
operator: op,
|
||||||
rhs: Box::new(rhs),
|
rhs: Box::new(rhs),
|
||||||
|
})
|
||||||
},
|
},
|
||||||
));
|
);
|
||||||
|
|
||||||
add_sub
|
add_sub
|
||||||
});
|
});
|
||||||
|
|
@ -82,12 +106,13 @@ where
|
||||||
.then_ignore(just(Token::Assign))
|
.then_ignore(just(Token::Assign))
|
||||||
.then(expr.clone())
|
.then(expr.clone())
|
||||||
.then_ignore(just(Token::NewLine).or_not())
|
.then_ignore(just(Token::NewLine).or_not())
|
||||||
.map(|(name, rhs)| Expr::VarExpr ( Var {
|
.map(|(name, rhs)| {
|
||||||
|
Expr::VarExpr(Var {
|
||||||
ty: None,
|
ty: None,
|
||||||
ident: name,
|
ident: name,
|
||||||
value: Box::new(rhs),
|
value: Box::new(rhs),
|
||||||
}
|
})
|
||||||
));
|
});
|
||||||
/*
|
/*
|
||||||
let fun = just(Token::Fun)
|
let fun = just(Token::Fun)
|
||||||
.ignore_then(ident.clone())
|
.ignore_then(ident.clone())
|
||||||
|
|
@ -104,7 +129,6 @@ where
|
||||||
});
|
});
|
||||||
|
|
||||||
decl.repeated().collect()
|
decl.repeated().collect()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use logos::{Logos};
|
use logos::Logos;
|
||||||
|
|
||||||
#[derive(Logos, Debug, Clone, PartialEq)]
|
#[derive(Logos, Debug, Clone, PartialEq)]
|
||||||
#[regex(r"[\t\f]+", logos::skip)]
|
#[regex(r"[\t\f]+", logos::skip)]
|
||||||
|
|
@ -40,7 +40,6 @@ pub enum Token {
|
||||||
#[token("enum")]
|
#[token("enum")]
|
||||||
Enum,
|
Enum,
|
||||||
|
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
#[token("int")]
|
#[token("int")]
|
||||||
IntType,
|
IntType,
|
||||||
|
|
@ -108,7 +107,6 @@ pub enum Token {
|
||||||
#[token("||")]
|
#[token("||")]
|
||||||
Or,
|
Or,
|
||||||
|
|
||||||
|
|
||||||
// Punctiuation
|
// Punctiuation
|
||||||
#[token("(")]
|
#[token("(")]
|
||||||
LParen,
|
LParen,
|
||||||
|
|
@ -138,7 +136,6 @@ pub enum Token {
|
||||||
Dot,
|
Dot,
|
||||||
|
|
||||||
// Special
|
// Special
|
||||||
|
|
||||||
#[regex(r"\n")]
|
#[regex(r"\n")]
|
||||||
NewLine,
|
NewLine,
|
||||||
|
|
||||||
|
|
@ -149,8 +146,6 @@ pub enum Token {
|
||||||
#[regex(r"[ \t\f]+", logos::skip)]
|
#[regex(r"[ \t\f]+", logos::skip)]
|
||||||
Whitespace,
|
Whitespace,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Eof,
|
Eof,
|
||||||
|
|
||||||
Error,
|
Error,
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
use chumsky::input::{Input, Stream};
|
|
||||||
use chumsky::Parser;
|
use chumsky::Parser;
|
||||||
|
use chumsky::input::{Input, Stream};
|
||||||
use logos::Logos;
|
use logos::Logos;
|
||||||
|
|
||||||
mod language_frontend;
|
mod language_frontend;
|
||||||
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
language_frontend::lexer::tokens::Token, language_frontend::abstract_syntax_tree::parser::parse};
|
language_frontend::abstract_syntax_tree::parser::parse, language_frontend::lexer::tokens::Token,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::language_frontend::abstract_syntax_tree::ast::{Expr};
|
use crate::language_frontend::abstract_syntax_tree::ast::Expr;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Simple Compiler -> 4 Stages:
|
Simple Compiler -> 4 Stages:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue