Skip to content

Commit 59cf485

Browse files
authored
feat(compiler): add support for scope name annotations (#29)
* feat(compiler): add support for scope name annotations * disallow spaces in # annotation * change scope names to be more natural * fix lint
1 parent 363cb95 commit 59cf485

File tree

7 files changed

+196
-57
lines changed

7 files changed

+196
-57
lines changed

core/compiler/examples/nested_inst.ar

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ cell top() {
2323
let mid_cell = middle();
2424
let left = inst(mid_cell)!;
2525
let right = inst(mid_cell, angle=90)!;
26-
eq(left.x, 0.);
27-
eq(left.y, 0.);
28-
eq(right.x, 300.);
29-
eq(right.y, 0.);
26+
#scope0
27+
{
28+
eq(left.x, 0.);
29+
eq(left.y, 0.);
30+
eq(right.x, 300.);
31+
eq(right.y, 0.);
32+
}
33+
#scope1
34+
if 1 > 2 { } else { }
3035
let met3 = rect("met3", x0=left.bot.met1.x0, y0=20., x1=right.bot.met1.x1, y1=80.)!;
3136
let duplicate_rect = left.bot.met3!;
3237
}

core/compiler/src/argon.l

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ let "LET"
1111
for "FOR"
1212
in "IN"
1313
as "AS"
14+
#[_a-zA-Z][_a-zA-Z0-9]* "ANNOTATION"
1415
[_a-zA-Z][_a-zA-Z0-9]* "IDENT"
1516
[0-9]+\.[0-9]* "FLOATLIT"
1617
[0-9]+ "INTLIT"
1718
"[^"]*" "STRLIT"
1819
//[^\r\n]* ;
20+
\# "#"
1921
\+ "+"
2022
\* "*"
2123
- "-"

core/compiler/src/argon.y

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,18 @@ FnDecl -> Result<FnDecl<'input, ParseMetadata>, ()>
132132
name: $2?,
133133
args: $4?,
134134
scope: $8?,
135-
return_ty: $7?,
135+
return_ty: Some($7?),
136+
span: $span,
137+
metadata: (),
138+
})
139+
}
140+
| 'FN' Ident '(' ArgDecls ')' Scope
141+
{
142+
Ok(FnDecl {
143+
name: $2?,
144+
args: $4?,
145+
scope: $6?,
146+
return_ty: None,
136147
span: $span,
137148
metadata: (),
138149
})
@@ -168,20 +179,26 @@ Statement -> Result<Statement<'input, ParseMetadata>, ()>
168179
}
169180
;
170181

