add readall, readall test

This commit is contained in:
chakr 2023-03-22 22:26:58 -04:00
parent be6ebfceb6
commit 6d1211c2fd
7 changed files with 37 additions and 4 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
bin/* bin/*
*.html ./*.html

2
.vscode/launch.json vendored
View File

@ -10,7 +10,7 @@
"request": "launch", "request": "launch",
"mode": "auto", "mode": "auto",
"program": "main.go", "program": "main.go",
"args": ["examples/http_hello_world.vqe"] "args": ["examples/http_read.vqe"]
} }
] ]
} }

3
examples/http_read.vqe Normal file
View File

@ -0,0 +1,3 @@
{
"examples/test.html" readall disp
} ":3333" servehttp

12
examples/test.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>test</h1>
</body>
</html>

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"math/rand" "math/rand"
"net" "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) { 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() fun, e := pop()
if e != nil { if e != nil {
return return
} }
if fun.Tok == FUNCTION { if fun.Tok == FUNCTION {
Interpret(fun, nil, w) Interpret(fun, req, w)
} else { } else {
return 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: case VERSION:
fmt.Fprintf(w, "var'aq -- 0.9.1 Martoq") fmt.Fprintf(w, "var'aq -- 0.9.1 Martoq")
case ARGV: case ARGV:

View File

@ -182,6 +182,7 @@ var keywords = map[string]TokenType{
"depth": DEPTH, "depth": DEPTH,
"juv": DEPTH, "juv": DEPTH,
"servehttp": SERVEHTTP, "servehttp": SERVEHTTP,
"readall": READALL,
} }
func isDigit(s string) bool { func isDigit(s string) bool {

View File

@ -106,6 +106,7 @@ const (
ROT ROT
DEPTH DEPTH
SERVEHTTP SERVEHTTP
READALL
) )
var tokens = [...]string{ var tokens = [...]string{
@ -211,6 +212,7 @@ var tokens = [...]string{
ROT: "ROT", ROT: "ROT",
DEPTH: "DEPTH", DEPTH: "DEPTH",
SERVEHTTP: "SERVEHTTP", SERVEHTTP: "SERVEHTTP",
READALL: "READALL",
} }
func (me TokenType) String() string { func (me TokenType) String() string {