|
|
|
|
@ -1,9 +1,15 @@
|
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
use std::{hash::Hash, ops::Range};
|
|
|
|
|
|
|
|
|
|
use chumsky::{
|
|
|
|
|
IterParser, Parser,
|
|
|
|
|
error::EmptyErr,
|
|
|
|
|
extra::{self, SimpleState},
|
|
|
|
|
input::{IterInput, MapExtra},
|
|
|
|
|
prelude::{choice, just, recursive},
|
|
|
|
|
select, text,
|
|
|
|
|
};
|
|
|
|
|
use internment::Intern;
|
|
|
|
|
use lexer::{Token, TokenConsumer, TokenItem, TokenItemIterator};
|
|
|
|
|
use logos::Logos;
|
|
|
|
|
use pomelo::pomelo;
|
|
|
|
|
use lexer::{Token, TokenItemIterator, TokenIterator};
|
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
@ -32,14 +38,14 @@ pub enum InnerType {
|
|
|
|
|
float_type: FloatType,
|
|
|
|
|
},
|
|
|
|
|
Pointer {
|
|
|
|
|
pointee: Box<Type>,
|
|
|
|
|
pointee: Type,
|
|
|
|
|
},
|
|
|
|
|
Array {
|
|
|
|
|
element: Box<Type>,
|
|
|
|
|
element: Type,
|
|
|
|
|
size: usize,
|
|
|
|
|
},
|
|
|
|
|
Function {
|
|
|
|
|
return_type: Box<Type>,
|
|
|
|
|
return_type: Type,
|
|
|
|
|
parameter_types: Vec<Type>,
|
|
|
|
|
},
|
|
|
|
|
Tuple {
|
|
|
|
|
@ -119,10 +125,7 @@ pub enum AstNode {
|
|
|
|
|
ParameterList {
|
|
|
|
|
parameters: Vec<Index>,
|
|
|
|
|
},
|
|
|
|
|
Parameter {
|
|
|
|
|
name: String,
|
|
|
|
|
param_type: Type,
|
|
|
|
|
},
|
|
|
|
|
Parameter(Parameter),
|
|
|
|
|
FunctionDecl(FunctionDecl),
|
|
|
|
|
Block {
|
|
|
|
|
statements: Vec<Index>,
|
|
|
|
|
@ -332,287 +335,178 @@ impl Ast {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct FunctionDecl {
|
|
|
|
|
pub struct FunctionDecl {
|
|
|
|
|
attrs: Option<Index>,
|
|
|
|
|
name: String,
|
|
|
|
|
visibility: Visibility,
|
|
|
|
|
return_type: Type,
|
|
|
|
|
parameter_list: Option<ParameterList>,
|
|
|
|
|
parameter_list: ParameterList,
|
|
|
|
|
body: Index,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Parameter {
|
|
|
|
|
pub struct Parameter {
|
|
|
|
|
mutable: bool,
|
|
|
|
|
name: String,
|
|
|
|
|
param_type: Type,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct ParameterList {
|
|
|
|
|
pub struct ParameterList {
|
|
|
|
|
parameters: Vec<Index>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct ExtraToken<'a> {
|
|
|
|
|
lexeme: &'a str,
|
|
|
|
|
offset: u32,
|
|
|
|
|
fn parse() {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pomelo! {
|
|
|
|
|
%include {
|
|
|
|
|
use super::AstNode;
|
|
|
|
|
use internment::Intern;
|
|
|
|
|
use super::{
|
|
|
|
|
Parameter, Ast, ParameterList, FunctionDecl, Type, InnerType,
|
|
|
|
|
FloatType, ExtraToken, Index, IntSize, Visibility, Value,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
%extra_argument Ast;
|
|
|
|
|
%parser pub struct Parser<'a>{};
|
|
|
|
|
%token #[derive(Debug)] pub enum Token<'a> {};
|
|
|
|
|
|
|
|
|
|
%type Ident &'a str;
|
|
|
|
|
%type DocComment &'a str;
|
|
|
|
|
%type Comment &'a str;
|
|
|
|
|
%type fn_decl FunctionDecl;
|
|
|
|
|
%type parameter Parameter;
|
|
|
|
|
%type parameter_list ParameterList;
|
|
|
|
|
%type typ Type;
|
|
|
|
|
%type return_type Type;
|
|
|
|
|
%type block Index;
|
|
|
|
|
%type decl Index;
|
|
|
|
|
%type decl_list Vec<Index>;
|
|
|
|
|
%type file Index;
|
|
|
|
|
|
|
|
|
|
file ::= decl_list?(list) {
|
|
|
|
|
let decls = list.unwrap_or_default();
|
|
|
|
|
extra.push(AstNode::File { decls })
|
|
|
|
|
};
|
|
|
|
|
decl_list ::= decl(decl) { vec![decl] };
|
|
|
|
|
decl_list ::= decl_list(dl) decl(decl) {
|
|
|
|
|
let mut list = dl;
|
|
|
|
|
list.push(decl);
|
|
|
|
|
list
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
%type attrs Index;
|
|
|
|
|
attrs ::= DocComment(text) {
|
|
|
|
|
let idx = extra.push(AstNode::Doc { text: text.to_string() });
|
|
|
|
|
extra.push(AstNode::Attributes { attrs: vec![idx] })
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typ ::= Bool { Intern::new(InnerType::Bool) };
|
|
|
|
|
typ ::= I1 { Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(1) }) };
|
|
|
|
|
typ ::= I8 { Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(8) }) };
|
|
|
|
|
typ ::= I16 { Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(16) }) };
|
|
|
|
|
typ ::= I32 { Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(32) }) };
|
|
|
|
|
typ ::= I64 { Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(64) }) };
|
|
|
|
|
typ ::= U1 { Intern::new(InnerType::Int { signed: false, size: IntSize::Bits(1) }) };
|
|
|
|
|
typ ::= U8 { Intern::new(InnerType::Int { signed: false, size: IntSize::Bits(8) }) };
|
|
|
|
|
typ ::= U16 { Intern::new(InnerType::Int { signed: false, size: IntSize::Bits(16) }) };
|
|
|
|
|
typ ::= U32 { Intern::new(InnerType::Int { signed: false, size: IntSize::Bits(32) }) };
|
|
|
|
|
typ ::= U64 { Intern::new(InnerType::Int { signed: false, size: IntSize::Bits(64) }) };
|
|
|
|
|
typ ::= ISize { Intern::new(InnerType::Int { signed: true, size: IntSize::Pointer }) };
|
|
|
|
|
typ ::= USize { Intern::new(InnerType::Int { signed: false, size: IntSize::Pointer }) };
|
|
|
|
|
typ ::= F32 { Intern::new(InnerType::Float { float_type: FloatType::F32 }) };
|
|
|
|
|
typ ::= F64 { Intern::new(InnerType::Float { float_type: FloatType::F64 }) };
|
|
|
|
|
typ ::= Bang { Intern::new(InnerType::Bottom) };
|
|
|
|
|
typ ::= unit { Intern::new(InnerType::Unit) };
|
|
|
|
|
typ ::= Void { Intern::new(InnerType::Unit) };
|
|
|
|
|
|
|
|
|
|
unit ::= LParen RParen;
|
|
|
|
|
|
|
|
|
|
%type immediate (Intern<Value>, Type);
|
|
|
|
|
immediate ::= unit { (Intern::new(Value::Unit), Intern::new(InnerType::Unit)) };
|
|
|
|
|
immediate ::= False { (Intern::new(Value::Bool(false)), Intern::new(InnerType::Bool)) };
|
|
|
|
|
immediate ::= True { (Intern::new(Value::Bool(true)), Intern::new(InnerType::Bool)) };
|
|
|
|
|
%type Constant lexer::Token<'a>;
|
|
|
|
|
immediate ::= Constant(token) {
|
|
|
|
|
crate::constants::parse_constant(token)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
%type expr Index;
|
|
|
|
|
%type stmt Index;
|
|
|
|
|
%type stmts Vec<Index>;
|
|
|
|
|
expr ::= immediate((value, ty)) {
|
|
|
|
|
extra.push(AstNode::Constant { ty, value })
|
|
|
|
|
};
|
|
|
|
|
stmt ::= Semi { extra.push(AstNode::NoopExpr) };
|
|
|
|
|
stmt ::= Comment(text) { extra.push(AstNode::Comment { text: text.to_string() }) };
|
|
|
|
|
stmt ::= expr(expr) Semi { extra.push(AstNode::Stmt { expr }) };
|
|
|
|
|
|
|
|
|
|
stmts ::= stmt(s) { vec![s] };
|
|
|
|
|
stmts ::= stmts(ss) stmt(s) {
|
|
|
|
|
let mut v = ss;
|
|
|
|
|
v.push(s);
|
|
|
|
|
v
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
%type block_inner (Vec<Index>, Option<Index>);
|
|
|
|
|
block_inner ::= {(vec![], None)};
|
|
|
|
|
block_inner ::= expr(expr) {(vec![], Some(expr))};
|
|
|
|
|
block_inner ::= stmts(ss) {(ss, None)};
|
|
|
|
|
block_inner ::= stmts(ss) expr(expr) {(ss, Some(expr))};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
block ::= LBrace block_inner((ss, expr)) RBrace {
|
|
|
|
|
extra.push(AstNode::Block {
|
|
|
|
|
statements: ss,
|
|
|
|
|
expr
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
%type vis Visibility;
|
|
|
|
|
vis ::= Pub { Visibility::Public };
|
|
|
|
|
|
|
|
|
|
%type mutable bool;
|
|
|
|
|
mutable ::= Mutable { true };
|
|
|
|
|
mutable ::= { false };
|
|
|
|
|
|
|
|
|
|
return_type ::= Arrow typ(return_type) { return_type };
|
|
|
|
|
parameter ::= mutable(mutable) Ident(name) Colon typ(param_type) {
|
|
|
|
|
Parameter { mutable, name: name.to_string(), param_type }
|
|
|
|
|
};
|
|
|
|
|
parameter_list ::= parameter(p) {
|
|
|
|
|
let idx = extra.push(AstNode::Parameter { name: p.name, param_type: p.param_type });
|
|
|
|
|
ParameterList { parameters: vec![idx] }
|
|
|
|
|
};
|
|
|
|
|
parameter_list ::= parameter_list(pl) Comma parameter(p) {
|
|
|
|
|
let idx = extra.push(AstNode::Parameter { name: p.name, param_type: p.param_type });
|
|
|
|
|
let mut parameters = pl.parameters;
|
|
|
|
|
parameters.push(idx);
|
|
|
|
|
ParameterList { parameters }
|
|
|
|
|
};
|
|
|
|
|
parameter_list ::= parameter_list(pl) Comma {
|
|
|
|
|
pl
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
decl ::= Comment(text) { extra.push(AstNode::Comment { text: text.to_string() }) };
|
|
|
|
|
decl ::= fn_decl(f) { extra.push(AstNode::FunctionDecl(f)) };
|
|
|
|
|
fn_decl ::= attrs?(attrs) vis?(visibility) Fn Ident(name) LParen parameter_list?(parameters) RParen return_type(rtype) block(body) {
|
|
|
|
|
let name = name.to_string();
|
|
|
|
|
FunctionDecl {
|
|
|
|
|
attrs,
|
|
|
|
|
name,
|
|
|
|
|
visibility: visibility.unwrap_or_default(),
|
|
|
|
|
return_type: rtype,
|
|
|
|
|
parameter_list: parameters,
|
|
|
|
|
body,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
struct SpannedToken<'a> {
|
|
|
|
|
token: Token<'a>,
|
|
|
|
|
span: std::ops::Range<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> From<lexer::Token<'a>> for parser::Token<'a> {
|
|
|
|
|
fn from(value: lexer::Token<'a>) -> Self {
|
|
|
|
|
use lexer::Token;
|
|
|
|
|
match value {
|
|
|
|
|
Token::Fn => Self::Fn,
|
|
|
|
|
Token::OpenParens => Self::LParen,
|
|
|
|
|
Token::CloseParens => Self::RParen,
|
|
|
|
|
Token::OpenBrace => Self::LBrace,
|
|
|
|
|
Token::CloseBrace => Self::RBrace,
|
|
|
|
|
Token::Ident(ident) => Self::Ident(ident),
|
|
|
|
|
Token::Comment(text) => Self::Comment(text),
|
|
|
|
|
Token::DocComment(text) => Self::DocComment(text),
|
|
|
|
|
Token::OpenSquareBracket => todo!(), // Self::LBracket,
|
|
|
|
|
Token::CloseSquareBracket => todo!(), // Self::RBracket,
|
|
|
|
|
Token::Comma => Self::Comma,
|
|
|
|
|
Token::Colon => Self::Colon,
|
|
|
|
|
Token::Semi => Self::Semi,
|
|
|
|
|
Token::Elipsis3 => todo!(),
|
|
|
|
|
Token::Elipsis2 => todo!(),
|
|
|
|
|
Token::Equal => todo!(),
|
|
|
|
|
Token::Void => Self::Void,
|
|
|
|
|
Token::Bool => Self::Bool,
|
|
|
|
|
Token::F32 => Self::F32,
|
|
|
|
|
Token::F64 => Self::F64,
|
|
|
|
|
Token::ISize => Self::ISize,
|
|
|
|
|
Token::USize => Self::USize,
|
|
|
|
|
Token::U1 => Self::U1,
|
|
|
|
|
Token::U8 => Self::U8,
|
|
|
|
|
Token::U16 => Self::U16,
|
|
|
|
|
Token::U32 => Self::U32,
|
|
|
|
|
Token::U64 => Self::U64,
|
|
|
|
|
Token::I1 => Self::I1,
|
|
|
|
|
Token::I8 => Self::I8,
|
|
|
|
|
Token::I16 => Self::I16,
|
|
|
|
|
Token::I32 => Self::I32,
|
|
|
|
|
Token::I64 => Self::I64,
|
|
|
|
|
Token::True => Self::True,
|
|
|
|
|
Token::False => Self::False,
|
|
|
|
|
Token::Const => todo!(), // Self::Const,
|
|
|
|
|
Token::Mutable => Self::Mutable,
|
|
|
|
|
Token::Volatile => todo!(),
|
|
|
|
|
Token::Noalias => todo!(),
|
|
|
|
|
Token::Let => todo!(),
|
|
|
|
|
Token::Var => todo!(),
|
|
|
|
|
Token::If => todo!(),
|
|
|
|
|
Token::As => todo!(),
|
|
|
|
|
Token::Else => todo!(),
|
|
|
|
|
Token::Return => todo!(),
|
|
|
|
|
Token::Struct => todo!(),
|
|
|
|
|
Token::Type => todo!(),
|
|
|
|
|
Token::Union => todo!(),
|
|
|
|
|
Token::Enum => todo!(),
|
|
|
|
|
Token::Packed => todo!(),
|
|
|
|
|
Token::Extern => todo!(),
|
|
|
|
|
Token::Pub => Self::Pub,
|
|
|
|
|
Token::Module => todo!(),
|
|
|
|
|
Token::Dot => todo!(),
|
|
|
|
|
Token::MinusGreater => Self::Arrow,
|
|
|
|
|
Token::Bang => Self::Bang,
|
|
|
|
|
Token::Tilde => todo!(),
|
|
|
|
|
Token::Plus => todo!(),
|
|
|
|
|
Token::Minus => todo!(),
|
|
|
|
|
Token::Star => todo!(),
|
|
|
|
|
Token::Slash => todo!(),
|
|
|
|
|
Token::Percent => todo!(),
|
|
|
|
|
Token::Less => todo!(),
|
|
|
|
|
Token::Greater => todo!(),
|
|
|
|
|
Token::LessEqual => todo!(),
|
|
|
|
|
Token::GreaterEqual => todo!(),
|
|
|
|
|
Token::EqualEqual => todo!(),
|
|
|
|
|
Token::BangEqual => todo!(),
|
|
|
|
|
Token::PipePipe => todo!(),
|
|
|
|
|
Token::AmpersandAmpersand => todo!(),
|
|
|
|
|
Token::Ampersand => todo!(),
|
|
|
|
|
Token::Caret => todo!(),
|
|
|
|
|
Token::Pipe => todo!(),
|
|
|
|
|
Token::LessLess => todo!(),
|
|
|
|
|
Token::GreaterGreater => todo!(),
|
|
|
|
|
Token::Question => todo!(),
|
|
|
|
|
Token::PlusEqual => todo!(),
|
|
|
|
|
Token::MinusEqual => todo!(),
|
|
|
|
|
Token::StarEqual => todo!(),
|
|
|
|
|
Token::SlashEqual => todo!(),
|
|
|
|
|
Token::PercentEqual => todo!(),
|
|
|
|
|
Token::AmpersandEqual => todo!(),
|
|
|
|
|
Token::PipeEqual => todo!(),
|
|
|
|
|
Token::CaretEqual => todo!(),
|
|
|
|
|
Token::LessLessEqual => todo!(),
|
|
|
|
|
Token::GreaterGreaterEqual => todo!(),
|
|
|
|
|
Token::Eof(_) => todo!(),
|
|
|
|
|
Token::ParseError(_) => todo!(),
|
|
|
|
|
Token::CharConstant(_) => todo!(),
|
|
|
|
|
Token::IntegerConstant(_) => Self::Constant(value),
|
|
|
|
|
Token::IntegerHexConstant(_) => Self::Constant(value),
|
|
|
|
|
Token::IntegerBinConstant(_) => Self::Constant(value),
|
|
|
|
|
Token::IntegerOctConstant(_) => Self::Constant(value),
|
|
|
|
|
Token::FloatingConstant(_) => Self::Constant(value),
|
|
|
|
|
Token::FloatingExpConstant(_) => Self::Constant(value),
|
|
|
|
|
Token::DotFloatingConstant(_) => Self::Constant(value),
|
|
|
|
|
Token::DotFloatingExpConstant(_) => Self::Constant(value),
|
|
|
|
|
Token::StringConstant(_) => todo!(),
|
|
|
|
|
}
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
struct SpannedTokenInput<'a> {
|
|
|
|
|
inner: TokenItemIterator<'a>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for SpannedTokenInput<'a> {
|
|
|
|
|
type Item = (Token<'a>, Range<u32>);
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
self.inner.next().map(|item| (item.token, item.span))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type TokenInput<'a> = IterInput<SpannedTokenInput<'a>, Range<u32>>;
|
|
|
|
|
|
|
|
|
|
fn new_token_input<'a>(input: &'a str) -> TokenInput<'a> {
|
|
|
|
|
let num_bytes = input.len() as u32;
|
|
|
|
|
let token_iter = TokenIterator::new(input).into_token_items();
|
|
|
|
|
let spanned_input = SpannedTokenInput { inner: token_iter };
|
|
|
|
|
IterInput::new(spanned_input, num_bytes..num_bytes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn type_parser<'a, E>() -> impl Parser<'a, TokenInput<'a>, Type, E>
|
|
|
|
|
where
|
|
|
|
|
E: chumsky::extra::ParserExtra<'a, TokenInput<'a>> + 'a,
|
|
|
|
|
{
|
|
|
|
|
let primitives = select! {
|
|
|
|
|
Token::Void => InnerType::Unit,
|
|
|
|
|
Token::F32 => InnerType::Float { float_type: FloatType::F32 },
|
|
|
|
|
Token::F64 => InnerType::Float { float_type: FloatType::F64 },
|
|
|
|
|
Token::Bool => InnerType::Bool,
|
|
|
|
|
Token::U1 => InnerType::Int { signed: false, size: IntSize::Bits(1) },
|
|
|
|
|
Token::U8 => InnerType::Int { signed: false, size: IntSize::Bits(8) },
|
|
|
|
|
Token::U16 => InnerType::Int { signed: false, size: IntSize::Bits(16) },
|
|
|
|
|
Token::U32 => InnerType::Int { signed: false, size: IntSize::Bits(32) },
|
|
|
|
|
Token::U64 => InnerType::Int { signed: false, size: IntSize::Bits(64) },
|
|
|
|
|
Token::USize => InnerType::Int { signed: false, size: IntSize::Pointer },
|
|
|
|
|
Token::I8 => InnerType::Int { signed: true, size: IntSize::Bits(8) },
|
|
|
|
|
Token::I16 => InnerType::Int { signed: true, size: IntSize::Bits(16) },
|
|
|
|
|
Token::I32 => InnerType::Int { signed: true, size: IntSize::Bits(32) },
|
|
|
|
|
Token::I64 => InnerType::Int { signed: true, size: IntSize::Bits(64) },
|
|
|
|
|
Token::ISize => InnerType::Int { signed: true, size: IntSize::Pointer },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let custom_int_inner = choice((just::<_, _, extra::Default>('u'), just('i')))
|
|
|
|
|
.then(text::int(10).to_slice().from_str::<u16>().unwrapped())
|
|
|
|
|
.map(|(sign, size)| InnerType::Int {
|
|
|
|
|
signed: sign == 'i',
|
|
|
|
|
size: IntSize::Bits(size),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let custom_int =
|
|
|
|
|
select! {Token::Ident(ident) => ident}.map(move |s| custom_int_inner.parse(s).unwrap());
|
|
|
|
|
|
|
|
|
|
recursive(|ty| {
|
|
|
|
|
let pointer = just(Token::Star)
|
|
|
|
|
.ignore_then(choice((
|
|
|
|
|
just(Token::Mutable).to(true),
|
|
|
|
|
just(Token::Const).to(false),
|
|
|
|
|
)))
|
|
|
|
|
.then(ty)
|
|
|
|
|
.map(|(_mutable, pointee)| InnerType::Pointer { pointee });
|
|
|
|
|
|
|
|
|
|
choice((primitives, custom_int, pointer)).map(|p| Intern::new(p))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visibility<'a>() -> impl Parser<'a, TokenInput<'a>, Visibility, ParserExtra> {
|
|
|
|
|
choice((just(Token::Pub).to(Visibility::Public),))
|
|
|
|
|
.or_not()
|
|
|
|
|
.map(|v| v.unwrap_or(Visibility::Private))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn func_parser() {
|
|
|
|
|
let ident = select! {Token::Ident(ident) => ident};
|
|
|
|
|
|
|
|
|
|
let param = just(Token::Mutable)
|
|
|
|
|
.to(())
|
|
|
|
|
.or_not()
|
|
|
|
|
.then(ident)
|
|
|
|
|
.then_ignore(just(Token::Colon))
|
|
|
|
|
.then(type_parser::<ParserExtra>())
|
|
|
|
|
.map_with(|((mutable, name), param_type), e| {
|
|
|
|
|
e.state().push(AstNode::Parameter(Parameter {
|
|
|
|
|
mutable: mutable.is_some(),
|
|
|
|
|
name: name.to_string(),
|
|
|
|
|
param_type,
|
|
|
|
|
}))
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let params = param
|
|
|
|
|
.separated_by(just(Token::Comma))
|
|
|
|
|
.allow_trailing()
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.delimited_by(just(Token::OpenParens), just(Token::CloseParens))
|
|
|
|
|
.labelled("function parameters")
|
|
|
|
|
.map(|params| ParameterList { parameters: params });
|
|
|
|
|
|
|
|
|
|
let func = visibility()
|
|
|
|
|
.then_ignore(just(Token::Fn))
|
|
|
|
|
.then(ident)
|
|
|
|
|
.then(params)
|
|
|
|
|
// optional return type
|
|
|
|
|
.then(
|
|
|
|
|
just(Token::MinusGreater)
|
|
|
|
|
.ignore_then(type_parser())
|
|
|
|
|
.or_not(),
|
|
|
|
|
)
|
|
|
|
|
.then(block())
|
|
|
|
|
.map_with(|((((vis, ident), params), ret), body), e| {
|
|
|
|
|
e.state().push(AstNode::FunctionDecl(FunctionDecl {
|
|
|
|
|
attrs: None,
|
|
|
|
|
name: ident.to_string(),
|
|
|
|
|
visibility: vis,
|
|
|
|
|
return_type: ret.unwrap_or_else(|| Intern::new(InnerType::Unit)),
|
|
|
|
|
parameter_list: params,
|
|
|
|
|
body,
|
|
|
|
|
}))
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ParserExtra = chumsky::extra::Full<EmptyErr, SimpleState<Ast>, ()>;
|
|
|
|
|
|
|
|
|
|
fn block<'a>() -> impl Parser<'a, TokenInput<'a>, Index, ParserExtra> {
|
|
|
|
|
just(Token::OpenBrace)
|
|
|
|
|
.ignored()
|
|
|
|
|
.then_ignore(just(Token::CloseBrace))
|
|
|
|
|
.map_with(|_, e: &mut MapExtra<'_, '_, _, ParserExtra>| {
|
|
|
|
|
e.state().push(AstNode::Block {
|
|
|
|
|
statements: vec![],
|
|
|
|
|
expr: None,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod constants;
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use crate::AstNode;
|
|
|
|
|
use chumsky::Parser;
|
|
|
|
|
|
|
|
|
|
use crate::{AstNode, new_token_input, type_parser};
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn print_ast_node_size() {
|
|
|
|
|
@ -620,46 +514,55 @@ mod tests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn parse_constant() {
|
|
|
|
|
use crate::parser::{Parser, Token};
|
|
|
|
|
let input = r#"
|
|
|
|
|
fn a() -> u32 {
|
|
|
|
|
42u32
|
|
|
|
|
}
|
|
|
|
|
fn b() -> u32 {
|
|
|
|
|
42i8
|
|
|
|
|
}
|
|
|
|
|
fn c() -> f32 {
|
|
|
|
|
42e4
|
|
|
|
|
}
|
|
|
|
|
"#;
|
|
|
|
|
let mut lex = lexer::TokenIterator::new(input);
|
|
|
|
|
let mut mapped = lex.map(Token::from);
|
|
|
|
|
let mut ast = crate::Ast::new();
|
|
|
|
|
let mut parser = Parser::new(ast);
|
|
|
|
|
while let Some(token) = mapped.next() {
|
|
|
|
|
parser.parse(token).unwrap();
|
|
|
|
|
}
|
|
|
|
|
let (out, ast) = parser.end_of_input().unwrap();
|
|
|
|
|
eprintln!("AST: {:#?}", ast);
|
|
|
|
|
}
|
|
|
|
|
fn parse_types() {
|
|
|
|
|
let ty = type_parser::<chumsky::extra::Default>()
|
|
|
|
|
.parse(new_token_input("i32"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*ty,
|
|
|
|
|
crate::InnerType::Int {
|
|
|
|
|
signed: true,
|
|
|
|
|
size: crate::IntSize::Bits(32)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn parse() {
|
|
|
|
|
use crate::parser::{Parser, Token};
|
|
|
|
|
let input = r#"
|
|
|
|
|
// A simple test case
|
|
|
|
|
/// A function that takes two u32 parameters and returns a u32
|
|
|
|
|
fn main(a: u32, b: u32) -> u32 {}
|
|
|
|
|
"#;
|
|
|
|
|
let mut lex = lexer::TokenIterator::new(input);
|
|
|
|
|
let mut mapped = lex.map(Token::from);
|
|
|
|
|
let mut ast = crate::Ast::new();
|
|
|
|
|
let mut parser = Parser::new(ast);
|
|
|
|
|
while let Some(token) = mapped.next() {
|
|
|
|
|
parser.parse(token).unwrap();
|
|
|
|
|
}
|
|
|
|
|
let (out, ast) = parser.end_of_input().unwrap();
|
|
|
|
|
eprintln!("AST: {:#?}", ast);
|
|
|
|
|
let ty = type_parser::<chumsky::extra::Default>()
|
|
|
|
|
.parse(new_token_input("*const i32"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*ty,
|
|
|
|
|
crate::InnerType::Pointer {
|
|
|
|
|
pointee: crate::Intern::new(crate::InnerType::Int {
|
|
|
|
|
signed: true,
|
|
|
|
|
size: crate::IntSize::Bits(32)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let ty = type_parser::<chumsky::extra::Default>()
|
|
|
|
|
.parse(new_token_input("*mut *const u8"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*ty,
|
|
|
|
|
crate::InnerType::Pointer {
|
|
|
|
|
pointee: crate::Intern::new(crate::InnerType::Pointer {
|
|
|
|
|
pointee: crate::Intern::new(crate::InnerType::Int {
|
|
|
|
|
signed: false,
|
|
|
|
|
size: crate::IntSize::Bits(8)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let ty = type_parser::<chumsky::extra::Default>()
|
|
|
|
|
.parse(new_token_input("i10"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*ty,
|
|
|
|
|
crate::InnerType::Int {
|
|
|
|
|
signed: true,
|
|
|
|
|
size: crate::IntSize::Bits(10)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|