Author Topic: String sigils optional  (Read 435 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1431
    • View Profile
String sigils optional
« on: June 03, 2022, 06:46:35 PM »
Have you ever wished that BCX could detect and correctly process UDT string variables
without needing you to always place a dollar sign "$" on the UDT string variable?

TYPE FOO
         MYNAME AS STRING
END TYPE


DIM A AS FOO
A.MYNAME = "MrBcx"  ' <= Like this
PRINT A.MYNAME        ' <= and this


Well, I finally worked it out.  Today I wrote the following internal BCX function
that makes detecting UDT string variables simple, so now the example
above correctly translates with my beta version of BCX 7.8.9. 

Code: [Select]

FUNCTION IM_UDT_STRING(A$)
   DIM VariName$, Member$
   VariName$ = EXTRACT$(A$,".")                ' Variable Name  ex.  A.tree  => "A"
   Member$ = MID$(A$,1 + INSTR(A$,"."))        ' Member Name    ex.  A.tree  => "tree"
   IF CheckType(VariName$) = vt_UDT OR CheckType(VariName$) = vt_STRUCT THEN
      FOR INT y = 1 TO TypeDefsCnt
         FOR INT z = 0 TO TypeDefs[y].EleCnt
            IF TypeDefs[y].Elements[z].ElementName$ = Member$   AND _
               TypeDefs[y].Elements[z].ElementType  = vt_STRVAR THEN
               FUNCTION = TRUE
            END IF
         NEXT
      NEXT
   END IF
   FUNCTION = FALSE
END FUNCTION








MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1431
    • View Profile
Re: String sigils optional
« Reply #1 on: June 05, 2022, 12:39:07 PM »
I had so much luck with IM_UDT_STRING () that I decided to clone it to
IM_UDT_SINGLE() and IM_UDT_DOUBLE.  I've added those new functions
to the print and input internal BCX parsing routines, so that now we can
do the following and expect to get the correct results:


TYPE FOO
   MYNAME AS STRING
   a AS SINGLE
   b AS DOUBLE
END TYPE

DIM A AS FOO

INPUT "Name? "  , A.MYNAME
INPUT "SINGLE? ", A.a
INPUT "DOUBLE? ", A.b

PRINT A.MYNAME              
PRINT A.a                    
PRINT A.b                    


dgarner

  • Newbie
  • *
  • Posts: 39
    • View Profile
Re: String sigils optional
« Reply #2 on: June 05, 2022, 03:44:57 PM »
This seems like a great improvement to me.  I recall banging my head on this type of problem several times.

Thanks for the effort.

Robert

  • Hero Member
  • *****
  • Posts: 966
    • View Profile
Re: String sigils optional
« Reply #3 on: June 05, 2022, 04:46:22 PM »
This seems like a great improvement to me.  I recall banging my head on this type of problem several times.

Thanks for the effort.

 ;D +1 8)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1431
    • View Profile
Re: String sigils optional
« Reply #4 on: June 05, 2022, 08:29:03 PM »
Thanks for the moral support fellas.

Today was a head-banger but, at the end, I think I'm winning.

Below is one of my finance examples from long ago.  I removed
all the sigils from the source code, translated, compiled and got
correct results.



Code: [Select]

$INCLUDE "FINANCE.INC"

DIM Fmt          AS STRING

DIM Z            AS DOUBLE
DIM Interest     AS DOUBLE
DIM NumPeriods   AS DOUBLE
DIM PresentValue AS DOUBLE
DIM FutureValue  AS DOUBLE

Fmt  = "$ #.##"

CLS
PRINT " Sample Run: "
PRINT " ----------------------------------"
PRINT " Annual Interest Rate... 12"
PRINT " Number of Payments.. .. 48"
PRINT " Present Value ......... 0"
PRINT " Future Value .......... 10000"
PRINT " ----------------------------------"
PRINT " Payments = $ 163.34"
PRINT " ----------------------------------"
PRINT ""

INPUT " Annual Interest Rate... " , Interest
INPUT " Number of Payments.. .. " , NumPeriods
INPUT " Present Value ......... " , PresentValue
INPUT " Future Value .......... " , FutureValue

Z   =  PMT (Interest,NumPeriods,PresentValue,FutureValue)

