Introduce 'ArgBy' to allow defining function arg not only by name.

This commit is contained in:
KtorZ 2024-06-07 11:17:16 +02:00
parent 257bd23019
commit 4d42c6cb19
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
21 changed files with 651 additions and 488 deletions

View File

@ -7,6 +7,7 @@ use crate::{
}; };
use indexmap::IndexMap; use indexmap::IndexMap;
use miette::Diagnostic; use miette::Diagnostic;
use ordinal::Ordinal;
use owo_colors::{OwoColorize, Stream::Stdout}; use owo_colors::{OwoColorize, Stream::Stdout};
use std::{ use std::{
fmt::{self, Display}, fmt::{self, Display},
@ -517,17 +518,17 @@ pub struct ModuleConstant<T> {
pub tipo: T, pub tipo: T,
} }
pub type TypedValidator = Validator<Rc<Type>, TypedExpr>; pub type TypedValidator = Validator<Rc<Type>, TypedArg, TypedExpr>;
pub type UntypedValidator = Validator<(), UntypedExpr>; pub type UntypedValidator = Validator<(), UntypedArg, UntypedExpr>;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Validator<T, Expr> { pub struct Validator<T, Arg, Expr> {
pub doc: Option<String>, pub doc: Option<String>,
pub end_position: usize, pub end_position: usize,
pub fun: Function<T, Expr, Arg<T>>, pub fun: Function<T, Expr, Arg>,
pub other_fun: Option<Function<T, Expr, Arg<T>>>, pub other_fun: Option<Function<T, Expr, Arg>>,
pub location: Span, pub location: Span,
pub params: Vec<Arg<T>>, pub params: Vec<Arg>,
} }
impl TypedValidator { impl TypedValidator {
@ -575,12 +576,12 @@ impl TypedValidator {
} }
} }
pub type TypedDefinition = Definition<Rc<Type>, TypedExpr, String>; pub type TypedDefinition = Definition<Rc<Type>, TypedArg, TypedExpr, String>;
pub type UntypedDefinition = Definition<(), UntypedExpr, ()>; pub type UntypedDefinition = Definition<(), UntypedArg, UntypedExpr, ()>;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Definition<T, Expr, PackageName> { pub enum Definition<T, Arg, Expr, PackageName> {
Fn(Function<T, Expr, Arg<T>>), Fn(Function<T, Expr, Arg>),
TypeAlias(TypeAlias<T>), TypeAlias(TypeAlias<T>),
@ -590,12 +591,12 @@ pub enum Definition<T, Expr, PackageName> {
ModuleConstant(ModuleConstant<T>), ModuleConstant(ModuleConstant<T>),
Test(Function<T, Expr, ArgVia<T, Expr>>), Test(Function<T, Expr, ArgVia<Arg, Expr>>),
Validator(Validator<T, Expr>), Validator(Validator<T, Arg, Expr>),
} }
impl<A, B, C> Definition<A, B, C> { impl<A, B, C, D> Definition<A, B, C, D> {
pub fn location(&self) -> Span { pub fn location(&self) -> Span {
match self { match self {
Definition::Fn(Function { location, .. }) Definition::Fn(Function { location, .. })
@ -790,28 +791,64 @@ impl<T: PartialEq> RecordConstructorArg<T> {
} }
} }
pub type TypedArg = Arg<Rc<Type>>; #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub type UntypedArg = Arg<()>; pub enum ArgBy {
ByName(ArgName),
ByPattern(UntypedPattern),
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Arg<T> { pub struct UntypedArg {
pub arg_name: ArgName, pub by: ArgBy,
pub location: Span, pub location: Span,
pub annotation: Option<Annotation>, pub annotation: Option<Annotation>,
pub doc: Option<String>, pub doc: Option<String>,
pub tipo: T,
} }
impl<A> Arg<A> { impl UntypedArg {
pub fn set_type<B>(self, tipo: B) -> Arg<B> { pub fn arg_name(&self, ix: usize) -> ArgName {
Arg { match self.by {
ArgBy::ByName(ref name) => name.clone(),
ArgBy::ByPattern(..) => {
// NOTE: We use ordinal here not only because it's cute, but because
// such a name cannot be parsed to begin with and thus, will not clash
// with any user-defined name.
let name = format!("{}_arg", Ordinal::<usize>(ix).suffix());
ArgName::Named {
label: name.clone(),
name,
location: self.location,
// TODO: This should likely be moved up inside 'UntypedArg'.
is_validator_param: false,
}
}
}
}
pub fn set_type(self, tipo: Rc<Type>, ix: usize) -> TypedArg {
TypedArg {
tipo, tipo,
arg_name: self.arg_name, arg_name: self.arg_name(ix),
location: self.location, location: self.location,
annotation: self.annotation, annotation: self.annotation,
doc: self.doc, doc: self.doc,
} }
} }
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct TypedArg {
pub arg_name: ArgName,
pub location: Span,
pub annotation: Option<Annotation>,
pub doc: Option<String>,
pub tipo: Rc<Type>,
}
impl TypedArg {
pub fn put_doc(&mut self, new_doc: String) {
self.doc = Some(new_doc);
}
pub fn get_variable_name(&self) -> Option<&str> { pub fn get_variable_name(&self) -> Option<&str> {
self.arg_name.get_variable_name() self.arg_name.get_variable_name()
@ -830,12 +867,6 @@ impl<A> Arg<A> {
false false
} }
pub fn put_doc(&mut self, new_doc: String) {
self.doc = Some(new_doc);
}
}
impl TypedArg {
pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> { pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> {
if self.arg_name.location().contains(byte_index) { if self.arg_name.location().contains(byte_index) {
Some(Located::Argument(&self.arg_name, self.tipo.clone())) Some(Located::Argument(&self.arg_name, self.tipo.clone()))
@ -847,40 +878,38 @@ impl TypedArg {
} }
} }
pub type TypedArgVia = ArgVia<Rc<Type>, TypedExpr>; pub type TypedArgVia = ArgVia<TypedArg, TypedExpr>;
pub type UntypedArgVia = ArgVia<(), UntypedExpr>; pub type UntypedArgVia = ArgVia<UntypedArg, UntypedExpr>;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ArgVia<T, Expr> { pub struct ArgVia<Arg, Expr> {
pub arg_name: ArgName, pub arg: Arg,
pub location: Span,
pub via: Expr, pub via: Expr,
pub tipo: T,
pub annotation: Option<Annotation>,
} }
impl<T, Ann> From<ArgVia<T, Ann>> for Arg<T> { impl<Expr> From<ArgVia<TypedArg, Expr>> for TypedArg {
fn from(arg: ArgVia<T, Ann>) -> Arg<T> { fn from(this: ArgVia<TypedArg, Expr>) -> TypedArg {
Arg { this.arg
arg_name: arg.arg_name,
location: arg.location,
tipo: arg.tipo,
annotation: None,
doc: None,
} }
} }
impl<Expr> From<ArgVia<UntypedArg, Expr>> for UntypedArg {
fn from(this: ArgVia<UntypedArg, Expr>) -> UntypedArg {
this.arg
}
} }
impl TypedArgVia { impl TypedArgVia {
pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> { pub fn find_node(&self, byte_index: usize) -> Option<Located<'_>> {
if self.arg_name.location().contains(byte_index) { if self.arg.arg_name.location().contains(byte_index) {
Some(Located::Argument(&self.arg_name, self.tipo.clone())) Some(Located::Argument(&self.arg.arg_name, self.arg.tipo.clone()))
} else { } else {
// `via` is done first here because when there is no manually written // `via` is done first here because when there is no manually written
// annotation, it seems one is injected leading to a `found` returning too early // annotation, it seems one is injected leading to a `found` returning too early
// because the span of the filled in annotation matches the span of the via expr. // because the span of the filled in annotation matches the span of the via expr.
self.via.find_node(byte_index).or_else(|| { self.via.find_node(byte_index).or_else(|| {
self.annotation self.arg
.annotation
.as_ref() .as_ref()
.and_then(|annotation| annotation.find_node(byte_index)) .and_then(|annotation| annotation.find_node(byte_index))
}) })

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
ast::{ ast::{
Annotation, Arg, ArgName, CallArg, DataTypeKey, Function, FunctionAccessKey, ModuleKind, Annotation, ArgName, CallArg, DataTypeKey, Function, FunctionAccessKey, ModuleKind,
OnTestFailure, Span, TypedDataType, TypedFunction, UnOp, OnTestFailure, Span, TypedArg, TypedDataType, TypedFunction, UnOp,
}, },
expr::TypedExpr, expr::TypedExpr,
tipo::{ tipo::{
@ -932,7 +932,7 @@ pub fn prelude_functions(id_gen: &IdGenerator) -> IndexMap<FunctionAccessKey, Ty
function_name: "not".to_string(), function_name: "not".to_string(),
}, },
Function { Function {
arguments: vec![Arg { arguments: vec![TypedArg {
arg_name: ArgName::Named { arg_name: ArgName::Named {
name: "self".to_string(), name: "self".to_string(),
label: "self".to_string(), label: "self".to_string(),
@ -989,7 +989,7 @@ pub fn prelude_functions(id_gen: &IdGenerator) -> IndexMap<FunctionAccessKey, Ty
function_name: "identity".to_string(), function_name: "identity".to_string(),
}, },
Function { Function {
arguments: vec![Arg { arguments: vec![TypedArg {
arg_name: ArgName::Named { arg_name: ArgName::Named {
name: "a".to_string(), name: "a".to_string(),
label: "a".to_string(), label: "a".to_string(),
@ -1045,7 +1045,7 @@ pub fn prelude_functions(id_gen: &IdGenerator) -> IndexMap<FunctionAccessKey, Ty
Function { Function {
on_test_failure: OnTestFailure::FailImmediately, on_test_failure: OnTestFailure::FailImmediately,
arguments: vec![ arguments: vec![
Arg { TypedArg {
arg_name: ArgName::Named { arg_name: ArgName::Named {
name: "a".to_string(), name: "a".to_string(),
label: "a".to_string(), label: "a".to_string(),
@ -1057,7 +1057,7 @@ pub fn prelude_functions(id_gen: &IdGenerator) -> IndexMap<FunctionAccessKey, Ty
doc: None, doc: None,
tipo: a_var.clone(), tipo: a_var.clone(),
}, },
Arg { TypedArg {
arg_name: ArgName::Discarded { arg_name: ArgName::Discarded {
name: "_b".to_string(), name: "_b".to_string(),
label: "_b".to_string(), label: "_b".to_string(),
@ -1122,7 +1122,7 @@ pub fn prelude_functions(id_gen: &IdGenerator) -> IndexMap<FunctionAccessKey, Ty
}, },
Function { Function {
on_test_failure: OnTestFailure::FailImmediately, on_test_failure: OnTestFailure::FailImmediately,
arguments: vec![Arg { arguments: vec![TypedArg {
arg_name: ArgName::Named { arg_name: ArgName::Named {
name: "f".to_string(), name: "f".to_string(),
label: "f".to_string(), label: "f".to_string(),
@ -1139,7 +1139,7 @@ pub fn prelude_functions(id_gen: &IdGenerator) -> IndexMap<FunctionAccessKey, Ty
tipo: return_type.clone(), tipo: return_type.clone(),
is_capture: false, is_capture: false,
args: vec![ args: vec![
Arg { TypedArg {
arg_name: ArgName::Named { arg_name: ArgName::Named {
name: "b".to_string(), name: "b".to_string(),
label: "b".to_string(), label: "b".to_string(),
@ -1151,7 +1151,7 @@ pub fn prelude_functions(id_gen: &IdGenerator) -> IndexMap<FunctionAccessKey, Ty
doc: None, doc: None,
tipo: b_var.clone(), tipo: b_var.clone(),
}, },
Arg { TypedArg {
arg_name: ArgName::Named { arg_name: ArgName::Named {
name: "a".to_string(), name: "a".to_string(),
label: "a".to_string(), label: "a".to_string(),

View File

@ -1,10 +1,11 @@
use crate::{ use crate::{
ast::{ ast::{
self, Annotation, Arg, ArgName, AssignmentPattern, BinOp, Bls12_381Point, self, Annotation, ArgBy, ArgName, AssignmentPattern, BinOp, Bls12_381Point,
ByteArrayFormatPreference, CallArg, Curve, DataType, DataTypeKey, DefinitionLocation, ByteArrayFormatPreference, CallArg, Curve, DataType, DataTypeKey, DefinitionLocation,
IfBranch, Located, LogicalOpChainKind, ParsedCallArg, Pattern, RecordConstructorArg, IfBranch, Located, LogicalOpChainKind, ParsedCallArg, Pattern, RecordConstructorArg,
RecordUpdateSpread, Span, TraceKind, TypedAssignmentKind, TypedClause, TypedDataType, RecordUpdateSpread, Span, TraceKind, TypedArg, TypedAssignmentKind, TypedClause,
TypedRecordUpdateArg, UnOp, UntypedAssignmentKind, UntypedClause, UntypedRecordUpdateArg, TypedDataType, TypedRecordUpdateArg, UnOp, UntypedArg, UntypedAssignmentKind,
UntypedClause, UntypedRecordUpdateArg,
}, },
builtins::void, builtins::void,
parser::token::Base, parser::token::Base,
@ -74,7 +75,7 @@ pub enum TypedExpr {
location: Span, location: Span,
tipo: Rc<Type>, tipo: Rc<Type>,
is_capture: bool, is_capture: bool,
args: Vec<Arg<Rc<Type>>>, args: Vec<TypedArg>,
body: Box<Self>, body: Box<Self>,
return_annotation: Option<Annotation>, return_annotation: Option<Annotation>,
}, },
@ -495,7 +496,7 @@ pub enum UntypedExpr {
Fn { Fn {
location: Span, location: Span,
fn_style: FnStyle, fn_style: FnStyle,
arguments: Vec<Arg<()>>, arguments: Vec<UntypedArg>,
body: Box<Self>, body: Box<Self>,
return_annotation: Option<Annotation>, return_annotation: Option<Annotation>,
}, },
@ -1192,17 +1193,16 @@ impl UntypedExpr {
} => { } => {
let name = format!("{}__{index}", ast::CAPTURE_VARIABLE); let name = format!("{}__{index}", ast::CAPTURE_VARIABLE);
holes.push(ast::Arg { holes.push(ast::UntypedArg {
location: Span::empty(), location: Span::empty(),
annotation: None, annotation: None,
doc: None, doc: None,
arg_name: ast::ArgName::Named { by: ArgBy::ByName(ast::ArgName::Named {
label: name.clone(), label: name.clone(),
name, name,
location: Span::empty(), location: Span::empty(),
is_validator_param: false, is_validator_param: false,
}, }),
tipo: (),
}); });
ast::CallArg { ast::CallArg {
@ -1358,12 +1358,11 @@ impl UntypedExpr {
fn_style: FnStyle::Plain, fn_style: FnStyle::Plain,
arguments: names arguments: names
.into_iter() .into_iter()
.map(|(arg_name, location, annotation)| Arg { .map(|(arg_name, location, annotation)| UntypedArg {
location, location,
doc: None, doc: None,
annotation, annotation,
tipo: (), by: ArgBy::ByName(arg_name),
arg_name,
}) })
.collect(), .collect(),
body: Self::Sequence { body: Self::Sequence {

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
ast::{ ast::{
Annotation, Arg, ArgName, ArgVia, AssignmentKind, AssignmentPattern, BinOp, Annotation, ArgBy, ArgName, ArgVia, AssignmentKind, AssignmentPattern, BinOp,
ByteArrayFormatPreference, CallArg, ClauseGuard, Constant, CurveType, DataType, Definition, ByteArrayFormatPreference, CallArg, ClauseGuard, Constant, CurveType, DataType, Definition,
Function, IfBranch, LogicalOpChainKind, ModuleConstant, OnTestFailure, Pattern, Function, IfBranch, LogicalOpChainKind, ModuleConstant, OnTestFailure, Pattern,
RecordConstructor, RecordConstructorArg, RecordUpdateSpread, Span, TraceKind, TypeAlias, RecordConstructor, RecordConstructorArg, RecordUpdateSpread, Span, TraceKind, TypeAlias,
@ -459,41 +459,39 @@ impl<'comments> Formatter<'comments> {
.append(line().append(self.annotation(typ)).group().nest(INDENT)) .append(line().append(self.annotation(typ)).group().nest(INDENT))
} }
fn fn_arg<'a, A>(&mut self, arg: &'a Arg<A>) -> Document<'a> { fn fn_arg<'a>(&mut self, arg: &'a UntypedArg) -> Document<'a> {
let comments = self.pop_comments(arg.location.start); let comments = self.pop_comments(arg.location.start);
let doc_comments = self.doc_comments(arg.location.start); let doc_comments = self.doc_comments(arg.location.start);
let doc = match &arg.annotation { let doc = match arg.by {
None => arg.arg_name.to_doc(), ArgBy::ByName(ref arg_name) => match &arg.annotation {
Some(a) => arg None => arg_name.to_doc(),
.arg_name Some(a) => arg_name.to_doc().append(": ").append(self.annotation(a)),
.to_doc()
.append(": ")
.append(self.annotation(a)),
} }
.group(); .group(),
ArgBy::ByPattern(..) => todo!(),
};
let doc = doc_comments.append(doc.group()).group(); let doc = doc_comments.append(doc.group()).group();
commented(doc, comments) commented(doc, comments)
} }
fn fn_arg_via<'a, A>(&mut self, arg: &'a ArgVia<A, UntypedExpr>) -> Document<'a> { fn fn_arg_via<'a>(&mut self, arg_via: &'a ArgVia<UntypedArg, UntypedExpr>) -> Document<'a> {
let comments = self.pop_comments(arg.location.start); let comments = self.pop_comments(arg_via.arg.location.start);
let doc_comments = self.doc_comments(arg.location.start); let doc_comments = self.doc_comments(arg_via.arg.location.start);
let doc = match &arg.annotation { let doc = match arg_via.arg.by {
None => arg.arg_name.to_doc(), ArgBy::ByName(ref arg_name) => match &arg_via.arg.annotation {
Some(a) => arg None => arg_name.to_doc(),
.arg_name Some(a) => arg_name.to_doc().append(": ").append(self.annotation(a)),
.to_doc() },
.append(": ") ArgBy::ByPattern(..) => todo!(),
.append(self.annotation(a)),
} }
.append(" via ") .append(" via ")
.append(self.expr(&arg.via, false)) .append(self.expr(&arg_via.via, false))
.group(); .group();
let doc = doc_comments.append(doc.group()).group(); let doc = doc_comments.append(doc.group()).group();
@ -1983,11 +1981,7 @@ impl<'a> Documentable<'a> for &'a ArgName {
} }
fn pub_(public: bool) -> Document<'static> { fn pub_(public: bool) -> Document<'static> {
if public { if public { "pub ".to_doc() } else { nil() }
"pub ".to_doc()
} else {
nil()
}
} }
impl<'a> Documentable<'a> for &'a UnqualifiedImport { impl<'a> Documentable<'a> for &'a UnqualifiedImport {

View File

@ -1,5 +1,6 @@
use crate::{ use crate::{
ast, ast,
ast::ArgBy::ByName,
expr::UntypedExpr, expr::UntypedExpr,
parser::{annotation, error::ParseError, expr, token::Token, utils}, parser::{annotation, error::ParseError, expr, token::Token, utils},
}; };
@ -78,12 +79,11 @@ pub fn param(is_validator_param: bool) -> impl Parser<Token, ast::UntypedArg, Er
}), }),
)) ))
.then(just(Token::Colon).ignore_then(annotation()).or_not()) .then(just(Token::Colon).ignore_then(annotation()).or_not())
.map_with_span(|(arg_name, annotation), span| ast::Arg { .map_with_span(|(arg_name, annotation), span| ast::UntypedArg {
location: span, location: span,
annotation, annotation,
doc: None, doc: None,
tipo: (), by: ByName(arg_name),
arg_name,
}) })
} }

View File

@ -6,34 +6,42 @@ Test(
Function { Function {
arguments: [ arguments: [
ArgVia { ArgVia {
arg_name: Named { arg: UntypedArg {
by: ByName(
Named {
name: "x", name: "x",
label: "x", label: "x",
location: 9..10, location: 9..10,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 9..10, location: 9..10,
annotation: None,
doc: None,
},
via: Var { via: Var {
location: 15..16, location: 15..16,
name: "f", name: "f",
}, },
tipo: (),
annotation: None,
}, },
ArgVia { ArgVia {
arg_name: Named { arg: UntypedArg {
by: ByName(
Named {
name: "y", name: "y",
label: "y", label: "y",
location: 18..19, location: 18..19,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 18..19, location: 18..19,
annotation: None,
doc: None,
},
via: Var { via: Var {
location: 24..25, location: 24..25,
name: "g", name: "g",
}, },
tipo: (),
annotation: None,
}, },
], ],
body: Var { body: Var {

View File

@ -6,13 +6,19 @@ Test(
Function { Function {
arguments: [ arguments: [
ArgVia { ArgVia {
arg_name: Named { arg: UntypedArg {
by: ByName(
Named {
name: "x", name: "x",
label: "x", label: "x",
location: 9..10, location: 9..10,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 9..10, location: 9..10,
annotation: None,
doc: None,
},
via: FieldAccess { via: FieldAccess {
location: 15..27, location: 15..27,
label: "any_int", label: "any_int",
@ -21,8 +27,6 @@ Test(
name: "fuzz", name: "fuzz",
}, },
}, },
tipo: (),
annotation: None,
}, },
], ],
body: Var { body: Var {

View File

@ -6,22 +6,16 @@ Test(
Function { Function {
arguments: [ arguments: [
ArgVia { ArgVia {
arg_name: Named { arg: UntypedArg {
by: ByName(
Named {
name: "x", name: "x",
label: "x", label: "x",
location: 9..10, location: 9..10,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 9..15, location: 9..15,
via: Call {
arguments: [],
fun: Var {
location: 20..23,
name: "foo",
},
location: 20..25,
},
tipo: (),
annotation: Some( annotation: Some(
Constructor { Constructor {
location: 12..15, location: 12..15,
@ -30,6 +24,16 @@ Test(
arguments: [], arguments: [],
}, },
), ),
doc: None,
},
via: Call {
arguments: [],
fun: Var {
location: 20..23,
name: "foo",
},
location: 20..25,
},
}, },
], ],
body: Var { body: Var {

View File

@ -8,41 +8,44 @@ Validator(
end_position: 90, end_position: 90,
fun: Function { fun: Function {
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "datum", name: "datum",
label: "datum", label: "datum",
location: 21..26, location: 21..26,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 21..26, location: 21..26,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "rdmr", name: "rdmr",
label: "rdmr", label: "rdmr",
location: 28..32, location: 28..32,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 28..32, location: 28..32,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "ctx", name: "ctx",
label: "ctx", label: "ctx",
location: 34..37, location: 34..37,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 34..37, location: 34..37,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
], ],
body: Var { body: Var {
@ -61,29 +64,31 @@ Validator(
other_fun: Some( other_fun: Some(
Function { Function {
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "rdmr", name: "rdmr",
label: "rdmr", label: "rdmr",
location: 64..68, location: 64..68,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 64..68, location: 64..68,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "ctx", name: "ctx",
label: "ctx", label: "ctx",
location: 70..73, location: 70..73,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 70..73, location: 70..73,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
], ],
body: Var { body: Var {

View File

@ -8,41 +8,44 @@ Validator(
end_position: 54, end_position: 54,
fun: Function { fun: Function {
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "datum", name: "datum",
label: "datum", label: "datum",
location: 21..26, location: 21..26,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 21..26, location: 21..26,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "rdmr", name: "rdmr",
label: "rdmr", label: "rdmr",
location: 28..32, location: 28..32,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 28..32, location: 28..32,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "ctx", name: "ctx",
label: "ctx", label: "ctx",
location: 34..37, location: 34..37,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 34..37, location: 34..37,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
], ],
body: Var { body: Var {

View File

@ -74,11 +74,13 @@ pub fn via() -> impl Parser<Token, ast::UntypedArgVia, Error = ParseError> {
.then_ignore(just(Token::Via)) .then_ignore(just(Token::Via))
.then(fuzzer()) .then(fuzzer())
.map(|((arg_name, annotation, location), via)| ast::ArgVia { .map(|((arg_name, annotation, location), via)| ast::ArgVia {
arg_name, arg: ast::UntypedArg {
via, by: ast::ArgBy::ByName(arg_name),
annotation, annotation,
tipo: (),
location, location,
doc: None,
},
via,
}) })
} }

View File

@ -1,10 +1,9 @@
use chumsky::prelude::*;
use crate::{ use crate::{
ast, ast,
expr::{FnStyle, UntypedExpr}, expr::{FnStyle, UntypedExpr},
parser::{error::ParseError, token::Token}, parser::{error::ParseError, token::Token},
}; };
use chumsky::prelude::*;
pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> { pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
select! { select! {
@ -41,29 +40,27 @@ pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
}; };
let arguments = vec![ let arguments = vec![
ast::Arg { ast::UntypedArg {
arg_name: ast::ArgName::Named { by: ast::ArgBy::ByName(ast::ArgName::Named {
name: "left".to_string(), name: "left".to_string(),
label: "left".to_string(), label: "left".to_string(),
location, location,
is_validator_param: false, is_validator_param: false,
}, }),
annotation: arg_annotation.clone(), annotation: arg_annotation.clone(),
doc: None, doc: None,
location, location,
tipo: (),
}, },
ast::Arg { ast::UntypedArg {
arg_name: ast::ArgName::Named { by: ast::ArgBy::ByName(ast::ArgName::Named {
name: "right".to_string(), name: "right".to_string(),
label: "right".to_string(), label: "right".to_string(),
location, location,
is_validator_param: false, is_validator_param: false,
}, }),
annotation: arg_annotation, annotation: arg_annotation,
doc: None, doc: None,
location, location,
tipo: (),
}, },
]; ];

View File

@ -1,10 +1,9 @@
use chumsky::prelude::*;
use crate::{ use crate::{
ast, ast,
expr::{FnStyle, UntypedExpr}, expr::{FnStyle, UntypedExpr},
parser::{annotation, error::ParseError, token::Token}, parser::{annotation, error::ParseError, token::Token},
}; };
use chumsky::prelude::*;
pub fn parser( pub fn parser(
sequence: Recursive<'_, Token, UntypedExpr, ParseError>, sequence: Recursive<'_, Token, UntypedExpr, ParseError>,
@ -47,12 +46,11 @@ pub fn params() -> impl Parser<Token, ast::UntypedArg, Error = ParseError> {
}), }),
)) ))
.then(just(Token::Colon).ignore_then(annotation()).or_not()) .then(just(Token::Colon).ignore_then(annotation()).or_not())
.map_with_span(|(arg_name, annotation), span| ast::Arg { .map_with_span(|(arg_name, annotation), span| ast::UntypedArg {
location: span, location: span,
annotation, annotation,
doc: None, doc: None,
tipo: (), by: ast::ArgBy::ByName(arg_name),
arg_name,
}) })
} }

View File

@ -6,13 +6,15 @@ Fn {
location: 0..28, location: 0..28,
fn_style: Plain, fn_style: Plain,
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "a", name: "a",
label: "a", label: "a",
location: 4..5, location: 4..5,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 4..10, location: 4..10,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -23,7 +25,6 @@ Fn {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {

View File

@ -0,0 +1,77 @@
---
source: crates/aiken-lang/src/parser/expr/anonymous_function.rs
description: "Code:\n\nfn (a: Int) -> Int {\n let b = 1\n a + b\n}"
---
Fn {
location: 0..46,
fn_style: Plain,
arguments: [
Arg {
arg_name: Named {
name: "a",
label: "a",
location: 4..5,
is_validator_param: false,
},
location: 4..10,
annotation: Some(
Constructor {
location: 7..10,
module: None,
name: "Int",
arguments: [],
},
),
doc: None,
tipo: (),
},
],
body: Sequence {
location: 39..44,
expressions: [
Assignment {
location: 25..34,
value: UInt {
location: 33..34,
value: "1",
base: Decimal {
numeric_underscore: false,
},
},
patterns: [
AssignmentPattern {
pattern: Var {
location: 29..30,
name: "b",
},
annotation: None,
location: 29..30,
},
],
kind: Let {
backpassing: false,
},
},
BinOp {
location: 39..44,
name: AddInt,
left: Var {
location: 39..40,
name: "a",
},
right: Var {
location: 43..44,
name: "b",
},
},
],
},
return_annotation: Some(
Constructor {
location: 15..18,
module: None,
name: "Int",
arguments: [],
},
),
}

View File

@ -24,13 +24,15 @@ Sequence {
GtInt, GtInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 16..17, location: 16..17,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 16..17, location: 16..17,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -41,15 +43,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 16..17, location: 16..17,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 16..17, location: 16..17,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -60,7 +63,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -119,13 +121,15 @@ Sequence {
GtEqInt, GtEqInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 38..40, location: 38..40,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 38..40, location: 38..40,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -136,15 +140,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 38..40, location: 38..40,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 38..40, location: 38..40,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -155,7 +160,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -214,13 +218,15 @@ Sequence {
LtInt, LtInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 61..62, location: 61..62,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 61..62, location: 61..62,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -231,15 +237,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 61..62, location: 61..62,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 61..62, location: 61..62,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -250,7 +257,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -309,13 +315,15 @@ Sequence {
LtEqInt, LtEqInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 83..85, location: 83..85,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 83..85, location: 83..85,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -326,15 +334,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 83..85, location: 83..85,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 83..85, location: 83..85,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -345,7 +354,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -404,29 +412,31 @@ Sequence {
Eq, Eq,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 106..108, location: 106..108,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 106..108, location: 106..108,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 106..108, location: 106..108,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 106..108, location: 106..108,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -485,29 +495,31 @@ Sequence {
NotEq, NotEq,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 129..131, location: 129..131,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 129..131, location: 129..131,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 129..131, location: 129..131,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 129..131, location: 129..131,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -566,13 +578,15 @@ Sequence {
And, And,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 152..154, location: 152..154,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 152..154, location: 152..154,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -583,15 +597,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 152..154, location: 152..154,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 152..154, location: 152..154,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -602,7 +617,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -661,13 +675,15 @@ Sequence {
Or, Or,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 175..177, location: 175..177,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 175..177, location: 175..177,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -678,15 +694,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 175..177, location: 175..177,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 175..177, location: 175..177,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -697,7 +714,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -756,13 +772,15 @@ Sequence {
AddInt, AddInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 198..199, location: 198..199,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 198..199, location: 198..199,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -773,15 +791,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 198..199, location: 198..199,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 198..199, location: 198..199,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -792,7 +811,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -851,13 +869,15 @@ Sequence {
SubInt, SubInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 220..221, location: 220..221,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 220..221, location: 220..221,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -868,15 +888,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 220..221, location: 220..221,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 220..221, location: 220..221,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -887,7 +908,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -946,13 +966,15 @@ Sequence {
DivInt, DivInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 242..243, location: 242..243,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 242..243, location: 242..243,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -963,15 +985,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 242..243, location: 242..243,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 242..243, location: 242..243,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -982,7 +1005,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -1041,13 +1063,15 @@ Sequence {
MultInt, MultInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 264..265, location: 264..265,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 264..265, location: 264..265,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -1058,15 +1082,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 264..265, location: 264..265,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 264..265, location: 264..265,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -1077,7 +1102,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {
@ -1136,13 +1160,15 @@ Sequence {
ModInt, ModInt,
), ),
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "left", name: "left",
label: "left", label: "left",
location: 286..287, location: 286..287,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 286..287, location: 286..287,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -1153,15 +1179,16 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "right", name: "right",
label: "right", label: "right",
location: 286..287, location: 286..287,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 286..287, location: 286..287,
annotation: Some( annotation: Some(
Constructor { Constructor {
@ -1172,7 +1199,6 @@ Sequence {
}, },
), ),
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {

View File

@ -47,17 +47,18 @@ Sequence {
location: 36..65, location: 36..65,
fn_style: Capture, fn_style: Capture,
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "_capture__0", name: "_capture__0",
label: "_capture__0", label: "_capture__0",
location: 0..0, location: 0..0,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 0..0, location: 0..0,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
], ],
body: Call { body: Call {
@ -77,17 +78,18 @@ Sequence {
location: 48..64, location: 48..64,
fn_style: Plain, fn_style: Plain,
arguments: [ arguments: [
Arg { UntypedArg {
arg_name: Named { by: ByName(
Named {
name: "y", name: "y",
label: "y", label: "y",
location: 52..53, location: 52..53,
is_validator_param: false, is_validator_param: false,
}, },
),
location: 52..53, location: 52..53,
annotation: None, annotation: None,
doc: None, doc: None,
tipo: (),
}, },
], ],
body: BinOp { body: BinOp {

View File

@ -1149,7 +1149,7 @@ impl<'a> Environment<'a> {
let mut field_map = FieldMap::new(arguments.len(), true); let mut field_map = FieldMap::new(arguments.len(), true);
for (i, arg) in arguments.iter().enumerate() { for (i, arg) in arguments.iter().enumerate() {
field_map.insert(arg.arg_name.get_label(), i, &arg.location)?; field_map.insert(arg.arg_name(i).get_label(), i, &arg.location)?;
} }
let field_map = field_map.into_option(); let field_map = field_map.into_option();

View File

@ -10,7 +10,7 @@ use super::{
}; };
use crate::{ use crate::{
ast::{ ast::{
self, Annotation, Arg, ArgName, AssignmentKind, AssignmentPattern, BinOp, Bls12_381Point, self, Annotation, ArgName, AssignmentKind, AssignmentPattern, BinOp, Bls12_381Point,
ByteArrayFormatPreference, CallArg, ClauseGuard, Constant, Curve, Function, IfBranch, ByteArrayFormatPreference, CallArg, ClauseGuard, Constant, Curve, Function, IfBranch,
LogicalOpChainKind, Pattern, RecordUpdateSpread, Span, TraceKind, TraceLevel, Tracing, LogicalOpChainKind, Pattern, RecordUpdateSpread, Span, TraceKind, TraceLevel, Tracing,
TypedArg, TypedCallArg, TypedClause, TypedClauseGuard, TypedIfBranch, TypedPattern, TypedArg, TypedCallArg, TypedClause, TypedClauseGuard, TypedIfBranch, TypedPattern,
@ -79,7 +79,8 @@ pub(crate) fn infer_function(
let arguments = arguments let arguments = arguments
.iter() .iter()
.zip(&args_types) .zip(&args_types)
.map(|(arg_name, tipo)| arg_name.to_owned().set_type(tipo.clone())) .enumerate()
.map(|(ix, (arg_name, tipo))| arg_name.to_owned().set_type(tipo.clone(), ix))
.collect(); .collect();
let hydrator = hydrators let hydrator = hydrators
@ -331,7 +332,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
let mut arguments = Vec::new(); let mut arguments = Vec::new();
for (i, arg) in args.into_iter().enumerate() { for (i, arg) in args.into_iter().enumerate() {
let arg = self.infer_param(arg, expected_args.get(i).cloned())?; let arg = self.infer_param(arg, expected_args.get(i).cloned(), i)?;
arguments.push(arg); arguments.push(arg);
} }
@ -1070,18 +1071,21 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
}) })
} }
// TODO: Handle arg pattern
fn infer_param( fn infer_param(
&mut self, &mut self,
arg: UntypedArg, untyped_arg: UntypedArg,
expected: Option<Rc<Type>>, expected: Option<Rc<Type>>,
ix: usize,
) -> Result<TypedArg, Error> { ) -> Result<TypedArg, Error> {
let Arg { let arg_name = untyped_arg.arg_name(ix);
arg_name,
let UntypedArg {
by,
annotation, annotation,
location, location,
doc, doc,
tipo: _, } = untyped_arg;
} = arg;
let tipo = annotation let tipo = annotation
.clone() .clone()
@ -1097,7 +1101,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
self.unify(expected, tipo.clone(), location, false)?; self.unify(expected, tipo.clone(), location, false)?;
} }
Ok(Arg { Ok(TypedArg {
arg_name, arg_name,
location, location,
annotation, annotation,

View File

@ -7,8 +7,8 @@ use super::{
}; };
use crate::{ use crate::{
ast::{ ast::{
Annotation, Arg, ArgName, ArgVia, DataType, Definition, Function, ModuleConstant, Annotation, ArgName, ArgVia, DataType, Definition, Function, ModuleConstant, ModuleKind,
ModuleKind, RecordConstructor, RecordConstructorArg, Tracing, TypeAlias, TypedDefinition, RecordConstructor, RecordConstructorArg, Tracing, TypeAlias, TypedArg, TypedDefinition,
TypedFunction, TypedModule, UntypedDefinition, UntypedModule, Use, Validator, TypedFunction, TypedModule, UntypedDefinition, UntypedModule, Use, Validator,
}, },
builtins, builtins,
@ -198,8 +198,12 @@ fn infer_definition(
.function_types() .function_types()
.expect("Preregistered type for fn was not a fn"); .expect("Preregistered type for fn was not a fn");
for (arg, t) in params.iter().zip(args_types[0..params.len()].iter()) { for (ix, (arg, t)) in params
match &arg.arg_name { .iter()
.zip(args_types[0..params.len()].iter())
.enumerate()
{
match &arg.arg_name(ix) {
ArgName::Named { ArgName::Named {
name, name,
is_validator_param, is_validator_param,
@ -326,7 +330,12 @@ fn infer_definition(
if f.arguments.len() > 1 { if f.arguments.len() > 1 {
return Err(Error::IncorrectTestArity { return Err(Error::IncorrectTestArity {
count: f.arguments.len(), count: f.arguments.len(),
location: f.arguments.get(1).expect("arguments.len() > 1").location, location: f
.arguments
.get(1)
.expect("arguments.len() > 1")
.arg
.location,
}); });
} }
@ -336,6 +345,7 @@ fn infer_definition(
let hydrator: &mut Hydrator = hydrators.get_mut(&f.name).unwrap(); let hydrator: &mut Hydrator = hydrators.get_mut(&f.name).unwrap();
let provided_inner_type = arg let provided_inner_type = arg
.arg
.annotation .annotation
.as_ref() .as_ref()
.map(|ann| hydrator.type_from_annotation(ann, environment)) .map(|ann| hydrator.type_from_annotation(ann, environment))
@ -352,13 +362,14 @@ fn infer_definition(
// Fuzzer. // Fuzzer.
if let Some(provided_inner_type) = provided_inner_type { if let Some(provided_inner_type) = provided_inner_type {
if !arg if !arg
.arg
.annotation .annotation
.as_ref() .as_ref()
.unwrap() .unwrap()
.is_logically_equal(&inferred_annotation) .is_logically_equal(&inferred_annotation)
{ {
return Err(Error::CouldNotUnify { return Err(Error::CouldNotUnify {
location: arg.location, location: arg.arg.location,
expected: inferred_inner_type.clone(), expected: inferred_inner_type.clone(),
given: provided_inner_type.clone(), given: provided_inner_type.clone(),
situation: Some(UnifyErrorSituation::FuzzerAnnotationMismatch), situation: Some(UnifyErrorSituation::FuzzerAnnotationMismatch),
@ -417,23 +428,17 @@ fn infer_definition(
public: typed_f.public, public: typed_f.public,
arguments: match typed_via { arguments: match typed_via {
Some((via, tipo)) => { Some((via, tipo)) => {
let Arg { let arg = typed_f
arg_name,
location,
annotation: _,
doc: _,
tipo: _,
} = typed_f
.arguments .arguments
.first() .first()
.expect("has exactly one argument") .expect("has exactly one argument")
.to_owned(); .to_owned();
vec![ArgVia { vec![ArgVia {
annotation, arg: TypedArg {
arg_name,
location,
tipo, tipo,
annotation,
..arg
},
via, via,
}] }]
} }

View File

@ -1,6 +1,6 @@
use aiken_lang::ast::OnTestFailure; use aiken_lang::ast::OnTestFailure;
pub(crate) use aiken_lang::{ pub(crate) use aiken_lang::{
ast::{Arg, BinOp, DataTypeKey, IfBranch, Span, TypedDataType, TypedTest}, ast::{BinOp, DataTypeKey, IfBranch, Span, TypedArg, TypedDataType, TypedTest},
builtins::bool, builtins::bool,
expr::{TypedExpr, UntypedExpr}, expr::{TypedExpr, UntypedExpr},
format::Formatter, format::Formatter,
@ -124,13 +124,13 @@ impl Test {
let via = parameter.via.clone(); let via = parameter.via.clone();
let type_info = parameter.tipo.clone(); let type_info = parameter.arg.tipo.clone();
let stripped_type_info = convert_opaque_type(&type_info, generator.data_types(), true); let stripped_type_info = convert_opaque_type(&type_info, generator.data_types(), true);
let program = generator.clone().generate_raw( let program = generator.clone().generate_raw(
&test.body, &test.body,
&[Arg { &[TypedArg {
tipo: stripped_type_info.clone(), tipo: stripped_type_info.clone(),
..parameter.clone().into() ..parameter.clone().into()
}], }],
@ -507,8 +507,10 @@ impl Prng {
fn as_prng(cst: &PlutusData) -> Prng { fn as_prng(cst: &PlutusData) -> Prng {
if let PlutusData::Constr(Constr { tag, fields, .. }) = cst { if let PlutusData::Constr(Constr { tag, fields, .. }) = cst {
if *tag == 121 + Prng::SEEDED { if *tag == 121 + Prng::SEEDED {
if let [PlutusData::BoundedBytes(bytes), PlutusData::BoundedBytes(choices)] = if let [
&fields[..] PlutusData::BoundedBytes(bytes),
PlutusData::BoundedBytes(choices),
] = &fields[..]
{ {
return Prng::Seeded { return Prng::Seeded {
choices: choices.to_vec(), choices: choices.to_vec(),
@ -1087,9 +1089,11 @@ impl TryFrom<TypedExpr> for Assertion<TypedExpr> {
final_else, final_else,
.. ..
} => { } => {
if let [IfBranch { if let [
IfBranch {
condition, body, .. condition, body, ..
}] = &branches[..] },
] = &branches[..]
{ {
let then_is_true = match body { let then_is_true = match body {
TypedExpr::Var { TypedExpr::Var {
@ -1508,13 +1512,14 @@ mod test {
} }
"#}); "#});
assert!(prop assert!(
.run::<()>( prop.run::<()>(
42, 42,
PropertyTest::DEFAULT_MAX_SUCCESS, PropertyTest::DEFAULT_MAX_SUCCESS,
&PlutusVersion::default() &PlutusVersion::default()
) )
.is_success()); .is_success()
);
} }
#[test] #[test]