diff --git a/davi.go b/davi.go index 60e4d0c..8ab91d8 100644 --- a/davi.go +++ b/davi.go @@ -18,17 +18,22 @@ func main() { //$url = "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m"; //fileGetContents($url); - function jsonHandler() { + $timeHandler = function() { $time = time(); + return($time); } //httpRegister("/", "Cool!"); //httpRegister("/hello", "Hello, World!"); //httpRegister("/json", "Json!"); - //httpListen(":3030"); - - $time = time(); - echo($time); + httpRegister("/time", $timeHandler); + httpListen(":3030"); + + lst = ["foo", "a", "z", "B"] + sort(lst, lower); + for x in lst { + echo(x); + } `) diff --git a/interpreter/functions.go b/interpreter/functions.go index 15d7982..2119a3c 100644 --- a/interpreter/functions.go +++ b/interpreter/functions.go @@ -497,20 +497,25 @@ func fileGetContentsFunction(interp *interpreter, pos Position, args []Value) Va func httpRegisterFunction(interp *interpreter, pos Position, args []Value) Value { ensureNumArgs(pos, "httpRegister", args, 2) - + // if len(args) != 1 && len(args) != 2 { panic(typeError(pos, "httpRegisterFunction() requires 2 args, got %d", len(args))) } + handlerFunction, ok := args[1].(functionType) + if !ok { + panic(typeError(pos, "httpRegisterFunction() requires second argument to be a function")) + } + formatter := prettyjson.NewFormatter() - output, _ := formatter.Marshal(args) + output, _ := formatter.Marshal(handlerFunction) fmt.Println(string(output)) pattern := args[0].(string) - handler := args[1].(string) getRoot := func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, handler) + outputFunction := interp.callFunction(pos, handlerFunction, []Value{}) + fmt.Fprintln(w, outputFunction) } http.HandleFunc(pattern, getRoot) diff --git a/parser/parser.go b/parser/parser.go index 8c1bb51..5ee4579 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -317,7 +317,12 @@ func (p *parser) call() Expression { p.error("can only have ... after last argument") } p.expect(RPAREN) - p.expect(SEMI) + if p.tok == SEMI { + p.expect(SEMI) + } else { + print(p.tok) + } + expr = &Call{pos, expr, args, gotEllipsis} } else if p.tok == LBRACKET { pos := p.pos