1+ use std:: num:: NonZero ;
2+
3+ use simplicityhl:: {
4+ num:: NonZeroPow2Usize ,
5+ parse:: CallName ,
6+ str:: { AliasName , FunctionName } ,
7+ types:: AliasedType ,
8+ } ;
9+
110use crate :: completion:: types:: FunctionTemplate ;
211
312/// Macro to convert string literals into `Vec<String>`
@@ -14,67 +23,117 @@ macro_rules! str_vec {
1423
1524/// Get completion of builtin functions. They are all defined in `simplicityhl::parse::CallName`
1625pub fn get_builtin_functions ( ) -> Vec < FunctionTemplate > {
17- vec ! [
18- FunctionTemplate :: simple(
26+ let ty = AliasedType :: from ( AliasName :: from_str_unchecked ( "T" ) ) ;
27+ let Some ( some) = NonZero :: new ( 1 ) else {
28+ return vec ! [ ] ;
29+ } ;
30+
31+ let functions = vec ! [
32+ CallName :: UnwrapLeft ( ty. clone( ) ) ,
33+ CallName :: UnwrapRight ( ty. clone( ) ) ,
34+ CallName :: Unwrap ,
35+ CallName :: IsNone ( ty. clone( ) ) ,
36+ CallName :: Assert ,
37+ CallName :: Debug ,
38+ CallName :: Panic ,
39+ CallName :: Fold (
40+ FunctionName :: from_str_unchecked( "name" ) ,
41+ NonZeroPow2Usize :: TWO ,
42+ ) ,
43+ CallName :: ArrayFold ( FunctionName :: from_str_unchecked( "name" ) , some) ,
44+ CallName :: ForWhile ( FunctionName :: from_str_unchecked( "name" ) ) ,
45+ ] ;
46+
47+ functions
48+ . iter ( )
49+ . filter_map ( |func| match_callname ( func. to_owned ( ) ) )
50+ . collect ( )
51+ }
52+
53+ fn match_callname ( call : CallName ) -> Option < FunctionTemplate > {
54+ match call {
55+ CallName :: UnwrapLeft ( aliased_type) => {
56+ let ty = aliased_type. to_string ( ) ;
57+ Some ( FunctionTemplate :: new (
58+ format ! ( "unwrap_left::<{ty}>" ) ,
59+ "unwrap_left" ,
60+ str_vec ! [ format!( "{ty}" ) ] ,
61+ str_vec ! [ format!( "Either<{ty}, U>" ) ] ,
62+ ty,
63+ "Unwrap left side of `Either`" ,
64+ ) )
65+ }
66+ CallName :: UnwrapRight ( aliased_type) => {
67+ let ty = aliased_type. to_string ( ) ;
68+ Some ( FunctionTemplate :: new (
69+ format ! ( "unwrap_right::<{ty}>" ) ,
70+ "unwrap_left" ,
71+ str_vec ! [ format!( "{ty}" ) ] ,
72+ str_vec ! [ format!( "Either<T, {ty}>" ) ] ,
73+ ty,
74+ "Unwrap right side of `Either`" ,
75+ ) )
76+ }
77+ CallName :: Unwrap => Some ( FunctionTemplate :: simple (
78+ "unwrap" ,
79+ str_vec ! [ "Option<T>" ] ,
80+ "T" ,
81+ "Unwrap `Option` type" ,
82+ ) ) ,
83+ CallName :: IsNone ( aliased_type) => {
84+ let ty = aliased_type. to_string ( ) ;
85+ Some ( FunctionTemplate :: new (
86+ format ! ( "is_none::<{ty}>" ) ,
87+ "is_none" ,
88+ str_vec ! [ format!( "{ty}" ) ] ,
89+ str_vec ! [ format!( "Option<{ty}>" ) . as_str( ) ] ,
90+ "bool" ,
91+ "Check if `Option` is None" ,
92+ ) )
93+ }
94+ CallName :: Assert => Some ( FunctionTemplate :: simple (
1995 "assert!" ,
2096 str_vec ! [ "bool" ] ,
2197 "" ,
2298 "Fails program if argument is 'false'" ,
23- ) ,
24- FunctionTemplate :: simple(
99+ ) ) ,
100+ CallName :: Panic => Some ( FunctionTemplate :: simple (
101+ "panic!" ,
102+ str_vec ! [ ] ,
103+ "" ,
104+ "Fails program" ,
105+ ) ) ,
106+ CallName :: Debug => Some ( FunctionTemplate :: simple (
25107 "dbg!" ,
26- str_vec![ "type" ] ,
27- "type" ,
28- "Print value and return it" ,
29- ) ,
30- FunctionTemplate :: simple( "panic!" , str_vec![ ] , "" , "Fails program" ) ,
31- FunctionTemplate :: new(
32- "unwrap_left::<T>" ,
33- "unwrap_left" ,
34108 str_vec ! [ "T" ] ,
35- str_vec![ "Either<T, U>" ] ,
36109 "T" ,
37- "Unwrap left side of Either" ,
38- ) ,
39- FunctionTemplate :: new(
40- "unwrap_right::<U>" ,
41- "unwrap_right" ,
42- str_vec![ "U" ] ,
43- str_vec![ "Either<T, U>" ] ,
44- "U" ,
45- "Unwrap right side of Either" ,
46- ) ,
47- FunctionTemplate :: new(
48- "is_none::<T>" ,
49- "is_none" ,
50- str_vec![ "T" ] ,
51- str_vec![ "Option<T>" ] ,
52- "bool" ,
53- "Check if Option is None" ,
54- ) ,
55- FunctionTemplate :: new(
110+ "Print value and return it" ,
111+ ) ) ,
112+ CallName :: Fold ( _, _) => Some ( FunctionTemplate :: new (
56113 "fold::<F, B>" ,
57114 "fold" ,
58115 str_vec ! [ "F" , "B" ] ,
59116 str_vec ! [ "iter" , "init" ] ,
60117 "B" ,
61118 "Fold operation over an iterator" ,
62- ) ,
63- FunctionTemplate :: new(
119+ ) ) ,
120+ CallName :: ArrayFold ( _ , _ ) => Some ( FunctionTemplate :: new (
64121 "array_fold::<F, N>" ,
65122 "array_fold" ,
66123 str_vec ! [ "F" , "N" ] ,
67124 str_vec ! [ "array" , "init" ] ,
68125 "B" ,
69126 "Fold operation over an array of size N" ,
70- ) ,
71- FunctionTemplate :: new(
127+ ) ) ,
128+ CallName :: ForWhile ( _ ) => Some ( FunctionTemplate :: new (
72129 "for_while::<F>" ,
73130 "for_while" ,
74131 str_vec ! [ "F" ] ,
75132 str_vec ! [ "condition" , "body" ] ,
76133 "()" ,
77134 "While loop with a function" ,
78- ) ,
79- ]
135+ ) ) ,
136+ // TODO: implement TypeCast definition
137+ CallName :: Jet ( _) | CallName :: TypeCast ( _) | CallName :: Custom ( _) => None ,
138+ }
80139}
0 commit comments