More stuff compiles

This commit is contained in:
Mia
2026-02-27 15:48:16 +01:00
parent 4b07d93ae3
commit 0b4f169c6f
20 changed files with 552 additions and 134 deletions
+38 -9
View File
@@ -1,3 +1,5 @@
use std::ops::Range;
use arcstr::Substr;
use derive_more::Deref;
@@ -32,6 +34,8 @@ pub enum Expr {
Block(Block),
#[debug("{_0:?}")]
Func(Box<Function>),
#[debug("{_0:?}")]
Type(Box<Type>),
#[debug("{_0:?}")]
ConstDecl(Box<ConstDecl>),
@@ -39,6 +43,8 @@ pub enum Expr {
VarDecl(Box<VarDecl>),
#[debug("{_0:?}")]
For(Box<For>),
#[debug("{_0:?}")]
While(Box<While>),
Call {
func: Box<Expr>,
@@ -46,6 +52,14 @@ pub enum Expr {
},
}
impl Expr {
pub fn range(&self) -> Range<usize> {
match self {
_ => todo!(),
}
}
}
#[derive(Debug)]
pub struct BinaryExpr {
pub lhs: Expr,
@@ -62,14 +76,23 @@ pub struct IndexingExpr {
#[rustfmt::skip]
#[derive(derive_more::Debug)]
pub enum BinaryOp {
#[debug("{_0:?}")] Add(Substr),
#[debug("{_0:?}")] Sub(Substr),
#[debug("{_0:?}")] Mul(Substr),
#[debug("{_0:?}")] Div(Substr),
#[debug("{_0:?}")] Mod(Substr),
#[debug("{_0:?}")] Dot(Substr),
#[debug("{_0:?}")] Range(Substr),
#[debug("{_0:?}")] Assign(Substr),
#[debug("{_0}")] Add(Substr),
#[debug("{_0}")] Sub(Substr),
#[debug("{_0}")] Mul(Substr),
#[debug("{_0}")] Div(Substr),
#[debug("{_0}")] Mod(Substr),
#[debug("{_0}")] Dot(Substr),
#[debug("{_0}")] Lt(Substr),
#[debug("{_0}")] Gt(Substr),
#[debug("{_0}")] Le(Substr),
#[debug("{_0}")] Ge(Substr),
#[debug("{_0}")] Range(Substr),
#[debug("{_0}")] Assign(Substr),
}
#[derive(Debug)]
pub enum Type {
Ptr { base: Expr, mutable: bool },
}
#[derive(Debug)]
@@ -107,7 +130,7 @@ impl NamePattern {
pub struct Function {
pub text: Substr,
pub args: Vec<NameValuePair>,
pub ret: Expr,
pub ret: Option<Expr>,
pub block: Option<Block>,
}
@@ -133,6 +156,12 @@ pub struct For {
pub block: Block,
}
#[derive(Debug)]
pub struct While {
pub value: Expr,
pub block: Block,
}
#[derive(Debug)]
pub struct CompilationUnit {
pub imports: Vec<Import>,
+12 -1
View File
@@ -84,14 +84,21 @@ peg::parser! {
--
lhs:(@) __ op:$("..") __ rhs:@ { Expr::Binary(Box::new(BinaryExpr { lhs, op: BinaryOp::Range(op), rhs })) }
--
lhs:(@) __ op:$("<") __ rhs:@ { Expr::Binary(Box::new(BinaryExpr { lhs, op: BinaryOp::Lt(op), rhs })) }
lhs:(@) __ op:$(">") __ rhs:@ { Expr::Binary(Box::new(BinaryExpr { lhs, op: BinaryOp::Gt(op), rhs })) }
lhs:(@) __ op:$("<=") __ rhs:@ { Expr::Binary(Box::new(BinaryExpr { lhs, op: BinaryOp::Le(op), rhs })) }
lhs:(@) __ op:$(">=") __ rhs:@ { Expr::Binary(Box::new(BinaryExpr { lhs, op: BinaryOp::Ge(op), rhs })) }
--
block:block() { Expr::Block(block)}
for_loop:for_loop() { Expr::For(Box::new(for_loop))}
while_loop:while_loop() { Expr::While(Box::new(while_loop))}
func:func() { Expr::Func(Box::new(func))}
var_decl:var_decl() { Expr::VarDecl(Box::new(var_decl)) }
const_decl:const_decl() { Expr::ConstDecl(Box::new(const_decl)) }
"(" __ tuple:(expr() **<2,> ("," __)) __ ")" { Expr::Tuple(tuple) }
"[" __ list:(expr() **<0,> ("," __)) __ "]" { Expr::List(list) }
"(" __ v:expr() __ ")" { v }
"*" __ m:"mut"? __ v:expr() { Expr::Type(Box::new(Type::Ptr { base:v, mutable: m.is_some() })) }
v:string() { Expr::String(v) }
v:number() { Expr::Number(v) }
v:ident() { Expr::Ident(v) }
@@ -101,7 +108,7 @@ peg::parser! {
= "{" __ exprs:(i:expr() statement_separator() {i})* __ "}" { Block(exprs) }
rule func() -> Function
= s:position!() t:$"fn" __ "(" __ args:name_value_pairs() __ ")" __ ret:expr() __ block:block()? e:position!()
= s:position!() t:$"fn" __ "(" __ args:name_value_pairs() __ ")" __ ret:("->" __ e:expr() {e})? __ block:block()? e:position!()
{ Function { args, ret, block, text: t.parent().substr(s..e), } }
rule name_value_pair() -> NameValuePair
@@ -130,6 +137,10 @@ peg::parser! {
"for" _ names:name_pattern() _ "in" _ value:expr() _ block:block()
{ For { names, value, block } }
rule while_loop() -> While =
"while" _ value:expr() _ block:block()
{ While { value, block } }
pub rule compilation_unit() -> CompilationUnit =
__ imports:(i:import() statement_separator() {i})*
__ decls:(d:const_decl() statement_separator() {d})*