streamlined q-form related calls
This commit is contained in:
+2
-2
@@ -468,7 +468,7 @@ func NewClyx(libfname string, libCode string) *Clyx {
|
|||||||
c.words["loop"] = func() { body, cond := c.pop().([]any), c.pop().([]any); for { c.execQForm(cond); if !isTruthy(c.pop()) { break }; c.execQForm(body) } }
|
c.words["loop"] = func() { body, cond := c.pop().([]any), c.pop().([]any); for { c.execQForm(cond); if !isTruthy(c.pop()) { break }; c.execQForm(body) } }
|
||||||
c.words["hsargs"] = func() { c.stack = append(c.stack, toAnySlice(os.Args)) }
|
c.words["hsargs"] = func() { c.stack = append(c.stack, toAnySlice(os.Args)) }
|
||||||
c.words["hsexit"] = func() { os.Exit(int(toInt(c.pop()))) }
|
c.words["hsexit"] = func() { os.Exit(int(toInt(c.pop()))) }
|
||||||
c.words["l2s"] = func() {
|
c.words["q2s"] = func() {
|
||||||
l := c.pop().([]any)
|
l := c.pop().([]any)
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
for _, val := range l {
|
for _, val := range l {
|
||||||
@@ -512,7 +512,7 @@ func NewClyx(libfname string, libCode string) *Clyx {
|
|||||||
regMath("exp", math.Exp)
|
regMath("exp", math.Exp)
|
||||||
regMath("log", math.Log)
|
regMath("log", math.Log)
|
||||||
|
|
||||||
c.Run(`"::" [ next defw ] defw :: readf [ "r" fopen dup read swap close ] :: src [ readf l2s i ]`)
|
c.Run(`"::" [ next defw ] defw :: readf [ "r" fopen dup read swap close ] :: src [ readf q2s i ]`)
|
||||||
|
|
||||||
if libfname == "" {
|
if libfname == "" {
|
||||||
c.Run(libCode)
|
c.Run(libCode)
|
||||||
|
|||||||
@@ -69,13 +69,13 @@ class Clyx:
|
|||||||
'loop': lambda: (lambda body, cond: self._exec_loop(cond, body))(self._stack.pop(), self._stack.pop()),
|
'loop': lambda: (lambda body, cond: self._exec_loop(cond, body))(self._stack.pop(), self._stack.pop()),
|
||||||
'hsargs': lambda: self._stack.append(list(sys.argv)),
|
'hsargs': lambda: self._stack.append(list(sys.argv)),
|
||||||
'hsexit': lambda: sys.exit(int(self._stack.pop())),
|
'hsexit': lambda: sys.exit(int(self._stack.pop())),
|
||||||
'l2s': lambda: self._stack.append("".join(chr(int(x)) for x in self._stack.pop())),
|
'q2s': lambda: self._stack.append("".join(chr(int(x)) for x in self._stack.pop())),
|
||||||
'hseval': lambda: self._stack.append(eval(self._stack.pop())),
|
'hseval': lambda: self._stack.append(eval(self._stack.pop())),
|
||||||
'chr': lambda: self._stack.append(chr(int(self._stack.pop()))),
|
'chr': lambda: self._stack.append(chr(int(self._stack.pop()))),
|
||||||
'sin': lambda: self._stack.append(math.sin(float(self._stack.pop()))), 'cos': lambda: self._stack.append(math.cos(float(self._stack.pop()))), 'atan': lambda: self._stack.append(math.atan(float(self._stack.pop()))),
|
'sin': lambda: self._stack.append(math.sin(float(self._stack.pop()))), 'cos': lambda: self._stack.append(math.cos(float(self._stack.pop()))), 'atan': lambda: self._stack.append(math.atan(float(self._stack.pop()))),
|
||||||
'exp': lambda: self._stack.append(math.exp(float(self._stack.pop()))), 'log': lambda: self._stack.append(math.log(float(self._stack.pop())))
|
'exp': lambda: self._stack.append(math.exp(float(self._stack.pop()))), 'log': lambda: self._stack.append(math.log(float(self._stack.pop())))
|
||||||
}
|
}
|
||||||
self.run('"::" [ next defw ] defw :: readf [ "r" fopen dup read swap close ] :: src [ readf l2s i ]')
|
self.run('"::" [ next defw ] defw :: readf [ "r" fopen dup read swap close ] :: src [ readf q2s i ]')
|
||||||
self.run(f'"{libpath}" src' if libfname is None else (libfname + ' src'))
|
self.run(f'"{libpath}" src' if libfname is None else (libfname + ' src'))
|
||||||
|
|
||||||
def _cmd_write(self):
|
def _cmd_write(self):
|
||||||
|
|||||||
+4
-4
@@ -117,13 +117,13 @@ Any line fragment starting with `#` to the end of the physical source line is re
|
|||||||
- **Stack Effect**: `( s -- code )`
|
- **Stack Effect**: `( s -- code )`
|
||||||
- **Description**: Converts the first character of the string `s` to its integer ASCII character code.
|
- **Description**: Converts the first character of the string `s` to its integer ASCII character code.
|
||||||
|
|
||||||
### `s2l` (String to Q-form)
|
### `s2q` (String to Q-form)
|
||||||
- **Stack Effect**: `( val -- [q] )`
|
- **Stack Effect**: `( val -- [q] )`
|
||||||
- **Description**: Converts the value `val` (if it is a string) to a Q-form of its byte values/integers. If `val` is already a Q-form, it does nothing.
|
- **Description**: Converts the value `val` (if it is a string) to a Q-form of its byte values/integers. If `val` is already a Q-form, it does nothing.
|
||||||
|
|
||||||
### `l2s` (Q-form to String)
|
### `q2s` (Q-form to String)
|
||||||
- **Stack Effect**: `( [q] -- s )`
|
- **Stack Effect**: `( [q] -- s )`
|
||||||
- **Description**: Converts the Q-form of integers `[q]` (character/byte codes) into its string representation `s`. Reverts the operation of `s2l`.
|
- **Description**: Converts the Q-form of integers `[q]` (character/byte codes) into its string representation `s`. Reverts the operation of `s2q`.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -337,7 +337,7 @@ These words are defined in Clyx itself during the interpreter's bootstrap phase:
|
|||||||
|
|
||||||
### `src` (Source)
|
### `src` (Source)
|
||||||
- **Stack Effect**: `( filename -- )`
|
- **Stack Effect**: `( filename -- )`
|
||||||
- **Description**: Reads the file `filename` using `readf`, converts the Q-form of bytes to a string using `l2s`, and runs the resulting Clyx code in real-time using `i`.
|
- **Description**: Reads the file `filename` using `readf`, converts the Q-form of bytes to a string using `q2s`, and runs the resulting Clyx code in real-time using `i`.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
:: neg [ 0 swap - ]
|
:: neg [ 0 swap - ]
|
||||||
:: 1/ [ 1 swap / ]
|
:: 1/ [ 1 swap / ]
|
||||||
:: shr [ neg shl ]
|
:: shr [ neg shl ]
|
||||||
:: readln [ 0 "r" fopen read l2s ]
|
:: readln [ 0 "r" fopen read q2s ]
|
||||||
:: writef [ "w" fopen dup rot write swap close ]
|
:: writef [ "w" fopen dup rot write swap close ]
|
||||||
:: nlstn [ "0.0.0.0" swap nopen ]
|
:: nlstn [ "0.0.0.0" swap nopen ]
|
||||||
:: = [ - 0= ]
|
:: = [ - 0= ]
|
||||||
@@ -35,15 +35,15 @@
|
|||||||
:: isstr [ type 1 = ]
|
:: isstr [ type 1 = ]
|
||||||
:: isq [ type 2 = ]
|
:: isq [ type 2 = ]
|
||||||
:: vlen [ type 2 = if [ qlen ] [ slen ] ]
|
:: vlen [ type 2 = if [ qlen ] [ slen ] ]
|
||||||
:: s2l [ type 2 = if [] [ dup slen 0 = if [ drop [] ] [ uncons drop swap cons ] ] ]
|
:: s2q [ type 2 = if [] [ dup slen 0 = if [ drop [] ] [ uncons drop swap cons ] ] ]
|
||||||
:: asc [ s2l 0 qget ]
|
:: asc [ s2q 0 qget ]
|
||||||
:: lcat [ [ rev ] dip swap uncons while [ [ swap ] dip swap cons swap uncons ] drop ]
|
:: lcat [ [ rev ] dip swap uncons while [ [ swap ] dip swap cons swap uncons ] drop ]
|
||||||
:: streq [ dup vlen [ swap dup vlen ] dip = if [ 1 while [ uncons if [ [ swap uncons drop ] dip = if [ dup qlen 0 = if [ drop drop 1 0 ] [ swap 1 ] ] [ drop drop 0 0 ] ] [ drop drop 1 0 ] ] ] [ drop drop 0 ] ]
|
:: streq [ dup vlen [ swap dup vlen ] dip = if [ 1 while [ uncons if [ [ swap uncons drop ] dip = if [ dup qlen 0 = if [ drop drop 1 0 ] [ swap 1 ] ] [ drop drop 0 0 ] ] [ drop drop 1 0 ] ] ] [ drop drop 0 ] ]
|
||||||
:: lower [ "" swap uncons while [ dup 65 >= [ dup 90 <= ] dip and if [ 32 + ] [] [ swap ] dip chr s+ swap uncons ] drop l2s ]
|
:: lower [ "" swap uncons while [ dup 65 >= [ dup 90 <= ] dip and if [ 32 + ] [] [ swap ] dip chr s+ swap uncons ] drop q2s ]
|
||||||
:: upper [ "" swap uncons while [ dup 97 >= [ dup 122 <= ] dip and if [ 32 - ] [] [ swap ] dip chr s+ swap uncons ] drop l2s ]
|
:: upper [ "" swap uncons while [ dup 97 >= [ dup 122 <= ] dip and if [ 32 - ] [] [ swap ] dip chr s+ swap uncons ] drop q2s ]
|
||||||
:: bitnot [ dup nand ]
|
:: bitnot [ dup nand ]
|
||||||
:: bitand [ nand bitnot ]
|
:: bitand [ nand bitnot ]
|
||||||
:: bitor [ bitnot swap bitnot nand ]
|
:: bitor [ bitnot swap bitnot nand ]
|
||||||
:: bitxor [ over over bitnot bitand [ swap bitnot bitand ] dip bitor ]
|
:: bitxor [ over over bitnot bitand [ swap bitnot bitand ] dip bitor ]
|
||||||
:: rev [ [] swap uncons while [ [ swap ] dip swap cons swap uncons ] drop ]
|
:: rev [ [] swap uncons while [ [ swap ] dip swap cons swap uncons ] drop ]
|
||||||
:: s+ [ s2l [ s2l ] dip lcat ]
|
:: s+ [ s2q [ s2q ] dip lcat ]
|
||||||
|
|||||||
@@ -159,16 +159,16 @@ if [
|
|||||||
"AbC" lower "abc" streq
|
"AbC" lower "abc" streq
|
||||||
"aBc" upper "ABC" streq and
|
"aBc" upper "ABC" streq and
|
||||||
"abc" "def" s+ "abcdef" streq and
|
"abc" "def" s+ "abcdef" streq and
|
||||||
"abc" s2l [ 97 98 99 ] streq and
|
"abc" s2q [ 97 98 99 ] streq and
|
||||||
"abc" s2l l2s "abc" streq and
|
"abc" s2q q2s "abc" streq and
|
||||||
[ 1 2 3 ] s2l [ 1 2 3 ] streq and
|
[ 1 2 3 ] s2q [ 1 2 3 ] streq and
|
||||||
"" s2l [ ] streq and
|
"" s2q [ ] streq and
|
||||||
[ 97 98 99 ] l2s "abc" streq and
|
[ 97 98 99 ] q2s "abc" streq and
|
||||||
[ ] l2s "" streq and
|
[ ] q2s "" streq and
|
||||||
if [
|
if [
|
||||||
" l2s, streq, lower, and upper PASS" puts cr
|
" q2s, streq, lower, and upper PASS" puts cr
|
||||||
] [
|
] [
|
||||||
" l2s, streq, lower, and upper FAIL" puts cr
|
" q2s, streq, lower, and upper FAIL" puts cr
|
||||||
]
|
]
|
||||||
|
|
||||||
# 8. Q-form Operations
|
# 8. Q-form Operations
|
||||||
@@ -182,11 +182,11 @@ if [
|
|||||||
99 [ 10 20 30 ] 1 qset [ 10 99 30 ] streq if [ " qset PASS" puts cr ] [ " qset FAIL" puts cr ] # Test qset
|
99 [ 10 20 30 ] 1 qset [ 10 99 30 ] streq if [ " qset PASS" puts cr ] [ " qset FAIL" puts cr ] # Test qset
|
||||||
[ 1 2 3 ] rev [ 3 2 1 ] streq
|
[ 1 2 3 ] rev [ 3 2 1 ] streq
|
||||||
[ 1 2 ] [ 3 4 ] lcat [ 1 2 3 4 ] streq and
|
[ 1 2 ] [ 3 4 ] lcat [ 1 2 3 4 ] streq and
|
||||||
[ 1 2 ] s2l [ 1 2 ] streq and
|
[ 1 2 ] s2q [ 1 2 ] streq and
|
||||||
if [
|
if [
|
||||||
" rev, lcat and s2l PASS" puts cr
|
" rev, lcat and s2q PASS" puts cr
|
||||||
] [
|
] [
|
||||||
" rev, lcat and s2l FAIL" puts cr
|
" rev, lcat and s2q FAIL" puts cr
|
||||||
]
|
]
|
||||||
|
|
||||||
# 9. Bitwise Operations
|
# 9. Bitwise Operations
|
||||||
|
|||||||
Reference in New Issue
Block a user