work
This commit is contained in:
@@ -90,6 +90,22 @@ impl Span {
|
||||
&text[self.start..=self.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(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
mod structs;
|
||||
pub use structs::*;
|
||||
|
||||
pub struct Ir {}
|
||||
@@ -0,0 +1 @@
|
||||
pub struct Module {}
|
||||
+14
-4
@@ -1,6 +1,10 @@
|
||||
use crate::{io::CompilerOutput, parser::parse_root};
|
||||
use crate::{
|
||||
io::CompilerOutput,
|
||||
parser::{Node, parse_root},
|
||||
};
|
||||
|
||||
mod io;
|
||||
mod ir;
|
||||
mod parser;
|
||||
|
||||
fn main() {
|
||||
@@ -10,9 +14,15 @@ fn main() {
|
||||
return;
|
||||
};
|
||||
let mut output = CompilerOutput::new();
|
||||
let nodes = parse_root(&path, &mut output);
|
||||
if let Some(root) = nodes {
|
||||
print!("{root}");
|
||||
let root = parse_root(&path, &mut output);
|
||||
if let Some(root) = 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());
|
||||
}
|
||||
|
||||
@@ -1,26 +1,38 @@
|
||||
use super::Token;
|
||||
use crate::io::Span;
|
||||
|
||||
pub struct Lit {
|
||||
pub ty: LitTy,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Lit {
|
||||
pub enum LitTy {
|
||||
Number(String),
|
||||
Bool(bool),
|
||||
String(String),
|
||||
Unit,
|
||||
}
|
||||
|
||||
impl From<Lit> for Token {
|
||||
fn from(value: Lit) -> Self {
|
||||
impl From<LitTy> for Token {
|
||||
fn from(value: LitTy) -> Self {
|
||||
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 {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Lit::Number(n) => write!(f, "{n}"),
|
||||
Lit::Bool(b) => write!(f, "{b}"),
|
||||
Lit::String(s) => write!(f, "\"{s}\""),
|
||||
Lit::Unit => write!(f, "()"),
|
||||
}
|
||||
self.ty.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,9 +58,10 @@ impl<'a> Cursor<'a> {
|
||||
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()?;
|
||||
if next == token {
|
||||
if next == *token {
|
||||
Ok(next)
|
||||
} else {
|
||||
self.unexpected(&next, &format!("'{token}'"))
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use super::{Lit, Span, Spanned};
|
||||
use crate::parser::cursor::LitTy;
|
||||
|
||||
use super::{Span, Spanned};
|
||||
use std::{iter::Peekable, str::CharIndices};
|
||||
|
||||
def_tokens! {
|
||||
@@ -39,7 +41,7 @@ def_tokens! {
|
||||
}
|
||||
other {
|
||||
Ident(String),
|
||||
Lit(Lit),
|
||||
Lit(LitTy),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +130,7 @@ impl Iterator for Tokens<'_> {
|
||||
span.end = *i;
|
||||
self.chars.next();
|
||||
}
|
||||
Lit::Number(s).into()
|
||||
LitTy::Number(s).into()
|
||||
}
|
||||
'"' => {
|
||||
let mut s = String::new();
|
||||
@@ -138,7 +140,7 @@ impl Iterator for Tokens<'_> {
|
||||
s.push(c);
|
||||
span.end = i;
|
||||
}
|
||||
Lit::String(s).into()
|
||||
LitTy::String(s).into()
|
||||
}
|
||||
_ => {
|
||||
let mut s = c.to_string();
|
||||
@@ -150,8 +152,8 @@ impl Iterator for Tokens<'_> {
|
||||
self.chars.next();
|
||||
}
|
||||
match s.as_str() {
|
||||
"true" => Lit::Bool(true).into(),
|
||||
"false" => Lit::Bool(false).into(),
|
||||
"true" => LitTy::Bool(true).into(),
|
||||
"false" => LitTy::Bool(false).into(),
|
||||
_ => from_str(s),
|
||||
}
|
||||
}
|
||||
|
||||
+18
-27
@@ -1,8 +1,8 @@
|
||||
use crate::{
|
||||
io::{CompilerMsg, Span},
|
||||
parser::{
|
||||
Ident, Node, Parsed,
|
||||
cursor::{Cursor, Lit, Token},
|
||||
Ident, Node,
|
||||
cursor::{Cursor, Lit, LitTy, Token},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -16,54 +16,45 @@ impl<'a> ParseCtx<'a> {
|
||||
Self { start: 0, cursor }
|
||||
}
|
||||
|
||||
pub fn parse_box<N: Node>(&mut self) -> Result<Box<Parsed<N>>, CompilerMsg> {
|
||||
self.parse_with(N::parse).map(Parsed::boxed)
|
||||
pub fn parse_box<N: Node>(&mut self) -> Result<Box<N>, CompilerMsg> {
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn parse_with<N: Node>(
|
||||
&mut self,
|
||||
f: impl FnOnce(&mut Self) -> Result<N, CompilerMsg>,
|
||||
) -> Result<Parsed<N>, CompilerMsg> {
|
||||
) -> Result<N, CompilerMsg> {
|
||||
let old_start = self.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;
|
||||
res
|
||||
}
|
||||
|
||||
pub fn ident(&mut self, s: String) -> Parsed<Ident> {
|
||||
pub fn ident(&mut self, s: String) -> Ident {
|
||||
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;
|
||||
Parsed::new(lit, span)
|
||||
Lit { ty, span }
|
||||
}
|
||||
|
||||
pub fn push_adv<N: Node>(&mut self, node: N) -> Parsed<N> {
|
||||
let res = self.push(node);
|
||||
self.cursor.next();
|
||||
res
|
||||
}
|
||||
|
||||
pub fn push<N: Node>(&mut self, node: N) -> Parsed<N> {
|
||||
pub fn span(&mut self) -> Span {
|
||||
let end = self.cursor.cur_end();
|
||||
Parsed::new(
|
||||
node,
|
||||
Span {
|
||||
file: self.cursor.file(),
|
||||
start: self.start,
|
||||
end,
|
||||
},
|
||||
)
|
||||
Span {
|
||||
file: self.cursor.file(),
|
||||
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();
|
||||
if self.next_if(&end) {
|
||||
return Ok(list);
|
||||
|
||||
+3
-12
@@ -1,4 +1,4 @@
|
||||
use crate::parser::{Node, Parsed};
|
||||
use crate::parser::Node;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct DisplayCtx {
|
||||
@@ -17,7 +17,7 @@ impl<N: Node> std::fmt::Display for NodeDsp<'_, N> {
|
||||
}
|
||||
|
||||
pub struct VecDsp<'a, N> {
|
||||
list: &'a Vec<Parsed<N>>,
|
||||
list: &'a Vec<N>,
|
||||
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> {
|
||||
fn dsp<'a, 'b>(&'a self, ctx: impl Into<DisplayCtx>) -> VecDsp<'b, N>
|
||||
where
|
||||
'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>
|
||||
where
|
||||
'a: 'b,
|
||||
|
||||
+8
-37
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
io::{CompilerMsg, CompilerOutput, Span},
|
||||
io::{CompilerMsg, CompilerOutput},
|
||||
parser::{Cursor, nodes::*},
|
||||
};
|
||||
|
||||
@@ -8,17 +8,18 @@ mod dsp;
|
||||
pub use ctx::*;
|
||||
pub use dsp::*;
|
||||
|
||||
pub struct Parsed<N> {
|
||||
pub node: N,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
pub trait Node: Sized {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>;
|
||||
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) {
|
||||
Ok(code) => code,
|
||||
Err(err) => {
|
||||
@@ -37,33 +38,3 @@ pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Parsed<Body
|
||||
};
|
||||
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 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -7,3 +7,7 @@ while true {
|
||||
let y = true;
|
||||
|
||||
if y => print("hello");
|
||||
|
||||
fn thing() {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user