ASPL User's Guide v 1.00
© 2024 by SetSphere.COM
The ASPL interpreter provides a scripting language and it can parse, analyze, and execute the statement of an ASPL scripts. However, how to get the kernel to load the interpeter so that the later can execute the content of the script?
The shebang, the first line that appears in the script of an interpreted language, dictates the starting point where the kernel will dispatch the interpreter, once dispatched the interpreter takes control then it interprets the statements of the script. For this to happen, the script must be executable, then the kernel reads the first two bytes in the opening line of the script, should it be matching #! then it will call exec with whatever follows #! passing any extra arguments followed (and ending) with the name of the script. On the UNIX system such an exec will also pass the parent environment to the interpreter.
Typically, the env command is used on many systems to execute whatever follows it as being the interpreter program followed by the rest of the arguments then ending with the script name. However, the parsing of the shebang line has been problematic when dissected by different UNIX vendors and even by different releases of the same vendor.
The execution of an interpreter along its script content via the kernel is typically done in two ways:
The scripting language of an interpreter typically provides a mechanism to load its scripts via the kernel of the UNIX system. This can be done in two ways:
In either case the script is loaded through the UNIX shell environment. The first case requires specifying the full path to the location of the interpreter, and may not work on some UNIX systems where the loading of the #! has been disabled. The second case may seem simple, yet we need to pay special attention considering the many UNIX systems and the fact that env loading mechanism has not been addressed by POSIX (different vendors implement the env following their needs). Furthermore some vendors provide the -S option supposedly to be followed by a string to be split into the script name and its arguments, but fail the split and present the whole string as it is: one long string that is assumed to be the interpreter name (hence failing again with "no such script" when concatenating the arguments to the script name). In addition, the location of the env command may vary between systems: the env program may be located in /usr/bin, or /bin, or somewhere else.
Since all UNIX vendors (at least) load what follows the env as a single string pointing to the interpreter named program, then it is possible to generalize a solution to the loading of the interpreter and its arguments. To overcome the env loading issues and to make ASPL portable on different UNIX systems, ASPL uses a two-lines shebangs line: the first line is to read the interpreter name, and the second line is to pass the arguments of the program. Note that the script name is the program name itself and is implicitly passed by env as $0 shell variable.
This chapter shows some scripting examples that can be run by the ASPL interpreter. All scripts can be found in the shared folder where you have installed the ASPL interpreter.
■ Script randomdice.aspl
The following ASPL script simulates three players rolling dice on a craptable. You run the script at the UNIX prompt by following it by the number of throws. Line 1 and line 2 represent a two lines shebang to invoke the interpreter on the UNIX system. Lines 20 to 22 create three set variables, p1 p2 p3, and assign them to the grouping data created by the global grouping function ggdice().
[Top Text]
1. #!/usr/bin/env aspl 2. #ENVARG= -wsname TRANSIENT -groupingclass DICE 3. 4. ;;*********************************************************************** 5. ;; randomdice.aspl 6. ;; Simulate three players throwing dice on a craptable. 7. ;; ASPL, A Set Programming Language 8. ;; Copyright © 2021-2024 Bassem W. Jamaleddine 9. ;; All rights reserved. 10. ;;*********************************************************************** 11. 12. endScriptIfShellArgsLessThan 1 13. 14. displayoff 15. 16. print SIMULATION FOR 3 PLAYERS THROWING $1 TIMES DICE ON A CRATABLE 17. println 18. ;;retrials for a face with 5 dots on one die, and a face with 3 dots on the other 19. ;;if $2 and $3 are not passed, the GG'function will set them to default 1 1 20. p1 = ggdice(player,player1,throws,$1,die1trials, 5 $2,die2trials, 3 $3) 21. p2 = ggdice(player,player2,throws,$1,die1trials, 5 $2,die2trials, 3 $3) 22. p3 = ggdice(player,player3,throws,$1,die1trials, 5 $2,die2trials, 3 $3) 23. 24. ;; set ks vector 25. ks faces face1 face2 chksum entropy ppdd ffl aelm 26. 27. displayon 28. 29. print #################################################################### 30. print # SHOW THE THROW NUMBERS WHEN p1 AND p2 HAVE ABSOULTELY THE SAME OUTCOME 31. print #################################################################### 32. fU`ks= p1 p2 33. ;; similarities on throw number (ffl) along the ks (z) vector 34. print #################################################################### 35. print # SIMILARITY WHEN p1 AND p2 HAVE ABSOULTELY THE SAME OUTCOME 36. print #################################################################### 37. sim`fflz p1 p2 38. 39. print #################################################################### 40. print # SHOW THE THROW NUMBERS WHEN ALL 3 PLAYERS HAVE ABSOULTELY THE SAME OUTCOME 41. print #################################################################### 42. fU`ks= p1 p2 p3 43. ;; similarities on throw number (ffl) along the ks (z) vector 44. print #################################################################### 45. print # SIMILARITY WHEN ALL 3 PLAYERS HAVE ABSOULTELY THE SAME OUTCOME 46. print #################################################################### 47. sim`fflz p1 p2 p3 48. 49. print #################################################################### 50. print # SHOW THE THROW NUMBERS WHEN ALL 3 PLAYERS HAVE THE SAME SUM 51. print #################################################################### 52. fU`c= p1 p2 p3 53. print #################################################################### 54. print # SIMILARITY WHEN ALL 3 PLAYERS HAVE SAME SUM 55. print #################################################################### 56. ;; similarities on throw number (ffl) along the chksum (c) 57. sim`fflc p1 p2 p3 58. 59. endscript 60. 61. __END__ 62. 63. $00 must be followed by three arguments: 64. number of throws, retrial on one die, retrial on the other die 65. 66. This script does the simulation of three players throwing dice on a craptable. 67. The script takes three arguments representing the number of throws, the number 68. of retrials for a specific face on one die, and the number of retrials for a 69. specific face on the other one. 70. Set the retrials to 1 and 1 for a fair dice roll. 71. Increasing the retrials cause the similarity to increase and the entropy to 72. decrease (since certainty is higher when bias increases). 73. The script prints the result of all three players that have the same sum, and also 74. prints when they have absolutely the same outcome (on both faces of the dice). 75. 76. simulate 900 throws for the three players 77. (1) $00 900 78. 79. simulate 900 throws for the three players retrying once each of the die 80. (2) $00 900 1 1 81. then (1) and (2) are equivalent 82. 83. simulate 900 throws for the three players retrying 3 times the one die and 4 times the other 84. $00 900 3 4 85. increase the retrials and the entropy will decrease as the certainty increases (that is 86. it is more likely to guess the outcome). 87. 88. NOTE: * to see the script trace as it is being processed by the interpreter add the -SCT 89. option on the command line 90. $00 1300 -SCT 91.