PRINT " ----------------------------------"
PRINT " Payments = ", USING$(Fmt$,Z)



Code: [Select]

int main(int argc, char *argv[])
{
  hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
  LowCase=(unsigned char*)calloc(257,1), LowCase = MakeLCaseTbl();
  UprCase=(unsigned char*)calloc(257,1), UprCase = MakeUCaseTbl();
  strcpy(Fmt,"$ #.##");
  cls();
  printf("%s\n"," Sample Run: ");
  printf("%s\n"," ----------------------------------");
  printf("%s\n"," Annual Interest Rate... 12");
  printf("%s\n"," Number of Payments.. .. 48");
  printf("%s\n"," Present Value ......... 0");
  printf("%s\n"," Future Value .......... 10000");
  printf("%s\n"," ----------------------------------");
  printf("%s\n"," Payments = $ 163.34");
  printf("%s\n"," ----------------------------------");
  printf("%s\n","");
  printf(" Annual Interest Rate... "), Interest=0, KBinput(), ScanError=scan(InputBuffer,"%lG",&Interest);
  printf(" Number of Payments.. .. "), NumPeriods=0, KBinput(), ScanError=scan(InputBuffer,"%lG",&NumPeriods);
  printf(" Present Value ......... "), PresentValue=0, KBinput(), ScanError=scan(InputBuffer,"%lG",&PresentValue);
  printf(" Future Value .......... "), FutureValue=0, KBinput(), ScanError=scan(InputBuffer,"%lG",&FutureValue);
  Z=PMT(Interest,NumPeriods,PresentValue,FutureValue);
  printf("%s\n"," ----------------------------------");
  printf("%s%s\n"," Payments = ",Using(Fmt,Z));
  return EXIT_SUCCESS;   // End of main program
  }



MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1431
    • View Profile
Re: String sigils optional
« Reply #5 on: June 06, 2022, 11:21:27 AM »
My latest efforts are bearing fruit. 

The following example could never translate correctly with previous versions of BCX.
BCX 789 (beta) translates it, and it compiles and runs predictably and correctly.  Pay
particular attention that there are NO sigils ( ! # $ ) in the example source code.

One idea that I had today is copying a UDT to a local copy and then copying it back.

This allows my enhanced datatype detection code to be used with UDT's passed as SUB
and FUNCTION arguments.  Also, copying UDT arguments to a local and then copying it
back allows us to use the "." operator instead of the "->" operator with those UDT
arguments.

The example below demonstrates what will be possible when I release 789.

Code: [Select]

TYPE UDTFOO
   a AS DOUBLE
   b AS INT
   c AS STRING
END TYPE

DIM F AS UDTFOO

F.a = 1.123456789
F.b = 2
F.c = "Hello World"

PRINT "These are our initial values:"
PRINT F.a
PRINT F.b
PRINT F.c
PRINT

CALL TestUDT (ADDRESSOF(F))

PRINT
PRINT "These were passed back from the SUB:"
PRINT F.a
PRINT F.b
PRINT F.c
PRINT


SUB TestUDT (z AS LPUDTFOO)
   DIM K AS UDTFOO

   COPY_UDT(z, ADDRESSOF(K), SIZEOF(UDTFOO))

   PRINT "These were passed to this SUB"
   PRINT K.a
   PRINT K.b
   PRINT K.c
   PRINT

   INPUT "Enter a DOUBLE:  ", K.a
   INPUT "Enter a INTEGER: ", K.b
   INPUT "Enter a STRING:  ", K.c

   COPY_UDT(ADDRESSOF(K), z, SIZEOF(UDTFOO))
END SUB


SUB COPY_UDT (Source AS PVOID, Destination AS PVOID, Count AS INT)
   POKE(Destination, PEEK$(Source, Count), Count)
END SUB






OUTPUT:


These are our initial values:
 1.123456789
 2
Hello World

These were passed to this SUB
 1.123456789
 2
Hello World

Enter a DOUBLE:  3.333
Enter a INTEGER: 10000
Enter a STRING:  This is useful!

These were passed back from the SUB:
 3.333
 10000
This is useful!




« Last Edit: June 06, 2022, 11:39:01 AM by MrBcx »

jbk

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Re: String sigils optional
« Reply #6 on: June 06, 2022, 08:46:26 PM »
looking good  :)