Skip to content

Commit

Permalink
Types in grammar based on PR powdr-labs#1545
Browse files Browse the repository at this point in the history
  • Loading branch information
gzanitti committed Jul 10, 2024
1 parent 7817c69 commit c41a89c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ast/src/parsed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl<R> Children<Expression<R>> for EnumVariant<Expression<R>> {
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TraitImplementation<Expr> {
pub name: SymbolPath,
pub type_scheme: TraitScheme<Expr>,
pub type_scheme: TraitScheme,
pub functions: Vec<NamedExpression<Expr>>,
}

Expand Down
4 changes: 2 additions & 2 deletions ast/src/parsed/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ impl From<Type> for TypeScheme {
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TraitScheme<E = u64> {
pub struct TraitScheme {
pub vars: TypeBounds,
pub types: Vec<Type<E>>,
pub types: Vec<Type>,
}

#[derive(
Expand Down
2 changes: 1 addition & 1 deletion parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl ParserContext {
lazy_static::lazy_static! {
static ref PIL_FILE_PARSER: powdr::PILFileParser = powdr::PILFileParser::new();
static ref ASM_MODULE_PARSER: powdr::ASMModuleParser = powdr::ASMModuleParser::new();
static ref TYPE_PARSER: powdr::TypeParser = powdr::TypeParser::new();
static ref TYPE_PARSER: powdr::TypeExprParser = powdr::TypeExprParser::new();
static ref TYPE_VAR_BOUNDS_PARSER: powdr::TypeVarBoundsParser = powdr::TypeVarBoundsParser::new();
}

Expand Down
48 changes: 30 additions & 18 deletions parser/src/powdr.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Part: Part = {
// Same as SymbolPath plus we allow "::<...>" at the end.
GenericSymbolPath: (SymbolPath, Option<Vec<Type<Expression>>>) = {
// If we "inline" SymbolPath here, we get an ambiguity error.
<abs:"::"?> <parts:( <Part> "::" )*> <end:Part> <types:("::" "<" <TypeTermList> ">")?> => (
<abs:"::"?> <parts:( <Part> "::" )*> <end:Part> <types:("::" "<" <TypeTermList<Expression>> ">")?> => (
SymbolPath::from_parts([
abs.map(|_| vec![Part::Named(String::new())]).unwrap_or_default(),
parts,
Expand Down Expand Up @@ -253,7 +253,7 @@ ConstantFixed = {

GenericTypedName: (String, Option<TypeScheme<Expression>>) = {
<name:Identifier> => (name, None),
<vars:("<" <TypeVarBounds> ">")?> <name:Identifier> <ty:(":" <Type>)> =>
<vars:("<" <TypeVarBounds> ">")?> <name:Identifier> <ty:(":" <Type<Expression>>)> =>
(name, Some(TypeScheme{ vars: vars.unwrap_or_default(), ty }))
}

Expand Down Expand Up @@ -683,8 +683,8 @@ PatternList: Vec<Pattern> = {

// ---------------------------- Trait/Impl Declaration -----------------------------

GenericTraitName: (SymbolPath, TraitScheme<Expression>) = {
<vars:("<" <TypeVarBounds> ">")> <name:SymbolPath> <types:("<" <TypeTermList> ">")> =>
GenericTraitName: (SymbolPath, TraitScheme) = {
<vars:("<" <TypeVarBounds> ">")> <name:SymbolPath> <types:("<" <TypeTermList<ArrayLengthNumber>> ">")> =>
(name, TraitScheme{ vars, types })
}

Expand Down Expand Up @@ -715,7 +715,7 @@ TraitFunctions: Vec<TraitFunction<Expression>> = {
}

TraitFunction: TraitFunction<Expression> = {
<name:Identifier> ":" <params:TypeTermList> "->" <value:TypeTermBox> => TraitFunction { name, ty: Type::Function(FunctionType{params, value}) }
<name:Identifier> ":" <params:TypeTermList<Expression>> "->" <value:TypeTermBox<Expression>> => TraitFunction { name, ty: Type::Function(FunctionType{params, value}) }
}


Expand All @@ -732,40 +732,51 @@ EnumVariants: Vec<EnumVariant<Expression>> = {
}

EnumVariant: EnumVariant<Expression> = {
<name:Identifier> <fields:("(" <TypeTermList> ")")?> => EnumVariant{<>}
<name:Identifier> <fields:("(" <TypeTermList<Expression>> ")")?> => EnumVariant{<>}
}

// ---------------------------- Type Names -----------------------------

pub Type: Type<Expression> = {
<params:TypeTermList> "->" <value:TypeTermBox> => Type::Function(FunctionType{<>}),
TypeTerm
pub TypeExpr = Type<Expression>;
pub TypeNumber = Type<Number>;

Type<ArrayLength>: Type<ArrayLength> = {
<params:TypeTermList<ArrayLength>> "->" <value:TypeTermBox<ArrayLength>> => Type::Function(FunctionType{<>}),
TypeTerm<ArrayLength>
}

TypeTermList: Vec<Type<Expression>> = {
TypeTermList<ArrayLength>: Vec<Type<ArrayLength>> = {
=> vec![],
<mut list:( <TypeTerm> "," )*> <end:TypeTerm> => { list.push(end); list }
<mut list:( <TypeTerm<ArrayLength>> "," )*> <end:TypeTerm<ArrayLength>> => { list.push(end); list }
}

TypeTermBox: Box<Type<Expression>> = {
TypeTerm => Box::new(<>)
TypeTermBox<ArrayLength>: Box<Type<ArrayLength>> = {
TypeTerm<ArrayLength> => Box::new(<>)
}

TypeTerm: Type<Expression> = {
TypeTerm<ArrayLength>: Type<ArrayLength> = {
// The parser parses all identifiers as NamedTypes, some are translated
// to TypeVars later.
TypeSymbolPath ("<" <TypeTermList> ">")? => Type::NamedType(<>),
TypeSymbolPath ("<" <TypeTermList<ArrayLength>> ">")? => Type::NamedType(<>),
"!" => Type::Bottom,
"bool" => Type::Bool,
"int" => Type::Int,
"fe" => Type::Fe,
"string" => Type::String,
"col" => Type::Col,
"expr" => Type::Expr,
<base:TypeTerm> "[" <length:Expression?> "]" => Type::Array(ArrayType{base: Box::new(base), length}),
"(" <mut items:( <TypeTerm> "," )+> <end:TypeTerm> ")" => { items.push(end); Type::Tuple(TupleType{items}) },
<base:TypeTerm<ArrayLength>> "[" <length:ArrayLength?> "]" => Type::Array(ArrayType{base: Box::new(base), length}),
"(" <mut items:( <TypeTerm<ArrayLength>> "," )+> <end:TypeTerm<ArrayLength>> ")" => { items.push(end); Type::Tuple(TupleType{items}) },
"(" ")" => Type::Tuple(TupleType{items: vec![]}),
"(" <Type> ")",
"(" <Type<ArrayLength>> ")",
}

ArrayLengthExpr: Expression = {
Expression
}

ArrayLengthNumber: u64 = {
Number => u64::try_from(<>).unwrap()
}

TypeVar: String = {
Expand All @@ -789,6 +800,7 @@ TypeBoundsList: BTreeSet<String> = {
}



// ---------------------------- Terminals -----------------------------


Expand Down

0 comments on commit c41a89c

Please sign in to comment.