@@ -789,15 +789,14 @@ impl Generate for IfCommand {
789789 //
790790 // This would allow us to remove the NVIM9.bool condition (assuming the type
791791 // system is correct in vim9script)
792- //
793- // let condition = match guess_type_of_expr(state, &self.condition).inner {
794- // Type::Bool => condition,
795- // _ => format!("NVIM9.bool({condition})"),
796- // };
792+ let condition = match guess_type_of_expr ( state, & self . condition ) {
793+ Type :: Bool => condition,
794+ _ => format ! ( "NVIM9.bool({condition})" ) ,
795+ } ;
797796
798797 format ! (
799798 r#"
800- if NVIM9.bool( {condition}) then
799+ if {condition} then
801800 {}
802801{}
803802{}
@@ -881,36 +880,65 @@ fn identifier_list(state: &mut State, unpacked: &UnpackIdentifier) -> String {
881880 . collect ( )
882881}
883882
883+ fn vim_to_type ( ret : & vimfuncs:: FuncReturnType ) -> Type {
884+ match ret {
885+ vimfuncs:: FuncReturnType :: Any => Type :: Any ,
886+ vimfuncs:: FuncReturnType :: Bool => Type :: Bool ,
887+ vimfuncs:: FuncReturnType :: Float => Type :: Float ,
888+ vimfuncs:: FuncReturnType :: Number => Type :: Number ,
889+ vimfuncs:: FuncReturnType :: NumberBool => Type :: BoolOrNumber ,
890+ vimfuncs:: FuncReturnType :: String => Type :: String ,
891+ // vimfuncs::FuncReturnType::Void => Type::Any,
892+ // vimfuncs::FuncReturnType::Dict(_) => Type,
893+ // vimfuncs::FuncReturnType::List(_) => todo!(),
894+ // vimfuncs::FuncReturnType::Func(_) => todo!(),
895+ _ => Type :: Any ,
896+ }
897+ }
898+
884899fn guess_type_of_expr ( state : & State , expr : & Expression ) -> Type {
885900 match expr {
886901 Expression :: Number ( _) => Type :: Number ,
887902 Expression :: String ( _) => Type :: String ,
888903 Expression :: Boolean ( _) => Type :: Bool ,
889- Expression :: Call ( c) => match c. name ( ) {
890- Some ( ident) => match ident {
891- // TODO: Use our very cool new generated stuff here
892- Identifier :: Raw ( raw) => match raw. name . as_str ( ) {
893- "charcol" => Type :: Number ,
894- _ => Type :: Any ,
895- } ,
904+ Expression :: Call ( c) => {
905+ match c. name ( ) {
906+ Some ( ident) => {
907+ match ident {
908+ // TODO: Use our very cool new generated stuff here
909+ Identifier :: Raw ( raw) => {
910+ match vimfuncs:: get_func_info ( & raw . name) {
911+ Some ( info) => vim_to_type ( & info. return_type ) ,
912+ _ => Type :: Any ,
913+ }
914+ }
915+ _ => Type :: Any ,
916+ }
917+ }
896918 _ => Type :: Any ,
897- } ,
898- _ => Type :: Any ,
899- } ,
919+ }
920+ }
900921 Expression :: Infix ( infix) => {
901922 if infix. operator . is_comparison ( ) {
902923 return Type :: Bool ;
903924 }
904925
926+ let left_ty = guess_type_of_expr ( state, infix. left . as_ref ( ) ) ;
927+ let right_ty = guess_type_of_expr ( state, infix. right . as_ref ( ) ) ;
928+
905929 if matches ! ( infix. operator, Operator :: And | Operator :: Or ) {
906- if guess_type_of_expr ( state, infix. left . as_ref ( ) ) == Type :: Bool
907- && guess_type_of_expr ( state, infix. right . as_ref ( ) )
908- == Type :: Bool
909- {
930+ if left_ty == Type :: Bool && right_ty == Type :: Bool {
910931 return Type :: Bool ;
911932 }
912933 }
913934
935+ if left_ty == Type :: Number
936+ && right_ty == Type :: Number
937+ && infix. operator . is_math ( )
938+ {
939+ return dbg ! ( Type :: Number ) ;
940+ }
941+
914942 Type :: Any
915943 }
916944 Expression :: Grouped ( g) => guess_type_of_expr ( state, g. expr . as_ref ( ) ) ,
@@ -1348,6 +1376,11 @@ impl Generate for InfixExpression {
13481376 if let Some ( literal) = self . operator . literal ( ) {
13491377 match ty {
13501378 Bool => return format ! ( "{left} {literal} {right}" , ) ,
1379+ Number => {
1380+ if self . operator . is_math ( ) {
1381+ return format ! ( "{left} {literal} {right}" ) ;
1382+ }
1383+ }
13511384 _ => { }
13521385 }
13531386 }
0 commit comments