Skip to content

Commit

Permalink
refactor: remove generics (#5119)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen authored Jan 27, 2025
1 parent 5dbb506 commit 08218c7
Show file tree
Hide file tree
Showing 19 changed files with 14 additions and 257 deletions.
17 changes: 3 additions & 14 deletions prqlc/prqlc-parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,34 +480,24 @@ where
.then(type_expr().delimited_by(ctrl('<'), ctrl('>')).or_not())
.then(ctrl(':').ignore_then(expr.clone().map(Box::new)).or_not());

let generic_args = ident_part()
.then_ignore(ctrl(':'))
.then(type_expr().separated_by(ctrl('|')))
.map(|(name, domain)| GenericTypeParam { name, domain })
.separated_by(ctrl(','))
.at_least(1)
.delimited_by(ctrl('<'), ctrl('>'))
.or_not()
.map(|x| x.unwrap_or_default());

choice((
// func
keyword("func").ignore_then(generic_args).then(
keyword("func").ignore_then(
param
.clone()
.separated_by(new_line().repeated())
.allow_leading()
.allow_trailing(),
),
// plain
param.repeated().map(|params| (Vec::new(), params)),
param.repeated(),
))
.then_ignore(just(TokenKind::ArrowThin))
// return type
.then(type_expr().delimited_by(ctrl('<'), ctrl('>')).or_not())
// body
.then(func_call(expr))
.map(|(((generic_type_params, params), return_ty), body)| {
.map(|((params, return_ty), body)| {
let (pos, name) = params
.into_iter()
.map(|((name, ty), default_value)| FuncParam {
Expand All @@ -523,7 +513,6 @@ where

body: Box::new(body),
return_ty,
generic_type_params,
})
})
.map(ExprKind::Func)
Expand Down
3 changes: 0 additions & 3 deletions prqlc/prqlc-parser/src/parser/pr/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ pub struct Func {

/// Named function parameters.
pub named_params: Vec<FuncParam>,

/// Generic type arguments within this function.
pub generic_type_params: Vec<GenericTypeParam>,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
Expand Down
6 changes: 0 additions & 6 deletions prqlc/prqlc-parser/src/parser/pr/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ pub enum TyKind {

/// Type of functions with defined params and return types.
Function(Option<TyFunc>),

/// Type that is the largest subtype of `base` while not a subtype of `exclude`.
Difference { base: Box<Ty>, exclude: Box<Ty> },

/// A generic argument. Contains id of the function call node and generic type param name.
GenericArg((usize, String)),
}

impl TyKind {
Expand Down
3 changes: 0 additions & 3 deletions prqlc/prqlc-parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,6 @@ mod tests {
name: ~
default_value: ~
named_params: []
generic_type_params: []
span: "0:21-57"
span: "0:0-57"
"#);
Expand Down Expand Up @@ -584,7 +583,6 @@ mod tests {
name: ~
default_value: ~
named_params: []
generic_type_params: []
span: "0:41-77"
span: "0:20-77"
- VarDef:
Expand All @@ -606,7 +604,6 @@ mod tests {
name: ~
default_value: ~
named_params: []
generic_type_params: []
span: "0:98-133"
span: "0:77-133"
span: "0:0-139"
Expand Down
11 changes: 0 additions & 11 deletions prqlc/prqlc-parser/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ fn test_function() {
- name: x
default_value: ~
named_params: []
generic_type_params: []
span: "0:15-26"
span: "0:0-26"
"#);
Expand All @@ -407,7 +406,6 @@ fn test_function() {
- name: x
default_value: ~
named_params: []
generic_type_params: []
span: "0:15-22"
span: "0:0-22"
"#);
Expand All @@ -434,7 +432,6 @@ fn test_function() {
- name: x
default_value: ~
named_params: []
generic_type_params: []
span: "0:15-28"
span: "0:0-28"
"#);
Expand All @@ -461,7 +458,6 @@ fn test_function() {
- name: x
default_value: ~
named_params: []
generic_type_params: []
span: "0:15-26"
span: "0:0-26"
"#);
Expand Down Expand Up @@ -510,7 +506,6 @@ fn test_function() {
- name: x
default_value: ~
named_params: []
generic_type_params: []
span: "0:10-51"
span: "0:0-51"
"#);
Expand All @@ -530,7 +525,6 @@ fn test_function() {
- name: return_constant
default_value: ~
named_params: []
generic_type_params: []
span: "0:0-27"
span: "0:0-27"
"#);
Expand All @@ -557,7 +551,6 @@ fn test_function() {
- name: X
default_value: ~
named_params: []
generic_type_params: []
span: "0:12-28"
span: "0:0-28"
"#);
Expand Down Expand Up @@ -620,7 +613,6 @@ fn test_function() {
- name: x
default_value: ~
named_params: []
generic_type_params: []
span: "0:27-147"
span: "0:0-147"
"#);
Expand Down Expand Up @@ -650,7 +642,6 @@ fn test_function() {
default_value:
Ident: a
span: "0:15-16"
generic_type_params: []
span: "0:10-27"
span: "0:0-27"
"#);
Expand Down Expand Up @@ -842,7 +833,6 @@ fn test_inline_pipeline() {
- name: x
default_value: ~
named_params: []
generic_type_params: []
span: "0:13-37"
span: "0:0-37"
"#);
Expand Down Expand Up @@ -1599,7 +1589,6 @@ fn test_annotation() {
- name: b
default_value: ~
named_params: []
generic_type_params: []
span: "0:49-61"
span: "0:0-61"
annotations:
Expand Down
17 changes: 0 additions & 17 deletions prqlc/prqlc/src/cli/docs_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,6 @@ pub fn generate_html_docs(stmts: Vec<Stmt>) -> String {
if let Some(expr) = &var_def.value {
match &expr.kind {
ExprKind::Func(func) => {
if !func.generic_type_params.is_empty() {
docs.push_str(" <h4 class=\"h6\">Type parameters</h4>\n");
docs.push_str(" <ul>\n");
for param in &func.generic_type_params {
docs.push_str(&format!(" <li><var>{}</var></li>\n", param.name));
}
docs.push_str(" </ul>\n");
}

if !func.params.is_empty() {
docs.push_str(" <h4 class=\"h6\">Parameters</h4>\n");
docs.push_str(" <ul>\n");
Expand Down Expand Up @@ -284,14 +275,6 @@ Generated with [prqlc](https://prql-lang.org/) {}.
if let Some(expr) = &var_def.value {
match &expr.kind {
ExprKind::Func(func) => {
if !func.generic_type_params.is_empty() {
docs.push_str("#### Type Parameters\n");
for param in &func.generic_type_params {
docs.push_str(&format!("* *{}*\n", param.name));
}
docs.push('\n');
}

if !func.params.is_empty() {
docs.push_str("#### Parameters\n");
for param in &func.params {
Expand Down
16 changes: 0 additions & 16 deletions prqlc/prqlc/src/codegen/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,6 @@ impl WriteSource for pr::ExprKind {
}
Func(c) => {
let mut r = "func ".to_string();
if !c.generic_type_params.is_empty() {
r += opt.consume("<")?;
for generic_param in &c.generic_type_params {
r += opt.consume(&write_ident_part(&generic_param.name))?;
r += opt.consume(": ")?;
r += &opt.consume(
SeparatedExprs {
exprs: &generic_param.domain,
inline: " | ",
line_end: "|",
}
.write(opt.clone())?,
)?;
}
r += opt.consume("> ")?;
}

for param in &c.params {
r += opt.consume(&write_ident_part(&param.name))?;
Expand Down
6 changes: 0 additions & 6 deletions prqlc/prqlc/src/codegen/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ impl WriteSource for pr::TyKind {
r += &func.return_ty.as_deref().write(opt)?;
Some(r)
}
Difference { base, exclude } => {
let base = base.write(opt.clone())?;
let exclude = exclude.write(opt.clone())?;
Some(format!("{base} - {exclude}"))
}
GenericArg(_) => Some("?".to_string()),
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions prqlc/prqlc/src/ir/pl/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};

use super::{Lineage, TransformCall};
use crate::codegen::write_ty;
use crate::pr::{GenericTypeParam, Ident, Literal, Span, Ty};
use crate::pr::{Ident, Literal, Span, Ty};

/// Expr is anything that has a value and thus a type.
/// Most of these can contain other [Expr] themselves; literals should be [ExprKind::Literal].
Expand Down Expand Up @@ -112,9 +112,6 @@ pub struct Func {
/// Named function parameters.
pub named_params: Vec<FuncParam>,

/// Generic type arguments within this function.
pub generic_type_params: Vec<GenericTypeParam>,

/// Arguments that have already been provided.
pub args: Vec<Expr>,

Expand Down
9 changes: 1 addition & 8 deletions prqlc/prqlc/src/ir/pl/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,7 @@ pub fn fold_type<T: ?Sized + PlFold>(fold: &mut T, ty: Ty) -> Result<Ty> {
})
.transpose()?,
),
TyKind::Difference { base, exclude } => TyKind::Difference {
base: Box::new(fold.fold_type(*base)?),
exclude: Box::new(fold.fold_type(*exclude)?),
},
TyKind::Ident(_)
| TyKind::Primitive(_)
| TyKind::Singleton(_)
| TyKind::GenericArg(_) => ty.kind,
TyKind::Ident(_) | TyKind::Primitive(_) | TyKind::Singleton(_) => ty.kind,
},
span: ty.span,
name: ty.name,
Expand Down
2 changes: 0 additions & 2 deletions prqlc/prqlc/src/semantic/ast_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pub fn expand_expr(expr: pr::Expr) -> Result<pl::Expr> {
name_hint: None,
args: Vec::new(),
env: HashMap::new(),
generic_type_params: v.generic_type_params,
}
.into(),
),
Expand Down Expand Up @@ -347,7 +346,6 @@ fn restrict_expr_kind(value: pl::ExprKind) -> pr::ExprKind {
body: restrict_expr_box(v.body),
params: restrict_func_params(v.params),
named_params: restrict_func_params(v.named_params),
generic_type_params: v.generic_type_params,
}
.into(),
);
Expand Down
1 change: 0 additions & 1 deletion prqlc/prqlc/src/semantic/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ fn new_func(name: &str, params: &[&str]) -> Expr {
named_params: Default::default(),
args: Default::default(),
env: Default::default(),
generic_type_params: Default::default(),
}));
Expr {
alias: Some(name.to_string()),
Expand Down
6 changes: 3 additions & 3 deletions prqlc/prqlc/src/semantic/resolver/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl pl::PlFold for Resolver<'_> {

DeclKind::Expr(expr) => match &expr.kind {
pl::ExprKind::Func(closure) => {
let closure = self.fold_function_types(closure.clone(), id)?;
let closure = self.fold_function_types(closure.clone())?;

let expr = pl::Expr::new(pl::ExprKind::Func(closure));

Expand Down Expand Up @@ -173,10 +173,10 @@ impl pl::PlFold for Resolver<'_> {

// fold function
let func = self.apply_args_to_closure(func, args, named_args)?;
self.fold_function(func, id, *span)?
self.fold_function(func, *span)?
}

pl::ExprKind::Func(closure) => self.fold_function(closure, id, *span)?,
pl::ExprKind::Func(closure) => self.fold_function(closure, *span)?,

pl::ExprKind::Tuple(exprs) => {
let exprs = self.fold_exprs(exprs)?;
Expand Down
Loading

0 comments on commit 08218c7

Please sign in to comment.