diff --git a/examples/ch1-02-hello-revolution/hello-alef/hello.alef b/examples/ch1-02-hello-revolution/hello-alef/hello.alef new file mode 100755 index 0000000..6c864c8 --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-b/main.b b/examples/ch1-02-hello-revolution/hello-b/main.b new file mode 100755 index 0000000..e77f5c3 --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-c-01/hello-c-01.c b/examples/ch1-02-hello-revolution/hello-c-01/hello-c-01.c new file mode 100755 index 0000000..5144cb4 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-c-01/hello-c-01.c @@ -0,0 +1,4 @@ +main() +{ + printf("hello, world"); +} diff --git a/examples/ch1-02-hello-revolution/hello-c-02/hello-c-02.c b/examples/ch1-02-hello-revolution/hello-c-02/hello-c-02.c new file mode 100755 index 0000000..eaab7bb --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-c-02/hello-c-02.c @@ -0,0 +1,4 @@ +main() +{ + printf("hello, world\n"); +} diff --git a/examples/ch1-02-hello-revolution/hello-c-03/hello-c-03.c b/examples/ch1-02-hello-revolution/hello-c-03/hello-c-03.c new file mode 100755 index 0000000..8626b30 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-c-03/hello-c-03.c @@ -0,0 +1,6 @@ +#include + +main() +{ + printf("hello, world\n"); +} diff --git a/examples/ch1-02-hello-revolution/hello-c-04/hello-c-04.c b/examples/ch1-02-hello-revolution/hello-c-04/hello-c-04.c new file mode 100755 index 0000000..1aed89f --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-go-200806/hello.go.txt b/examples/ch1-02-hello-revolution/hello-go-200806/hello.go.txt new file mode 100755 index 0000000..ac52ff5 --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-go-20080627/hello.go.txt b/examples/ch1-02-hello-revolution/hello-go-20080627/hello.go.txt new file mode 100755 index 0000000..f5ddc15 --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-go-20080811/hello.go.txt b/examples/ch1-02-hello-revolution/hello-go-20080811/hello.go.txt new file mode 100755 index 0000000..67fcaad --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-go-20081024/hello.go.txt b/examples/ch1-02-hello-revolution/hello-go-20081024/hello.go.txt new file mode 100755 index 0000000..85051d0 --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-go-20090915/hello.go.txt b/examples/ch1-02-hello-revolution/hello-go-20090915/hello.go.txt new file mode 100755 index 0000000..0cb5e81 --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-go-20091211/hello.go b/examples/ch1-02-hello-revolution/hello-go-20091211/hello.go new file mode 100755 index 0000000..c2fbf9c --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-go-asm/hello.go b/examples/ch1-02-hello-revolution/hello-go-asm/hello.go new file mode 100755 index 0000000..20addf0 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-go-asm/hello.go @@ -0,0 +1,6 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +func main() diff --git a/examples/ch1-02-hello-revolution/hello-go-asm/hello_amd64.s b/examples/ch1-02-hello-revolution/hello-go-asm/hello_amd64.s new file mode 100755 index 0000000..3bf0038 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-go-asm/hello_amd64.s @@ -0,0 +1,19 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#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-revolution/hello-go-cgo/hello.go b/examples/ch1-02-hello-revolution/hello-go-cgo/hello.go new file mode 100755 index 0000000..f2a04aa --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-go-cgo/hello.go @@ -0,0 +1,16 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +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-revolution/hello-go-swig/hello.cc b/examples/ch1-02-hello-revolution/hello-go-swig/hello.cc new file mode 100755 index 0000000..f71d644 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-go-swig/hello.cc @@ -0,0 +1,8 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include + +void SayHello() { + std::cout << "Hello, World!" << std::endl; +} diff --git a/examples/ch1-02-hello-revolution/hello-go-swig/hello.go b/examples/ch1-02-hello-revolution/hello-go-swig/hello.go new file mode 100755 index 0000000..dde2af5 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-go-swig/hello.go @@ -0,0 +1,14 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +// +build ignore + +package main + +import ( + hello "." +) + +func main() { + hello.SayHello() +} diff --git a/examples/ch1-02-hello-revolution/hello-go-swig/hello.swigcxx b/examples/ch1-02-hello-revolution/hello-go-swig/hello.swigcxx new file mode 100755 index 0000000..ac0f3c5 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-go-swig/hello.swigcxx @@ -0,0 +1,8 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +%module main + +%inline %{ +extern void SayHello(); +%} diff --git a/examples/ch1-02-hello-revolution/hello-go-v2/hello.go b/examples/ch1-02-hello-revolution/hello-go-v2/hello.go new file mode 100755 index 0000000..7c2b866 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-go-v2/hello.go @@ -0,0 +1,23 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +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-revolution/hello-limbo/hello.limbo b/examples/ch1-02-hello-revolution/hello-limbo/hello.limbo new file mode 100755 index 0000000..dcd78cb --- /dev/null +++ b/examples/ch1-02-hello-revolution/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-revolution/hello-newsqueak/hello.newsqueak b/examples/ch1-02-hello-revolution/hello-newsqueak/hello.newsqueak new file mode 100755 index 0000000..409e345 --- /dev/null +++ b/examples/ch1-02-hello-revolution/hello-newsqueak/hello.newsqueak @@ -0,0 +1 @@ +print("Hello,", "World", "\n"); diff --git a/examples/ch1-02-hello-revolution/prime-newsqueak/prime.newsqueak b/examples/ch1-02-hello-revolution/prime-newsqueak/prime.newsqueak new file mode 100755 index 0000000..d915c32 --- /dev/null +++ b/examples/ch1-02-hello-revolution/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();