work
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use super::*;
|
||||
|
||||
pub struct Body {
|
||||
pub items: Vec<Parsed<Item>>,
|
||||
pub items: Vec<Item>,
|
||||
pub final_semicolon: bool,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl Node for Body {
|
||||
@@ -15,7 +16,7 @@ impl Node for Body {
|
||||
if at_end(ctx) {
|
||||
break true;
|
||||
}
|
||||
let item: Parsed<Item> = ctx.parse()?;
|
||||
let item: Item = ctx.parse()?;
|
||||
let needs_semicolon = item.needs_semicolon();
|
||||
items.push(item);
|
||||
if at_end(ctx) {
|
||||
@@ -29,6 +30,7 @@ impl Node for Body {
|
||||
Ok(Self {
|
||||
items,
|
||||
final_semicolon,
|
||||
span: ctx.span(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+75
-66
@@ -2,57 +2,58 @@ use crate::parser::VecDspT;
|
||||
|
||||
pub use super::*;
|
||||
|
||||
pub type BoxExpr = Box<Parsed<Expr>>;
|
||||
pub struct Expr {
|
||||
span: Span,
|
||||
ty: ExprTy,
|
||||
}
|
||||
|
||||
pub enum Expr {
|
||||
Block(Parsed<Body>),
|
||||
Group(BoxExpr),
|
||||
pub enum ExprTy {
|
||||
Block(Body),
|
||||
Group(Box<Expr>),
|
||||
Ident(Ident),
|
||||
Lit(Lit),
|
||||
Negate(BoxExpr),
|
||||
Call {
|
||||
target: BoxExpr,
|
||||
args: Vec<Parsed<Expr>>,
|
||||
},
|
||||
Assign {
|
||||
target: BoxExpr,
|
||||
val: BoxExpr,
|
||||
},
|
||||
If {
|
||||
cond: BoxExpr,
|
||||
body: BoxExpr,
|
||||
},
|
||||
Loop {
|
||||
body: BoxExpr,
|
||||
},
|
||||
While {
|
||||
cond: BoxExpr,
|
||||
body: BoxExpr,
|
||||
},
|
||||
Fn(Box<Parsed<Func>>),
|
||||
Negate(Box<Expr>),
|
||||
Call { target: Box<Expr>, args: Vec<Expr> },
|
||||
Assign { target: Box<Expr>, val: Box<Expr> },
|
||||
If { cond: Box<Expr>, body: Box<Expr> },
|
||||
Loop { body: Box<Expr> },
|
||||
While { cond: Box<Expr>, body: Box<Expr> },
|
||||
Fn(Box<Func>),
|
||||
}
|
||||
|
||||
impl Node for Expr {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
let mut res = Self::unit(ctx)?;
|
||||
while let Some(next) = ctx.peek() {
|
||||
res = match next {
|
||||
let ty = match next {
|
||||
Token::Equal => {
|
||||
let target = ctx.push_adv(res).boxed();
|
||||
let val = ctx.parse_with(Self::unit)?.boxed();
|
||||
Expr::Assign { target, val }
|
||||
ctx.next();
|
||||
let target = Box::new(res);
|
||||
let val = Box::new(ctx.parse_with(Self::unit)?);
|
||||
ExprTy::Assign { target, val }
|
||||
}
|
||||
Token::OpenParen => {
|
||||
let target = ctx.push_adv(res).boxed();
|
||||
ctx.next();
|
||||
let target = Box::new(res);
|
||||
let args = ctx.list(Token::Comma, Token::CloseParen)?;
|
||||
Expr::Call { target, args }
|
||||
ExprTy::Call { target, args }
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
};
|
||||
res = Self {
|
||||
ty,
|
||||
span: ctx.span(),
|
||||
};
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
self.ty.fmt(f, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl ExprTy {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Ident(ident) => ident.fmt(f, ctx),
|
||||
@@ -92,60 +93,77 @@ impl Node for Expr {
|
||||
}
|
||||
|
||||
impl Expr {
|
||||
pub fn fmt_body(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match self.ty {
|
||||
ExprTy::Block(_) => self.fmt(f, ctx),
|
||||
_ => write!(f, "=> {}", self.dsp(ctx)),
|
||||
}
|
||||
}
|
||||
|
||||
fn unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
Ok(match ctx.expect_next()? {
|
||||
Token::Dash => Self::Negate(ctx.parse_box()?),
|
||||
Token::Ident(s) => Self::Ident(Ident(s)),
|
||||
Token::Lit(l) => Self::Lit(l),
|
||||
Token::Fn => Self::Fn(ctx.parse_box()?),
|
||||
let ty = match ctx.expect_next()? {
|
||||
Token::Dash => ExprTy::Negate(ctx.parse_box()?),
|
||||
Token::Ident(s) => ExprTy::Ident(ctx.ident(s)),
|
||||
Token::Lit(l) => ExprTy::Lit(ctx.lit(l)),
|
||||
Token::Fn => ExprTy::Fn(ctx.parse_box()?),
|
||||
Token::If => {
|
||||
let cond = ctx.parse_box()?;
|
||||
let body = Self::body(ctx)?.boxed();
|
||||
Self::If { cond, body }
|
||||
let body = Box::new(Self::body(ctx)?);
|
||||
ExprTy::If { cond, body }
|
||||
}
|
||||
Token::While => {
|
||||
let cond = ctx.parse_box()?;
|
||||
let body = Self::body(ctx)?.boxed();
|
||||
Self::While { cond, body }
|
||||
let body = Box::new(Self::body(ctx)?);
|
||||
ExprTy::While { cond, body }
|
||||
}
|
||||
Token::Loop => {
|
||||
let body = ctx.parse_box()?;
|
||||
Self::Loop { body }
|
||||
ExprTy::Loop { body }
|
||||
}
|
||||
Token::OpenParen => {
|
||||
if ctx.next_if(Token::CloseParen) {
|
||||
Self::Lit(Lit::Unit)
|
||||
ExprTy::Lit(Lit {
|
||||
ty: LitTy::Unit,
|
||||
span: ctx.span(),
|
||||
})
|
||||
} else {
|
||||
let inner = ctx.parse_box()?;
|
||||
ctx.expect(Token::CloseParen)?;
|
||||
Self::Group(inner)
|
||||
ExprTy::Group(inner)
|
||||
}
|
||||
}
|
||||
Token::OpenCurly => {
|
||||
let body = ctx.parse()?;
|
||||
ctx.expect(Token::CloseCurly)?;
|
||||
Self::Block(body)
|
||||
ExprTy::Block(body)
|
||||
}
|
||||
other => return ctx.unexpected(&other, "an expression"),
|
||||
};
|
||||
Ok(Self {
|
||||
ty,
|
||||
span: ctx.span(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_group(&self) -> bool {
|
||||
matches!(self, Expr::Group(_))
|
||||
matches!(self.ty, ExprTy::Group(_))
|
||||
}
|
||||
|
||||
pub fn is_block(&self) -> bool {
|
||||
matches!(self, Expr::Block(_))
|
||||
matches!(self.ty, ExprTy::Block(_))
|
||||
}
|
||||
|
||||
pub fn block(ctx: &mut ParseCtx) -> Result<Expr, CompilerMsg> {
|
||||
ctx.expect(Token::OpenCurly)?;
|
||||
let id = ctx.parse()?;
|
||||
ctx.expect(Token::CloseCurly)?;
|
||||
Ok(Expr::Block(id))
|
||||
Ok(Expr {
|
||||
ty: ExprTy::Block(id),
|
||||
span: ctx.span(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn body(ctx: &mut ParseCtx) -> Result<Parsed<Expr>, CompilerMsg> {
|
||||
pub fn body(ctx: &mut ParseCtx) -> Result<Expr, CompilerMsg> {
|
||||
if ctx.next_if(Token::DoubleArrow) {
|
||||
ctx.parse()
|
||||
} else {
|
||||
@@ -154,24 +172,15 @@ impl Expr {
|
||||
}
|
||||
|
||||
pub fn ends_with_block(&self) -> bool {
|
||||
match self {
|
||||
Expr::Block(..) => true,
|
||||
Expr::Loop { body }
|
||||
| Expr::While { body, .. }
|
||||
| Expr::If { body, .. }
|
||||
| Expr::Negate(body)
|
||||
| Expr::Assign { val: body, .. } => body.ends_with_block(),
|
||||
Expr::Fn(f) => f.ends_with_block(),
|
||||
match &self.ty {
|
||||
ExprTy::Block(..) => true,
|
||||
ExprTy::Loop { body }
|
||||
| ExprTy::While { body, .. }
|
||||
| ExprTy::If { body, .. }
|
||||
| ExprTy::Negate(body)
|
||||
| ExprTy::Assign { val: body, .. } => body.ends_with_block(),
|
||||
ExprTy::Fn(f) => f.ends_with_block(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsed<Expr> {
|
||||
pub fn fmt_body(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match &self.node {
|
||||
Expr::Block(_) => self.node.fmt(f, ctx),
|
||||
_ => write!(f, "=> {}", self.dsp(ctx)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
use super::*;
|
||||
|
||||
pub struct Func {
|
||||
args: Vec<Parsed<Param>>,
|
||||
ret: Option<Parsed<Type>>,
|
||||
body: Parsed<Expr>,
|
||||
args: Vec<Param>,
|
||||
name: Option<Ident>,
|
||||
ret: Option<Type>,
|
||||
body: Expr,
|
||||
span: Span,
|
||||
}
|
||||
|
||||
impl Node for Func {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
let mut name = None;
|
||||
if let Token::Ident(ident) = ctx.expect_peek()? {
|
||||
// yucky
|
||||
let ident = ident.to_string();
|
||||
ctx.next();
|
||||
let ident = ctx.ident(ident);
|
||||
name = Some(ident);
|
||||
}
|
||||
ctx.expect(Token::OpenParen)?;
|
||||
let args = ctx.list(Token::Comma, Token::CloseParen)?;
|
||||
let mut ret = None;
|
||||
@@ -15,11 +25,21 @@ impl Node for Func {
|
||||
ret = Some(ctx.parse()?);
|
||||
}
|
||||
let body = Expr::body(ctx)?;
|
||||
Ok(Self { args, ret, body })
|
||||
Ok(Self {
|
||||
args,
|
||||
ret,
|
||||
body,
|
||||
name,
|
||||
span: ctx.span(),
|
||||
})
|
||||
}
|
||||
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
write!(f, "fn(")?;
|
||||
write!(f, "fn")?;
|
||||
if let Some(name) = &self.name {
|
||||
write!(f, " {name}")?;
|
||||
}
|
||||
write!(f, "(")?;
|
||||
if let Some((last, rest)) = self.args.split_last() {
|
||||
for arg in rest {
|
||||
write!(f, "{}, ", arg.dsp(ctx))?;
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
use super::*;
|
||||
|
||||
pub struct Ident(pub String);
|
||||
pub struct Ident {
|
||||
pub name: String,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl Node for Ident {
|
||||
fn parse(ctx: &mut super::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
|
||||
match ctx.expect_next()? {
|
||||
Token::Ident(ident) => Ok(Self(ident)),
|
||||
Token::Ident(ident) => Ok(ctx.ident(ident)),
|
||||
t => ctx.unexpected(&t, "an identifier"),
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
write!(f, "{}", self.name)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Ident {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.name.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
+23
-14
@@ -1,17 +1,22 @@
|
||||
use super::*;
|
||||
|
||||
pub enum Item {
|
||||
pub struct Item {
|
||||
pub ty: ItemTy,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
pub enum ItemTy {
|
||||
Let {
|
||||
name: Parsed<Ident>,
|
||||
ty: Option<Parsed<Type>>,
|
||||
val: Parsed<Expr>,
|
||||
name: Ident,
|
||||
ty: Option<Type>,
|
||||
val: Expr,
|
||||
},
|
||||
Expr(Parsed<Expr>),
|
||||
Expr(Expr),
|
||||
}
|
||||
|
||||
impl Node for Item {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
Ok(match ctx.expect_peek()? {
|
||||
let ty = match ctx.expect_peek()? {
|
||||
Token::Let => {
|
||||
ctx.next();
|
||||
let name = ctx.parse()?;
|
||||
@@ -21,22 +26,26 @@ impl Node for Item {
|
||||
}
|
||||
ctx.expect(Token::Equal)?;
|
||||
let val = ctx.parse()?;
|
||||
Self::Let { name, ty, val }
|
||||
ItemTy::Let { name, ty, val }
|
||||
}
|
||||
_ => Self::Expr(ctx.parse()?),
|
||||
_ => ItemTy::Expr(ctx.parse()?),
|
||||
};
|
||||
Ok(Self {
|
||||
ty,
|
||||
span: ctx.span(),
|
||||
})
|
||||
}
|
||||
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match self {
|
||||
Item::Let { name, ty, val } => {
|
||||
match &self.ty {
|
||||
ItemTy::Let { name, ty, val } => {
|
||||
write!(f, "let {}", name.dsp(ctx))?;
|
||||
if let Some(ty) = ty {
|
||||
write!(f, ": {}", ty.dsp(ctx))?;
|
||||
}
|
||||
write!(f, " = {}", val.dsp(ctx))?;
|
||||
}
|
||||
Item::Expr(id) => id.fmt(f, ctx)?,
|
||||
ItemTy::Expr(id) => id.fmt(f, ctx)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -44,9 +53,9 @@ impl Node for Item {
|
||||
|
||||
impl Item {
|
||||
pub fn ends_with_block(&self) -> bool {
|
||||
match self {
|
||||
Item::Let { val, .. } => val.ends_with_block(),
|
||||
Item::Expr(id) => id.ends_with_block(),
|
||||
match &self.ty {
|
||||
ItemTy::Let { val, .. } => val.ends_with_block(),
|
||||
ItemTy::Expr(id) => id.ends_with_block(),
|
||||
}
|
||||
}
|
||||
pub fn needs_semicolon(&self) -> bool {
|
||||
|
||||
@@ -14,5 +14,5 @@ pub use item::*;
|
||||
pub use param::*;
|
||||
pub use ty::*;
|
||||
|
||||
use super::{DisplayCtx, Lit, Node, ParseCtx, Parsed, Token};
|
||||
use crate::io::CompilerMsg;
|
||||
use super::{DisplayCtx, Lit, LitTy, Node, ParseCtx, Token};
|
||||
use crate::io::{CompilerMsg, Span};
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use super::*;
|
||||
|
||||
pub struct Param {
|
||||
name: Parsed<Ident>,
|
||||
ty: Option<Parsed<Type>>,
|
||||
name: Ident,
|
||||
ty: Option<Type>,
|
||||
}
|
||||
|
||||
impl Node for Param {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
pub enum Type {
|
||||
Ident(Parsed<Ident>),
|
||||
Ident(Ident),
|
||||
}
|
||||
|
||||
impl Node for Type {
|
||||
|
||||
Reference in New Issue
Block a user