fix: remove hardcoded port. (#27)

* fix: remove hardcoded port.

* fix: genop.
This commit is contained in:
Ludovic Fernandez
2019-01-22 15:31:28 +01:00
committed by Marc Vertes
parent 08f4aabdc0
commit 3c674c2cc4
8 changed files with 58 additions and 31 deletions

View File

@@ -4,11 +4,12 @@ import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
)
func client() {
resp, err := http.Get("http://localhost:8080/")
func client(uri string) {
resp, err := http.Get(uri)
if err != nil {
log.Fatal(err)
}
@@ -19,20 +20,26 @@ func client() {
fmt.Println(string(body))
}
func server(ready chan bool) {
func server(ln net.Listener, ready chan bool) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome to my website!")
})
go http.ListenAndServe(":8080", nil)
go http.Serve(ln, nil)
ready <- true
}
func main() {
ln, err := net.Listen("tcp", ":0")
if err != nil {
log.Fatal(err)
}
ready := make(chan bool)
go server(ready)
go server(ln, ready)
<-ready
client()
client(fmt.Sprintf("http://%s", ln.Addr().String()))
}
// Output:

View File

@@ -1,5 +1,7 @@
#!/bin/sh
export LANG=C
echo '// Code Generated by ../_test/gen_example.sh. DO NOT EDIT.'
echo
echo 'package interp'

View File

@@ -2,6 +2,7 @@ package main
import (
"bytes"
"go/format"
"io/ioutil"
"log"
"text/template"
@@ -68,6 +69,7 @@ func {{$name}}(n *Node) {
{{end}}
`
// Op FIXME
type Op struct {
Name string
Str bool
@@ -85,23 +87,30 @@ func main() {
b := &bytes.Buffer{}
data := map[string]interface{}{
"Ops": map[string]Op{
"add": Op{"+", true, true, false},
"sub": Op{"-", false, true, false},
"mul": Op{"*", false, true, false},
"quo": Op{"/", false, true, false},
"rem": Op{"%", false, false, false},
"shl": Op{"<<", false, false, true},
"shr": Op{">>", false, false, true},
"and": Op{"&", false, false, false},
"or": Op{"|", false, false, false},
"xor": Op{"^", false, false, false},
"andnot": Op{"&^", false, false, false},
"add": {"+", true, true, false},
"sub": {"-", false, true, false},
"mul": {"*", false, true, false},
"quo": {"/", false, true, false},
"rem": {"%", false, false, false},
"shl": {"<<", false, false, true},
"shr": {">>", false, false, true},
"and": {"&", false, false, false},
"or": {"|", false, false, false},
"xor": {"^", false, false, false},
"andnot": {"&^", false, false, false},
},
}
if err = parse.Execute(b, data); err != nil {
log.Fatal(err)
}
if err = ioutil.WriteFile("op.go", b.Bytes(), 0666); err != nil {
// gofmt
source, err := format.Source(b.Bytes())
if err != nil {
log.Fatal(err)
}
if err = ioutil.WriteFile("op.go", source, 0666); err != nil {
log.Fatal(err)
}
}

View File

@@ -385,7 +385,7 @@ func (interp *Interpreter) Cfg(root *Node) ([]*Node, error) {
case Greater, Lower:
n.typ = scope.getType("bool")
default:
n.typ = nodeType(interp, scope, n)
n.typ, err = nodeType(interp, scope, n)
}
case IndexExpr:

View File

@@ -912,11 +912,12 @@ import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
)
func client() {
resp, err := http.Get("http://localhost:8080/")
func client(uri string) {
resp, err := http.Get(uri)
if err != nil {
log.Fatal(err)
}
@@ -927,20 +928,26 @@ func client() {
fmt.Println(string(body))
}
func server(ready chan bool) {
func server(ln net.Listener, ready chan bool) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome to my website!")
})
go http.ListenAndServe(":8080", nil)
go http.Serve(ln, nil)
ready <- true
}
func main() {
ln, err := net.Listen("tcp", ":0")
if err != nil {
log.Fatal(err)
}
ready := make(chan bool)
go server(ready)
go server(ln, ready)
<-ready
client()
client(fmt.Sprintf("http://%s", ln.Addr().String()))
}
`
i := New(Opt{Entry: "main"})

View File

@@ -4,7 +4,6 @@ package interp
import "reflect"
func add(n *Node) {
i := n.findex
next := getExec(n.tnext)
@@ -320,4 +319,3 @@ func xor(n *Node) {
}
}
}

View File

@@ -18,8 +18,8 @@ func (i *Interpreter) importSrcFile(path string) error {
return err
}
initNodes := []*Node{}
rootNodes := []*Node{}
var initNodes []*Node
var rootNodes []*Node
var root *Node

View File

@@ -182,9 +182,13 @@ func nodeType(interp *Interpreter, scope *Scope, n *Node) (*Type, error) {
}
case BinaryExpr:
t = nodeType(interp, scope, n.child[0])
t, err = nodeType(interp, scope, n.child[0])
if err != nil {
return nil, err
}
if t.untyped {
t1 := nodeType(interp, scope, n.child[1])
var t1 *Type
t1, err = nodeType(interp, scope, n.child[1])
if !(t1.untyped && isInt(t1) && isFloat(t)) {
t = t1
}