work
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
use super::*;
|
||||
|
||||
pub struct Block {
|
||||
statements: Vec<Id<Statement>>,
|
||||
}
|
||||
|
||||
impl Parsable for Block {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
let mut statements = Vec::new();
|
||||
if *ctx.expect_peek()? != Token::CloseCurly {
|
||||
statements.push(ctx.parse()?);
|
||||
while *ctx.expect_peek()? == Token::Semicolon {
|
||||
ctx.next();
|
||||
statements.push(ctx.parse()?);
|
||||
}
|
||||
}
|
||||
Ok(Self { statements })
|
||||
}
|
||||
}
|
||||
|
||||
impl FmtNode for Block {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
|
||||
ctx.indent += 3;
|
||||
write!(f, "{{")?;
|
||||
if !self.statements.is_empty() {
|
||||
writeln!(f)?;
|
||||
}
|
||||
for &s in &self.statements {
|
||||
writeln!(f, "{}{};", " ".repeat(ctx.indent), s.dsp(ctx))?;
|
||||
}
|
||||
write!(f, "}}")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ impl Parsable for Expr {
|
||||
Token::Dash => Self::Negate(ctx.parse()?),
|
||||
Token::Ident(s) => Self::Ident(ctx.ident(s)),
|
||||
Token::Lit(l) => Self::Lit(ctx.lit(l)),
|
||||
other => return ctx.unexpected(other, "an expression"),
|
||||
other => return ctx.unexpected(&other, "an expression"),
|
||||
};
|
||||
let Some(next) = ctx.peek() else {
|
||||
return Ok(e1);
|
||||
|
||||
@@ -14,7 +14,7 @@ impl Parsable for Ident {
|
||||
fn parse(ctx: &mut super::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
|
||||
match ctx.expect_next()? {
|
||||
Token::Ident(ident) => Ok(Self { inner: ident }),
|
||||
t => ctx.unexpected(t, "an identifier"),
|
||||
t => ctx.unexpected(&t, "an identifier"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
use super::*;
|
||||
|
||||
pub enum Item {
|
||||
Module(Id<Module>),
|
||||
Statement(Id<Statement>),
|
||||
}
|
||||
|
||||
impl Parsable for Item {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
Ok(match ctx.expect_peek()? {
|
||||
Token::Fn => Self::Module(ctx.parse()?),
|
||||
_ => Self::Statement(ctx.parse()?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FmtNode for Item {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match self {
|
||||
Item::Module(id) => write!(f, "{}", id.dsp(ctx)),
|
||||
Item::Statement(id) => write!(f, "{}", id.dsp(ctx)),
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
-3
@@ -1,11 +1,44 @@
|
||||
mod block;
|
||||
mod expr;
|
||||
mod ident;
|
||||
mod item;
|
||||
mod module;
|
||||
mod statement;
|
||||
pub use block::*;
|
||||
mod ty;
|
||||
pub use expr::*;
|
||||
pub use ident::*;
|
||||
pub use item::*;
|
||||
pub use module::*;
|
||||
pub use statement::*;
|
||||
pub use ty::*;
|
||||
|
||||
use super::{DisplayCtx, FmtNode, Id, Lit, Parsable, ParseCtx, Token};
|
||||
use super::{DisplayCtx, FmtNode, Id, Lit, Node, NodeVec, Parsable, ParseCtx, Token};
|
||||
use crate::io::CompilerMsg;
|
||||
|
||||
def_nodes! {
|
||||
exprs: Expr,
|
||||
idents: Ident,
|
||||
statements: Statement,
|
||||
blocks: Module,
|
||||
lits: Lit,
|
||||
types: Type,
|
||||
items: Item,
|
||||
}
|
||||
|
||||
macro_rules! def_nodes {
|
||||
{$($field:ident: $ty:ident,)*} => {
|
||||
#[derive(Default)]
|
||||
pub struct Nodes {
|
||||
$(pub $field: NodeVec<$ty>,)*
|
||||
}
|
||||
|
||||
$(impl Node for $ty {
|
||||
fn vec(nodes: &Nodes) -> &NodeVec<Self> {
|
||||
&nodes.$field
|
||||
}
|
||||
fn vec_mut(nodes: &mut Nodes) -> &mut NodeVec<Self> {
|
||||
&mut nodes.$field
|
||||
}
|
||||
})*
|
||||
};
|
||||
}
|
||||
use def_nodes;
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
use super::*;
|
||||
|
||||
pub struct Module {
|
||||
items: Vec<Id<Item>>,
|
||||
}
|
||||
|
||||
impl Parsable for Module {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
let mut items = Vec::new();
|
||||
if ctx.peek().is_none() {
|
||||
return Ok(Self { items });
|
||||
}
|
||||
items.push(ctx.parse()?);
|
||||
while *ctx.expect_peek()? == Token::Semicolon {
|
||||
ctx.next();
|
||||
items.push(ctx.parse()?);
|
||||
}
|
||||
Ok(Self { items })
|
||||
}
|
||||
}
|
||||
|
||||
impl FmtNode for Module {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
|
||||
ctx.indent += 3;
|
||||
write!(f, "{{")?;
|
||||
if !self.items.is_empty() {
|
||||
writeln!(f)?;
|
||||
}
|
||||
for &i in &self.items {
|
||||
writeln!(f, "{}{};", " ".repeat(ctx.indent), i.dsp(ctx))?;
|
||||
}
|
||||
write!(f, "}}")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,15 @@
|
||||
pub use super::*;
|
||||
|
||||
pub enum Statement {
|
||||
Let(Id<Ident>, Id<Expr>),
|
||||
If { cond: Id<Expr>, body: Id<Expr> },
|
||||
Let {
|
||||
name: Id<Ident>,
|
||||
ty: Option<Id<Type>>,
|
||||
val: Id<Expr>,
|
||||
},
|
||||
If {
|
||||
cond: Id<Expr>,
|
||||
body: Id<Expr>,
|
||||
},
|
||||
Expr(Id<Expr>),
|
||||
}
|
||||
|
||||
@@ -12,8 +19,16 @@ impl Parsable for Statement {
|
||||
Token::Let => {
|
||||
ctx.next();
|
||||
let name = ctx.parse()?;
|
||||
let mut ty = None;
|
||||
if ctx.next_if(Token::Colon) {
|
||||
ty = Some(ctx.parse()?);
|
||||
}
|
||||
ctx.expect(Token::Equal)?;
|
||||
Self::Let(name, ctx.parse()?)
|
||||
Self::Let {
|
||||
name,
|
||||
ty,
|
||||
val: ctx.parse()?,
|
||||
}
|
||||
}
|
||||
Token::If => {
|
||||
ctx.next();
|
||||
@@ -32,8 +47,12 @@ impl FmtNode for Statement {
|
||||
Self::If { cond, body } => {
|
||||
write!(f, "if {} {}", cond.dsp(ctx), body.dsp(ctx))
|
||||
}
|
||||
Self::Let(name, expr) => {
|
||||
write!(f, "let {} = {}", name.dsp(ctx), expr.dsp(ctx))
|
||||
Self::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))
|
||||
}
|
||||
Self::Expr(expr) => expr.fmt(f, ctx),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
use super::*;
|
||||
|
||||
pub enum Type {
|
||||
Ident(Id<Ident>),
|
||||
}
|
||||
|
||||
impl Parsable for Type {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
Ok(match ctx.expect_next()? {
|
||||
Token::Ident(s) => Self::Ident(ctx.ident(s)),
|
||||
t => ctx.unexpected(&t, "a type")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FmtNode for Type {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match self {
|
||||
Type::Ident(id) => id.fmt(f, ctx),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user