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
+4 -4
View File
@@ -1,4 +1,4 @@
use crate::{io::CompilerOutput, parser::Nodes}; use crate::{io::CompilerOutput, parser::parse_root};
mod io; mod io;
mod parser; mod parser;
@@ -10,9 +10,9 @@ fn main() {
return; return;
}; };
let mut output = CompilerOutput::new(); let mut output = CompilerOutput::new();
let nodes = Nodes::parse_root(&path, &mut output); let nodes = parse_root(&path, &mut output);
if let Some((nodes, root)) = nodes { if let Some(root) = nodes {
print!("{}", root.dsp(&nodes)); print!("{root}");
} }
output.write(&mut std::io::stdout()); output.write(&mut std::io::stdout());
} }
-8
View File
@@ -1,5 +1,3 @@
use crate::parser::{DisplayCtx, FmtNode};
use super::Token; use super::Token;
#[derive(PartialEq)] #[derive(PartialEq)]
@@ -26,9 +24,3 @@ impl std::fmt::Display for Lit {
} }
} }
} }
impl FmtNode for Lit {
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
write!(f, "{self}")
}
}
@@ -1,40 +1,33 @@
use std::ops::Index;
use crate::{ use crate::{
io::{CompilerMsg, Span}, io::{CompilerMsg, Span},
parser::{ parser::{
Id, Ident, Node, Nodes, Ident, Node, Parsed,
cursor::{Cursor, Lit, Token}, cursor::{Cursor, Lit, Token},
}, },
}; };
pub trait Parsable: Sized + Node {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>;
}
pub struct ParseCtx<'a> { pub struct ParseCtx<'a> {
start: usize, start: usize,
cursor: Cursor<'a>, cursor: Cursor<'a>,
pub nodes: Nodes,
} }
impl<'a> ParseCtx<'a> { impl<'a> ParseCtx<'a> {
pub fn new(cursor: Cursor<'a>) -> Self { pub fn new(cursor: Cursor<'a>) -> Self {
Self { Self { start: 0, cursor }
start: 0,
nodes: Nodes::default(),
cursor,
}
} }
pub fn parse<N: Parsable>(&mut self) -> Result<Id<N>, CompilerMsg> { pub fn parse_box<N: Node>(&mut self) -> Result<Box<Parsed<N>>, CompilerMsg> {
self.parse_with(N::parse).map(Parsed::boxed)
}
pub fn parse<N: Node>(&mut self) -> Result<Parsed<N>, CompilerMsg> {
self.parse_with(N::parse) self.parse_with(N::parse)
} }
pub fn parse_with<N: Node>( pub fn parse_with<N: Node>(
&mut self, &mut self,
f: impl FnOnce(&mut Self) -> Result<N, CompilerMsg>, f: impl FnOnce(&mut Self) -> Result<N, CompilerMsg>,
) -> Result<Id<N>, CompilerMsg> { ) -> Result<Parsed<N>, CompilerMsg> {
let old_start = self.start; let old_start = self.start;
self.start = self.cursor.peek_start(); self.start = self.cursor.peek_start();
let res = f(self).map(|r| self.push(r)); let res = f(self).map(|r| self.push(r));
@@ -42,25 +35,25 @@ impl<'a> ParseCtx<'a> {
res res
} }
pub fn ident(&mut self, s: String) -> Id<Ident> { pub fn ident(&mut self, s: String) -> Parsed<Ident> {
let span = self.cursor.span; let span = self.cursor.span;
self.nodes.idents.add(Ident { inner: s }, span) Parsed::new(Ident(s), span)
} }
pub fn lit(&mut self, lit: Lit) -> Id<Lit> { pub fn lit(&mut self, lit: Lit) -> Parsed<Lit> {
let span = self.cursor.span; let span = self.cursor.span;
self.nodes.lits.add(lit, span) Parsed::new(lit, span)
} }
pub fn push_adv<N: Node>(&mut self, node: N) -> Id<N> { pub fn push_adv<N: Node>(&mut self, node: N) -> Parsed<N> {
let res = self.push(node); let res = self.push(node);
self.cursor.next(); self.cursor.next();
res res
} }
pub fn push<N: Node>(&mut self, node: N) -> Id<N> { pub fn push<N: Node>(&mut self, node: N) -> Parsed<N> {
let end = self.cursor.cur_end(); let end = self.cursor.cur_end();
N::vec_mut(&mut self.nodes).add( Parsed::new(
node, node,
Span { Span {
file: self.cursor.file(), file: self.cursor.file(),
@@ -70,7 +63,7 @@ impl<'a> ParseCtx<'a> {
) )
} }
pub fn list<N: Parsable>(&mut self, sep: Token, end: Token) -> Result<Vec<Id<N>>, CompilerMsg> { pub fn list<N: Node>(&mut self, sep: Token, end: Token) -> Result<Vec<Parsed<N>>, CompilerMsg> {
let mut list = Vec::new(); let mut list = Vec::new();
if self.next_if(&end) { if self.next_if(&end) {
return Ok(list); return Ok(list);
@@ -82,10 +75,6 @@ impl<'a> ParseCtx<'a> {
self.expect(end)?; self.expect(end)?;
Ok(list) Ok(list)
} }
pub fn finish(self) -> Nodes {
self.nodes
}
} }
impl<'a> std::ops::Deref for ParseCtx<'a> { impl<'a> std::ops::Deref for ParseCtx<'a> {
@@ -101,11 +90,3 @@ impl<'a> std::ops::DerefMut for ParseCtx<'a> {
&mut self.cursor &mut self.cursor
} }
} }
impl<N: Node> Index<Id<N>> for ParseCtx<'_> {
type Output = N;
fn index(&self, index: Id<N>) -> &Self::Output {
&self.nodes[index]
}
}
+59
View File
@@ -0,0 +1,59 @@
use crate::parser::{Node, Parsed};
#[derive(Clone, Copy)]
pub struct DisplayCtx {
pub indent: usize,
}
pub struct NodeDsp<'a, N: Node> {
pub node: &'a N,
pub ctx: DisplayCtx,
}
impl<N: Node> std::fmt::Display for NodeDsp<'_, N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.node.fmt(f, self.ctx)
}
}
pub struct VecDsp<'a, N> {
list: &'a Vec<Parsed<N>>,
ctx: DisplayCtx,
}
impl<N: Node> std::fmt::Display for VecDsp<'_, N> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some((last, rest)) = self.list.split_last() {
for arg in rest {
write!(f, "{}, ", arg.dsp(self.ctx))?;
}
write!(f, "{}", last.dsp(self.ctx))?;
}
Ok(())
}
}
impl<N: Node> Parsed<N> {
pub fn dsp(&self, ctx: DisplayCtx) -> NodeDsp<'_, N>
where
N: Node,
{
NodeDsp { node: self, ctx }
}
}
pub trait VecDspT<N> {
fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx>) -> VecDsp<'b, N>
where
'a: 'b;
}
impl<N> VecDspT<N> for Vec<Parsed<N>> {
fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx>) -> VecDsp<'b, N>
where
'a: 'b,
{
let ctx = ctx.into();
VecDsp { list: self, ctx }
}
}
-116
View File
@@ -1,116 +0,0 @@
use crate::parser::{Node, Nodes};
use std::marker::PhantomData;
pub struct Id<T> {
id: usize,
_pd: PhantomData<T>,
}
impl<T> Clone for Id<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Id<T> {}
impl<N: Node> std::ops::Index<Id<N>> for Nodes {
type Output = N;
fn index(&self, index: Id<N>) -> &Self::Output {
&N::vec(self).vec[index.id]
}
}
impl<N: Node> std::ops::Index<&Id<N>> for Nodes {
type Output = N;
fn index(&self, index: &Id<N>) -> &Self::Output {
&N::vec(self).vec[index.id]
}
}
#[derive(Clone, Copy)]
pub struct DisplayCtx<'a> {
pub nodes: &'a Nodes,
pub indent: usize,
}
pub struct IdDisplay<'a, N> {
id: Id<N>,
ctx: DisplayCtx<'a>,
}
pub trait FmtNode: Node {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result;
}
impl<N: FmtNode> std::fmt::Display for IdDisplay<'_, N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.ctx.nodes[self.id].fmt(f, self.ctx)
}
}
impl<N> Id<N> {
pub(super) fn new(id: usize) -> Self {
Self {
id,
_pd: PhantomData,
}
}
}
impl<N: FmtNode> Id<N> {
pub fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
ctx.nodes[*self].fmt(f, ctx)
}
pub fn dsp<'a, 'b>(&self, ctx: impl Into<DisplayCtx<'a>>) -> IdDisplay<'b, N>
where
'a: 'b,
{
let ctx = ctx.into();
IdDisplay { id: *self, ctx }
}
}
impl<'a> From<&'a Nodes> for DisplayCtx<'a> {
fn from(node: &'a Nodes) -> Self {
Self {
nodes: node,
indent: 0,
}
}
}
pub struct VecDsp<'a, N> {
list: &'a Vec<Id<N>>,
ctx: DisplayCtx<'a>,
}
impl<N: FmtNode> std::fmt::Display for VecDsp<'_, N> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some((last, rest)) = self.list.split_last() {
for arg in rest {
write!(f, "{}, ", arg.dsp(self.ctx))?;
}
write!(f, "{}", last.dsp(self.ctx))?;
}
Ok(())
}
}
pub trait VecDspT<N> {
fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx<'a>>) -> VecDsp<'b, N>
where
'a: 'b;
}
impl<N> VecDspT<N> for Vec<Id<N>> {
fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx<'a>>) -> VecDsp<'b, N>
where
'a: 'b,
{
let ctx = ctx.into();
VecDsp { list: self, ctx }
}
}
+60 -51
View File
@@ -1,60 +1,69 @@
use crate::{ use crate::{
io::{CompilerOutput, Span}, io::{CompilerMsg, CompilerOutput, Span},
parser::{Cursor, nodes::*}, parser::{Cursor, nodes::*},
}; };
mod id; mod ctx;
mod parse; mod dsp;
pub use id::*; pub use ctx::*;
pub use parse::*; pub use dsp::*;
impl Nodes { pub struct Parsed<N> {
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<(Self, Id<Body>)> { pub node: N,
let root_code = match std::fs::read_to_string(path) { pub span: Span,
Ok(code) => code,
Err(err) => {
output.error(format!("Failed to read input file: {err}"));
return None;
}
};
output.files.push(path.to_string());
let mut ctx = ParseCtx::new(Cursor::new(&root_code, 0));
let root = match ctx.parse() {
Ok(expr) => expr,
Err(msg) => {
output.error(msg);
return None;
}
};
Some((ctx.finish(), root))
}
}
#[derive(Debug)]
pub struct NodeVec<N> {
vec: Vec<N>,
spans: Vec<Span>,
}
impl<N> NodeVec<N> {
pub fn add(&mut self, v: N, span: Span) -> Id<N> {
let id = self.vec.len();
self.vec.push(v);
self.spans.push(span);
Id::new(id)
}
}
impl<N> Default for NodeVec<N> {
fn default() -> Self {
Self {
vec: Default::default(),
spans: Default::default(),
}
}
} }
pub trait Node: Sized { pub trait Node: Sized {
fn vec(nodes: &Nodes) -> &NodeVec<Self>; fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>;
fn vec_mut(nodes: &mut Nodes) -> &mut NodeVec<Self>; fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result;
}
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Parsed<Body>> {
let root_code = match std::fs::read_to_string(path) {
Ok(code) => code,
Err(err) => {
output.error(format!("Failed to read input file: {err}"));
return None;
}
};
output.files.push(path.to_string());
let mut ctx = ParseCtx::new(Cursor::new(&root_code, 0));
let root = match ctx.parse() {
Ok(v) => v,
Err(msg) => {
output.error(msg);
return None;
}
};
Some(root)
}
impl<N> Parsed<N> {
pub fn new(node: N, span: Span) -> Self {
Self { node, span }
}
pub fn boxed(self) -> Box<Self> {
Box::new(self)
}
}
impl<N> std::ops::Deref for Parsed<N> {
type Target = N;
fn deref(&self) -> &Self::Target {
&self.node
}
}
impl<N> std::ops::DerefMut for Parsed<N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.node
}
}
impl<N: Node> std::fmt::Display for Parsed<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.node.fmt(f, DisplayCtx { indent: 0 })
}
} }
+6 -13
View File
@@ -1,11 +1,11 @@
use super::*; use super::*;
pub struct Body { pub struct Body {
pub items: Vec<Id<Item>>, pub items: Vec<Parsed<Item>>,
pub final_semicolon: bool, pub final_semicolon: bool,
} }
impl Parsable for Body { impl Node for Body {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> { fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut items = Vec::new(); let mut items = Vec::new();
fn at_end(ctx: &mut ParseCtx) -> bool { fn at_end(ctx: &mut ParseCtx) -> bool {
@@ -15,8 +15,8 @@ impl Parsable for Body {
if at_end(ctx) { if at_end(ctx) {
break true; break true;
} }
let item: Id<Item> = ctx.parse()?; let item: Parsed<Item> = ctx.parse()?;
let needs_semicolon = item.needs_semicolon(&ctx.nodes); let needs_semicolon = item.needs_semicolon();
items.push(item); items.push(item);
if at_end(ctx) { if at_end(ctx) {
break false; break false;
@@ -31,23 +31,16 @@ impl Parsable for Body {
final_semicolon, final_semicolon,
}) })
} }
}
impl FmtNode for Body {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result { 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() { if let Some((last, rest)) = self.items.split_last() {
for &i in rest { for i in rest {
writeln!( writeln!(
f, f,
"{}{}{}", "{}{}{}",
" ".repeat(ctx.indent), " ".repeat(ctx.indent),
i.dsp(ctx), i.dsp(ctx),
if i.needs_semicolon(ctx.nodes) { if i.needs_semicolon() { ";" } else { "" }
";"
} else {
""
}
)?; )?;
} }
writeln!( writeln!(
+84 -78
View File
@@ -2,46 +2,48 @@ use crate::parser::VecDspT;
pub use super::*; pub use super::*;
pub type BoxExpr = Box<Parsed<Expr>>;
pub enum Expr { pub enum Expr {
Block(Id<Body>), Block(Parsed<Body>),
Group(Id<Expr>), Group(BoxExpr),
Ident(Id<Ident>), Ident(Ident),
Lit(Id<Lit>), Lit(Lit),
Negate(Id<Expr>), Negate(BoxExpr),
Call { Call {
target: Id<Expr>, target: BoxExpr,
args: Vec<Id<Expr>>, args: Vec<Parsed<Expr>>,
}, },
Assign { Assign {
target: Id<Expr>, target: BoxExpr,
val: Id<Expr>, val: BoxExpr,
}, },
If { If {
cond: Id<Expr>, cond: BoxExpr,
body: Id<Expr>, body: BoxExpr,
}, },
Loop { Loop {
body: Id<Expr>, body: BoxExpr,
}, },
While { While {
cond: Id<Expr>, cond: BoxExpr,
body: Id<Expr>, 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> { fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut res = Self::unit(ctx)?; let mut res = Self::unit(ctx)?;
while let Some(next) = ctx.peek() { while let Some(next) = ctx.peek() {
res = match next { res = match next {
Token::Equal => { Token::Equal => {
let target = ctx.push_adv(res); let target = ctx.push_adv(res).boxed();
let val = ctx.parse_with(Self::unit)?; let val = ctx.parse_with(Self::unit)?.boxed();
Expr::Assign { target, val } Expr::Assign { target, val }
} }
Token::OpenParen => { Token::OpenParen => {
let target = ctx.push_adv(res); let target = ctx.push_adv(res).boxed();
let args = ctx.list(Token::Comma, Token::CloseParen)?; let args = ctx.list(Token::Comma, Token::CloseParen)?;
Expr::Call { target, args } Expr::Call { target, args }
} }
@@ -50,34 +52,71 @@ impl Parsable for Expr {
} }
Ok(res) 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 { impl Expr {
fn unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> { fn unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_next()? { Ok(match ctx.expect_next()? {
Token::Dash => Self::Negate(ctx.parse()?), Token::Dash => Self::Negate(ctx.parse_box()?),
Token::Ident(s) => Self::Ident(ctx.ident(s)), Token::Ident(s) => Self::Ident(Ident(s)),
Token::Lit(l) => Self::Lit(ctx.lit(l)), Token::Lit(l) => Self::Lit(l),
Token::Fn => Self::Fn(ctx.parse()?), Token::Fn => Self::Fn(ctx.parse_box()?),
Token::If => { Token::If => {
let cond = ctx.parse()?; let cond = ctx.parse_box()?;
let body = cond_body(ctx)?; let body = Self::body(ctx)?.boxed();
Self::If { cond, body } Self::If { cond, body }
} }
Token::While => { Token::While => {
let cond = ctx.parse()?; let cond = ctx.parse_box()?;
let body = cond_body(ctx)?; let body = Self::body(ctx)?.boxed();
Self::While { cond, body } Self::While { cond, body }
} }
Token::Loop => { Token::Loop => {
let body = ctx.parse()?; let body = ctx.parse_box()?;
Self::Loop { body } Self::Loop { body }
} }
Token::OpenParen => { Token::OpenParen => {
if ctx.next_if(Token::CloseParen) { if ctx.next_if(Token::CloseParen) {
Self::Lit(ctx.push(Lit::Unit)) Self::Lit(Lit::Unit)
} else { } else {
let inner = ctx.parse()?; let inner = ctx.parse_box()?;
ctx.expect(Token::CloseParen)?; ctx.expect(Token::CloseParen)?;
Self::Group(inner) Self::Group(inner)
} }
@@ -105,67 +144,34 @@ impl Expr {
ctx.expect(Token::CloseCurly)?; ctx.expect(Token::CloseCurly)?;
Ok(Expr::Block(id)) Ok(Expr::Block(id))
} }
}
fn cond_body(ctx: &mut ParseCtx) -> Result<Id<Expr>, CompilerMsg> { pub fn body(ctx: &mut ParseCtx) -> Result<Parsed<Expr>, CompilerMsg> {
if ctx.next_if(Token::Do) { if ctx.next_if(Token::DoubleArrow) {
ctx.parse() ctx.parse()
} else { } else {
ctx.parse_with(Expr::block) ctx.parse_with(Expr::block)
}
} }
}
impl Id<Expr> { pub fn ends_with_block(&self) -> bool {
pub fn ends_with_block(&self, nodes: &Nodes) -> bool { match self {
match nodes[self] {
Expr::Block(..) => true, Expr::Block(..) => true,
Expr::Loop { body } Expr::Loop { body }
| Expr::While { body, .. } | Expr::While { body, .. }
| Expr::If { body, .. } | Expr::If { body, .. }
| Expr::Negate(body) | Expr::Negate(body)
| Expr::Assign { val: body, .. } => body.ends_with_block(nodes), | Expr::Assign { val: body, .. } => body.ends_with_block(),
Expr::Fn(f) => f.ends_with_block(nodes), Expr::Fn(f) => f.ends_with_block(),
_ => false, _ => false,
} }
} }
} }
impl FmtNode for Expr { impl Parsed<Expr> {
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result { pub fn fmt_body(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
let do_ = |id: Id<Expr>| if ctx.nodes[id].is_block() { "" } else { "do " }; match &self.node {
match *self { Expr::Block(_) => self.node.fmt(f, ctx),
Self::Ident(id) => id.fmt(f, ctx), _ => write!(f, "=> {}", self.dsp(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(())
}
} }
} }
} }
+10 -16
View File
@@ -1,12 +1,12 @@
use super::*; use super::*;
pub struct Func { pub struct Func {
args: Vec<Id<Param>>, args: Vec<Parsed<Param>>,
ret: Option<Id<Type>>, ret: Option<Parsed<Type>>,
body: Id<Expr>, body: Parsed<Expr>,
} }
impl Parsable for Func { impl Node for Func {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> { fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
ctx.expect(Token::OpenParen)?; ctx.expect(Token::OpenParen)?;
let args = ctx.list(Token::Comma, Token::CloseParen)?; let args = ctx.list(Token::Comma, Token::CloseParen)?;
@@ -14,16 +14,10 @@ impl Parsable for Func {
if ctx.next_if(Token::Arrow) { if ctx.next_if(Token::Arrow) {
ret = Some(ctx.parse()?); ret = Some(ctx.parse()?);
} }
let body = if ret.is_some() { let body = Expr::body(ctx)?;
ctx.parse_with(Expr::block)
} else {
ctx.parse()
}?;
Ok(Self { args, ret, body }) Ok(Self { args, ret, body })
} }
}
impl FmtNode for Func {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "fn(")?; write!(f, "fn(")?;
if let Some((last, rest)) = self.args.split_last() { if let Some((last, rest)) = self.args.split_last() {
@@ -33,16 +27,16 @@ impl FmtNode for Func {
write!(f, "{}", last.dsp(ctx))?; write!(f, "{}", last.dsp(ctx))?;
} }
write!(f, ") ")?; write!(f, ") ")?;
if let Some(ret) = self.ret { if let Some(ret) = &self.ret {
write!(f, "-> {} ", ret.dsp(ctx))?; write!(f, "-> {} ", ret.dsp(ctx))?;
} }
self.body.fmt(f, ctx)?; self.body.fmt_body(f, ctx)?;
Ok(()) Ok(())
} }
} }
impl Id<Func> { impl Func {
pub fn ends_with_block(&self, nodes: &Nodes) -> bool { pub fn ends_with_block(&self) -> bool {
nodes[self].body.ends_with_block(nodes) self.body.ends_with_block()
} }
} }
+7 -11
View File
@@ -1,20 +1,16 @@
use super::*; use super::*;
pub struct Ident { pub struct Ident(pub String);
pub inner: String,
}
impl FmtNode for Ident { impl Node for Ident {
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl Parsable for Ident {
fn parse(ctx: &mut super::ParseCtx) -> Result<Self, crate::io::CompilerMsg> { fn parse(ctx: &mut super::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
match ctx.expect_next()? { match ctx.expect_next()? {
Token::Ident(ident) => Ok(Self { inner: ident }), Token::Ident(ident) => Ok(Self(ident)),
t => ctx.unexpected(&t, "an identifier"), 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 { pub enum Item {
Let { Let {
name: Id<Ident>, name: Parsed<Ident>,
ty: Option<Id<Type>>, ty: Option<Parsed<Type>>,
val: Id<Expr>, val: Parsed<Expr>,
}, },
Struct(Id<Struct>), Expr(Parsed<Expr>),
Expr(Id<Expr>),
} }
impl Parsable for Item { impl Node for Item {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> { fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_peek()? { Ok(match ctx.expect_peek()? {
Token::Let => { Token::Let => {
@@ -27,9 +26,7 @@ impl Parsable for Item {
_ => Self::Expr(ctx.parse()?), _ => Self::Expr(ctx.parse()?),
}) })
} }
}
impl FmtNode for Item {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
match self { match self {
Item::Let { name, ty, val } => { Item::Let { name, ty, val } => {
@@ -45,14 +42,14 @@ impl FmtNode for Item {
} }
} }
impl Id<Item> { impl Item {
pub fn ends_with_block(&self, nodes: &Nodes) -> bool { pub fn ends_with_block(&self) -> bool {
match nodes[self] { match self {
Item::Let { name, ty, val } => val.ends_with_block(nodes), Item::Let { val, .. } => val.ends_with_block(),
Item::Expr(id) => id.ends_with_block(nodes), Item::Expr(id) => id.ends_with_block(),
} }
} }
pub fn needs_semicolon(&self, nodes: &Nodes) -> bool { pub fn needs_semicolon(&self) -> bool {
!self.ends_with_block(nodes) !self.ends_with_block()
} }
} }
+1 -33
View File
@@ -12,39 +12,7 @@ pub use func::*;
pub use ident::*; pub use ident::*;
pub use item::*; pub use item::*;
pub use param::*; pub use param::*;
pub use struct_::*;
pub use ty::*; 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; 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::*; use super::*;
pub struct Param { pub struct Param {
name: Id<Ident>, name: Parsed<Ident>,
ty: Option<Id<Type>>, ty: Option<Parsed<Type>>,
} }
impl Parsable for Param { impl Node for Param {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> { fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let name = ctx.parse()?; let name = ctx.parse()?;
let mut ty = None; let mut ty = None;
@@ -14,12 +14,10 @@ impl Parsable for Param {
} }
Ok(Self { name, ty }) Ok(Self { name, ty })
} }
}
impl FmtNode for Param {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
self.name.fmt(f, ctx)?; self.name.fmt(f, ctx)?;
if let Some(ty) = self.ty { if let Some(ty) = &self.ty {
write!(f, ": {}", ty.dsp(ctx))?; write!(f, ": {}", ty.dsp(ctx))?;
} }
Ok(()) Ok(())
+1 -7
View File
@@ -5,10 +5,4 @@ pub struct Struct {
fields: Vec<Field>, fields: Vec<Field>,
} }
impl Parsable for Struct { pub struct Field {}
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
}
}
pub struct Field {
}
+2 -4
View File
@@ -1,19 +1,17 @@
use super::*; use super::*;
pub enum Type { 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> { fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_next()? { Ok(match ctx.expect_next()? {
Token::Ident(s) => Self::Ident(ctx.ident(s)), Token::Ident(s) => Self::Ident(ctx.ident(s)),
t => ctx.unexpected(&t, "a type")?, t => ctx.unexpected(&t, "a type")?,
}) })
} }
}
impl FmtNode for Type {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
match self { match self {
Type::Ident(id) => id.fmt(f, ctx), Type::Ident(id) => id.fmt(f, ctx),
+2 -4
View File
@@ -4,8 +4,6 @@ while true {
print(x); print(x);
}; };
if x { let y = true;
print("hello");
}
let y = fn(x: i32) -> i32 {x}; if y => print("hello");