Jump to content











Photo
- - - - -

[Release] pcalc.g4b i.e. calc with parenthesis


  • Please log in to reply
9 replies to this topic

#1 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 04 August 2021 - 04:13 PM

As usually half-@§§ed but seemingly working.

 

calc (or set /a /A) in grub4dos is a simple calculator from left to right, pcalc.g4b adds evaluation of parenthesis (brackets) which may be useful either in more complex formulas or for not needing to re-order an expression.

 

No instructions, no error control (make sure that opening and closing brackets match), no frills, you need to know where your towel is, more a POC than anything else.

!BAT
setlocal
set string=%*
:begin
set string
set /a counter=0 > nul
set /a open=0 > nul
set /a close=0 > nul
set /a length=0  > nul
:loop
call set char=%^string:~%counter%,1%
if %char%.==(. set /a open=%counter%  > nul
if %char%.==). set /a close=%counter%  > nul
set /a length=%close%-%open%  > nul
if %length%>=1 goto :do_math
set /a counter=%counter%+1 > nul
if not "%char%."=="." goto :loop
set /a result=%string%
endlocal && set result=%result%
goto :eof


:do_math
set /a start=%open%+1 > nul
set /a end=%close%+1 > nul
set /a length=%end%-%start%-1 > nul
call set between=%^string:~%start%,%length%%%
call set before=%^string:~0,%open%%%
call set after=%^string:~%end%%%
set /a result=%between%  > nul
set string= %before%%%result%%%after%
goto :begin
goto :eof

:duff:

Wonko

Attached Files



#2 deomsh

deomsh

    Frequent Member

  • Advanced user
  • 196 posts
  •  
    Netherlands

Posted 04 August 2021 - 11:23 PM

Congratulations, works terrific so far. :)

If you like I can post a screenshot of to tests on Imgur...



#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 05 August 2021 - 07:47 AM

Good, thanks :)

 

About imgur, it is IMHO not the best idea, it is not accessible by a number of older browsers or with javascript disabled, you should now have the possibility to attach to the post an image :unsure:.

 

I put down this thing because I wanted to test the "canonical" math formula to invert between Little and Big Endian (as opposed to the usual one based on the hex string), and it works, but the formula is so long that I doubt it can ever have practical uses, unless, for *some reasons*, you don't have (or don't want) the hex representation of the number.

 

i..e.:

 

 

set /a num=305419896

305419896 (HEX:0x12345678)

 

#but this works as well

set num=305419896

 

/pcalc.g4b (%num%&0xFF<<24)+(%num%&0xFF00<<8)+(%num%&0xFF0000>>8)+(%num%&0xFF000000>>24)

...

...

2018915346 (HEX:0x78564321) (HEX:0x78563421)

 

:duff:

Wonko



#4 deomsh

deomsh

    Frequent Member

  • Advanced user
  • 196 posts
  •  
    Netherlands

Posted 05 August 2021 - 10:22 PM

As usually half-@§§ed but seemingly working.

 

calc (or set /a /A) in grub4dos is a simple calculator from left to right, pcalc.g4b adds evaluation of parenthesis (brackets) which may be useful either in more complex formulas or for not needing to re-order an expression.

 

No instructions, no error control (make sure that opening and closing brackets match), no frills, you need to know where your towel is, more a POC than anything else.

I did some more tests, PCALC.G4B has no problems with 2x 40 parentheses.

 

Only spaces doesn't work, don't know if that is a problem with calc ?

Can be easily cured with use of double qoutes in the four call set lines.

 

