Skip to content

Commit

Permalink
upload 49
Browse files Browse the repository at this point in the history
  • Loading branch information
haifenghuang authored Jul 1, 2022
1 parent f342a68 commit ecc49c8
Show file tree
Hide file tree
Showing 53 changed files with 10,373 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 49/examples/Cfor.mp
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++ # 更新语句
}
64 changes: 64 additions & 0 deletions 49/examples/basic.mp
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)
}
42 changes: 42 additions & 0 deletions 49/examples/basic2.mp
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")
#}
33 changes: 33 additions & 0 deletions 49/examples/cmd.mp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 使用命令对象的方式的脚本代码 */
res = `curl.exe -s https://api.ipify.org?format=json`
if !res.ok() {
printf("An error occurred: %s\n", res)
} else {
printf("res: %s\n", res)
}

date = `date /t`
if !date.ok() {
printf("An error occurred: %s\n", res)
} else {
printf("date: %s\n", date)
}



/* 使用返回多个值的方式的脚本代码
res, ok = `curl.exe -s https://api.ipify.org?format=json`

if !ok {
printf("An error occurred: %s\n", res)
} else {
printf("res: %s\n", res)
}

res, ok = `date /t`
if !ok {
printf("An error occurred: %s\n", res)
} else {
printf("date: %s\n", res)
}
*/
17 changes: 17 additions & 0 deletions 49/examples/compound_assign_operators.mp
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)
95 changes: 95 additions & 0 deletions 49/examples/control_flow.mp
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()
23 changes: 23 additions & 0 deletions 49/examples/decorator.mp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
fn timer(otherfn) {
return fn() {
println("timer start")
otherfn($_)
println("timer end")
}
}

fn log(otherfn) {
return fn() {
println("otherfn start")
otherfn($_)
println("otherfn end")
}
}

@log
@timer
fn sum(x, y) {
printf("%d + %d = %d\n", x, y, x+y)
}

sum(1,2)
39 changes: 39 additions & 0 deletions 49/examples/enhanchedHash.mp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
h = {"a": 1, "b": 2, "x":[12,34,56]} # hash
h.a = 10
printf("h.a=%d\n", h.a)
h.c= fn(x,y) { return x + y }
println(h.c(2,3))
h["d"] = "www"
println(h.d)
println(h.x[2])


fn demo() {
h = {}
h.a = 10
h.b = 2
h.c = fn(x,y) { return x + y }
return h
}

hs = demo()
printf("hs.a=%d\n", hs.a)
println(hs.c(2,3))
println(hs.b)

//array, tuple & string
a = [1, "hello world"] // array
println(a.1)
a.1 = "hello"
println(a.1)

t = (1, "hello world") // tuple
println(t.1)


s = "Hello World" //string
println(s.6)
s.6 = "myz"
println(s.6)
println(s)

28 changes: 28 additions & 0 deletions 49/examples/file.mp
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 + "!")
Loading

0 comments on commit ecc49c8

Please sign in to comment.