From 6d1211c2fd2857177bb69a1c0896a8458f085389 Mon Sep 17 00:00:00 2001 From: chakr Date: Wed, 22 Mar 2023 22:26:58 -0400 Subject: [PATCH] add readall, readall test --- .gitignore | 2 +- .vscode/launch.json | 2 +- examples/http_read.vqe | 3 +++ examples/test.html | 12 ++++++++++++ varaq/interpreter.go | 19 +++++++++++++++++-- varaq/scanner.go | 1 + varaq/token.go | 2 ++ 7 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 examples/http_read.vqe create mode 100644 examples/test.html diff --git a/.gitignore b/.gitignore index b001011..794ede4 100755 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ bin/* -*.html \ No newline at end of file +./*.html \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index cf1b9c2..ce3dcd2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "mode": "auto", "program": "main.go", - "args": ["examples/http_hello_world.vqe"] + "args": ["examples/http_read.vqe"] } ] } diff --git a/examples/http_read.vqe b/examples/http_read.vqe new file mode 100644 index 0000000..3b50890 --- /dev/null +++ b/examples/http_read.vqe @@ -0,0 +1,3 @@ +{ +"examples/test.html" readall disp +} ":3333" servehttp diff --git a/examples/test.html b/examples/test.html new file mode 100644 index 0000000..70aa527 --- /dev/null +++ b/examples/test.html @@ -0,0 +1,12 @@ + + + + + + + Document + + +

test

+ + \ No newline at end of file diff --git a/varaq/interpreter.go b/varaq/interpreter.go index b82e347..53db90a 100644 --- a/varaq/interpreter.go +++ b/varaq/interpreter.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io" + "io/ioutil" "math" "math/rand" "net" @@ -1243,18 +1244,32 @@ func Interpret(code Expr, argv []string, w io.Writer) error { } http.ListenAndServe(str.Value.(string), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") + req := make([]string, 0) + req = append(req, r.RequestURI) fun, e := pop() if e != nil { return } if fun.Tok == FUNCTION { - Interpret(fun, nil, w) + Interpret(fun, req, w) } else { return } })) + case READALL: + str, e := pop() + if e != nil { + return e + } + if str.Tok != STRING { + return fmt.Errorf("cannot `servehttp` exptected a string for the port @%v", idx) + } + filedata, err := ioutil.ReadFile(str.Value.(string)) // the file is inside the local directory + if err != nil { + return err + } + push(Expr{STRING, nil, string(filedata), 0}) case VERSION: fmt.Fprintf(w, "var'aq -- 0.9.1 Martoq") case ARGV: diff --git a/varaq/scanner.go b/varaq/scanner.go index 8cfe958..f1815ac 100644 --- a/varaq/scanner.go +++ b/varaq/scanner.go @@ -182,6 +182,7 @@ var keywords = map[string]TokenType{ "depth": DEPTH, "juv": DEPTH, "servehttp": SERVEHTTP, + "readall": READALL, } func isDigit(s string) bool { diff --git a/varaq/token.go b/varaq/token.go index 84dacc4..1846445 100644 --- a/varaq/token.go +++ b/varaq/token.go @@ -106,6 +106,7 @@ const ( ROT DEPTH SERVEHTTP + READALL ) var tokens = [...]string{ @@ -211,6 +212,7 @@ var tokens = [...]string{ ROT: "ROT", DEPTH: "DEPTH", SERVEHTTP: "SERVEHTTP", + READALL: "READALL", } func (me TokenType) String() string {