Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions program/method.rlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class A {
m() {
print "method";
}
}

A().m();
10 changes: 10 additions & 0 deletions rikulox-ast/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ pub enum ExprKind<'src> {
callee: Box<Expr<'src>>,
args: Vec<Expr<'src>>,
},
Get {
left: Box<Expr<'src>>,
name: Identifier<'src>,
},
Set {
left: Box<Expr<'src>>,
name: Identifier<'src>,
value: Box<Expr<'src>>,
},
This,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
7 changes: 7 additions & 0 deletions rikulox-ast/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum StmtKind<'src> {
},
Function(FunctionDecl<'src>),
Return(Option<Expr<'src>>),
Class(ClassDecl<'src>),
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -39,3 +40,9 @@ pub struct FunctionDecl<'src> {
pub params: Vec<Identifier<'src>>,
pub body: Vec<Stmt<'src>>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClassDecl<'src> {
pub name: Identifier<'src>,
pub methods: Vec<FunctionDecl<'src>>,
}
4 changes: 2 additions & 2 deletions rikulox-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "rikulox-cli"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
version.workspace = true
# version.workspace = true
readme.workspace = true
license.workspace = true
repository.workspace = true
Expand All @@ -18,5 +19,4 @@ clap = { version = "4.5.41", features = ["derive"] }
rikulox-parse = { version = "0.1.0", path = "../rikulox-parse" }
rikulox-lex = { version = "0.1.0", path = "../rikulox-lex" }
rikulox-treewalk = { version = "0.1.0", path = "../rikulox-treewalk" }
rikulox-runtime = { version = "0.1.0", path = "../rikulox-runtime" }
rikulox-resolve = { version = "0.1.0", path = "../rikulox-resolve" }
39 changes: 39 additions & 0 deletions rikulox-gc/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@ use crate::{
trace::{Trace, Tracer},
};

pub struct Gc<T: Trace, F, I>
where
F: Fn() -> I,
I: Iterator<Item = EntryRef<T>>,
{
heap: Heap<T>,
alloc_count: u32,
gc_threshold: u32,
get_roots: F,
}

impl<T: Trace, F, I> Gc<T, F, I>
where
F: Fn() -> I,
I: Iterator<Item = EntryRef<T>>,
{
pub fn new(gc_threshold: u32, get_roots: F) -> Self {
Self {
heap: Heap::new(),
alloc_count: 0,
gc_threshold,
get_roots,
}
}

pub fn alloc(&mut self, value: T) -> EntryRef<T> {
self.alloc_count += 1;

if self.alloc_count == self.gc_threshold {
self.alloc_count = 0;
let roots = (self.get_roots)();
self.heap.mark(roots);
self.heap.sweep();
}

self.heap.alloc(value)
}
}

#[derive(Debug, Clone)]
pub struct Heap<T: Trace> {
list: FreeList<T>,
Expand Down
129 changes: 111 additions & 18 deletions rikulox-parse/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rikulox_ast::{
expr::{BinOp, Expr, ExprKind, Identifier, Literal, LogicalOp, UnaryOp},
id::IdGen,
span::Span,
stmt::{FunctionDecl, Stmt, StmtKind},
stmt::{ClassDecl, FunctionDecl, Stmt, StmtKind},
token::{Keyword, Token, TokenKind},
};

Expand Down Expand Up @@ -50,7 +50,16 @@ where
}
TokenKind::Keyword(Keyword::Fun) => {
let fun_span = self.advance().unwrap().span;
self.function_decl(Some(fun_span))
let (fun, end_span) = self.function_decl(Some(fun_span))?;
Ok(Stmt {
kind: StmtKind::Function(fun),
span: fun_span.with_end_from(end_span),
id: self.id_gen.next_id(),
})
}
TokenKind::Keyword(Keyword::Class) => {
let class_span = self.advance().unwrap().span;
self.class_decl(class_span)
}
_ => self.statement(),
}
Expand Down Expand Up @@ -132,7 +141,7 @@ where
fn function_decl(
&mut self,
fun_span: Option<Span>,
) -> Result<Stmt<'src>, ParseError<'src>> {
) -> Result<(FunctionDecl<'src>, Span), ParseError<'src>> {
let Token {
kind: TokenKind::Identifier(ident),
span: ident_span,
Expand Down Expand Up @@ -171,13 +180,58 @@ where
self.consume(&TokenKind::LBrace)?;
let (body, end_span) = self.block()?;

Ok(Stmt {
kind: StmtKind::Function(FunctionDecl {
Ok((
FunctionDecl {
name: Identifier { symbol: ident },
params,
body,
},
fun_span.unwrap_or(ident_span).with_end_from(end_span),
))
}

fn class_decl(
&mut self,
class_span: Span,
) -> Result<Stmt<'src>, ParseError<'src>> {
let name = match self.peek_or_err(ExpectedItem::Ident)? {
&Token {
kind: TokenKind::Identifier(name),
..
} => {
self.advance().unwrap();
name
}
unexpected => {
return Err(ParseError {
kind: ParseErrorKind::UnexpectedToken {
expected: ExpectedItem::Ident,
found: unexpected.kind,
},
span: unexpected.span,
});
}
};

self.consume(&TokenKind::LBrace)?;

let mut methods = Vec::new();

while let Some(token) = self.peek()
&& !matches!(token.kind, TokenKind::RBrace)
{
let (fun, _end_span) = self.function_decl(None)?;
methods.push(fun);
}

let r_brace = self.consume(&TokenKind::RBrace)?;

Ok(Stmt {
kind: StmtKind::Class(ClassDecl {
name: Identifier { symbol: name },
methods,
}),
span: fun_span.unwrap_or(ident_span).with_end_from(end_span),
span: class_span.with_end_from(r_brace.span),
id: self.id_gen.next_id(),
})
}
Expand Down Expand Up @@ -461,6 +515,16 @@ where
span: expr.span.with_end_from(token_eq.span),
id: self.id_gen.next_id(),
})
} else if let ExprKind::Get { left: object, name } = expr.kind {
Ok(Expr {
kind: ExprKind::Set {
left: object,
name,
value: Box::new(value),
},
span: expr.span.with_end_from(token_eq.span),
id: self.id_gen.next_id(),
})
} else {
Err(ParseError {
kind: ParseErrorKind::UnexpectedToken {
Expand Down Expand Up @@ -668,19 +732,43 @@ where
let mut expr = self.primary()?;
let expr_span = expr.span;

while let Some(token) = self.peek()
&& matches!(token.kind, TokenKind::LParen)
{
let l_paren = self.advance().unwrap();
let (args, r_paren_span) = self.arguments(l_paren.span)?;
loop {
match self.peek().map(|token| token.kind) {
Some(TokenKind::LParen) => {
let l_paren = self.advance().unwrap();
let (args, r_paren_span) = self.arguments(l_paren.span)?;

expr = Expr {
kind: ExprKind::Call {
callee: Box::new(expr),
args,
},
span: expr_span.with_end_from(r_paren_span),
id: self.id_gen.next_id(),
expr = Expr {
kind: ExprKind::Call {
callee: Box::new(expr),
args,
},
span: expr_span.with_end_from(r_paren_span),
id: self.id_gen.next_id(),
}
}

Some(TokenKind::Dot) => {
let _dot = self.advance().unwrap();
let Token {
kind: TokenKind::Identifier(ident),
span: ident_span,
} = self.consume(&TokenKind::Identifier(""))?
else {
unreachable!();
};

expr = Expr {
kind: ExprKind::Get {
left: Box::new(expr),
name: Identifier { symbol: ident },
},
span: expr_span.with_end_from(ident_span),
id: self.id_gen.next_id(),
}
}

_ => break,
}
}

Expand Down Expand Up @@ -740,6 +828,11 @@ where
span: token.span,
id: self.id_gen.next_id(),
},
Keyword::This => Expr {
kind: ExprKind::This,
span: token.span,
id: self.id_gen.next_id(),
},
_ => {
return Err(ParseError {
kind: ParseErrorKind::UnexpectedToken {
Expand Down
2 changes: 2 additions & 0 deletions rikulox-resolve/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ pub enum ResolveErrorKind {
UninitializedVariable(String),
VariableAlreadyDeclared(String),
ReturnOutsideFunction,
ThisOutsideClass,
ReturnInInit,
}
Loading