This commit is contained in:
2026-04-17 18:51:12 -04:00
parent 2f91e454dd
commit b3f77076d4
20 changed files with 244 additions and 192 deletions
+16
View File
@@ -90,6 +90,22 @@ impl Span {
&text[self.start..=self.end], &text[self.start..=self.end],
&text[(self.end + 1)..range.end] &text[(self.end + 1)..range.end]
)?; )?;
} else if let [(sline, srange), (eline, erange)] = &spans[..] {
writeln!(
w,
" {sline:3} | {}{underline}{underline_color}{}{end}",
&text[srange.start..self.start],
&text[self.start..=srange.end - 1],
)?;
if *eline != *sline + 1 {
writeln!(w, " ...")?;
}
writeln!(
w,
" {eline:3} | {underline}{underline_color}{}{end}{}",
&text[erange.start..=self.end],
&text[(self.end + 1)..=erange.end - 1],
)?;
} }
Ok(()) Ok(())
} }
+4
View File
@@ -0,0 +1,4 @@
mod structs;
pub use structs::*;
pub struct Ir {}
+1
View File
@@ -0,0 +1 @@
pub struct Module {}
+14 -4
View File
@@ -1,6 +1,10 @@
use crate::{io::CompilerOutput, parser::parse_root}; use crate::{
io::CompilerOutput,
parser::{Node, parse_root},
};
mod io; mod io;
mod ir;
mod parser; mod parser;
fn main() { fn main() {
@@ -10,9 +14,15 @@ fn main() {
return; return;
}; };
let mut output = CompilerOutput::new(); let mut output = CompilerOutput::new();
let nodes = parse_root(&path, &mut output); let root = parse_root(&path, &mut output);
if let Some(root) = nodes { if let Some(root) = root {
print!("{root}"); print!("{}", root.new_dsp());
// for item in &root.items {
// output.errors.push(io::CompilerMsg {
// spans: vec![item.span],
// msg: format!("hello"),
// });
// }
} }
output.write(&mut std::io::stdout()); output.write(&mut std::io::stdout());
} }
+21 -9
View File
@@ -1,26 +1,38 @@
use super::Token; use super::Token;
use crate::io::Span;
pub struct Lit {
pub ty: LitTy,
pub span: Span,
}
#[derive(PartialEq)] #[derive(PartialEq)]
pub enum Lit { pub enum LitTy {
Number(String), Number(String),
Bool(bool), Bool(bool),
String(String), String(String),
Unit, Unit,
} }
impl From<Lit> for Token { impl From<LitTy> for Token {
fn from(value: Lit) -> Self { fn from(value: LitTy) -> Self {
Self::Lit(value) Self::Lit(value)
} }
} }
impl std::fmt::Display for LitTy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Number(n) => write!(f, "{n}"),
Self::Bool(b) => write!(f, "{b}"),
Self::String(s) => write!(f, "\"{s}\""),
Self::Unit => write!(f, "()"),
}
}
}
impl std::fmt::Display for Lit { impl std::fmt::Display for Lit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { self.ty.fmt(f)
Lit::Number(n) => write!(f, "{n}"),
Lit::Bool(b) => write!(f, "{b}"),
Lit::String(s) => write!(f, "\"{s}\""),
Lit::Unit => write!(f, "()"),
}
} }
} }
+3 -2
View File
@@ -58,9 +58,10 @@ impl<'a> Cursor<'a> {
self.peek().ok_or_else(CompilerMsg::unexpected_eof) self.peek().ok_or_else(CompilerMsg::unexpected_eof)
} }
pub fn expect(&mut self, token: Token) -> Result<Token, CompilerMsg> { pub fn expect(&mut self, token: impl Borrow<Token>) -> Result<Token, CompilerMsg> {
let token = token.borrow();
let next = self.expect_next()?; let next = self.expect_next()?;
if next == token { if next == *token {
Ok(next) Ok(next)
} else { } else {
self.unexpected(&next, &format!("'{token}'")) self.unexpected(&next, &format!("'{token}'"))
+8 -6
View File
@@ -1,4 +1,6 @@
use super::{Lit, Span, Spanned}; use crate::parser::cursor::LitTy;
use super::{Span, Spanned};
use std::{iter::Peekable, str::CharIndices}; use std::{iter::Peekable, str::CharIndices};
def_tokens! { def_tokens! {
@@ -39,7 +41,7 @@ def_tokens! {
} }
other { other {
Ident(String), Ident(String),
Lit(Lit), Lit(LitTy),
} }
} }
@@ -128,7 +130,7 @@ impl Iterator for Tokens<'_> {
span.end = *i; span.end = *i;
self.chars.next(); self.chars.next();
} }
Lit::Number(s).into() LitTy::Number(s).into()
} }
'"' => { '"' => {
let mut s = String::new(); let mut s = String::new();
@@ -138,7 +140,7 @@ impl Iterator for Tokens<'_> {
s.push(c); s.push(c);
span.end = i; span.end = i;
} }
Lit::String(s).into() LitTy::String(s).into()
} }
_ => { _ => {
let mut s = c.to_string(); let mut s = c.to_string();
@@ -150,8 +152,8 @@ impl Iterator for Tokens<'_> {
self.chars.next(); self.chars.next();
} }
match s.as_str() { match s.as_str() {
"true" => Lit::Bool(true).into(), "true" => LitTy::Bool(true).into(),
"false" => Lit::Bool(false).into(), "false" => LitTy::Bool(false).into(),
_ => from_str(s), _ => from_str(s),
} }
} }
+18 -27
View File
@@ -1,8 +1,8 @@
use crate::{ use crate::{
io::{CompilerMsg, Span}, io::{CompilerMsg, Span},
parser::{ parser::{
Ident, Node, Parsed, Ident, Node,
cursor::{Cursor, Lit, Token}, cursor::{Cursor, Lit, LitTy, Token},
}, },
}; };
@@ -16,54 +16,45 @@ impl<'a> ParseCtx<'a> {
Self { start: 0, cursor } Self { start: 0, cursor }
} }
pub fn parse_box<N: Node>(&mut self) -> Result<Box<Parsed<N>>, CompilerMsg> { pub fn parse_box<N: Node>(&mut self) -> Result<Box<N>, CompilerMsg> {
self.parse_with(N::parse).map(Parsed::boxed) self.parse_with(N::parse).map(Box::new)
} }
pub fn parse<N: Node>(&mut self) -> Result<Parsed<N>, CompilerMsg> { pub fn parse<N: Node>(&mut self) -> Result<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<Parsed<N>, CompilerMsg> { ) -> Result<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| r);
self.start = old_start; self.start = old_start;
res res
} }
pub fn ident(&mut self, s: String) -> Parsed<Ident> { pub fn ident(&mut self, s: String) -> Ident {
let span = self.cursor.span; let span = self.cursor.span;
Parsed::new(Ident(s), span) Ident { name: s, span }
} }
pub fn lit(&mut self, lit: Lit) -> Parsed<Lit> { pub fn lit(&mut self, ty: LitTy) -> Lit {
let span = self.cursor.span; let span = self.cursor.span;
Parsed::new(lit, span) Lit { ty, span }
} }
pub fn push_adv<N: Node>(&mut self, node: N) -> Parsed<N> { pub fn span(&mut self) -> Span {
let res = self.push(node);
self.cursor.next();
res
}
pub fn push<N: Node>(&mut self, node: N) -> Parsed<N> {
let end = self.cursor.cur_end(); let end = self.cursor.cur_end();
Parsed::new( Span {
node, file: self.cursor.file(),
Span { start: self.start,
file: self.cursor.file(), end,
start: self.start, }
end,
},
)
} }
pub fn list<N: Node>(&mut self, sep: Token, end: Token) -> Result<Vec<Parsed<N>>, CompilerMsg> { pub fn list<N: Node>(&mut self, sep: Token, end: Token) -> Result<Vec<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);
+3 -12
View File
@@ -1,4 +1,4 @@
use crate::parser::{Node, Parsed}; use crate::parser::Node;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct DisplayCtx { pub struct DisplayCtx {
@@ -17,7 +17,7 @@ impl<N: Node> std::fmt::Display for NodeDsp<'_, N> {
} }
pub struct VecDsp<'a, N> { pub struct VecDsp<'a, N> {
list: &'a Vec<Parsed<N>>, list: &'a Vec<N>,
ctx: DisplayCtx, ctx: DisplayCtx,
} }
@@ -33,22 +33,13 @@ impl<N: Node> std::fmt::Display for VecDsp<'_, N> {
} }
} }
impl<N: Node> Parsed<N> {
pub fn dsp(&self, ctx: DisplayCtx) -> NodeDsp<'_, N>
where
N: Node,
{
NodeDsp { node: self, ctx }
}
}
pub trait VecDspT<N> { pub trait VecDspT<N> {
fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx>) -> VecDsp<'b, N> fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx>) -> VecDsp<'b, N>
where where
'a: 'b; 'a: 'b;
} }
impl<N> VecDspT<N> for Vec<Parsed<N>> { impl<N> VecDspT<N> for Vec<N> {
fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx>) -> VecDsp<'b, N> fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx>) -> VecDsp<'b, N>
where where
'a: 'b, 'a: 'b,
+8 -37
View File
@@ -1,5 +1,5 @@
use crate::{ use crate::{
io::{CompilerMsg, CompilerOutput, Span}, io::{CompilerMsg, CompilerOutput},
parser::{Cursor, nodes::*}, parser::{Cursor, nodes::*},
}; };
@@ -8,17 +8,18 @@ mod dsp;
pub use ctx::*; pub use ctx::*;
pub use dsp::*; pub use dsp::*;
pub struct Parsed<N> {
pub node: N,
pub span: Span,
}
pub trait Node: Sized { pub trait Node: Sized {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>; fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>;
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;
fn dsp(&self, ctx: DisplayCtx) -> NodeDsp<'_, Self> {
NodeDsp { node: self, ctx }
}
fn new_dsp(&self) -> NodeDsp<'_, Self> {
self.dsp(DisplayCtx { indent: 0 })
}
} }
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Parsed<Body>> { pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Body> {
let root_code = match std::fs::read_to_string(path) { let root_code = match std::fs::read_to_string(path) {
Ok(code) => code, Ok(code) => code,
Err(err) => { Err(err) => {
@@ -37,33 +38,3 @@ pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Parsed<Body
}; };
Some(root) 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 })
}
}
+4 -2
View File
@@ -1,8 +1,9 @@
use super::*; use super::*;
pub struct Body { pub struct Body {
pub items: Vec<Parsed<Item>>, pub items: Vec<Item>,
pub final_semicolon: bool, pub final_semicolon: bool,
pub span: Span,
} }
impl Node for Body { impl Node for Body {
@@ -15,7 +16,7 @@ impl Node for Body {
if at_end(ctx) { if at_end(ctx) {
break true; break true;
} }
let item: Parsed<Item> = ctx.parse()?; let item: Item = ctx.parse()?;
let needs_semicolon = item.needs_semicolon(); let needs_semicolon = item.needs_semicolon();
items.push(item); items.push(item);
if at_end(ctx) { if at_end(ctx) {
@@ -29,6 +30,7 @@ impl Node for Body {
Ok(Self { Ok(Self {
items, items,
final_semicolon, final_semicolon,
span: ctx.span(),
}) })
} }
+75 -66
View File
@@ -2,57 +2,58 @@ use crate::parser::VecDspT;
pub use super::*; pub use super::*;
pub type BoxExpr = Box<Parsed<Expr>>; pub struct Expr {
span: Span,
ty: ExprTy,
}
pub enum Expr { pub enum ExprTy {
Block(Parsed<Body>), Block(Body),
Group(BoxExpr), Group(Box<Expr>),
Ident(Ident), Ident(Ident),
Lit(Lit), Lit(Lit),
Negate(BoxExpr), Negate(Box<Expr>),
Call { Call { target: Box<Expr>, args: Vec<Expr> },
target: BoxExpr, Assign { target: Box<Expr>, val: Box<Expr> },
args: Vec<Parsed<Expr>>, If { cond: Box<Expr>, body: Box<Expr> },
}, Loop { body: Box<Expr> },
Assign { While { cond: Box<Expr>, body: Box<Expr> },
target: BoxExpr, Fn(Box<Func>),
val: BoxExpr,
},
If {
cond: BoxExpr,
body: BoxExpr,
},
Loop {
body: BoxExpr,
},
While {
cond: BoxExpr,
body: BoxExpr,
},
Fn(Box<Parsed<Func>>),
} }
impl Node 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 { let ty = match next {
Token::Equal => { Token::Equal => {
let target = ctx.push_adv(res).boxed(); ctx.next();
let val = ctx.parse_with(Self::unit)?.boxed(); let target = Box::new(res);
Expr::Assign { target, val } let val = Box::new(ctx.parse_with(Self::unit)?);
ExprTy::Assign { target, val }
} }
Token::OpenParen => { Token::OpenParen => {
let target = ctx.push_adv(res).boxed(); ctx.next();
let target = Box::new(res);
let args = ctx.list(Token::Comma, Token::CloseParen)?; let args = ctx.list(Token::Comma, Token::CloseParen)?;
Expr::Call { target, args } ExprTy::Call { target, args }
} }
_ => break, _ => break,
} };
res = Self {
ty,
span: ctx.span(),
};
} }
Ok(res) 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 { fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
match self { match self {
Self::Ident(ident) => ident.fmt(f, ctx), Self::Ident(ident) => ident.fmt(f, ctx),
@@ -92,60 +93,77 @@ impl Node for Expr {
} }
impl 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> { fn unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_next()? { let ty = match ctx.expect_next()? {
Token::Dash => Self::Negate(ctx.parse_box()?), Token::Dash => ExprTy::Negate(ctx.parse_box()?),
Token::Ident(s) => Self::Ident(Ident(s)), Token::Ident(s) => ExprTy::Ident(ctx.ident(s)),
Token::Lit(l) => Self::Lit(l), Token::Lit(l) => ExprTy::Lit(ctx.lit(l)),
Token::Fn => Self::Fn(ctx.parse_box()?), Token::Fn => ExprTy::Fn(ctx.parse_box()?),
Token::If => { Token::If => {
let cond = ctx.parse_box()?; let cond = ctx.parse_box()?;
let body = Self::body(ctx)?.boxed(); let body = Box::new(Self::body(ctx)?);
Self::If { cond, body } ExprTy::If { cond, body }
} }
Token::While => { Token::While => {
let cond = ctx.parse_box()?; let cond = ctx.parse_box()?;
let body = Self::body(ctx)?.boxed(); let body = Box::new(Self::body(ctx)?);
Self::While { cond, body } ExprTy::While { cond, body }
} }
Token::Loop => { Token::Loop => {
let body = ctx.parse_box()?; let body = ctx.parse_box()?;
Self::Loop { body } ExprTy::Loop { body }
} }
Token::OpenParen => { Token::OpenParen => {
if ctx.next_if(Token::CloseParen) { if ctx.next_if(Token::CloseParen) {
Self::Lit(Lit::Unit) ExprTy::Lit(Lit {
ty: LitTy::Unit,
span: ctx.span(),
})
} else { } else {
let inner = ctx.parse_box()?; let inner = ctx.parse_box()?;
ctx.expect(Token::CloseParen)?; ctx.expect(Token::CloseParen)?;
Self::Group(inner) ExprTy::Group(inner)
} }
} }
Token::OpenCurly => { Token::OpenCurly => {
let body = ctx.parse()?; let body = ctx.parse()?;
ctx.expect(Token::CloseCurly)?; ctx.expect(Token::CloseCurly)?;
Self::Block(body) ExprTy::Block(body)
} }
other => return ctx.unexpected(&other, "an expression"), other => return ctx.unexpected(&other, "an expression"),
};
Ok(Self {
ty,
span: ctx.span(),
}) })
} }
pub fn is_group(&self) -> bool { pub fn is_group(&self) -> bool {
matches!(self, Expr::Group(_)) matches!(self.ty, ExprTy::Group(_))
} }
pub fn is_block(&self) -> bool { pub fn is_block(&self) -> bool {
matches!(self, Expr::Block(_)) matches!(self.ty, ExprTy::Block(_))
} }
pub fn block(ctx: &mut ParseCtx) -> Result<Expr, CompilerMsg> { pub fn block(ctx: &mut ParseCtx) -> Result<Expr, CompilerMsg> {
ctx.expect(Token::OpenCurly)?; ctx.expect(Token::OpenCurly)?;
let id = ctx.parse()?; let id = ctx.parse()?;
ctx.expect(Token::CloseCurly)?; 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) { if ctx.next_if(Token::DoubleArrow) {
ctx.parse() ctx.parse()
} else { } else {
@@ -154,24 +172,15 @@ impl Expr {
} }
pub fn ends_with_block(&self) -> bool { pub fn ends_with_block(&self) -> bool {
match self { match &self.ty {
Expr::Block(..) => true, ExprTy::Block(..) => true,
Expr::Loop { body } ExprTy::Loop { body }
| Expr::While { body, .. } | ExprTy::While { body, .. }
| Expr::If { body, .. } | ExprTy::If { body, .. }
| Expr::Negate(body) | ExprTy::Negate(body)
| Expr::Assign { val: body, .. } => body.ends_with_block(), | ExprTy::Assign { val: body, .. } => body.ends_with_block(),
Expr::Fn(f) => f.ends_with_block(), ExprTy::Fn(f) => f.ends_with_block(),
_ => false, _ => 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)),
}
}
}
+25 -5
View File
@@ -1,13 +1,23 @@
use super::*; use super::*;
pub struct Func { pub struct Func {
args: Vec<Parsed<Param>>, args: Vec<Param>,
ret: Option<Parsed<Type>>, name: Option<Ident>,
body: Parsed<Expr>, ret: Option<Type>,
body: Expr,
span: Span,
} }
impl Node for Func { impl Node for Func {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> { 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)?; ctx.expect(Token::OpenParen)?;
let args = ctx.list(Token::Comma, Token::CloseParen)?; let args = ctx.list(Token::Comma, Token::CloseParen)?;
let mut ret = None; let mut ret = None;
@@ -15,11 +25,21 @@ impl Node for Func {
ret = Some(ctx.parse()?); ret = Some(ctx.parse()?);
} }
let body = Expr::body(ctx)?; 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 { 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() { if let Some((last, rest)) = self.args.split_last() {
for arg in rest { for arg in rest {
write!(f, "{}, ", arg.dsp(ctx))?; write!(f, "{}, ", arg.dsp(ctx))?;
+12 -3
View File
@@ -1,16 +1,25 @@
use super::*; use super::*;
pub struct Ident(pub String); pub struct Ident {
pub name: String,
pub span: Span,
}
impl Node for Ident { impl Node 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(ident)), Token::Ident(ident) => Ok(ctx.ident(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 { 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
View File
@@ -1,17 +1,22 @@
use super::*; use super::*;
pub enum Item { pub struct Item {
pub ty: ItemTy,
pub span: Span,
}
pub enum ItemTy {
Let { Let {
name: Parsed<Ident>, name: Ident,
ty: Option<Parsed<Type>>, ty: Option<Type>,
val: Parsed<Expr>, val: Expr,
}, },
Expr(Parsed<Expr>), Expr(Expr),
} }
impl Node 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()? { let ty = match ctx.expect_peek()? {
Token::Let => { Token::Let => {
ctx.next(); ctx.next();
let name = ctx.parse()?; let name = ctx.parse()?;
@@ -21,22 +26,26 @@ impl Node for Item {
} }
ctx.expect(Token::Equal)?; ctx.expect(Token::Equal)?;
let val = ctx.parse()?; 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 { fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
match self { match &self.ty {
Item::Let { name, ty, val } => { ItemTy::Let { name, ty, val } => {
write!(f, "let {}", name.dsp(ctx))?; write!(f, "let {}", name.dsp(ctx))?;
if let Some(ty) = ty { if let Some(ty) = ty {
write!(f, ": {}", ty.dsp(ctx))?; write!(f, ": {}", ty.dsp(ctx))?;
} }
write!(f, " = {}", val.dsp(ctx))?; write!(f, " = {}", val.dsp(ctx))?;
} }
Item::Expr(id) => id.fmt(f, ctx)?, ItemTy::Expr(id) => id.fmt(f, ctx)?,
} }
Ok(()) Ok(())
} }
@@ -44,9 +53,9 @@ impl Node for Item {
impl Item { impl Item {
pub fn ends_with_block(&self) -> bool { pub fn ends_with_block(&self) -> bool {
match self { match &self.ty {
Item::Let { val, .. } => val.ends_with_block(), ItemTy::Let { val, .. } => val.ends_with_block(),
Item::Expr(id) => id.ends_with_block(), ItemTy::Expr(id) => id.ends_with_block(),
} }
} }
pub fn needs_semicolon(&self) -> bool { pub fn needs_semicolon(&self) -> bool {
+2 -2
View File
@@ -14,5 +14,5 @@ pub use item::*;
pub use param::*; pub use param::*;
pub use ty::*; pub use ty::*;
use super::{DisplayCtx, Lit, Node, ParseCtx, Parsed, Token}; use super::{DisplayCtx, Lit, LitTy, Node, ParseCtx, Token};
use crate::io::CompilerMsg; use crate::io::{CompilerMsg, Span};
+2 -2
View File
@@ -1,8 +1,8 @@
use super::*; use super::*;
pub struct Param { pub struct Param {
name: Parsed<Ident>, name: Ident,
ty: Option<Parsed<Type>>, ty: Option<Type>,
} }
impl Node for Param { impl Node for Param {
+1 -1
View File
@@ -1,7 +1,7 @@
use super::*; use super::*;
pub enum Type { pub enum Type {
Ident(Parsed<Ident>), Ident(Ident),
} }
impl Node for Type { impl Node for Type {
+4
View File
@@ -7,3 +7,7 @@ while true {
let y = true; let y = true;
if y => print("hello"); if y => print("hello");
fn thing() {
}
View File