This commit is contained in:
2026-04-17 01:49:43 -04:00
parent e5ae506a84
commit 2f91e454dd
16 changed files with 268 additions and 401 deletions
+6 -13
View File
@@ -1,11 +1,11 @@
use super::*;
pub struct Body {
pub items: Vec<Id<Item>>,
pub items: Vec<Parsed<Item>>,
pub final_semicolon: bool,
}
impl Parsable for Body {
impl Node for Body {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut items = Vec::new();
fn at_end(ctx: &mut ParseCtx) -> bool {
@@ -15,8 +15,8 @@ impl Parsable for Body {
if at_end(ctx) {
break true;
}
let item: Id<Item> = ctx.parse()?;
let needs_semicolon = item.needs_semicolon(&ctx.nodes);
let item: Parsed<Item> = ctx.parse()?;
let needs_semicolon = item.needs_semicolon();
items.push(item);
if at_end(ctx) {
break false;
@@ -31,23 +31,16 @@ impl Parsable for Body {
final_semicolon,
})
}
}
impl FmtNode for Body {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
// surely there's a better way to do this
if let Some((last, rest)) = self.items.split_last() {
for &i in rest {
for i in rest {
writeln!(
f,
"{}{}{}",
" ".repeat(ctx.indent),
i.dsp(ctx),
if i.needs_semicolon(ctx.nodes) {
";"
} else {
""
}
if i.needs_semicolon() { ";" } else { "" }
)?;
}
writeln!(
+84 -78
View File
@@ -2,46 +2,48 @@ use crate::parser::VecDspT;
pub use super::*;
pub type BoxExpr = Box<Parsed<Expr>>;
pub enum Expr {
Block(Id<Body>),
Group(Id<Expr>),
Ident(Id<Ident>),
Lit(Id<Lit>),
Negate(Id<Expr>),
Block(Parsed<Body>),
Group(BoxExpr),
Ident(Ident),
Lit(Lit),
Negate(BoxExpr),
Call {
target: Id<Expr>,
args: Vec<Id<Expr>>,
target: BoxExpr,
args: Vec<Parsed<Expr>>,
},
Assign {
target: Id<Expr>,
val: Id<Expr>,
target: BoxExpr,
val: BoxExpr,
},
If {
cond: Id<Expr>,
body: Id<Expr>,
cond: BoxExpr,
body: BoxExpr,
},
Loop {
body: Id<Expr>,
body: BoxExpr,
},
While {
cond: Id<Expr>,
body: Id<Expr>,
cond: BoxExpr,
body: BoxExpr,
},
Fn(Id<Func>),
Fn(Box<Parsed<Func>>),
}
impl Parsable for Expr {
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 {
Token::Equal => {
let target = ctx.push_adv(res);
let val = ctx.parse_with(Self::unit)?;
let target = ctx.push_adv(res).boxed();
let val = ctx.parse_with(Self::unit)?.boxed();
Expr::Assign { target, val }
}
Token::OpenParen => {
let target = ctx.push_adv(res);
let target = ctx.push_adv(res).boxed();
let args = ctx.list(Token::Comma, Token::CloseParen)?;
Expr::Call { target, args }
}
@@ -50,34 +52,71 @@ impl Parsable for Expr {
}
Ok(res)
}
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
match self {
Self::Ident(ident) => ident.fmt(f, ctx),
Self::Group(expr) => write!(f, "({})", expr.dsp(ctx)),
Self::Fn(func) => func.fmt(f, ctx),
Self::Lit(lit) => write!(f, "{}", lit),
Self::Negate(expr) => {
write!(f, "-{}", expr.dsp(ctx))
}
Self::Call { target, args } => {
write!(f, "{}({})", target.dsp(ctx), args.dsp(ctx))
}
Self::Assign { target, val } => {
write!(f, "{} = {}", target.dsp(ctx), val.dsp(ctx))
}
Self::If { cond, body } => {
write!(f, "if {} {}", cond.dsp(ctx), body.dsp(ctx))
}
Self::While { cond, body } => {
write!(f, "while {} {}", cond.dsp(ctx), body.dsp(ctx))
}
Self::Loop { body } => {
write!(f, "loop {}", body.dsp(ctx))
}
Self::Block(body) => {
write!(f, "{{")?;
if !body.items.is_empty() {
writeln!(f)?;
ctx.indent += 3;
body.fmt(f, ctx)?;
}
write!(f, "}}")?;
Ok(())
}
}
}
}
impl Expr {
fn unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_next()? {
Token::Dash => Self::Negate(ctx.parse()?),
Token::Ident(s) => Self::Ident(ctx.ident(s)),
Token::Lit(l) => Self::Lit(ctx.lit(l)),
Token::Fn => Self::Fn(ctx.parse()?),
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()?),
Token::If => {
let cond = ctx.parse()?;
let body = cond_body(ctx)?;
let cond = ctx.parse_box()?;
let body = Self::body(ctx)?.boxed();
Self::If { cond, body }
}
Token::While => {
let cond = ctx.parse()?;
let body = cond_body(ctx)?;
let cond = ctx.parse_box()?;
let body = Self::body(ctx)?.boxed();
Self::While { cond, body }
}
Token::Loop => {
let body = ctx.parse()?;
let body = ctx.parse_box()?;
Self::Loop { body }
}
Token::OpenParen => {
if ctx.next_if(Token::CloseParen) {
Self::Lit(ctx.push(Lit::Unit))
Self::Lit(Lit::Unit)
} else {
let inner = ctx.parse()?;
let inner = ctx.parse_box()?;
ctx.expect(Token::CloseParen)?;
Self::Group(inner)
}
@@ -105,67 +144,34 @@ impl Expr {
ctx.expect(Token::CloseCurly)?;
Ok(Expr::Block(id))
}
}
fn cond_body(ctx: &mut ParseCtx) -> Result<Id<Expr>, CompilerMsg> {
if ctx.next_if(Token::Do) {
ctx.parse()
} else {
ctx.parse_with(Expr::block)
pub fn body(ctx: &mut ParseCtx) -> Result<Parsed<Expr>, CompilerMsg> {
if ctx.next_if(Token::DoubleArrow) {
ctx.parse()
} else {
ctx.parse_with(Expr::block)
}
}
}
impl Id<Expr> {
pub fn ends_with_block(&self, nodes: &Nodes) -> bool {
match nodes[self] {
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(nodes),
Expr::Fn(f) => f.ends_with_block(nodes),
| Expr::Assign { val: body, .. } => body.ends_with_block(),
Expr::Fn(f) => f.ends_with_block(),
_ => false,
}
}
}
impl FmtNode for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
let do_ = |id: Id<Expr>| if ctx.nodes[id].is_block() { "" } else { "do " };
match *self {
Self::Ident(id) => id.fmt(f, ctx),
Self::Group(id) => write!(f, "({})", id.dsp(ctx)),
Self::Fn(id) => id.fmt(f, ctx),
Self::Lit(id) => id.fmt(f, ctx),
Self::Negate(id) => {
write!(f, "-{}", id.dsp(ctx))
}
Self::Call { target, ref args } => {
write!(f, "{}({})", target.dsp(ctx), args.dsp(ctx))
}
Self::Assign { target, val } => {
write!(f, "{} = {}", target.dsp(ctx), val.dsp(ctx))
}
Self::If { cond, body } => {
write!(f, "if {} {}{}", cond.dsp(ctx), do_(body), body.dsp(ctx))
}
Self::While { cond, body } => {
write!(f, "while {} {}{}", cond.dsp(ctx), do_(body), body.dsp(ctx))
}
Self::Loop { body } => {
write!(f, "loop {}", body.dsp(ctx))
}
Self::Block(body) => {
write!(f, "{{")?;
if !ctx.nodes[body].items.is_empty() {
writeln!(f)?;
ctx.indent += 3;
body.fmt(f, ctx)?;
}
write!(f, "}}")?;
Ok(())
}
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)),
}
}
}
+10 -16
View File
@@ -1,12 +1,12 @@
use super::*;
pub struct Func {
args: Vec<Id<Param>>,
ret: Option<Id<Type>>,
body: Id<Expr>,
args: Vec<Parsed<Param>>,
ret: Option<Parsed<Type>>,
body: Parsed<Expr>,
}
impl Parsable for Func {
impl Node for Func {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
ctx.expect(Token::OpenParen)?;
let args = ctx.list(Token::Comma, Token::CloseParen)?;
@@ -14,16 +14,10 @@ impl Parsable for Func {
if ctx.next_if(Token::Arrow) {
ret = Some(ctx.parse()?);
}
let body = if ret.is_some() {
ctx.parse_with(Expr::block)
} else {
ctx.parse()
}?;
let body = Expr::body(ctx)?;
Ok(Self { args, ret, body })
}
}
impl FmtNode for Func {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "fn(")?;
if let Some((last, rest)) = self.args.split_last() {
@@ -33,16 +27,16 @@ impl FmtNode for Func {
write!(f, "{}", last.dsp(ctx))?;
}
write!(f, ") ")?;
if let Some(ret) = self.ret {
if let Some(ret) = &self.ret {
write!(f, "-> {} ", ret.dsp(ctx))?;
}
self.body.fmt(f, ctx)?;
self.body.fmt_body(f, ctx)?;
Ok(())
}
}
impl Id<Func> {
pub fn ends_with_block(&self, nodes: &Nodes) -> bool {
nodes[self].body.ends_with_block(nodes)
impl Func {
pub fn ends_with_block(&self) -> bool {
self.body.ends_with_block()
}
}
+7 -11
View File
@@ -1,20 +1,16 @@
use super::*;
pub struct Ident {
pub inner: String,
}
pub struct Ident(pub String);
impl FmtNode for Ident {
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl Parsable for Ident {
impl Node for Ident {
fn parse(ctx: &mut super::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
match ctx.expect_next()? {
Token::Ident(ident) => Ok(Self { inner: ident }),
Token::Ident(ident) => Ok(Self(ident)),
t => ctx.unexpected(&t, "an identifier"),
}
}
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
+12 -15
View File
@@ -2,15 +2,14 @@ use super::*;
pub enum Item {
Let {
name: Id<Ident>,
ty: Option<Id<Type>>,
val: Id<Expr>,
name: Parsed<Ident>,
ty: Option<Parsed<Type>>,
val: Parsed<Expr>,
},
Struct(Id<Struct>),
Expr(Id<Expr>),
Expr(Parsed<Expr>),
}
impl Parsable for Item {
impl Node for Item {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_peek()? {
Token::Let => {
@@ -27,9 +26,7 @@ impl Parsable for Item {
_ => Self::Expr(ctx.parse()?),
})
}
}
impl FmtNode for Item {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
match self {
Item::Let { name, ty, val } => {
@@ -45,14 +42,14 @@ impl FmtNode for Item {
}
}
impl Id<Item> {
pub fn ends_with_block(&self, nodes: &Nodes) -> bool {
match nodes[self] {
Item::Let { name, ty, val } => val.ends_with_block(nodes),
Item::Expr(id) => id.ends_with_block(nodes),
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(),
}
}
pub fn needs_semicolon(&self, nodes: &Nodes) -> bool {
!self.ends_with_block(nodes)
pub fn needs_semicolon(&self) -> bool {
!self.ends_with_block()
}
}
+1 -33
View File
@@ -12,39 +12,7 @@ pub use func::*;
pub use ident::*;
pub use item::*;
pub use param::*;
pub use struct_::*;
pub use ty::*;
use super::{DisplayCtx, FmtNode, Id, Lit, Node, NodeVec, Parsable, ParseCtx, Token};
use super::{DisplayCtx, Lit, Node, ParseCtx, Parsed, Token};
use crate::io::CompilerMsg;
def_nodes! {
exprs: Expr,
idents: Ident,
blocks: Body,
lits: Lit,
types: Type,
funcs: Func,
params: Param,
items: Item,
structs: Struct,
}
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;
+4 -6
View File
@@ -1,11 +1,11 @@
use super::*;
pub struct Param {
name: Id<Ident>,
ty: Option<Id<Type>>,
name: Parsed<Ident>,
ty: Option<Parsed<Type>>,
}
impl Parsable for Param {
impl Node for Param {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let name = ctx.parse()?;
let mut ty = None;
@@ -14,12 +14,10 @@ impl Parsable for Param {
}
Ok(Self { name, ty })
}
}
impl FmtNode for Param {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
self.name.fmt(f, ctx)?;
if let Some(ty) = self.ty {
if let Some(ty) = &self.ty {
write!(f, ": {}", ty.dsp(ctx))?;
}
Ok(())
+1 -7
View File
@@ -5,10 +5,4 @@ pub struct Struct {
fields: Vec<Field>,
}
impl Parsable for Struct {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
}
}
pub struct Field {
}
pub struct Field {}
+2 -4
View File
@@ -1,19 +1,17 @@
use super::*;
pub enum Type {
Ident(Id<Ident>),
Ident(Parsed<Ident>),
}
impl Parsable for Type {
impl Node 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),