gaming
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
x :i32 = 3;
|
||||||
|
y := fn(a: i32, b, c) x = 3;
|
||||||
|
while true {
|
||||||
|
print("hello");
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
use crate::io::{CompilerMsg, Span, Spanned};
|
use crate::io::{CompilerMsg, Span, Spanned};
|
||||||
|
|
||||||
mod lit;
|
mod lit;
|
||||||
@@ -35,8 +37,8 @@ impl<'a> Cursor<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_if(&mut self, token: &Token) -> bool {
|
pub fn next_if(&mut self, token: impl Borrow<Token>) -> bool {
|
||||||
if self.peek().is_some_and(|t| t == token) {
|
if self.peek().is_some_and(|t| t == token.borrow()) {
|
||||||
self.next();
|
self.next();
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@@ -52,11 +54,6 @@ impl<'a> Cursor<'a> {
|
|||||||
self.next().ok_or_else(CompilerMsg::unexpected_eof)
|
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> {
|
pub fn expect(&mut self, token: Token) -> Result<Token, CompilerMsg> {
|
||||||
let next = self.expect_next()?;
|
let next = self.expect_next()?;
|
||||||
if next == token {
|
if next == token {
|
||||||
@@ -87,7 +84,7 @@ impl CompilerMsg {
|
|||||||
pub fn unexpected_token(token: &Token, span: Span, expected: &str) -> Self {
|
pub fn unexpected_token(token: &Token, span: Span, expected: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
spans: vec![span],
|
spans: vec![span],
|
||||||
msg: format!("Unexpected token '{}'; expected {expected}", token),
|
msg: format!("Unexpected token '{}', expected {expected}", token),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::ops::Index;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
io::{CompilerMsg, Span},
|
io::{CompilerMsg, Span},
|
||||||
parser::{
|
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;
|
let old_start = self.start;
|
||||||
self.start = self.cursor.peek_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;
|
self.start = old_start;
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
@@ -92,3 +101,11 @@ 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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,25 +8,19 @@ pub struct Body {
|
|||||||
impl Parsable for Body {
|
impl Parsable for Body {
|
||||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||||
let mut exprs = Vec::new();
|
let mut exprs = Vec::new();
|
||||||
let mut final_semicolon = false;
|
fn at_end(ctx: &mut ParseCtx) -> bool {
|
||||||
match ctx.peek() {
|
ctx.peek().is_none_or(|t| *t == Token::CloseCurly)
|
||||||
None | Some(Token::CloseCurly) => {
|
|
||||||
return Ok(Self {
|
|
||||||
exprs,
|
|
||||||
final_semicolon,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
_ => (),
|
let final_semicolon = loop {
|
||||||
|
if at_end(ctx) {
|
||||||
|
break true;
|
||||||
}
|
}
|
||||||
exprs.push(ctx.parse()?);
|
exprs.push(ctx.parse()?);
|
||||||
while ctx.next_if(&Token::Semicolon) {
|
if at_end(ctx) {
|
||||||
final_semicolon = true;
|
break false;
|
||||||
if ctx.peek().is_none_or(|t| *t == Token::CloseCurly) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
exprs.push(ctx.parse()?);
|
|
||||||
final_semicolon = false;
|
|
||||||
}
|
}
|
||||||
|
ctx.expect(Token::Semicolon)?;
|
||||||
|
};
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
exprs,
|
exprs,
|
||||||
final_semicolon,
|
final_semicolon,
|
||||||
|
|||||||
+27
-12
@@ -37,22 +37,22 @@ pub enum Expr {
|
|||||||
|
|
||||||
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 mut res = Self::parse_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);
|
||||||
let val = Self::push_unit(ctx)?;
|
let val = ctx.parse_with(Self::unit)?;
|
||||||
Expr::Assign { target, val }
|
Expr::Assign { target, val }
|
||||||
}
|
}
|
||||||
Token::Colon => {
|
Token::Colon => {
|
||||||
let target = ctx.push_adv(res);
|
let target = ctx.push_adv(res);
|
||||||
let mut ty = None;
|
let mut ty = None;
|
||||||
if !ctx.next_if(&Token::Equal) {
|
if !ctx.next_if(Token::Equal) {
|
||||||
ty = Some(ctx.parse()?);
|
ty = Some(ctx.parse()?);
|
||||||
ctx.expect(Token::Equal)?;
|
ctx.expect(Token::Equal)?;
|
||||||
}
|
}
|
||||||
let val = Self::push_unit(ctx)?;
|
let val = ctx.parse_with(Self::unit)?;
|
||||||
Expr::Define { target, ty, val }
|
Expr::Define { target, ty, val }
|
||||||
}
|
}
|
||||||
Token::OpenParen => {
|
Token::OpenParen => {
|
||||||
@@ -68,11 +68,7 @@ impl Parsable for Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Expr {
|
impl Expr {
|
||||||
fn push_unit(ctx: &mut ParseCtx) -> Result<Id<Self>, CompilerMsg> {
|
fn unit(ctx: &mut ParseCtx) -> Result<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()? {
|
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)),
|
||||||
@@ -80,12 +76,12 @@ impl Expr {
|
|||||||
Token::Fn => Self::Fn(ctx.parse()?),
|
Token::Fn => Self::Fn(ctx.parse()?),
|
||||||
Token::If => {
|
Token::If => {
|
||||||
let cond = ctx.parse()?;
|
let cond = ctx.parse()?;
|
||||||
let body = ctx.parse()?;
|
let body = cond_body(cond, ctx)?;
|
||||||
Self::If { cond, body }
|
Self::If { cond, body }
|
||||||
}
|
}
|
||||||
Token::While => {
|
Token::While => {
|
||||||
let cond = ctx.parse()?;
|
let cond = ctx.parse()?;
|
||||||
let body = ctx.parse()?;
|
let body = cond_body(cond, ctx)?;
|
||||||
Self::While { cond, body }
|
Self::While { cond, body }
|
||||||
}
|
}
|
||||||
Token::Loop => {
|
Token::Loop => {
|
||||||
@@ -93,7 +89,7 @@ impl Expr {
|
|||||||
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(ctx.push(Lit::Unit))
|
||||||
} else {
|
} else {
|
||||||
let inner = ctx.parse()?;
|
let inner = ctx.parse()?;
|
||||||
@@ -109,6 +105,25 @@ impl Expr {
|
|||||||
other => return ctx.unexpected(&other, "an expression"),
|
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 {
|
impl FmtNode for Expr {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub struct Func {
|
pub struct Func {
|
||||||
args: Vec<Id<Ident>>,
|
args: Vec<Id<Param>>,
|
||||||
|
ret: Option<Id<Type>>,
|
||||||
body: Id<Expr>,
|
body: Id<Expr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -9,21 +10,33 @@ impl Parsable 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)?;
|
||||||
let body = ctx.parse()?;
|
let mut ret = None;
|
||||||
Ok(Self { args, body })
|
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 {
|
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, "(")?;
|
write!(f, "fn(")?;
|
||||||
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))?;
|
||||||
}
|
}
|
||||||
write!(f, "{}", last.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ mod body;
|
|||||||
mod expr;
|
mod expr;
|
||||||
mod func;
|
mod func;
|
||||||
mod ident;
|
mod ident;
|
||||||
|
mod param;
|
||||||
mod ty;
|
mod ty;
|
||||||
pub use body::*;
|
pub use body::*;
|
||||||
pub use expr::*;
|
pub use expr::*;
|
||||||
pub use func::*;
|
pub use func::*;
|
||||||
pub use ident::*;
|
pub use ident::*;
|
||||||
|
pub use param::*;
|
||||||
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};
|
||||||
@@ -19,6 +21,7 @@ def_nodes! {
|
|||||||
lits: Lit,
|
lits: Lit,
|
||||||
types: Type,
|
types: Type,
|
||||||
funcs: Func,
|
funcs: Func,
|
||||||
|
params: Param,
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! def_nodes {
|
macro_rules! def_nodes {
|
||||||
|
|||||||
@@ -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
@@ -1,6 +1,7 @@
|
|||||||
x :i32 = 3;
|
x :i32 = 3;
|
||||||
y := fn(a, b, c) {};
|
while true {
|
||||||
while Some(a) := b {
|
|
||||||
print("hello");
|
print("hello");
|
||||||
f(x, y, z)
|
print(x);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
y := fn(x: i32) -> i32 {x};
|
||||||
|
|||||||
Reference in New Issue
Block a user