11//! Abstract syntax tree types
22
3- use crate :: { span:: Spanned , types:: Type } ;
3+ use crate :: { prelude :: CompileTimeEnv , span:: Spanned , types:: Type } ;
44
55#[ derive( Debug , PartialEq ) ]
66pub enum Expr {
@@ -16,6 +16,14 @@ impl Expr {
1616 Self :: Identifier ( Box :: new ( ExprIdentifier :: new ( identifier) ) )
1717 }
1818
19+ pub fn identifier_with_type ( identifier : & str , ty : Type ) -> Self {
20+ Self :: Identifier ( Box :: new ( ExprIdentifier (
21+ identifier. to_string ( ) ,
22+ ExprIdentifier :: get_identifier_kind ( identifier) ,
23+ Some ( ty) ,
24+ ) ) )
25+ }
26+
1927 pub fn identifier_name ( & self ) -> Option < & str > {
2028 match self {
2129 Expr :: Identifier ( expr_identifier) => Some ( expr_identifier. lookup_name ( ) ) ,
@@ -152,3 +160,87 @@ impl ExprBool {
152160}
153161
154162pub type ExprS = Spanned < Expr > ;
163+
164+ pub fn add_type_to_expr_parse ( expr : & mut Expr ) {
165+ match expr {
166+ Expr :: Identifier ( expr_identifier) => match expr_identifier. identifier_kind ( ) {
167+ IdentifierKind :: Builtin => { }
168+ IdentifierKind :: Var => {
169+ expr_identifier. 2 = Some ( Type :: String ) ;
170+ }
171+ IdentifierKind :: Prompt => {
172+ expr_identifier. 2 = Some ( Type :: String ) ;
173+ }
174+ IdentifierKind :: Secret => {
175+ expr_identifier. 2 = Some ( Type :: String ) ;
176+ }
177+ IdentifierKind :: Client => {
178+ expr_identifier. 2 = Some ( Type :: String ) ;
179+ }
180+ } ,
181+ Expr :: Call ( expr_call) => {
182+ for arg in & mut expr_call. args {
183+ add_type_to_expr_parse ( & mut arg. 0 ) ;
184+ }
185+ }
186+ _ => { }
187+ }
188+ }
189+
190+ pub fn add_type_to_expr ( expr : & mut Expr , env : & CompileTimeEnv ) {
191+ match expr {
192+ Expr :: Identifier ( expr_identifier) => match expr_identifier. identifier_kind ( ) {
193+ IdentifierKind :: Builtin => {
194+ if let Some ( ( _, index) ) = env. get_builtin_index ( expr_identifier. lookup_name ( ) ) {
195+ if let Some ( v) = env. get_builtin ( index as usize ) {
196+ let v_type: Type = v. clone ( ) . into ( ) ;
197+
198+ expr_identifier. 2 = Some ( v_type) ;
199+ }
200+ } else if let Some ( ( _, index) ) =
201+ env. get_user_builtin_index ( expr_identifier. lookup_name ( ) )
202+ {
203+ if let Some ( v) = env. get_builtin ( index as usize ) {
204+ let v_type: Type = v. clone ( ) . into ( ) ;
205+
206+ expr_identifier. 2 = Some ( v_type) ;
207+ }
208+ }
209+ }
210+ IdentifierKind :: Var => {
211+ let index = env. get_var_index ( expr_identifier. lookup_name ( ) ) ;
212+
213+ if let Some ( _) = index {
214+ expr_identifier. 2 = Some ( Type :: String ) ;
215+ }
216+ }
217+ IdentifierKind :: Prompt => {
218+ let index = env. get_prompt_index ( expr_identifier. lookup_name ( ) ) ;
219+
220+ if let Some ( _) = index {
221+ expr_identifier. 2 = Some ( Type :: String ) ;
222+ }
223+ }
224+ IdentifierKind :: Secret => {
225+ let index = env. get_secret_index ( expr_identifier. lookup_name ( ) ) ;
226+
227+ if let Some ( _) = index {
228+ expr_identifier. 2 = Some ( Type :: String ) ;
229+ }
230+ }
231+ IdentifierKind :: Client => {
232+ let index = env. get_client_context_index ( expr_identifier. lookup_name ( ) ) ;
233+
234+ if let Some ( _) = index {
235+ expr_identifier. 2 = Some ( Type :: String ) ;
236+ }
237+ }
238+ } ,
239+ Expr :: Call ( expr_call) => {
240+ for arg in & mut expr_call. args {
241+ add_type_to_expr ( & mut arg. 0 , env) ;
242+ }
243+ }
244+ _ => { }
245+ }
246+ }
0 commit comments