171-
Scope -> Result<Scope<'input, ParseMetadata>, ()>
182+
ScopeAnnotation -> Result<Ident<'input, ParseMetadata>, ()>
183+
: 'ANNOTATION' { Ok(Ident { span: $span, name: &$lexer.span_str($span)[1..], metadata: () }) }
184+
;
185+
186+
UnannotatedScope -> Result<Scope<'input, ParseMetadata>, ()>
172187
: '{' Statements '}'
173188
{
174189
let mut __stmts = $2?;
175190
if let Some(Statement::Expr { value, semicolon }) = __stmts.last().cloned() && !semicolon {
176191
__stmts.pop().unwrap();
177192
return Ok(Scope {
193+
scope_annotation: None,
178194
span: $span,
179195
stmts: __stmts,
180196
tail: Some(value),
181197
metadata: (),
182198
})
183199
}
184200
Ok(Scope {
201+
scope_annotation: None,
185202
span: $span,
186203
stmts: __stmts,
187204
tail: None,
@@ -191,6 +208,7 @@ Scope -> Result<Scope<'input, ParseMetadata>, ()>
191208
| '{' Statements NonBlockExpr '}'
192209
{
193210
Ok(Scope {
211+
scope_annotation: None,
194212
span: $span,
195213
stmts: $2?,
196214
tail: Some($3?),
@@ -199,13 +217,28 @@ Scope -> Result<Scope<'input, ParseMetadata>, ()>
199217
}
200218
;
201219

220+
Scope -> Result<Scope<'input, ParseMetadata>, ()>
221+
: ScopeAnnotation UnannotatedScope
222+
{
223+
Ok(Scope {
224+
scope_annotation: Some($1?),
225+
..$2?
226+
})
227+
}
228+
| UnannotatedScope
229+
{
230+
$1
231+
}
232+
;
233+
202234
Expr -> Result<Expr<'input, ParseMetadata>, ()>
203235
: NonBlockExpr { $1 }
204236
| BlockExpr { $1 }
205237
;
206238

207239
BlockExpr -> Result<Expr<'input, ParseMetadata>, ()>
208-
: 'IF' Expr Scope 'ELSE' Scope { Ok(Expr::If(Box::new(IfExpr { cond: $2?, then: Expr::Scope(Box::new($3?)), else_: Expr::Scope(Box::new($5?)), span: $span, metadata: (), }))) }
240+
: 'IF' Expr Scope 'ELSE' Scope { Ok(Expr::If(Box::new(IfExpr { scope_annotation: None, cond: $2?, then: Expr::Scope(Box::new($3?)), else_: Expr::Scope(Box::new($5?)), span: $span, metadata: (), }))) }
241+
| ScopeAnnotation 'IF' Expr Scope 'ELSE' Scope { Ok(Expr::If(Box::new(IfExpr { scope_annotation: Some($1?), cond: $3?, then: Expr::Scope(Box::new($4?)), else_: Expr::Scope(Box::new($6?)), span: $span, metadata: (), }))) }
209242
| Scope { Ok(Expr::Scope(Box::new($1?))) }
210243
;
211244

core/compiler/src/ast.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub struct CellDecl<'a, T: AstMetadata> {
8080
pub struct FnDecl<'a, T: AstMetadata> {
8181
pub name: Ident<'a, T>,
8282
pub args: Vec<ArgDecl<'a, T>>,
83-
pub return_ty: Ident<'a, T>,
83+
pub return_ty: Option<Ident<'a, T>>,
8484
pub scope: Scope<'a, T>,
8585
pub span: Span,
8686
pub metadata: T::FnDecl,
@@ -96,6 +96,7 @@ pub struct ConstantDecl<'a, T: AstMetadata> {
9696

9797
#[derive_where(Debug, Clone)]
9898
pub struct Scope<'a, T: AstMetadata> {
99+
pub scope_annotation: Option<Ident<'a, T>>,
99100
pub span: Span,
100101
pub stmts: Vec<Statement<'a, T>>,
101102
pub tail: Option<Expr<'a, T>>,
@@ -166,6 +167,7 @@ pub struct VarExpr<'a, T: AstMetadata> {
166167

167168
#[derive_where(Debug, Clone)]
168169
pub struct IfExpr<'a, T: AstMetadata> {
170+
pub scope_annotation: Option<Ident<'a, T>>,
169171
pub cond: Expr<'a, T>,
170172
pub then: Expr<'a, T>,
171173
pub else_: Expr<'a, T>,
@@ -356,7 +358,7 @@ pub trait AstTransformer<'a> {
356358
input: &FnDecl<'a, Self::Input>,
357359
name: &Ident<'a, Self::Output>,
358360
args: &[ArgDecl<'a, Self::Output>],
359-
return_ty: &Ident<'a, Self::Output>,
361+
return_ty: &Option<Ident<'a, Self::Output>>,
360362
scope: &Scope<'a, Self::Output>,
361363
) -> <Self::Output as AstMetadata>::FnDecl;
362364
fn dispatch_constant_decl(
@@ -512,7 +514,10 @@ pub trait AstTransformer<'a> {
512514
.iter()
513515
.map(|arg| self.transform_arg_decl(arg))
514516
.collect_vec();
515-
let return_ty = self.transform_ident(&input.return_ty);
517+
let return_ty = input
518+
.return_ty
519+
.as_ref()
520+
.map(|ident| self.transform_ident(ident));
516521
let scope = self.transform_scope(&input.scope);
517522
let metadata = self.dispatch_fn_decl(input, &name, &args, &return_ty, &scope);
518523
FnDecl {
@@ -566,11 +571,16 @@ pub trait AstTransformer<'a> {
566571
}
567572
}
568573
fn transform_if_expr(&mut self, input: &IfExpr<'a, Self::Input>) -> IfExpr<'a, Self::Output> {
574+
let scope_annotation = input
575+
.scope_annotation
576+
.as_ref()
577+
.map(|ident| self.transform_ident(ident));
569578
let cond = self.transform_expr(&input.cond);
570579
let then = self.transform_expr(&input.then);
571580
let else_ = self.transform_expr(&input.else_);
572581
let metadata = self.dispatch_if_expr(input, &cond, &then, &else_);
573582
IfExpr {
583+
scope_annotation,
574584
span: input.span,
575585
metadata,
576586
cond,
@@ -722,6 +732,10 @@ pub trait AstTransformer<'a> {
722732

723733
fn transform_scope(&mut self, input: &Scope<'a, Self::Input>) -> Scope<'a, Self::Output> {
724734
self.enter_scope(input);
735+
let scope_annotation = input
736+
.scope_annotation
737+
.as_ref()
738+
.map(|ident| self.transform_ident(ident));
725739
let stmts = input
726740
.stmts
727741
.iter()
@@ -730,6 +744,7 @@ pub trait AstTransformer<'a> {
730744
let tail = input.tail.as_ref().map(|stmt| self.transform_expr(stmt));
731745
let metadata = self.dispatch_scope(input, &stmts, &tail);
732746
let output = Scope {
747+
scope_annotation,
733748
span: input.span,
734749
stmts,
735750
tail,

0 commit comments

Comments
 (0)