Skip to content

Commit

Permalink
Add raise
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Apr 16, 2024
1 parent f97a52d commit 74e76d1
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 7 deletions.
11 changes: 10 additions & 1 deletion crates/rue-compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a> Codegen<'a> {
Lir::FunctionBody(body) => self.gen_quote(body),
Lir::First(value) => self.gen_first(value),
Lir::Rest(value) => self.gen_rest(value),
Lir::Raise => self.list(&[self.ops.x]),
Lir::Raise(value) => self.gen_raise(value),
Lir::Sha256(value) => self.gen_sha256(value),
Lir::IsCons(value) => self.gen_is_cons(value),
Lir::Strlen(value) => self.gen_strlen(value),
Expand Down Expand Up @@ -137,6 +137,15 @@ impl<'a> Codegen<'a> {
self.list(&[self.ops.r, value])
}

fn gen_raise(&mut self, value: Option<LirId>) -> NodePtr {
if let Some(value) = value {
let value = self.gen_lir(value);
self.list(&[self.ops.x, value])
} else {
self.list(&[self.ops.x])
}
}

fn gen_sha256(&mut self, value: LirId) -> NodePtr {
let value = self.gen_lir(value);
self.list(&[self.ops.sha256, value])
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum Hir {
First(HirId),
Rest(HirId),
Not(HirId),
Raise,
Raise(Option<HirId>),
Sha256(HirId),
IsCons(HirId),
Strlen(HirId),
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/src/lir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum Lir {
FunctionBody(LirId),
First(LirId),
Rest(LirId),
Raise,
Raise(Option<LirId>),
Sha256(LirId),
IsCons(LirId),
Strlen(LirId),
Expand Down
11 changes: 10 additions & 1 deletion crates/rue-compiler/src/lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,15 @@ impl<'a> Lowerer<'a> {
.unwrap_or_else(|| self.unknown());
statements.push(Statement::Return(value));
}
Stmt::RaiseStmt(raise_stmt) => {
let value = raise_stmt
.expr()
.map(|expr| self.compile_expr(expr, Some(self.bytes_type)).hir());

let value = self.db.alloc_hir(Hir::Raise(value));

statements.push(Statement::Return(Value::typed(value, self.unknown_type)));
}
Stmt::AssertStmt(assert_stmt) => {
let condition = assert_stmt
.expr()
Expand All @@ -519,7 +528,7 @@ impl<'a> Lowerer<'a> {
);

let not_condition = self.db.alloc_hir(Hir::Not(condition.hir()));
let raise = self.db.alloc_hir(Hir::Raise);
let raise = self.db.alloc_hir(Hir::Raise(None));

statements.push(Statement::If(not_condition, raise))
}
Expand Down
14 changes: 12 additions & 2 deletions crates/rue-compiler/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a> Optimizer<'a> {
fn compute_captures_hir(&mut self, scope_id: ScopeId, hir_id: HirId) {
match self.db.hir(hir_id).clone() {
Hir::Unknown => unreachable!(),
Hir::Atom(_) | Hir::Raise => {}
Hir::Atom(_) => {}
Hir::Reference(symbol_id) => self.compute_reference_captures(scope_id, symbol_id),
Hir::Scope {
scope_id: new_scope_id,
Expand All @@ -53,6 +53,11 @@ impl<'a> Optimizer<'a> {
self.compute_captures_hir(scope_id, lhs);
self.compute_captures_hir(scope_id, rhs);
}
Hir::Raise(value) => {
if let Some(value) = value {
self.compute_captures_hir(scope_id, value);
}
}
Hir::First(value)
| Hir::Rest(value)
| Hir::Not(value)
Expand Down Expand Up @@ -317,7 +322,7 @@ impl<'a> Optimizer<'a> {
Hir::First(value) => self.opt_first(scope_id, *value),
Hir::Rest(value) => self.opt_rest(scope_id, *value),
Hir::Not(value) => self.opt_not(scope_id, *value),
Hir::Raise => self.db.alloc_lir(Lir::Raise),
Hir::Raise(value) => self.opt_raise(scope_id, *value),
Hir::Sha256(value) => self.opt_sha256(scope_id, *value),
Hir::IsCons(value) => self.opt_is_cons(scope_id, *value),
Hir::Strlen(value) => self.opt_strlen(scope_id, *value),
Expand Down Expand Up @@ -487,6 +492,11 @@ impl<'a> Optimizer<'a> {
self.db.alloc_lir(Lir::Not(value))
}

fn opt_raise(&mut self, scope_id: ScopeId, value: Option<HirId>) -> LirId {
let value = value.map(|value| self.opt_hir(scope_id, value));
self.db.alloc_lir(Lir::Raise(value))
}

fn opt_if(
&mut self,
scope_id: ScopeId,
Expand Down
3 changes: 3 additions & 0 deletions crates/rue-lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl<'a> Lexer<'a> {
"if" => TokenKind::If,
"else" => TokenKind::Else,
"return" => TokenKind::Return,
"raise" => TokenKind::Raise,
"assert" => TokenKind::Assert,
"nil" => TokenKind::Nil,
"true" => TokenKind::True,
Expand Down Expand Up @@ -278,6 +279,8 @@ mod tests {
check("if", &[TokenKind::If]);
check("else", &[TokenKind::Else]);
check("return", &[TokenKind::Return]);
check("raise", &[TokenKind::Raise]);
check("assert", &[TokenKind::Assert]);
check("true", &[TokenKind::True]);
check("false", &[TokenKind::False]);
check("nil", &[TokenKind::Nil]);
Expand Down
1 change: 1 addition & 0 deletions crates/rue-lexer/src/token_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum TokenKind {
If,
Else,
Return,
Raise,
Assert,
Nil,
True,
Expand Down
9 changes: 8 additions & 1 deletion crates/rue-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ ast_node!(PairType);
ast_node!(FunctionType);
ast_node!(FunctionTypeParam);

ast_enum!(Stmt, LetStmt, IfStmt, ReturnStmt, AssertStmt);
ast_enum!(Stmt, LetStmt, IfStmt, ReturnStmt, RaiseStmt, AssertStmt);
ast_node!(LetStmt);
ast_node!(IfStmt);
ast_node!(ReturnStmt);
ast_node!(RaiseStmt);
ast_node!(AssertStmt);

impl Root {
Expand Down Expand Up @@ -336,6 +337,12 @@ impl ReturnStmt {
}
}

impl RaiseStmt {
pub fn expr(&self) -> Option<Expr> {
self.syntax().children().find_map(Expr::cast)
}
}

impl AssertStmt {
pub fn expr(&self) -> Option<Expr> {
self.syntax().children().find_map(Expr::cast)
Expand Down
12 changes: 12 additions & 0 deletions crates/rue-parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ fn block(p: &mut Parser) {
let_stmt(p);
} else if p.at(SyntaxKind::Return) {
return_stmt(p);
} else if p.at(SyntaxKind::Raise) {
raise_stmt(p);
} else if p.at(SyntaxKind::If) {
if if_stmt_maybe_else(p, false) {
break;
Expand Down Expand Up @@ -196,6 +198,16 @@ fn return_stmt(p: &mut Parser) {
p.finish();
}

fn raise_stmt(p: &mut Parser) {
p.start(SyntaxKind::RaiseStmt);
p.expect(SyntaxKind::Raise);
if !p.try_eat(SyntaxKind::Semicolon) {
expr(p);
p.expect(SyntaxKind::Semicolon);
}
p.finish();
}

fn assert_stmt(p: &mut Parser) {
p.start(SyntaxKind::AssertStmt);
p.expect(SyntaxKind::Assert);
Expand Down
1 change: 1 addition & 0 deletions crates/rue-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ fn convert_tokens<'a>(
TokenKind::If => SyntaxKind::If,
TokenKind::Else => SyntaxKind::Else,
TokenKind::Return => SyntaxKind::Return,
TokenKind::Raise => SyntaxKind::Raise,
TokenKind::Assert => SyntaxKind::Assert,
TokenKind::Nil => SyntaxKind::Nil,
TokenKind::True => SyntaxKind::True,
Expand Down
4 changes: 4 additions & 0 deletions crates/rue-parser/src/syntax_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum SyntaxKind {
If,
Else,
Return,
Raise,
Assert,
Nil,
True,
Expand Down Expand Up @@ -77,6 +78,7 @@ pub enum SyntaxKind {
LetStmt,
IfStmt,
ReturnStmt,
RaiseStmt,
AssertStmt,

Block,
Expand Down Expand Up @@ -137,6 +139,7 @@ impl fmt::Display for SyntaxKind {
SyntaxKind::If => "'if'",
SyntaxKind::Else => "'else'",
SyntaxKind::Return => "'return'",
SyntaxKind::Raise => "'raise'",
SyntaxKind::Assert => "'assert'",
SyntaxKind::Nil => "'nil'",
SyntaxKind::True => "'true'",
Expand Down Expand Up @@ -184,6 +187,7 @@ impl fmt::Display for SyntaxKind {
SyntaxKind::LetStmt => "let statement",
SyntaxKind::IfStmt => "if statement",
SyntaxKind::ReturnStmt => "return statement",
SyntaxKind::RaiseStmt => "raise statement",
SyntaxKind::AssertStmt => "assert statement",

SyntaxKind::Block => "block",
Expand Down

0 comments on commit 74e76d1

Please sign in to comment.