From 53c151016a140db7c17072306d2f22ce788e7d76 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Wed, 3 Jan 2018 08:09:42 +0800 Subject: [PATCH] =?UTF-8?q?ch1-02:=20=E6=B7=BB=E5=8A=A0=E4=BE=8B=E5=AD=90?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/ch1-02/hello-alef/hello.alef | 19 +++++++++ examples/ch1-02/hello-b/main.b | 8 ++++ examples/ch1-02/hello-c-01/hello-c-01.c | 4 ++ examples/ch1-02/hello-c-02/hello-c-02.c | 4 ++ examples/ch1-02/hello-c-03/hello-c-03.c | 6 +++ examples/ch1-02/hello-c-04/hello-c-04.c | 6 +++ examples/ch1-02/hello-go-200806/hello.go.txt | 8 ++++ .../ch1-02/hello-go-20080627/hello.go.txt | 7 ++++ .../ch1-02/hello-go-20080811/hello.go.txt | 5 +++ .../ch1-02/hello-go-20081024/hello.go.txt | 9 ++++ .../ch1-02/hello-go-20090915/hello.go.txt | 9 ++++ examples/ch1-02/hello-go-20091211/hello.go | 7 ++++ examples/ch1-02/hello-go-asm/hello.go | 3 ++ examples/ch1-02/hello-go-asm/hello_amd64.s | 16 ++++++++ examples/ch1-02/hello-go-cgo/hello.go | 13 ++++++ examples/ch1-02/hello-go-swig/hello.cc | 5 +++ examples/ch1-02/hello-go-swig/hello.go | 11 +++++ examples/ch1-02/hello-go-swig/hello.swigcxx | 5 +++ examples/ch1-02/hello-go-v2/hello.go | 20 +++++++++ examples/ch1-02/hello-limbo/hello.limbo | 15 +++++++ .../ch1-02/hello-newsqueak/hello.newsqueak | 1 + .../ch1-02/prime-newsqueak/prime.newsqueak | 41 +++++++++++++++++++ 22 files changed, 222 insertions(+) create mode 100644 examples/ch1-02/hello-alef/hello.alef create mode 100644 examples/ch1-02/hello-b/main.b create mode 100644 examples/ch1-02/hello-c-01/hello-c-01.c create mode 100644 examples/ch1-02/hello-c-02/hello-c-02.c create mode 100644 examples/ch1-02/hello-c-03/hello-c-03.c create mode 100644 examples/ch1-02/hello-c-04/hello-c-04.c create mode 100644 examples/ch1-02/hello-go-200806/hello.go.txt create mode 100644 examples/ch1-02/hello-go-20080627/hello.go.txt create mode 100644 examples/ch1-02/hello-go-20080811/hello.go.txt create mode 100644 examples/ch1-02/hello-go-20081024/hello.go.txt create mode 100644 examples/ch1-02/hello-go-20090915/hello.go.txt create mode 100644 examples/ch1-02/hello-go-20091211/hello.go create mode 100644 examples/ch1-02/hello-go-asm/hello.go create mode 100644 examples/ch1-02/hello-go-asm/hello_amd64.s create mode 100644 examples/ch1-02/hello-go-cgo/hello.go create mode 100644 examples/ch1-02/hello-go-swig/hello.cc create mode 100644 examples/ch1-02/hello-go-swig/hello.go create mode 100644 examples/ch1-02/hello-go-swig/hello.swigcxx create mode 100644 examples/ch1-02/hello-go-v2/hello.go create mode 100644 examples/ch1-02/hello-limbo/hello.limbo create mode 100644 examples/ch1-02/hello-newsqueak/hello.newsqueak create mode 100644 examples/ch1-02/prime-newsqueak/prime.newsqueak diff --git a/examples/ch1-02/hello-alef/hello.alef b/examples/ch1-02/hello-alef/hello.alef new file mode 100644 index 0000000..6c864c8 --- /dev/null +++ b/examples/ch1-02/hello-alef/hello.alef @@ -0,0 +1,19 @@ +#include + +void receive(chan(byte*) c) { + byte *s; + s = <- c; + print("%s\n", s); + terminate(nil); +} + +void main(void) { + chan(byte*) c; + alloc c; + proc receive(c); + task receive(c); + c <- = "hello proc or task"; + c <- = "hello proc or task"; + print("done\n"); + terminate(nil); +} diff --git a/examples/ch1-02/hello-b/main.b b/examples/ch1-02/hello-b/main.b new file mode 100644 index 0000000..e77f5c3 --- /dev/null +++ b/examples/ch1-02/hello-b/main.b @@ -0,0 +1,8 @@ +main() { + extrn a, b, c; + putchar(a); putchar(b); putchar(c); + putchar('!*n'); +} +a 'hell'; +b 'o, w'; +c 'orld'; diff --git a/examples/ch1-02/hello-c-01/hello-c-01.c b/examples/ch1-02/hello-c-01/hello-c-01.c new file mode 100644 index 0000000..5144cb4 --- /dev/null +++ b/examples/ch1-02/hello-c-01/hello-c-01.c @@ -0,0 +1,4 @@ +main() +{ + printf("hello, world"); +} diff --git a/examples/ch1-02/hello-c-02/hello-c-02.c b/examples/ch1-02/hello-c-02/hello-c-02.c new file mode 100644 index 0000000..eaab7bb --- /dev/null +++ b/examples/ch1-02/hello-c-02/hello-c-02.c @@ -0,0 +1,4 @@ +main() +{ + printf("hello, world\n"); +} diff --git a/examples/ch1-02/hello-c-03/hello-c-03.c b/examples/ch1-02/hello-c-03/hello-c-03.c new file mode 100644 index 0000000..8626b30 --- /dev/null +++ b/examples/ch1-02/hello-c-03/hello-c-03.c @@ -0,0 +1,6 @@ +#include + +main() +{ + printf("hello, world\n"); +} diff --git a/examples/ch1-02/hello-c-04/hello-c-04.c b/examples/ch1-02/hello-c-04/hello-c-04.c new file mode 100644 index 0000000..1aed89f --- /dev/null +++ b/examples/ch1-02/hello-c-04/hello-c-04.c @@ -0,0 +1,6 @@ +#include + +main(void) +{ + printf("hello, world\n"); +} diff --git a/examples/ch1-02/hello-go-200806/hello.go.txt b/examples/ch1-02/hello-go-200806/hello.go.txt new file mode 100644 index 0000000..ac52ff5 --- /dev/null +++ b/examples/ch1-02/hello-go-200806/hello.go.txt @@ -0,0 +1,8 @@ +// +build ignore + +package main + +func main() int { + print "hello, world\n"; + return 0; +} diff --git a/examples/ch1-02/hello-go-20080627/hello.go.txt b/examples/ch1-02/hello-go-20080627/hello.go.txt new file mode 100644 index 0000000..f5ddc15 --- /dev/null +++ b/examples/ch1-02/hello-go-20080627/hello.go.txt @@ -0,0 +1,7 @@ +// +build ignore + +package main + +func main() { + print "hello, world\n"; +} diff --git a/examples/ch1-02/hello-go-20080811/hello.go.txt b/examples/ch1-02/hello-go-20080811/hello.go.txt new file mode 100644 index 0000000..67fcaad --- /dev/null +++ b/examples/ch1-02/hello-go-20080811/hello.go.txt @@ -0,0 +1,5 @@ +package main + +func main() { + print("hello, world\n"); +} diff --git a/examples/ch1-02/hello-go-20081024/hello.go.txt b/examples/ch1-02/hello-go-20081024/hello.go.txt new file mode 100644 index 0000000..85051d0 --- /dev/null +++ b/examples/ch1-02/hello-go-20081024/hello.go.txt @@ -0,0 +1,9 @@ +// +build ignore + +package main + +import "fmt" + +func main() { + fmt.printf("hello, world\n"); +} diff --git a/examples/ch1-02/hello-go-20090915/hello.go.txt b/examples/ch1-02/hello-go-20090915/hello.go.txt new file mode 100644 index 0000000..0cb5e81 --- /dev/null +++ b/examples/ch1-02/hello-go-20090915/hello.go.txt @@ -0,0 +1,9 @@ +// +build ignore + +package main + +import "fmt" + +func main() { + fmt.Printf("hello, world\n"); +} diff --git a/examples/ch1-02/hello-go-20091211/hello.go b/examples/ch1-02/hello-go-20091211/hello.go new file mode 100644 index 0000000..c2fbf9c --- /dev/null +++ b/examples/ch1-02/hello-go-20091211/hello.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Printf("hello, world\n") +} diff --git a/examples/ch1-02/hello-go-asm/hello.go b/examples/ch1-02/hello-go-asm/hello.go new file mode 100644 index 0000000..551d99a --- /dev/null +++ b/examples/ch1-02/hello-go-asm/hello.go @@ -0,0 +1,3 @@ +package main + +func main() diff --git a/examples/ch1-02/hello-go-asm/hello_amd64.s b/examples/ch1-02/hello-go-asm/hello_amd64.s new file mode 100644 index 0000000..d78d55b --- /dev/null +++ b/examples/ch1-02/hello-go-asm/hello_amd64.s @@ -0,0 +1,16 @@ +#include "textflag.h" +#include "funcdata.h" + +// "Hello World!\n" +DATA text<>+0(SB)/8,$"Hello Wo" +DATA text<>+8(SB)/8,$"rld!\n" +GLOBL text<>(SB),NOPTR,$16 + +// func main() +TEXT ·main(SB), $16-0 + NO_LOCAL_POINTERS + MOVQ $text<>+0(SB), AX + MOVQ AX, (SP) + MOVQ $16, 8(SP) + CALL runtime·printstring(SB) + RET diff --git a/examples/ch1-02/hello-go-cgo/hello.go b/examples/ch1-02/hello-go-cgo/hello.go new file mode 100644 index 0000000..ff473f6 --- /dev/null +++ b/examples/ch1-02/hello-go-cgo/hello.go @@ -0,0 +1,13 @@ +package main + +// #include +// #include +import "C" +import "unsafe" + +func main() { + msg := C.CString("Hello, World!\n") + defer C.free(unsafe.Pointer(msg)) + + C.fputs(msg, C.stdout) +} diff --git a/examples/ch1-02/hello-go-swig/hello.cc b/examples/ch1-02/hello-go-swig/hello.cc new file mode 100644 index 0000000..0af3690 --- /dev/null +++ b/examples/ch1-02/hello-go-swig/hello.cc @@ -0,0 +1,5 @@ +#include + +void SayHello() { + std::cout << "Hello, World!" << std::endl; +} diff --git a/examples/ch1-02/hello-go-swig/hello.go b/examples/ch1-02/hello-go-swig/hello.go new file mode 100644 index 0000000..c9dd265 --- /dev/null +++ b/examples/ch1-02/hello-go-swig/hello.go @@ -0,0 +1,11 @@ +// +build ignore + +package main + +import ( + hello "." +) + +func main() { + hello.SayHello() +} diff --git a/examples/ch1-02/hello-go-swig/hello.swigcxx b/examples/ch1-02/hello-go-swig/hello.swigcxx new file mode 100644 index 0000000..4ed88c8 --- /dev/null +++ b/examples/ch1-02/hello-go-swig/hello.swigcxx @@ -0,0 +1,5 @@ +%module main + +%inline %{ +extern void SayHello(); +%} diff --git a/examples/ch1-02/hello-go-v2/hello.go b/examples/ch1-02/hello-go-v2/hello.go new file mode 100644 index 0000000..e2b1cdd --- /dev/null +++ b/examples/ch1-02/hello-go-v2/hello.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "time" +) + +func main() { + fmt.Println("Please visit http://127.0.0.1:12345/") + http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + s := fmt.Sprintf("你好, 世界! -- Time: %s", time.Now().String()) + fmt.Fprintf(w, "%v\n", s) + log.Printf("%v\n", s) + }) + if err := http.ListenAndServe(":12345", nil); err != nil { + log.Fatal("ListenAndServe: ", err) + } +} diff --git a/examples/ch1-02/hello-limbo/hello.limbo b/examples/ch1-02/hello-limbo/hello.limbo new file mode 100644 index 0000000..dcd78cb --- /dev/null +++ b/examples/ch1-02/hello-limbo/hello.limbo @@ -0,0 +1,15 @@ +implement Hello; + +include "sys.m"; sys: Sys; +include "draw.m"; + +Hello: module +{ + init: fn(ctxt: ref Draw->Context, args: list of string); +}; + +init(ctxt: ref Draw->Context, args: list of string) +{ + sys = load Sys Sys->PATH; + sys->print("hello, world\n"); +} diff --git a/examples/ch1-02/hello-newsqueak/hello.newsqueak b/examples/ch1-02/hello-newsqueak/hello.newsqueak new file mode 100644 index 0000000..409e345 --- /dev/null +++ b/examples/ch1-02/hello-newsqueak/hello.newsqueak @@ -0,0 +1 @@ +print("Hello,", "World", "\n"); diff --git a/examples/ch1-02/prime-newsqueak/prime.newsqueak b/examples/ch1-02/prime-newsqueak/prime.newsqueak new file mode 100644 index 0000000..d915c32 --- /dev/null +++ b/examples/ch1-02/prime-newsqueak/prime.newsqueak @@ -0,0 +1,41 @@ +// 向管道输出从2开始的自然数序列 +counter := prog(c:chan of int) { + i := 2; + for(;;) { + c <-= i++; + } +}; + +// 针对listen管道获取的数列,过滤掉是prime倍数的数 +// 新的序列输出到send管道 +filter := prog(prime:int, listen, send:chan of int) { + i:int; + for(;;) { + if((i = <-listen)%prime) { + send <-= i; + } + } +}; + +// 主函数 +// 每个管道第一个流出的数必然是素数 +// 然后基于这个新的素数构建新的素数过滤器 +sieve := prog() of chan of int { + c := mk(chan of int); + begin counter(c); + prime := mk(chan of int); + begin prog(){ + p:int; + newc:chan of int; + for(;;){ + prime <-= p =<- c; + newc = mk(); + begin filter(p, c, newc); + c = newc; + } + }(); + become prime; +}; + +// 启动素数筛 +prime := sieve();