Add MEASURE fix List operators

This commit is contained in:
zongor 2024-12-07 21:19:43 -05:00
parent 1b26f3e65a
commit 8edcea969e
4 changed files with 160 additions and 121 deletions

View File

@ -1 +1 @@
(1 2 3 4 "test this" pi(*test*)) empty? disp
("test this" (*test*) ) pi cons 2 cons 1 cons 4 { split exch disp newline } repeat

2
tcp3333 Executable file
View File

@ -0,0 +1,2 @@
#!/bin/rc
exec bin/varaq examples/http_read.vqe >>[2]/sys/log/www

View File

@ -352,14 +352,14 @@ func Interpret(code Expr, argv []string, w io.Writer) error {
return fmt.Errorf("cannot `split` not a list @%v", idx)
}
case CONS:
list, e := pop()
if e != nil {
return e
}
o, eo := pop()
if eo != nil {
return eo
}
list, e := pop()
if e != nil {
return e
}
if list.Tok == LIST {
list.Exprs = append(list.Exprs, o)
push(list)
@ -378,6 +378,16 @@ func Interpret(code Expr, argv []string, w io.Writer) error {
} else {
return fmt.Errorf("cannot `shatter` not a list @%v", idx)
}
case MEASURE:
list, e := pop()
if e != nil {
return e
}
if list.Tok == LIST {
push(Expr{NUMBER, nil, float64(len(list.Exprs)), 0})
} else {
return fmt.Errorf("cannot `measure` not a list @%v", idx)
}
case EMPTY:
list, e := pop()
if e != nil {
@ -1014,11 +1024,35 @@ func Interpret(code Expr, argv []string, w io.Writer) error {
fmt.Fprintf(w, "%v", a.Value)
}
case LISTEN:
// FIXME: should there be a way to do this in wasm??
if runtime.GOARCH == "wasm" {
return nil
// In WASM mode fetch does a fetch request from a foreign website
v1, e := pop()
if e != nil {
return e
}
if v1.Tok == STRING {
url := v1.Value.(string)
resp, err := http.Get(url)
if err != nil {
// we will get an error at this stage if the request fails, such as if the
// requested URL is not found, or if the server is not reachable.
return err
}
defer resp.Body.Close()
// print the response
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
push(Expr{STRING, nil, string(data), 0})
} else {
return fmt.Errorf("cannot `streq?` value is not a string @%v", idx)
}
} else {
var str string
s.Scan()
@ -1028,6 +1062,7 @@ func Interpret(code Expr, argv []string, w io.Writer) error {
return s.Err()
}
push(Expr{STRING, nil, str, 0})
}
case COMPLAIN:
a, e := popExpr()
if e != nil {

View File

@ -4,109 +4,110 @@ type TokenType int64
const (
UNDEFINED TokenType = iota
IDENTIFIER
STRING
NUMBER
BOOLEAN
LIST
SCRIPT
FUNCTION
ERROR
LEFTPAREN
RIGHTPAREN
LEFTBRACE
RIGHTBRACE
TILDE
SLASH
STAR
FALSE
TRUE
PI
E
EOF
POP
DUP
EXCH
CLEAR
REMEMBER
FORGET
DUMP
NAME
SET
IFYES
IFNO
CHOOSE
EVAL
ESCAPE
REPEAT
SPLIT
CONS
SHATTER
EMPTY
COMPOSE
STREQ
STRCUT
STRMEASURE
STRTIE
EXPLODE
ADD
SUB
MUL
DIV
IDIV
MOD
POW
SQRT
ADD1
SUB1
SIN
COS
TAN
ATAN
LN
LOG
LOG3
CLIP
SMOOTH
HOWMUCH
SETRAND
RAND
NUMBERIZE
ISOLATE
MIX
CONTRADICT
COMPL
SHIFTRIGHT
SHIFTLEFT
GT
LT
EQ
GE
LE
NE
NULL
NEGATIVE
ISNULL
ISINT
ISNUMBER
AND
OR
XOR
DISP
LISTEN
COMPLAIN
NEWLINE
TAB
WHEREAMI
VERSION
ARGV
TIME
GARBAGECOLLECT
OVER
ROT
DEPTH
SERVEHTTP
READALL
IDENTIFIER // id
STRING // string
NUMBER // float
BOOLEAN // bool
LIST // list
SCRIPT // script
FUNCTION // function
ERROR // error
LEFTPAREN // (
RIGHTPAREN // )
LEFTBRACE // {
RIGHTBRACE // }
TILDE // The ~ operator is a special form, as it is not a postfix operator. When the interpreter encounters a ~, it pushes the next token on the stack as is regardless of whether it is a defined name.
SLASH // /
STAR // *
FALSE // false
TRUE // true
PI // 3.14159
E // 2.718
EOF // end of file
POP // Pops and discards the top item on the stack. The literal meaning is discard.
DUP // Duplicates the top object on the stack.
EXCH // Inverts the order of the top two objects on the stack.
CLEAR // Empties the stack.
REMEMBER // Puts a flag (like PostScripts mark) on the stack. The internal representation of the flag is not available to the programmer.
FORGET // Clears the stack down to the flag and pops the flag. If there is no flag present, the stack is emptied completely.
DUMP // Prints the contents of the operand stack to STDOUT without changing them.
NAME // Associates obj with id and places it in the system lookup space. Conventionally used to associate new operator names with procedure objects.
SET // Reassigns the value of a value already in the system lookup space. Used primarily for variable assignments.
IFYES //
IFNO //
CHOOSE //
EVAL //
ESCAPE //
REPEAT //
SPLIT //
CONS //
SHATTER //
MEASURE //
EMPTY //
COMPOSE //
STREQ //
STRCUT //
STRMEASURE //
STRTIE //
EXPLODE //
ADD //
SUB //
MUL //
DIV //
IDIV //
MOD //
POW //
SQRT //
ADD1 //
SUB1 //
SIN //
COS //
TAN //
ATAN //
LN //
LOG //
LOG3 //
CLIP //
SMOOTH //
HOWMUCH //
SETRAND //
RAND //
NUMBERIZE //
ISOLATE //
MIX //
CONTRADICT //
COMPL //
SHIFTRIGHT //
SHIFTLEFT //
GT //
LT //
EQ //
GE //
LE //
NE //
NULL //
NEGATIVE //
ISNULL //
ISINT //
ISNUMBER //
AND //
OR //
XOR //
DISP //
LISTEN //
COMPLAIN //
NEWLINE //
TAB //
WHEREAMI //
VERSION //
ARGV //
TIME //
GARBAGECOLLECT //
OVER //
ROT //
DEPTH //
SERVEHTTP //
READALL //
)
var tokens = [...]string{
@ -148,6 +149,7 @@ var tokens = [...]string{
SPLIT: "SPLIT",
CONS: "CONS",
SHATTER: "SHATTER",
MEASURE: "MEASURE",
EMPTY: "EMPTY?",
COMPOSE: "COMPOSE",
STREQ: "STREQ?",