-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cb6ae7
commit a62d684
Showing
100 changed files
with
19,606 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
for (i = 0; i < 5; i++) { # 类似c语言的for循环, '()'必须要有 | ||
if (i > 4) { break } | ||
if (i == 2) { continue } | ||
printf("i is %d\n", i) | ||
} | ||
|
||
i = 0 | ||
for (; i < 5; i++) { # 无初期化语句 | ||
if (i > 4) { break } | ||
if (i == 2) { continue } | ||
printf("i is %d\n", i) | ||
} | ||
|
||
i = 0 | ||
for (; i < 5;;) { # 无初期化和更新语句 | ||
if (i > 4) { break } | ||
if (i == 2) { i++ continue } | ||
printf("i is %d\n", i) | ||
i++ # 更新语句 | ||
} | ||
|
||
i = 0 | ||
for (;;;) { # 等价于'for { block }'语句 | ||
if (i > 4) { break } | ||
printf("i is %d\n", i) | ||
i++ # 更新语句 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
s1 = "hello, 黄" # strings are UTF-8 encoded | ||
println(s1) | ||
|
||
三 = 3 # UTF-8 identifier | ||
println(三) | ||
|
||
i = 20000000 # int | ||
println(i) | ||
|
||
b = true # bool | ||
println(b) | ||
|
||
a = [1, "2"] # array | ||
println(a) | ||
|
||
if 1 in a { | ||
println("1 in [1, \"2\"]") | ||
} | ||
if !(2 in a) { | ||
println("2 not in [1, \"2\"]") | ||
} | ||
|
||
h = {"a": 1, "b": 2} # hash | ||
println(h) | ||
|
||
if "b" in h { | ||
println("\"b\" in h") | ||
} | ||
|
||
t = (1,2,3) # tuple | ||
println(t) | ||
|
||
n = nil | ||
println(n) | ||
|
||
#printf builtin | ||
printf("2**3=%g, 2.34.floor=%.0f\n", 2.pow(3), 2.34.floor()) | ||
|
||
/* this is a | ||
multiple assignment | ||
*/ | ||
a, b, c = 2, false, ["x", "y", "z"] | ||
printf("a=%d,b=%t, c=%v\n", a, b, c) | ||
|
||
if "hello" in "hello world" { | ||
println("\"hello\" in \"hello world\"") | ||
} | ||
|
||
nums = 1..10 | ||
printf("nums = %s\n", nums) | ||
|
||
for item in 1..10 { | ||
println(item) | ||
} | ||
|
||
println("--------------------------------") | ||
for item in 10..5 { | ||
println(item) | ||
} | ||
|
||
println("--------------------------------") | ||
for item in fn(a,b){ a + b }(1,1) .. fn(a,b){ a - b }(10,5) { # 即 'for item in 2..5' | ||
println(item) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
println(10 < 25 < 30) | ||
println(10 < (10 + 15) <= 25) | ||
println(10 < 5 <= 8) | ||
println(10 <= 25 <= 25) | ||
println("abc" < "def" < "ghi") | ||
|
||
fn sum (x,y) { x + y } | ||
println(10 < sum(10, 15) <= 25) | ||
|
||
if 10 < 25 < 30 { | ||
println("10 < 25 < 30") | ||
} | ||
|
||
if 10 < 25 <= 25 { | ||
println("10 < 25 <= 25") | ||
} | ||
|
||
if 10 < 5 <= 8 { | ||
println("10 < 5 <= 8") | ||
} | ||
|
||
if 10 <= 25 <= 25 { | ||
println("10 <= 25 <= 25") | ||
} | ||
|
||
if 10 <= 25 != 30 { | ||
println("10 <= 25 != 30") | ||
} | ||
|
||
if "abc" < "def" < "ghi" { | ||
println("abc < def < ghi") | ||
} | ||
|
||
a = 12 | ||
if 10 != a < 13 { | ||
println("10 != a < 13") | ||
} | ||
|
||
#error | ||
#if "abc" < "def" < "ghi" < "jkl" { | ||
# println("abc < def < ghi < jkl") | ||
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
a = 5 | ||
a += 12 | ||
|
||
b = 8 | ||
b -= 3 | ||
|
||
c = 12 | ||
c *= 2 | ||
|
||
d = 16 | ||
d /= 2 | ||
|
||
printf("a=%d, b=%d, c=%d, d=%d\n", a, b, c, d) | ||
|
||
s = "hello" | ||
s += " world" | ||
println(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#if/else-if | ||
x = 12 | ||
result = if x > 10 {2} else if x > 5 {3} else {4} | ||
println(result == 2) | ||
|
||
x = 10 | ||
result = if x > 10 {2} else if x > 5 {3} else {4} | ||
println(result == 3) | ||
|
||
x = 3 | ||
result = if x > 10 {2} else if x > 5 {3} else {4} | ||
println(result == 4) | ||
|
||
x = 8 | ||
result = if x >= 8 {2} else if x > 5 {3} else {4} | ||
println(result == 2) | ||
|
||
x = 8 | ||
result = if x <= 8 {2} else if x > 5 {3} else {4} | ||
println(result == 2) | ||
|
||
x = 8 | ||
result = if x == 8 {2} else if x > 5 {3} else {4} | ||
println(result == 2) | ||
|
||
x = 8 | ||
result = if x != 8 {2} else if x > 5 {3} else {4} | ||
println(result == 3) | ||
|
||
#for | ||
arr = [1, true, "Hello"]; | ||
for item in arr { | ||
println(item) | ||
} | ||
println() | ||
|
||
for idx, item in arr { | ||
if idx == 2 { break } | ||
println(item) | ||
} | ||
println() | ||
|
||
hash = {"name": "huanghaifeng", "height": 165} | ||
for k, v in hash { | ||
printf("key=%s, value=%v\n", k, v) | ||
} | ||
|
||
str = "Hello" | ||
for c in str { | ||
println(c) | ||
} | ||
println() | ||
|
||
tup = (1, true, "Hello") | ||
for item in tup { | ||
println(item) | ||
} | ||
println() | ||
|
||
for idx, item in tup { | ||
if idx == 2 { break } | ||
println(item) | ||
} | ||
println() | ||
|
||
#while | ||
x = 3 | ||
while x-- > 0 { | ||
println(x) | ||
} | ||
println() | ||
|
||
x = 5 | ||
while x-- > 0 { | ||
println(x) | ||
if x == 2 { break } | ||
} | ||
println() | ||
|
||
x = 5 | ||
while x-- > 0 { | ||
if x == 4 { continue } | ||
else if x == 2 { break } | ||
println(x) | ||
} | ||
println() | ||
|
||
#do | ||
x = 3 | ||
do { | ||
x-- | ||
println(x) | ||
if x == 1 { break } | ||
} | ||
println() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
file, err = open("./file.log", "w+") | ||
if err { | ||
println(err) | ||
os.exit(1) | ||
} | ||
|
||
file.writeLine("This is the first line") | ||
file.writeLine("This is the second line") | ||
file.writeString("这是第三行\n") | ||
file.close() | ||
|
||
|
||
printf("=====Reading file=====\n") | ||
file, err = open("./file.log", "r") | ||
if err { | ||
println(err) | ||
os.exit(1) | ||
} | ||
|
||
println(file.readLine()) | ||
println(file.readLine()) | ||
println(file.readLine()) | ||
file.close() | ||
|
||
# stdin, stdout | ||
print("Please type your name:") | ||
name = stdin.readLine() | ||
stdout.writeLine("Hello " + name + "!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#function statement | ||
fn addFunc(x,y) { | ||
return x+ y | ||
} | ||
println(addFunc(2,3)) | ||
|
||
#function literal | ||
sumFunc = fn(x,y) { | ||
return x+ y | ||
} | ||
println(sumFunc(2,3)) | ||
|
||
##function literal with direct call | ||
sumResult = fn(x,y) { return x+ y }(2,3) | ||
println(sumResult) | ||
|
||
#function with multiple return value | ||
fn math(x, y) { | ||
return x+y, x-y | ||
} | ||
a, b = math(5,3) | ||
printf("a=%g, b=%g\n", a, b) | ||
|
||
fn math2(x, y){ | ||
return x+y, x-y, x * y | ||
} | ||
a, _, c = math2(5,3) | ||
printf("a=%g, c=%g\n", a, c) | ||
|
||
# anonymous functions(lambdas) | ||
let add = fn (x, factor) { | ||
x + factor(x) | ||
} | ||
result = add(5, x => x * 2) | ||
println(result) # result: 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# using go's Printf | ||
fmt.Printf("Hello %s!\n", "go function") | ||
|
||
# using go's Sprintf | ||
s = fmt.Sprintf("Hello %s!", "World") | ||
# print with magpie's builtin function `print` | ||
println(s) | ||
|
||
fmt.Println(runtime.GOOS) | ||
fmt.Println(runtime.GOARCH) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import sub_package.calc | ||
|
||
println(Add(2,3)) | ||
|
||
math = Math(2,3) | ||
printf("math.Add() = %g\n", math.Add()) | ||
|
||
printf("Math.add() = %g\n", Math(2,3).Add()) | ||
|
||
# error, '_add' not exported | ||
# println(_add(2,3)) |
Oops, something went wrong.