Skip to content

Commit fa96b12

Browse files
committed
wip generics
1 parent ef43ddb commit fa96b12

File tree

7 files changed

+102
-7
lines changed

7 files changed

+102
-7
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn pair<T>(x: T) -> [T] {
2+
cons(x, cons(x, []))
3+
}
4+
5+
cell top() {
6+
let float_pair = pair(800.);
7+
let r = rect("met1", x0=0., y0=200., x1=head(float_pair), y1=head(tail(float_pair)));
8+
}

core/compiler/src/argon.y

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,25 @@ CellDecl -> Result<CellDecl<&'input str, ParseMetadata>, ()>
170170
;
171171

172172
FnDecl -> Result<FnDecl<&'input str, ParseMetadata>, ()>
173-
: 'FN' Ident '(' ArgDecls ')' '->' TySpec Scope
173+
: 'FN' Ident BracketedGenericDecls '(' ArgDecls ')' '->' TySpec Scope
174174
{
175175
Ok(FnDecl {
176176
name: $2?,
177-
args: $4?,
178-
scope: $8?,
179-
return_ty: Some($7?),
177+
generics: $3?,
178+
args: $5?,
179+
scope: $9?,
180+
return_ty: Some($8?),
180181
span: $span,
181182
metadata: (),
182183
})
183184
}
184-
| 'FN' Ident '(' ArgDecls ')' Scope
185+
| 'FN' Ident BracketedGenericDecls '(' ArgDecls ')' Scope
185186
{
186187
Ok(FnDecl {
187188
name: $2?,
188-
args: $4?,
189-
scope: $6?,
189+
generics: $3?,
190+
args: $5?,
191+
scope: $7?,
190192
return_ty: None,
191193
span: $span,
192194
metadata: (),
@@ -396,6 +398,26 @@ ArgDecl -> Result<ArgDecl<&'input str, ParseMetadata>, ()>
396398
: Ident ':' TySpec { Ok(ArgDecl { name: $1?, ty: $3?, metadata: () }) }
397399
;
398400
401+
BracketedGenericDecls -> Result<Vec<GenericDecl<&'input str, ParseMetadata>>, ()>
402+
: { Ok(Vec::new()) }
403+
| '<' GenericDecls '>' { $2 }
404+
;
405+
406+
GenericDecls -> Result<Vec<GenericDecl<&'input str, ParseMetadata>>, ()>
407+
: { Ok(Vec::new()) }
408+
| GenericDecls1 { $1 }
409+
| GenericDecls1 ',' { $1 }
410+
;
411+
412+
GenericDecls1 -> Result<Vec<GenericDecl<&'input str, ParseMetadata>>, ()>
413+
: GenericDecls1 ',' GenericDecl { flatten($1, $3) }
414+
| GenericDecl { Ok(vec![$1?]) }
415+
;
416+
417+
GenericDecl -> Result<GenericDecl<&'input str, ParseMetadata>, ()>
418+
: Ident { Ok(GenericDecl { name: $1?, metadata: () }) }
419+
;
420+
399421
TySpec -> Result<TySpec<&'input str, ParseMetadata>, ()>
400422
: Ident { Ok(TySpec { kind: TySpecKind::Ident($1?), span: $span, }) }
401423
| '[' TySpec ']' { Ok(TySpec { kind: TySpecKind::Seq(Box::new($2?)), span: $span, }) }

core/compiler/src/ast/annotated.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ impl<S, T: AstMetadata> AstTransformer for AstAnnotationPass<S, T> {
244244
input.metadata.clone()
245245
}
246246

247+
fn dispatch_generic_decl(
248+
&mut self,
249+
input: &super::GenericDecl<Self::InputS, Self::InputMetadata>,
250+
_name: &Ident<Self::OutputS, Self::OutputMetadata>,
251+
) -> <Self::OutputMetadata as AstMetadata>::GenericDecl {
252+
input.metadata.clone()
253+
}
254+
247255
fn dispatch_scope(
248256
&mut self,
249257
input: &Scope<Self::InputS, Self::InputMetadata>,

core/compiler/src/ast/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub struct TySpec<S, T: AstMetadata> {
135135
#[derive_where(Debug, Clone, Serialize, Deserialize; S)]
136136
pub struct FnDecl<S, T: AstMetadata> {
137137
pub name: Ident<S, T>,
138+
pub generics: Vec<GenericDecl<S, T>>,
138139
pub args: Vec<ArgDecl<S, T>>,
139140
pub return_ty: Option<TySpec<S, T>>,
140141
pub scope: Scope<S, T>,
@@ -316,6 +317,12 @@ pub struct ArgDecl<S, T: AstMetadata> {
316317
pub metadata: T::ArgDecl,
317318
}
318319

320+
#[derive_where(Debug, Clone, Serialize, Deserialize; S)]
321+
pub struct GenericDecl<S, T: AstMetadata> {
322+
pub name: Ident<S, T>,
323+
pub metadata: T::GenericDecl,
324+
}
325+
319326
#[derive_where(Debug, Clone, Serialize, Deserialize; S)]
320327
pub struct CastExpr<S, T: AstMetadata> {
321328
pub value: Expr<S, T>,
@@ -383,6 +390,7 @@ pub trait AstMetadata {
383390
type Args: Debug + Clone + Serialize + DeserializeOwned;
384391
type KwArgValue: Debug + Clone + Serialize + DeserializeOwned;
385392
type ArgDecl: Debug + Clone + Serialize + DeserializeOwned;
393+
type GenericDecl: Debug + Clone + Serialize + DeserializeOwned;
386394
type Scope: Debug + Clone + Serialize + DeserializeOwned;
387395
type Typ: Debug + Clone + Serialize + DeserializeOwned;
388396
type CastExpr: Debug + Clone + Serialize + DeserializeOwned;
@@ -507,6 +515,11 @@ pub trait AstTransformer {
507515
name: &Ident<Self::OutputS, Self::OutputMetadata>,
508516
ty: &TySpec<Self::OutputS, Self::OutputMetadata>,
509517
) -> <Self::OutputMetadata as AstMetadata>::ArgDecl;
518+
fn dispatch_generic_decl(
519+
&mut self,
520+
input: &GenericDecl<Self::InputS, Self::InputMetadata>,
521+
name: &Ident<Self::OutputS, Self::OutputMetadata>,
522+
) -> <Self::OutputMetadata as AstMetadata>::GenericDecl;
510523
fn dispatch_scope(
511524
&mut self,
512525
input: &Scope<Self::InputS, Self::InputMetadata>,
@@ -608,6 +621,11 @@ pub trait AstTransformer {
608621
input: &FnDecl<Self::InputS, Self::InputMetadata>,
609622
) -> FnDecl<Self::OutputS, Self::OutputMetadata> {
610623
let name = self.transform_ident(&input.name);
624+
let generics = input
625+
.generics
626+
.iter()
627+
.map(|decl| self.transform_generic_decl(decl))
628+
.collect_vec();
611629
let args = input
612630
.args
613631
.iter()
@@ -621,6 +639,7 @@ pub trait AstTransformer {
621639
let metadata = self.dispatch_fn_decl(input, &name, &args, &return_ty, &scope);
622640
FnDecl {
623641
name,
642+
generics,
624643
args,
625644
return_ty,
626645
scope,
@@ -859,6 +878,15 @@ pub trait AstTransformer {
859878
ArgDecl { name, ty, metadata }
860879
}
861880

881+
fn transform_generic_decl(
882+
&mut self,
883+
input: &GenericDecl<Self::InputS, Self::InputMetadata>,
884+
) -> GenericDecl<Self::OutputS, Self::OutputMetadata> {
885+
let name = self.transform_ident(&input.name);
886+
let metadata = self.dispatch_generic_decl(input, &name);
887+
GenericDecl { name, metadata }
888+
}
889+
862890
fn transform_scope(
863891
&mut self,
864892
input: &Scope<Self::InputS, Self::InputMetadata>,

core/compiler/src/compile.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ impl<'a> AstTransformer for ImportPass<'a> {
343343
) -> <Self::OutputMetadata as AstMetadata>::ArgDecl {
344344
}
345345

346+
fn dispatch_generic_decl(
347+
&mut self,
348+
_input: &crate::ast::GenericDecl<Self::InputS, Self::InputMetadata>,
349+
_name: &Ident<Self::OutputS, Self::OutputMetadata>,
350+
) -> <Self::OutputMetadata as AstMetadata>::GenericDecl {
351+
}
352+
346353
fn dispatch_scope(
347354
&mut self,
348355
_input: &Scope<Self::InputS, Self::InputMetadata>,
@@ -570,6 +577,7 @@ impl AstMetadata for VarIdTyMetadata {
570577
type Args = ();
571578
type KwArgValue = Ty;
572579
type ArgDecl = (VarId, Ty);
580+
type GenericDecl = ();
573581
type Scope = Ty;
574582
type Typ = ();
575583
type CastExpr = Ty;
@@ -1028,6 +1036,11 @@ impl<'a> AstTransformer for VarIdTyPass<'a> {
10281036
.scope_annotation
10291037
.as_ref()
10301038
.map(|ident| self.transform_ident(ident));
1039+
let generics = input
1040+
.generics
1041+
.iter()
1042+
.map(|decl| self.transform_generic_decl(decl))
1043+
.collect_vec();
10311044
let args: Vec<_> = input
10321045
.args
10331046
.iter()
@@ -1056,6 +1069,7 @@ impl<'a> AstTransformer for VarIdTyPass<'a> {
10561069
let metadata = self.dispatch_fn_decl(input, &name, &args, &return_ty, &scope);
10571070
FnDecl {
10581071
name,
1072+
generics,
10591073
args,
10601074
return_ty,
10611075
scope,
@@ -1675,6 +1689,13 @@ impl<'a> AstTransformer for VarIdTyPass<'a> {
16751689
(self.alloc(&input.name.name, ty.clone()), ty)
16761690
}
16771691

1692+
fn dispatch_generic_decl(
1693+
&mut self,
1694+
_input: &crate::ast::GenericDecl<Self::InputS, Self::InputMetadata>,
1695+
_name: &Ident<Self::OutputS, Self::OutputMetadata>,
1696+
) -> <Self::OutputMetadata as AstMetadata>::GenericDecl {
1697+
}
1698+
16781699
fn dispatch_scope(
16791700
&mut self,
16801701
_input: &Scope<Substr, Self::InputMetadata>,

core/compiler/src/parse.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl AstMetadata for ParseMetadata {
4545
type Args = ();
4646
type KwArgValue = ();
4747
type ArgDecl = ();
48+
type GenericDecl = ();
4849
type Scope = ();
4950
type Typ = ();
5051
type FnDecl = ();

core/lsp-server/src/import.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ impl<'a> AstTransformer for ScopeAnnotationPass<'a> {
269269
) -> <Self::OutputMetadata as AstMetadata>::ArgDecl {
270270
}
271271

272+
fn dispatch_generic_decl(
273+
&mut self,
274+
_input: &compiler::ast::GenericDecl<Self::InputS, Self::InputMetadata>,
275+
_name: &Ident<Self::OutputS, Self::OutputMetadata>,
276+
) -> <Self::OutputMetadata as AstMetadata>::GenericDecl {
277+
}
278+
272279
fn dispatch_scope(
273280
&mut self,
274281
_input: &Scope<Substr, Self::InputMetadata>,

0 commit comments

Comments
 (0)