Parse let and number expression.
This commit is contained in:
+65
-3
@@ -1,7 +1,6 @@
|
||||
pub struct Script {
|
||||
pub items: Vec<Stmt>,
|
||||
}
|
||||
use crate::span::Span;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Stmt {
|
||||
Let(Let),
|
||||
Assign(Assign),
|
||||
@@ -9,56 +8,119 @@ pub enum Stmt {
|
||||
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 name: Ident,
|
||||
pub ty: Option<Type>,
|
||||
pub value: Expr,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Assign {
|
||||
pub name: Ident,
|
||||
pub value: Expr,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct FnDef {
|
||||
pub name: Ident,
|
||||
pub params: Vec<FnParam>,
|
||||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct FnParam {
|
||||
pub name: Ident,
|
||||
pub ty: Type,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Expr {
|
||||
Literal(Literal),
|
||||
Variable(Ident),
|
||||
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 {
|
||||
Number(LitNumber),
|
||||
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 value: f64,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct LitString {
|
||||
pub value: String,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct FnCall {
|
||||
pub name: Ident,
|
||||
pub args: Vec<Expr>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Ident {
|
||||
pub name: String,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Type {
|
||||
Number,
|
||||
String,
|
||||
|
||||
Reference in New Issue
Block a user