Skip to content

Commit b7ce141

Browse files
author
Niko Matsakis
committed
introduce minirust
1 parent 3aafbd1 commit b7ce141

File tree

3 files changed

+123
-163
lines changed

3 files changed

+123
-163
lines changed

crates/formality-rust/src/grammar.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ use formality_types::{
99
},
1010
rust::Term,
1111
};
12+
use minirust::Body;
1213

13-
use crate::grammar::mir::MirFnBody;
14-
15-
pub mod mir;
14+
pub mod minirust;
1615

1716
#[term($crates)]
1817
pub struct Program {
@@ -237,8 +236,8 @@ pub enum FnBody {
237236
TrustedFnBody,
238237

239238
#[cast]
240-
#[grammar(= $v0 ;)]
241-
MirFnBody(MirFnBody),
239+
#[grammar(= $v0;)]
240+
MiniRust(minirust::Body),
242241
}
243242

244243
#[term(type $id $binder ;)]
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use std::sync::Arc;
2+
3+
use formality_core::{id, Downcast, DowncastFrom, Upcast};
4+
use formality_macros::term;
5+
use formality_types::grammar::{FieldId, Ty};
6+
7+
// This definition is based on [MiniRust](https://github.com/minirust/minirust/blob/master/spec/lang/syntax.md).
8+
9+
id!(BbId);
10+
id!(LocalId);
11+
12+
/// Based on [MiniRust statements](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators).
13+
#[term(minirust($,args) -> $ret {
14+
$*locals
15+
$*blocks
16+
})]
17+
pub struct Body {
18+
pub args: Vec<LocalId>,
19+
pub ret: LocalId,
20+
pub locals: Vec<LocalDecl>,
21+
pub blocks: Vec<BasicBlock>,
22+
}
23+
24+
/// Based on [MiniRust statements](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators).
25+
#[term(let $id: $ty;)]
26+
pub struct LocalDecl {
27+
pub id: LocalId,
28+
pub ty: Ty,
29+
}
30+
31+
/// Based on [MiniRust statements](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators).
32+
#[term($id: ${statements} $terminator;)]
33+
pub struct BasicBlock {
34+
pub id: BbId,
35+
pub statements: Vec<Statement>,
36+
pub terminator: Terminator,
37+
}
38+
39+
/// Based on [MiniRust statements](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators).
40+
#[term]
41+
pub enum Statement {
42+
#[grammar($v0 = $v1;)]
43+
Assign(PlaceExpression, ValueExpression),
44+
45+
#[grammar($v0;)]
46+
PlaceMention(PlaceExpression),
47+
48+
// SetDiscriminant
49+
// Validate
50+
// Deinit
51+
// StorageLive
52+
// StorageDead
53+
}
54+
55+
/// Based on [MiniRust terminators](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators).
56+
#[term]
57+
pub enum Terminator {
58+
#[grammar(goto $v0)]
59+
Goto(BbId),
60+
61+
// Switch
62+
// Unreachable
63+
// Intrinsic
64+
65+
#[grammar(call $?cc $callee$(arguments) -> $ret $:goto $next_block)]
66+
Call {
67+
callee: ValueExpression,
68+
cc: CallingConvention,
69+
arguments: Vec<ArgumentExpression>,
70+
ret: PlaceExpression,
71+
next_block: Option<BbId>,
72+
},
73+
74+
#[grammar(return)]
75+
Return,
76+
}
77+
78+
#[term]
79+
#[derive(Default)]
80+
pub enum CallingConvention {
81+
#[default]
82+
#[grammar((Rust))]
83+
Rust,
84+
85+
#[grammar((C))]
86+
C,
87+
}
88+
89+
#[term]
90+
pub enum ArgumentExpression {
91+
ByValue(ValueExpression),
92+
InPlace(PlaceExpression),
93+
}
94+
95+
#[term]
96+
pub enum ValueExpression {
97+
// Const
98+
#[grammar($(v0) as $v1)]
99+
Tuple(Vec<ValueExpression>, Ty),
100+
// Union
101+
// Variant
102+
// GetDiscriminant
103+
#[grammar(load($v0))]
104+
Load(PlaceExpression),
105+
// AddrOf
106+
// UnOp
107+
// BinOp
108+
}
109+
110+
#[term]
111+
pub enum PlaceExpression {
112+
Local(LocalId),
113+
Deref(Arc<ValueExpression>),
114+
Field(Arc<PlaceExpression>, FieldId),
115+
// Index
116+
// Downcast
117+
}
118+
119+

crates/formality-rust/src/grammar/mir.rs

Lines changed: 0 additions & 158 deletions
This file was deleted.

0 commit comments

Comments
 (0)