This commit is contained in:
2026-04-11 15:21:03 -04:00
parent 229b026573
commit 2582e8c87e
15 changed files with 301 additions and 199 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ fn main() {
let mut output = CompilerOutput::new(); let mut output = CompilerOutput::new();
let nodes = Nodes::parse_root(&path, &mut output); let nodes = Nodes::parse_root(&path, &mut output);
if let Some((nodes, root)) = nodes { if let Some((nodes, root)) = nodes {
println!("{}", root.dsp(&nodes)); print!("{}", root.dsp(&nodes));
} }
output.write(&mut std::io::stdout()); output.write(&mut std::io::stdout());
} }
+3 -2
View File
@@ -5,6 +5,7 @@ pub enum Lit {
Number(String), Number(String),
Bool(bool), Bool(bool),
String(String), String(String),
Unit,
} }
impl From<Lit> for Token { impl From<Lit> for Token {
@@ -18,8 +19,8 @@ impl std::fmt::Display for Lit {
match self { match self {
Lit::Number(n) => write!(f, "{n}"), Lit::Number(n) => write!(f, "{n}"),
Lit::Bool(b) => write!(f, "{b}"), Lit::Bool(b) => write!(f, "{b}"),
Lit::String(s) => write!(f, "{s}"), Lit::String(s) => write!(f, "\"{s}\""),
Lit::Unit => write!(f, "()"),
} }
} }
} }
+3 -2
View File
@@ -35,8 +35,8 @@ impl<'a> Cursor<'a> {
}) })
} }
pub fn next_if(&mut self, token: Token) -> bool { pub fn next_if(&mut self, token: &Token) -> bool {
if self.peek().is_some_and(|t| *t == token) { if self.peek().is_some_and(|t| t == token) {
self.next(); self.next();
true true
} else { } else {
@@ -54,6 +54,7 @@ impl<'a> Cursor<'a> {
pub fn expect_peek(&self) -> Result<&Token, CompilerMsg> { pub fn expect_peek(&self) -> Result<&Token, CompilerMsg> {
self.peek().ok_or_else(CompilerMsg::unexpected_eof) self.peek().ok_or_else(CompilerMsg::unexpected_eof)
// Ok(self.peek().unwrap())
} }
pub fn expect(&mut self, token: Token) -> Result<Token, CompilerMsg> { pub fn expect(&mut self, token: Token) -> Result<Token, CompilerMsg> {
+10 -7
View File
@@ -3,6 +3,8 @@ use std::{iter::Peekable, str::CharIndices};
def_tokens! { def_tokens! {
symbol { symbol {
Dot: ".",
Comma: ",",
Equal: "=", Equal: "=",
Colon: ":", Colon: ":",
Semicolon: ";", Semicolon: ";",
@@ -23,9 +25,9 @@ def_tokens! {
AsteriskEqual: "*=", AsteriskEqual: "*=",
SlashEqual: "/=", SlashEqual: "/=",
DoubleColon: "::", DoubleColon: "::",
Hash: "#",
} }
keyword { keyword {
Let: "let",
Fn: "fn", Fn: "fn",
If: "if", If: "if",
Loop: "loop", Loop: "loop",
@@ -80,12 +82,15 @@ impl Iterator for Tokens<'_> {
}; };
} }
let inner = match c { let inner = match c {
'.' => Token::Dot,
',' => Token::Comma,
'(' => Token::OpenParen, '(' => Token::OpenParen,
')' => Token::CloseParen, ')' => Token::CloseParen,
'[' => Token::OpenSquare, '[' => Token::OpenSquare,
']' => Token::CloseSquare, ']' => Token::CloseSquare,
'{' => Token::OpenCurly, '{' => Token::OpenCurly,
'}' => Token::CloseCurly, '}' => Token::CloseCurly,
'#' => Token::Hash,
'+' => then! { '+' => then! {
_ => Token::Plus, _ => Token::Plus,
'=' => Token::PlusEqual, '=' => Token::PlusEqual,
@@ -124,15 +129,13 @@ impl Iterator for Tokens<'_> {
Lit::Number(s).into() Lit::Number(s).into()
} }
'"' => { '"' => {
let mut s = c.to_string(); let mut s = String::new();
while let Some((i, c)) = self.chars.peek() while let Some((i, c)) = self.chars.next()
&& !matches!(c, '"') && !matches!(c, '"')
{ {
s.push(*c); s.push(c);
span.end = *i; span.end = i;
self.chars.next();
} }
self.chars.next();
Lit::String(s).into() Lit::String(s).into()
} }
_ => { _ => {
+36 -14
View File
@@ -38,8 +38,7 @@ pub struct DisplayCtx<'a> {
pub struct IdDisplay<'a, N> { pub struct IdDisplay<'a, N> {
id: Id<N>, id: Id<N>,
nodes: &'a Nodes, ctx: DisplayCtx<'a>,
indent: usize,
} }
pub trait FmtNode: Node { pub trait FmtNode: Node {
@@ -48,13 +47,7 @@ pub trait FmtNode: Node {
impl<N: FmtNode> std::fmt::Display for IdDisplay<'_, N> { impl<N: FmtNode> std::fmt::Display for IdDisplay<'_, N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.nodes[self.id].fmt( self.ctx.nodes[self.id].fmt(f, self.ctx)
f,
DisplayCtx {
nodes: self.nodes,
indent: self.indent,
},
)
} }
} }
@@ -76,11 +69,7 @@ impl<N: FmtNode> Id<N> {
'a: 'b, 'a: 'b,
{ {
let ctx = ctx.into(); let ctx = ctx.into();
IdDisplay { IdDisplay { id: *self, ctx }
id: *self,
nodes: ctx.nodes,
indent: ctx.indent,
}
} }
} }
@@ -92,3 +81,36 @@ impl<'a> From<&'a Nodes> for DisplayCtx<'a> {
} }
} }
} }
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 }
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ pub use id::*;
pub use parse::*; pub use parse::*;
impl Nodes { impl Nodes {
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<(Self, Id<Module>)> { pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<(Self, Id<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) => {
+19 -19
View File
@@ -2,7 +2,7 @@ use crate::{
io::{CompilerMsg, Span}, io::{CompilerMsg, Span},
parser::{ parser::{
Id, Ident, Node, Nodes, Id, Ident, Node, Nodes,
cursor::{Cursor, Lit}, cursor::{Cursor, Lit, Token},
}, },
}; };
@@ -10,16 +10,6 @@ pub trait Parsable: Sized + Node {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>; fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>;
} }
pub trait ParsableWith<Input>: Sized + Node {
fn parse_with(ctx: &mut ParseCtx, input: Input) -> Result<Self, CompilerMsg>;
}
impl<P: Parsable> ParsableWith<()> for P {
fn parse_with(ctx: &mut ParseCtx, _: ()) -> Result<Self, CompilerMsg> {
P::parse(ctx)
}
}
pub struct ParseCtx<'a> { pub struct ParseCtx<'a> {
start: usize, start: usize,
cursor: Cursor<'a>, cursor: Cursor<'a>,
@@ -36,16 +26,9 @@ impl<'a> ParseCtx<'a> {
} }
pub fn parse<P: Parsable>(&mut self) -> Result<Id<P>, CompilerMsg> { pub fn parse<P: Parsable>(&mut self) -> Result<Id<P>, CompilerMsg> {
self.parse_with(())
}
pub fn parse_with<P: ParsableWith<Input>, Input>(
&mut self,
input: Input,
) -> Result<Id<P>, 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 = P::parse_with(self, input).map(|r| self.push(r)); let res = P::parse(self).map(|r| self.push(r));
self.start = old_start; self.start = old_start;
res res
} }
@@ -54,15 +37,18 @@ impl<'a> ParseCtx<'a> {
let span = self.cursor.span; let span = self.cursor.span;
self.nodes.idents.add(Ident { inner: s }, span) self.nodes.idents.add(Ident { inner: s }, span)
} }
pub fn lit(&mut self, lit: Lit) -> Id<Lit> { pub fn lit(&mut self, lit: Lit) -> Id<Lit> {
let span = self.cursor.span; let span = self.cursor.span;
self.nodes.lits.add(lit, span) self.nodes.lits.add(lit, span)
} }
pub fn push_adv<N: Node>(&mut self, node: N) -> Id<N> { pub fn push_adv<N: Node>(&mut self, node: N) -> Id<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) -> Id<N> {
let end = self.cursor.cur_end(); let end = self.cursor.cur_end();
N::vec_mut(&mut self.nodes).add( N::vec_mut(&mut self.nodes).add(
@@ -74,6 +60,20 @@ impl<'a> ParseCtx<'a> {
}, },
) )
} }
pub fn list<N: Parsable>(&mut self, sep: Token, end: Token) -> Result<Vec<Id<N>>, CompilerMsg> {
let mut list = Vec::new();
if self.next_if(&end) {
return Ok(list);
}
list.push(self.parse()?);
while self.next_if(&sep) {
list.push(self.parse()?);
}
self.expect(end)?;
Ok(list)
}
pub fn finish(self) -> Nodes { pub fn finish(self) -> Nodes {
self.nodes self.nodes
} }
+52
View File
@@ -0,0 +1,52 @@
use super::*;
pub struct Body {
pub exprs: Vec<Id<Expr>>,
pub final_semicolon: bool,
}
impl Parsable for Body {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut exprs = Vec::new();
let mut final_semicolon = false;
match ctx.peek() {
None | Some(Token::CloseCurly) => {
return Ok(Self {
exprs,
final_semicolon,
});
}
_ => (),
}
exprs.push(ctx.parse()?);
while ctx.next_if(&Token::Semicolon) {
final_semicolon = true;
if ctx.peek().is_none_or(|t| *t == Token::CloseCurly) {
break;
}
exprs.push(ctx.parse()?);
final_semicolon = false;
}
Ok(Self {
exprs,
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.exprs.split_last() {
for &i in rest {
writeln!(f, "{}{};", " ".repeat(ctx.indent), i.dsp(ctx))?;
}
if self.final_semicolon {
writeln!(f, "{}{};", " ".repeat(ctx.indent), last.dsp(ctx))?;
} else {
writeln!(f, "{}{}", " ".repeat(ctx.indent), last.dsp(ctx))?;
}
}
Ok(())
}
}
+135 -24
View File
@@ -1,44 +1,159 @@
use crate::parser::VecDspT;
pub use super::*; pub use super::*;
pub enum Expr { pub enum Expr {
Block(Id<Body>),
Group(Id<Expr>),
Ident(Id<Ident>), Ident(Id<Ident>),
Lit(Id<Lit>), Lit(Id<Lit>),
Negate(Id<Expr>), Negate(Id<Expr>),
Assign(Id<Expr>, Id<Expr>), Call {
target: Id<Expr>,
args: Vec<Id<Expr>>,
},
Assign {
target: Id<Expr>,
val: Id<Expr>,
},
Define {
target: Id<Expr>,
ty: Option<Id<Type>>,
val: Id<Expr>,
},
If {
cond: Id<Expr>,
body: Id<Expr>,
},
Loop {
body: Id<Expr>,
},
While {
cond: Id<Expr>,
body: Id<Expr>,
},
Fn(Id<Func>),
} }
impl Parsable for Expr { impl Parsable for Expr {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> { fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let e1 = match ctx.expect_next()? { let mut res = Self::parse_unit(ctx)?;
while let Some(next) = ctx.peek() {
res = match next {
Token::Equal => {
let target = ctx.push_adv(res);
let val = Self::push_unit(ctx)?;
Expr::Assign { target, val }
}
Token::Colon => {
let target = ctx.push_adv(res);
let mut ty = None;
if !ctx.next_if(&Token::Equal) {
ty = Some(ctx.parse()?);
ctx.expect(Token::Equal)?;
}
let val = Self::push_unit(ctx)?;
Expr::Define { target, ty, val }
}
Token::OpenParen => {
let target = ctx.push_adv(res);
let args = ctx.list(Token::Comma, Token::CloseParen)?;
Expr::Call { target, args }
}
_ => break,
}
}
Ok(res)
}
}
impl Expr {
fn push_unit(ctx: &mut ParseCtx) -> Result<Id<Self>, CompilerMsg> {
let res = Self::parse_unit(ctx)?;
Ok(ctx.push(res))
}
fn parse_unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_next()? {
Token::Dash => Self::Negate(ctx.parse()?), Token::Dash => Self::Negate(ctx.parse()?),
Token::Ident(s) => Self::Ident(ctx.ident(s)), Token::Ident(s) => Self::Ident(ctx.ident(s)),
Token::Lit(l) => Self::Lit(ctx.lit(l)), Token::Lit(l) => Self::Lit(ctx.lit(l)),
other => return ctx.unexpected(&other, "an expression"), Token::Fn => Self::Fn(ctx.parse()?),
}; Token::If => {
let Some(next) = ctx.peek() else { let cond = ctx.parse()?;
return Ok(e1); let body = ctx.parse()?;
}; Self::If { cond, body }
Ok(match next {
Token::Equal => {
let e1 = ctx.push_adv(e1);
let e2: Id<Expr> = ctx.parse()?;
Expr::Assign(e1, e2)
} }
_ => e1, Token::While => {
let cond = ctx.parse()?;
let body = ctx.parse()?;
Self::While { cond, body }
}
Token::Loop => {
let body = ctx.parse()?;
Self::Loop { body }
}
Token::OpenParen => {
if ctx.next_if(&Token::CloseParen) {
Self::Lit(ctx.push(Lit::Unit))
} else {
let inner = ctx.parse()?;
ctx.expect(Token::CloseParen)?;
Self::Group(inner)
}
}
Token::OpenCurly => {
let body = ctx.parse()?;
ctx.expect(Token::CloseCurly)?;
Self::Block(body)
}
other => return ctx.unexpected(&other, "an expression"),
}) })
} }
} }
impl FmtNode for Expr { impl FmtNode for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
match *self { match *self {
Expr::Ident(id) => id.fmt(f, ctx), Self::Ident(id) => id.fmt(f, ctx),
Expr::Lit(id) => id.fmt(f, ctx), Self::Group(id) => write!(f, "({})", id.dsp(ctx)),
Expr::Negate(id) => { Self::Fn(id) => id.fmt(f, ctx),
Self::Lit(id) => id.fmt(f, ctx),
Self::Negate(id) => {
write!(f, "-{}", id.dsp(ctx)) write!(f, "-{}", id.dsp(ctx))
} }
Expr::Assign(id1, id2) => { Self::Call { target, ref args } => {
write!(f, "{} = {}", id1.dsp(ctx), id2.dsp(ctx)) write!(f, "{}({})", target.dsp(ctx), args.dsp(ctx))
}
Self::Assign { target, val } => {
write!(f, "{} = {}", target.dsp(ctx), val.dsp(ctx))
}
Self::Define { target, ty, val } => {
target.fmt(f, ctx)?;
if let Some(ty) = ty {
write!(f, ": {} ", ty.dsp(ctx))?;
} else {
write!(f, " :")?;
}
write!(f, "= {}", 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 !ctx.nodes[body].exprs.is_empty() {
writeln!(f)?;
ctx.indent += 3;
body.fmt(f, ctx)?;
}
write!(f, "}}")?;
Ok(())
} }
} }
} }
@@ -46,10 +161,6 @@ impl FmtNode for Expr {
impl FmtNode for Lit { impl FmtNode for Lit {
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
match self { write!(f, "{self}")
Lit::Number(v) => write!(f, "{v}"),
Lit::Bool(v) => write!(f, "{v}"),
Lit::String(v) => write!(f, "{v}"),
}
} }
} }
+29
View File
@@ -0,0 +1,29 @@
use super::*;
pub struct Func {
args: Vec<Id<Ident>>,
body: Id<Expr>,
}
impl Parsable for Func {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
ctx.expect(Token::OpenParen)?;
let args = ctx.list(Token::Comma, Token::CloseParen)?;
let body = ctx.parse()?;
Ok(Self { args, body })
}
}
impl FmtNode for Func {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "(")?;
if let Some((last, rest)) = self.args.split_last() {
for arg in rest {
write!(f, "{}, ", arg.dsp(ctx))?;
}
write!(f, "{}", last.dsp(ctx))?;
}
write!(f, ") {}", self.body.dsp(ctx))?;
Ok(())
}
}
-24
View File
@@ -1,24 +0,0 @@
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)),
}
}
}
+6 -9
View File
@@ -1,14 +1,12 @@
mod body;
mod expr; mod expr;
mod func;
mod ident; mod ident;
mod item;
mod module;
mod statement;
mod ty; mod ty;
pub use body::*;
pub use expr::*; pub use expr::*;
pub use func::*;
pub use ident::*; pub use ident::*;
pub use item::*;
pub use module::*;
pub use statement::*;
pub use ty::*; pub use ty::*;
use super::{DisplayCtx, FmtNode, Id, Lit, Node, NodeVec, Parsable, ParseCtx, Token}; use super::{DisplayCtx, FmtNode, Id, Lit, Node, NodeVec, Parsable, ParseCtx, Token};
@@ -17,11 +15,10 @@ use crate::io::CompilerMsg;
def_nodes! { def_nodes! {
exprs: Expr, exprs: Expr,
idents: Ident, idents: Ident,
statements: Statement, blocks: Body,
blocks: Module,
lits: Lit, lits: Lit,
types: Type, types: Type,
items: Item, funcs: Func,
} }
macro_rules! def_nodes { macro_rules! def_nodes {
-35
View File
@@ -1,35 +0,0 @@
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(())
}
}
-60
View File
@@ -1,60 +0,0 @@
pub use super::*;
pub enum Statement {
Let {
name: Id<Ident>,
ty: Option<Id<Type>>,
val: Id<Expr>,
},
If {
cond: Id<Expr>,
body: Id<Expr>,
},
Expr(Id<Expr>),
}
impl Parsable for Statement {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_peek()? {
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,
ty,
val: ctx.parse()?,
}
}
Token::If => {
ctx.next();
let cond = ctx.parse()?;
let body = ctx.parse()?;
Self::If { cond, body }
}
_ => Self::Expr(ctx.parse()?),
})
}
}
impl FmtNode for Statement {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
match *self {
Self::If { cond, body } => {
write!(f, "if {} {}", cond.dsp(ctx), body.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),
}
}
}
+6 -1
View File
@@ -1 +1,6 @@
let x: i32 = arst -3 x: i32 = 3;
y := fn(a, b, c) {};
while Some(a) := b {
print("hello");
f(x, y, z)
}