From 8edcea969eec27958beb1a509c7c312dd41d05c7 Mon Sep 17 00:00:00 2001 From: zongor Date: Sat, 7 Dec 2024 21:19:43 -0500 Subject: [PATCH] Add MEASURE fix List operators --- examples/list.vqe | 2 +- tcp3333 | 2 + varaq/interpreter.go | 67 ++++++++++---- varaq/token.go | 210 ++++++++++++++++++++++--------------------- 4 files changed, 160 insertions(+), 121 deletions(-) create mode 100755 tcp3333 diff --git a/examples/list.vqe b/examples/list.vqe index a04f24c..82dbdcc 100755 --- a/examples/list.vqe +++ b/examples/list.vqe @@ -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 diff --git a/tcp3333 b/tcp3333 new file mode 100755 index 0000000..0c6df37 --- /dev/null +++ b/tcp3333 @@ -0,0 +1,2 @@ +#!/bin/rc +exec bin/varaq examples/http_read.vqe >>[2]/sys/log/www diff --git a/varaq/interpreter.go b/varaq/interpreter.go index 1a5a489..e0a0749 100644 --- a/varaq/interpreter.go +++ b/varaq/interpreter.go @@ -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,20 +1024,45 @@ 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() + str = s.Text() + + if s.Err() != nil { + return s.Err() + } + push(Expr{STRING, nil, str, 0}) } - - var str string - - s.Scan() - str = s.Text() - - if s.Err() != nil { - return s.Err() - } - push(Expr{STRING, nil, str, 0}) case COMPLAIN: a, e := popExpr() if e != nil { diff --git a/varaq/token.go b/varaq/token.go index 1846445..2a0851e 100644 --- a/varaq/token.go +++ b/varaq/token.go @@ -3,110 +3,111 @@ package varaq 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 + UNDEFINED TokenType = iota + 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 PostScript’s 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?",