This commit is contained in:
2026-04-12 17:26:39 -04:00
parent 2582e8c87e
commit f702f47714
9 changed files with 121 additions and 49 deletions
+5
View File
@@ -0,0 +1,5 @@
x :i32 = 3;
y := fn(a: i32, b, c) x = 3;
while true {
print("hello");
}
+5 -8
View File
@@ -1,3 +1,5 @@
use std::borrow::Borrow;
use crate::io::{CompilerMsg, Span, Spanned};
mod lit;
@@ -35,8 +37,8 @@ impl<'a> Cursor<'a> {
})
}
pub fn next_if(&mut self, token: &Token) -> bool {
if self.peek().is_some_and(|t| t == token) {
pub fn next_if(&mut self, token: impl Borrow<Token>) -> bool {
if self.peek().is_some_and(|t| t == token.borrow()) {
self.next();
true
} else {
@@ -52,11 +54,6 @@ impl<'a> Cursor<'a> {
self.next().ok_or_else(CompilerMsg::unexpected_eof)
}
pub fn expect_peek(&self) -> Result<&Token, CompilerMsg> {
self.peek().ok_or_else(CompilerMsg::unexpected_eof)
// Ok(self.peek().unwrap())
}
pub fn expect(&mut self, token: Token) -> Result<Token, CompilerMsg> {
let next = self.expect_next()?;
if next == token {
@@ -87,7 +84,7 @@ impl CompilerMsg {
pub fn unexpected_token(token: &Token, span: Span, expected: &str) -> Self {
Self {
spans: vec![span],
msg: format!("Unexpected token '{}'; expected {expected}", token),
msg: format!("Unexpected token '{}', expected {expected}", token),
}
}
+19 -2
View File
@@ -1,3 +1,5 @@
use std::ops::Index;
use crate::{
io::{CompilerMsg, Span},
parser::{
@@ -25,10 +27,17 @@ impl<'a> ParseCtx<'a> {
}
}
pub fn parse<P: Parsable>(&mut self) -> Result<Id<P>, CompilerMsg> {
pub fn parse<N: Parsable>(&mut self) -> Result<Id<N>, CompilerMsg> {
self.parse_with(N::parse)
}
pub fn parse_with<N: Node>(
&mut self,
f: impl FnOnce(&mut Self) -> Result<N, CompilerMsg>,
) -> Result<Id<N>, CompilerMsg> {
let old_start = self.start;
self.start = self.cursor.peek_start();
let res = P::parse(self).map(|r| self.push(r));
let res = f(self).map(|r| self.push(r));
self.start = old_start;
res
}
@@ -92,3 +101,11 @@ impl<'a> std::ops::DerefMut for ParseCtx<'a> {
&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]
}
}
+9 -15
View File
@@ -8,25 +8,19 @@ pub struct Body {
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,
});
fn at_end(ctx: &mut ParseCtx) -> bool {
ctx.peek().is_none_or(|t| *t == Token::CloseCurly)
}
_ => (),
let final_semicolon = loop {
if at_end(ctx) {
break true;
}
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;
if at_end(ctx) {
break false;
}
ctx.expect(Token::Semicolon)?;
};
Ok(Self {
exprs,
final_semicolon,
+27 -12
View File
@@ -37,22 +37,22 @@ pub enum Expr {
impl Parsable for Expr {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut res = Self::parse_unit(ctx)?;
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 = Self::push_unit(ctx)?;
let val = ctx.parse_with(Self::unit)?;
Expr::Assign { target, val }
}
Token::Colon => {
let target = ctx.push_adv(res);
let mut ty = None;
if !ctx.next_if(&Token::Equal) {
if !ctx.next_if(Token::Equal) {
ty = Some(ctx.parse()?);
ctx.expect(Token::Equal)?;
}
let val = Self::push_unit(ctx)?;
let val = ctx.parse_with(Self::unit)?;
Expr::Define { target, ty, val }
}
Token::OpenParen => {
@@ -68,11 +68,7 @@ impl Parsable for Expr {
}
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> {
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)),
@@ -80,12 +76,12 @@ impl Expr {
Token::Fn => Self::Fn(ctx.parse()?),
Token::If => {
let cond = ctx.parse()?;
let body = ctx.parse()?;
let body = cond_body(cond, ctx)?;
Self::If { cond, body }
}
Token::While => {
let cond = ctx.parse()?;
let body = ctx.parse()?;
let body = cond_body(cond, ctx)?;
Self::While { cond, body }
}
Token::Loop => {
@@ -93,7 +89,7 @@ impl Expr {
Self::Loop { body }
}
Token::OpenParen => {
if ctx.next_if(&Token::CloseParen) {
if ctx.next_if(Token::CloseParen) {
Self::Lit(ctx.push(Lit::Unit))
} else {
let inner = ctx.parse()?;
@@ -109,6 +105,25 @@ impl Expr {
other => return ctx.unexpected(&other, "an expression"),
})
}
pub fn is_group(&self) -> bool {
matches!(self, Expr::Group(_))
}
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))
}
}
fn cond_body(cond: Id<Expr>, ctx: &mut ParseCtx) -> Result<Id<Expr>, CompilerMsg> {
if ctx[cond].is_group() {
ctx.parse()
} else {
ctx.parse_with(Expr::block)
}
}
impl FmtNode for Expr {
+18 -5
View File
@@ -1,7 +1,8 @@
use super::*;
pub struct Func {
args: Vec<Id<Ident>>,
args: Vec<Id<Param>>,
ret: Option<Id<Type>>,
body: Id<Expr>,
}
@@ -9,21 +10,33 @@ 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 })
let mut ret = None;
if ctx.next_if(Token::Arrow) {
ret = Some(ctx.parse()?);
}
let body = if ret.is_some() {
ctx.parse_with(Expr::block)
} else {
ctx.parse()
}?;
Ok(Self { args, ret, body })
}
}
impl FmtNode for Func {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "(")?;
write!(f, "fn(")?;
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))?;
write!(f, ") ")?;
if let Some(ret) = self.ret {
write!(f, "-> {} ", ret.dsp(ctx))?;
}
self.body.fmt(f, ctx)?;
Ok(())
}
}
+3
View File
@@ -2,11 +2,13 @@ mod body;
mod expr;
mod func;
mod ident;
mod param;
mod ty;
pub use body::*;
pub use expr::*;
pub use func::*;
pub use ident::*;
pub use param::*;
pub use ty::*;
use super::{DisplayCtx, FmtNode, Id, Lit, Node, NodeVec, Parsable, ParseCtx, Token};
@@ -19,6 +21,7 @@ def_nodes! {
lits: Lit,
types: Type,
funcs: Func,
params: Param,
}
macro_rules! def_nodes {
+27
View File
@@ -0,0 +1,27 @@
use super::*;
pub struct Param {
name: Id<Ident>,
ty: Option<Id<Type>>,
}
impl Parsable for Param {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let name = ctx.parse()?;
let mut ty = None;
if ctx.next_if(Token::Colon) {
ty = Some(ctx.parse()?);
}
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 {
write!(f, ": {}", ty.dsp(ctx))?;
}
Ok(())
}
}
+5 -4
View File
@@ -1,6 +1,7 @@
x :i32 = 3;
y := fn(a, b, c) {};
while Some(a) := b {
while true {
print("hello");
f(x, y, z)
}
print(x);
};
y := fn(x: i32) -> i32 {x};