Further I have a question about lines like: if %char%.==(. set /a open=%counter% > nul . Are the dot's used because in the end %char% will not exist anymore, so to avoid Error 30?
 

I found counting the parentheses not always easy. For instance following lines just before :begin seems helpful:

pause --wait=0 %string% > (md)0x300+1
cat --locate=( (md)0x300+1 > nul ;; set /a leftpar=%@retval% > nul
cat --locate=) (md)0x300+1 > nul ;; set /a rightpar=%@retval% > nul
if not %leftpar%==%rightpar% && echo Abort: number of Left Parentheses=%leftpar% and number of Right Parentheses=%rightpar% && endlocal && echo -n > (md)0x300+1 && goto :eof

About uploading images: I tried to paste image content, but i get You are not allowed to use that image extension on this community. ??? I don't see any button to upload images....

 

About the Big/ Little-Endian conversion: interesting, but the result seems not fully good, after first two GOOD (0x7856) Little-Endian-bytes. Or am I wrong? But I have to study the formula more closely....

...



#5 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 06 August 2021 - 06:45 AM

Yep, no spaces, I don't think that it is a calc issue, but rather my batch, anyway spaces are tolerated in calc but not for some operators, like < and &, so not having them is a good idea anyway.

 

The "dot after" is only a good ol' trick that avoids issues when (if) the variable is empty (not defined), more correctly it could be if "%char%"=="(" ...

 

You should be able to attach a picture (.jpg or .bmp, maybe also .png) as a file, the board will show it as a (clickable/enlargeable) miniature.

 

I am not sure to understand what you find "not fully good" in the conversion. :unsure:

 

Basically you "isolate" each byte and then you shift it left of right of the (right) offset.

you could use multiplication and division instead of shift operator, to the same result.

I am attaching a couple screnshots (images as files).

 

Or am I missing something? :dubbio:

 

:duff:

Wonko

 

P.S.: Ok, now I see it, copy error (typo), corrected.

Attached Thumbnails

  • pcalc_test.jpg
  • pcalc_test2.jpg


#6 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 07 August 2021 - 08:24 AM

EXCLUSIVELY for the fun of it:



!BAT
#chendian.g4b simple batch to change endianness of a 32 bit number
#(ab)using grub4dos 64 bits numbers and labels and only one variable
setlocal
set /A num=%1 > nul
set num
call :chend32 48 32 16
set num
goto :eof



:chend32
:chend16
set /A num=%num%&0xFF<<%1+%num% > nul
set /A num=%num%>>8 > nul
shift
goto :chend%1
:chend
goto :eof

:duff:

Wonko


  • deomsh likes this

#7 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 09 August 2021 - 09:38 AM

Still only for the fun of it, the most "compact" version I could come out with, "self contained" and suitable to be included in a .lll file:

!BAT
#revend.g4b simple batch to change endianness of a 32 bit number
#no variable but "result" used
calc %1&0xFF<<24 > nul
calc %1>>8&0xFF<<16+%@retval%  > nul
calc %1>>16&0xFF<<8+%@retval%  > nul
set /A result=%1>>24&0xFF+%@retval%
goto :eof

:duff:

Wonko



#8 deomsh

deomsh

    Frequent Member

  • Advanced user
  • 196 posts
  •  
    Netherlands

Posted 09 August 2021 - 10:04 PM

Nice, works like a charm :)

 

I made following first version (!) of a .lll-function: RevEndian. I think 'Rev' can be considerd as a (short/ abbreviated) name of a class. Or would 'Cnv' suits better?

For instance I am working on a Cnv-function to convert text-files to other EOL-formats (needed for my line-editor).

!BAT
#REVEND.LLL v0.1 (20210808), by deomsh. Used script: REVEND.G4B, created by Wonko the Sane (see last #)
#Function: Simple batch to change endianness of a 32 bit number
#Use: REVEND.LLL RevEndian N
#Mandatory Arguments: N
#Optional Arguments:
#Remarks: Function name not case-sensitive. N is max 32 bit value
#debug 0
#echo %*
call :%~1 %*
echo Function: %~1 result=%result%
goto :eof

:RevEndian
setlocal && set *
calc %~2&0xFF<<24 > nul
calc %~2>>8&0xFF<<16+%@retval% > nul
calc %~2>>16&0xFF<<8+%@retval% > nul
set /A result=%~2>>24&0xFF+%@retval% > nul
endlocal && set result=%result%
goto :eof

#!BAT #revend.g4b simple batch to change endianness of a 32 bit number #no variable but "result" used calc %1&0xFF<<24 > nul calc %1>>8&0xFF<<16+%@retval% > nul calc %1>>16&0xFF<<8+%@retval% > nul set /A result=%1>>24&0xFF+%@retval% goto :eof

BTW to have endless linking/ porting possibilities, %1 should not be used in sub-routines.  :blink:

BTW2 While copy/ paste the script, all EOL-formatting was lost. Because it's short, I added the original (without EOL's) as remark. Is  this a good idea? Don't know habits/ issues on reboot.pro. Are scripts intellectual property? 
 


Edited by deomsh, 09 August 2021 - 10:19 PM.


#9 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 10 August 2021 - 08:31 AM

Nice, works like a charm :)

 
Sure, you mean you had a doubt about it? :w00t:
;)
 
 
 

BTW2 While copy/ paste the script, all EOL-formatting was lost. Because it's short, I added the original (without EOL's) as remark. Is  this a good idea?

Yep, sometimes for *whatever reasons* working on a post the text inside code qualifiers goes astray, it is a problem with the(crappy) board software.
Typically if you edit a post with code (even without touching the code part) a line feed is inserted at the top of the code (once every time you edit the post) then, if you try to remove it, ALL the code gets crumpled together. :(
 

Don't know habits/ issues on reboot.pro. Are scripts intellectual property?

All the (half-@§§ed) scripts/snippets I post are licensed under my careware license, JFYI:
https://enterprise.d...no#license-text
(not very restrictive ;) )

:duff:
Wonko

#10 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 17 August 2021 - 09:05 AM

Before I forget, a side-side note.

 

While it generally makes little sense in small self-standing batches, in multi-fuction libraries like these .lll's or in longish, more complex batches, it would be advisable to remove the "> nul" redirections on set /a and similar and use at the beginning after setlocal a line with debug msg=0 and restore when you endlocal with debug msg=1.

 

Basically you add 2*13(including CR+LF)=26 bytes that can be essentially counterbalanced by only 4*6=24  removed " > nul", so each further removed one is a 6 bytes "credit". 

 

:duff:

Wonko


  • deomsh likes this




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users