mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Обновлены примеры
This commit is contained in:
parent
2d7feeb0cd
commit
3348a513df
57
examples/canvas/animate_line_thread.own
Normal file
57
examples/canvas/animate_line_thread.own
Normal file
@ -0,0 +1,57 @@
|
||||
use "canvas"
|
||||
use "std"
|
||||
|
||||
w = 800 h = 600
|
||||
window("Animate line with thread", w, h)
|
||||
|
||||
// Крайние точки линии
|
||||
x1 = rand(w) y1 = rand(h)
|
||||
x2 = rand(w) y2 = rand(h)
|
||||
|
||||
// Направление движения
|
||||
d1x = rand() d1y = -rand()
|
||||
d2x = rand() d2y = rand()
|
||||
|
||||
// Очищаем экран
|
||||
color(rand(#FFFFFF))
|
||||
frect(0,0,w,h)
|
||||
|
||||
run = 1
|
||||
thread("changehue")
|
||||
|
||||
def changehue() {
|
||||
hue = 0
|
||||
while run {
|
||||
hue = hue + 0.0001
|
||||
if (hue >= 1) hue = 0
|
||||
qr = hue * 6 // временно для расчёта
|
||||
hueindex = (qr - qr % 1) % 6
|
||||
f = qr - (qr - qr % 1)
|
||||
|
||||
if hueindex == 0 color(255, f*255, 0)
|
||||
else if hueindex == 1 color(255 - f*255, 255, 0)
|
||||
else if hueindex == 2 color(0, 255, f*255)
|
||||
else if hueindex == 3 color(0, 255-f*255, 255)
|
||||
else if hueindex == 4 color(f*255, 0, 255)
|
||||
else if hueindex == 5 color(255, 0, 255-f*255)
|
||||
sleep(5)
|
||||
}
|
||||
}
|
||||
|
||||
while run {
|
||||
if checkhoriz(x1) d1x = -d1x
|
||||
if checkhoriz(x2) d2x = -d2x
|
||||
if checkvert(y1) d1y = -d1y
|
||||
if checkvert(y2) d2y = -d2y
|
||||
|
||||
x1 = x1 + d1x x2 = x2 + d2x
|
||||
y1 = y1 + d1y y2 = y2 + d2y
|
||||
|
||||
line(x1, y1, x2, y2)
|
||||
repaint()
|
||||
sleep(10)
|
||||
if keypressed() == VK_ESCAPE run = 0
|
||||
}
|
||||
|
||||
def checkhoriz(px) return (px >= w || px < 0)
|
||||
def checkvert(py) return (py >= h || py < 0)
|
24
examples/common/array.own
Normal file
24
examples/common/array.own
Normal file
@ -0,0 +1,24 @@
|
||||
arr1 = [1, 2, 3, 4, 5]
|
||||
println arr1[0]
|
||||
println arr1
|
||||
|
||||
use "std"
|
||||
arr2 = newarray(5)
|
||||
arr2[2] = 9
|
||||
arr2 = arr2 :: 4
|
||||
println arr2
|
||||
|
||||
arr3 = arr1 :: arr2
|
||||
arr4 = arr1 << arr2
|
||||
println arr3
|
||||
println arr4
|
||||
|
||||
|
||||
// Array 2d
|
||||
arr5 = newarray(4, 4)
|
||||
println arr5
|
||||
arr5[1] = arr1
|
||||
println arr5
|
||||
|
||||
arr6 = [arr5[1], arr5[2]]
|
||||
println arr6
|
12
examples/common/destructuring_assignment.own
Normal file
12
examples/common/destructuring_assignment.own
Normal file
@ -0,0 +1,12 @@
|
||||
use "std"
|
||||
|
||||
println "Destructuring assignment"
|
||||
arr = ["a", "b", "c"]
|
||||
extract(var1, var2, var3) = arr
|
||||
echo(var1, var2, var3)
|
||||
|
||||
// Swap
|
||||
println "Swap variables"
|
||||
echo(var1, var2)
|
||||
extract(var2, var1) = [var1, var2]
|
||||
echo(var1, var2)
|
52
examples/common/loops.own
Normal file
52
examples/common/loops.own
Normal file
@ -0,0 +1,52 @@
|
||||
// While loop
|
||||
println "While loop"
|
||||
a = 0
|
||||
while a < 3 {
|
||||
print a
|
||||
a++
|
||||
}
|
||||
|
||||
|
||||
// Do-while loop
|
||||
println "\n\nDo-while loop"
|
||||
a = 0
|
||||
do {
|
||||
print a
|
||||
a++
|
||||
} while (a < 3)
|
||||
|
||||
|
||||
// For loop
|
||||
println "\n\nFor loop"
|
||||
for a = 0, a < 10, a++
|
||||
print a
|
||||
|
||||
|
||||
// Foreach loop
|
||||
println "\n\nForeach loop on array"
|
||||
arr = [1, 2, 3, 4, 5]
|
||||
for a : arr
|
||||
print a
|
||||
|
||||
use "std"
|
||||
println "\n\nForeach loop on map"
|
||||
object = {"key1": "value1", "key2": 100, "arr": [0, 1]}
|
||||
for key, value : object
|
||||
echo(key, ":", value)
|
||||
|
||||
|
||||
use "functional"
|
||||
|
||||
// Functional loop
|
||||
println "\n\nFunctional loop on array"
|
||||
foreach(arr, ::echo)
|
||||
foreach(arr, def(v) {
|
||||
print v
|
||||
})
|
||||
|
||||
println "\n\nfunctional loop on map"
|
||||
foreach(object, ::echo)
|
||||
foreach(object, def(k, v) {
|
||||
print " " + k + " : " + v
|
||||
})
|
||||
println ""
|
9
examples/common/map.own
Normal file
9
examples/common/map.own
Normal file
@ -0,0 +1,9 @@
|
||||
map1 = {"key": "value"}
|
||||
println map1["key"]
|
||||
println map1.key
|
||||
|
||||
map1["newkey"] = "newvalue"
|
||||
map1[0] = "zero"
|
||||
x = 400
|
||||
map1[x] = [1, 2, 3]
|
||||
println map1
|
@ -1,7 +1,7 @@
|
||||
use "std"
|
||||
|
||||
// Ternary operator
|
||||
a = 0
|
||||
b = 1
|
||||
|
||||
print (a ? "text1" : "text2") + "\n"
|
||||
print (b ? "text3" : "text4") + "\n"
|
39
examples/common/pattern_matching.own
Normal file
39
examples/common/pattern_matching.own
Normal file
@ -0,0 +1,39 @@
|
||||
use "std"
|
||||
use "types"
|
||||
|
||||
v = rand(10)
|
||||
println match v {
|
||||
case 0: "Zero"
|
||||
case 1: "One"
|
||||
case x if x <= 5: "Two..five"
|
||||
case _: "Six..nine"
|
||||
}
|
||||
|
||||
def match1(v) = match v {
|
||||
case 1: "One"
|
||||
case "Two": 2
|
||||
case x if typeof(x) == NUMBER: "Number"
|
||||
case x if typeof(x) == STRING: "String"
|
||||
case _: "other"
|
||||
}
|
||||
|
||||
println match1(1)
|
||||
println match1(322)
|
||||
println match1("test")
|
||||
println match1("Two")
|
||||
println match1([2])
|
||||
|
||||
// Recursive factorial
|
||||
println factorial(6)
|
||||
def factorial(n) = match n {
|
||||
case 0: 1
|
||||
case _: n * factorial(n - 1)
|
||||
}
|
||||
|
||||
// Pattern matching on arrays
|
||||
println printArrayRecursive([1, 2, 3, 4, 5, 6, 7])
|
||||
def printArrayRecursive(arr) = match arr {
|
||||
case [head :: tail]: "[" + head + ", " + printArrayRecursive(tail) + "]"
|
||||
case []: "[]"
|
||||
case last: "[" + last + ", []]"
|
||||
}
|
21
examples/common/thread.own
Normal file
21
examples/common/thread.own
Normal file
@ -0,0 +1,21 @@
|
||||
use "std"
|
||||
|
||||
def thread1() {
|
||||
i = 0
|
||||
while (i < 100) {
|
||||
print "i = " + i + "\n"
|
||||
i = i + 1
|
||||
sleep(100)
|
||||
}
|
||||
}
|
||||
|
||||
// thread("thread1")
|
||||
thread(::thread1)
|
||||
|
||||
k = 0
|
||||
while (k < 10) {
|
||||
print "k = " + k + "\n"
|
||||
k = k + 1
|
||||
sleep(1000)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Вычисляем факториал
|
||||
// Factorial
|
||||
def factorial(n) {
|
||||
if n == 1 return 1
|
||||
return n * factorial(n-1)
|
||||
@ -22,7 +22,7 @@ for i = 0, i < 10, i++
|
||||
print "\n"
|
||||
|
||||
|
||||
// Вычисляем факториал с помощью хвостовой рекурсии
|
||||
// Tail recursive factorial
|
||||
def factorial_tailrec(n) {
|
||||
def fact(acc, n) {
|
||||
if n == 0 return acc
|
||||
|
@ -66,7 +66,7 @@ while run {
|
||||
color(playerColor)
|
||||
circle(w2, h2, playerSize)
|
||||
color(0)
|
||||
drawstring(playerSize, w2-10, h2+4)
|
||||
drawstring(playerSize, w2-7, h2+4)
|
||||
|
||||
repaint()
|
||||
sleep(20)
|
||||
|
Loading…
Reference in New Issue
Block a user