Parse let and number expression.
This commit is contained in:
Generated
+25
@@ -2,6 +2,31 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diff"
|
||||||
|
version = "0.1.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pretty_assertions"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d"
|
||||||
|
dependencies = [
|
||||||
|
"diff",
|
||||||
|
"yansi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rscript"
|
name = "rscript"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"pretty_assertions",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "yansi"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
pretty_assertions = "1.4.1"
|
||||||
|
|||||||
+65
-3
@@ -1,7 +1,6 @@
|
|||||||
pub struct Script {
|
use crate::span::Span;
|
||||||
pub items: Vec<Stmt>,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Stmt {
|
pub enum Stmt {
|
||||||
Let(Let),
|
Let(Let),
|
||||||
Assign(Assign),
|
Assign(Assign),
|
||||||
@@ -9,56 +8,119 @@ pub enum Stmt {
|
|||||||
Fn(FnDef),
|
Fn(FnDef),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<Let> for Stmt {
|
||||||
|
fn from(let_stmt: Let) -> Self {
|
||||||
|
Stmt::Let(let_stmt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Assign> for Stmt {
|
||||||
|
fn from(assign_stmt: Assign) -> Self {
|
||||||
|
Stmt::Assign(assign_stmt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Expr> for Stmt {
|
||||||
|
fn from(expr_stmt: Expr) -> Self {
|
||||||
|
Stmt::Expr(expr_stmt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<FnDef> for Stmt {
|
||||||
|
fn from(fn_def: FnDef) -> Self {
|
||||||
|
Stmt::Fn(fn_def)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Let {
|
pub struct Let {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub ty: Option<Type>,
|
pub ty: Option<Type>,
|
||||||
pub value: Expr,
|
pub value: Expr,
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Assign {
|
pub struct Assign {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub value: Expr,
|
pub value: Expr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct FnDef {
|
pub struct FnDef {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub params: Vec<FnParam>,
|
pub params: Vec<FnParam>,
|
||||||
pub body: Vec<Stmt>,
|
pub body: Vec<Stmt>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct FnParam {
|
pub struct FnParam {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub ty: Type,
|
pub ty: Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
Variable(Ident),
|
Variable(Ident),
|
||||||
FnCall(FnCall),
|
FnCall(FnCall),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<LitNumber> for Expr {
|
||||||
|
fn from(lit: LitNumber) -> Self {
|
||||||
|
Expr::Literal(lit.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<LitString> for Expr {
|
||||||
|
fn from(lit: LitString) -> Self {
|
||||||
|
Expr::Literal(lit.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Literal {
|
pub enum Literal {
|
||||||
Number(LitNumber),
|
Number(LitNumber),
|
||||||
String(LitString),
|
String(LitString),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<LitNumber> for Literal {
|
||||||
|
fn from(lit: LitNumber) -> Self {
|
||||||
|
Literal::Number(lit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<LitString> for Literal {
|
||||||
|
fn from(lit: LitString) -> Self {
|
||||||
|
Literal::String(lit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct LitNumber {
|
pub struct LitNumber {
|
||||||
pub value: f64,
|
pub value: f64,
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct LitString {
|
pub struct LitString {
|
||||||
pub value: String,
|
pub value: String,
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct FnCall {
|
pub struct FnCall {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub args: Vec<Expr>,
|
pub args: Vec<Expr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Ident {
|
pub struct Ident {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Type {
|
pub enum Type {
|
||||||
Number,
|
Number,
|
||||||
String,
|
String,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
pub mod ast;
|
pub mod ast;
|
||||||
pub mod cursor;
|
pub mod cursor;
|
||||||
pub mod lexer;
|
pub mod lexer;
|
||||||
|
pub mod parser;
|
||||||
pub mod span;
|
pub mod span;
|
||||||
|
|||||||
+217
@@ -1 +1,218 @@
|
|||||||
|
use std::iter::Peekable;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
ast::{Expr, Ident, Let, LitNumber, Stmt},
|
||||||
|
lexer::Token,
|
||||||
|
span::{Span, Spanned},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Parser<'a, T>
|
||||||
|
where
|
||||||
|
T: Iterator<Item = Spanned<Token>>,
|
||||||
|
{
|
||||||
|
input: &'a str,
|
||||||
|
tokens: Peekable<T>,
|
||||||
|
current_span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> Parser<'a, T>
|
||||||
|
where
|
||||||
|
T: Iterator<Item = Spanned<Token>>,
|
||||||
|
{
|
||||||
|
pub fn new(input: &'a str, tokens: T) -> Self {
|
||||||
|
Self {
|
||||||
|
input,
|
||||||
|
tokens: tokens.peekable(),
|
||||||
|
current_span: Span::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn peek_token(&mut self) -> Option<&Token> {
|
||||||
|
self.tokens.peek().map(|spanned| spanned.node())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_token(&mut self) -> Option<Spanned<Token>> {
|
||||||
|
self.tokens.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expect(&mut self, expected: Token) -> Result<Spanned<Token>, ParseError> {
|
||||||
|
let next = self.next_token().ok_or_else(|| ParseError {
|
||||||
|
span: self.current_span,
|
||||||
|
message: format!("Expected {:?}, found end of file", expected),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if next.node() == &expected {
|
||||||
|
Ok(next)
|
||||||
|
} else {
|
||||||
|
Err(ParseError {
|
||||||
|
span: self.current_span,
|
||||||
|
message: format!("Expected {:?}, found {:?}", expected, next.node()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn slice(&self, span: Span) -> Result<&'a str, ParseError> {
|
||||||
|
self.input
|
||||||
|
.get(span.start()..span.end())
|
||||||
|
.ok_or_else(|| ParseError {
|
||||||
|
span,
|
||||||
|
message: "Span out of bounds".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(&mut self) -> Vec<Result<Stmt, ParseError>> {
|
||||||
|
let mut stmts = Vec::new();
|
||||||
|
while self.peek_token().is_some() {
|
||||||
|
stmts.push(self.parse_stmt());
|
||||||
|
}
|
||||||
|
stmts
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_stmt(&mut self) -> Result<Stmt, ParseError> {
|
||||||
|
match self.peek_token() {
|
||||||
|
Some(Token::Let) => {
|
||||||
|
let r#let = self.next_token().unwrap();
|
||||||
|
let ident = self.expect(Token::Ident).map(|spanned| {
|
||||||
|
Ok(Ident {
|
||||||
|
name: self.slice(*spanned.span())?.to_string(),
|
||||||
|
span: *spanned.span(),
|
||||||
|
})
|
||||||
|
})??;
|
||||||
|
|
||||||
|
self.expect(Token::Equals)?;
|
||||||
|
let expr = self.parse_expr()?;
|
||||||
|
let semi = self.expect(Token::Semicolon)?;
|
||||||
|
|
||||||
|
Ok(Let {
|
||||||
|
name: ident,
|
||||||
|
ty: None,
|
||||||
|
value: expr,
|
||||||
|
span: r#let.span_to(&semi),
|
||||||
|
}
|
||||||
|
.into())
|
||||||
|
}
|
||||||
|
Some(Token::Ident) => {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
Some(Token::Fn) => {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
Some(_) => Err(ParseError {
|
||||||
|
span: self.current_span,
|
||||||
|
message: "Unexpected token in statement".to_string(),
|
||||||
|
}),
|
||||||
|
None => Err(ParseError {
|
||||||
|
span: self.current_span,
|
||||||
|
message: "Unexpected end of file".to_string(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_expr(&mut self) -> Result<Expr, ParseError> {
|
||||||
|
match self.peek_token() {
|
||||||
|
Some(Token::Number) => {
|
||||||
|
let number = self.next_token().unwrap();
|
||||||
|
Ok(LitNumber {
|
||||||
|
value: self
|
||||||
|
.slice(*number.span())?
|
||||||
|
.parse::<f64>()
|
||||||
|
.map_err(|e| ParseError {
|
||||||
|
span: *number.span(),
|
||||||
|
message: format!("Failed to parse number: {}", e),
|
||||||
|
})?,
|
||||||
|
span: *number.span(),
|
||||||
|
}
|
||||||
|
.into())
|
||||||
|
}
|
||||||
|
Some(_) => Err(ParseError {
|
||||||
|
span: self.current_span,
|
||||||
|
message: "Unexpected token in expression".to_string(),
|
||||||
|
}),
|
||||||
|
None => Err(ParseError {
|
||||||
|
span: self.current_span,
|
||||||
|
message: "Unexpected end of file".to_string(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct ParseError {
|
||||||
|
span: Span,
|
||||||
|
message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParseError {
|
||||||
|
pub fn span(&self) -> Span {
|
||||||
|
self.span
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn message(&self) -> &str {
|
||||||
|
&self.message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::lexer::Lexer;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_stmt_let_untyped() {
|
||||||
|
let input = "let x = 500.0;";
|
||||||
|
let lexer = Lexer::new(input);
|
||||||
|
let mut parser = Parser::new(input, lexer);
|
||||||
|
let result = parser.parse_stmt();
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
result,
|
||||||
|
Ok(Stmt::Let(Let {
|
||||||
|
name: Ident {
|
||||||
|
name: "x".to_string(),
|
||||||
|
span: Span::new(4, 5),
|
||||||
|
},
|
||||||
|
ty: None,
|
||||||
|
value: LitNumber {
|
||||||
|
value: 500.0,
|
||||||
|
span: Span::new(8, 13)
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
span: Span::new(0, 14),
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
pretty_assertions::assert_eq!(parser.next_token(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_expr_number() {
|
||||||
|
let input = "500.0";
|
||||||
|
let lexer = Lexer::new(input);
|
||||||
|
let mut parser = Parser::new(input, lexer);
|
||||||
|
let result = parser.parse_expr();
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
result,
|
||||||
|
Ok(LitNumber {
|
||||||
|
value: 500.0,
|
||||||
|
span: Span::new(0, 5)
|
||||||
|
}
|
||||||
|
.into())
|
||||||
|
);
|
||||||
|
pretty_assertions::assert_eq!(parser.next_token(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_expr_invalid_number() {
|
||||||
|
let input = "500.0.";
|
||||||
|
let lexer = Lexer::new(input);
|
||||||
|
let mut parser = Parser::new(input, lexer);
|
||||||
|
let result = parser.parse_expr();
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
result,
|
||||||
|
Err(ParseError {
|
||||||
|
span: Span::new(0, 6),
|
||||||
|
message: "Failed to parse number: invalid float literal".to_string()
|
||||||
|
})
|
||||||
|
);
|
||||||
|
pretty_assertions::assert_eq!(parser.next_token(), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+11
-1
@@ -1,4 +1,4 @@
|
|||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct Span {
|
pub struct Span {
|
||||||
start: usize,
|
start: usize,
|
||||||
end: usize,
|
end: usize,
|
||||||
@@ -22,6 +22,12 @@ impl Span {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Span {
|
||||||
|
fn default() -> Self {
|
||||||
|
Span { start: 0, end: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Spanned<T> {
|
pub struct Spanned<T> {
|
||||||
node: T,
|
node: T,
|
||||||
@@ -40,4 +46,8 @@ impl<T> Spanned<T> {
|
|||||||
pub fn span(&self) -> &Span {
|
pub fn span(&self) -> &Span {
|
||||||
&self.span
|
&self.span
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn span_to(&self, other: &Spanned<T>) -> Span {
|
||||||
|
Span::new(self.span.start(), other.span.end())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user