From 3348a513df31d73b765c5a668f483d0dde2ae381 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 20 Feb 2016 10:12:26 +0200 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/canvas/animate_line_thread.own | 57 ++++++++++++++++++++ examples/common/array.own | 24 +++++++++ examples/common/destructuring_assignment.own | 12 +++++ examples/common/loops.own | 52 ++++++++++++++++++ examples/common/map.own | 9 ++++ examples/common/operators.own | 4 +- examples/common/pattern_matching.own | 39 ++++++++++++++ examples/common/thread.own | 21 ++++++++ examples/functions/factorial.own | 4 +- examples/game/agar.own | 2 +- 10 files changed, 219 insertions(+), 5 deletions(-) create mode 100644 examples/canvas/animate_line_thread.own create mode 100644 examples/common/array.own create mode 100644 examples/common/destructuring_assignment.own create mode 100644 examples/common/loops.own create mode 100644 examples/common/map.own create mode 100644 examples/common/pattern_matching.own create mode 100644 examples/common/thread.own diff --git a/examples/canvas/animate_line_thread.own b/examples/canvas/animate_line_thread.own new file mode 100644 index 0000000..f6a28cb --- /dev/null +++ b/examples/canvas/animate_line_thread.own @@ -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) \ No newline at end of file diff --git a/examples/common/array.own b/examples/common/array.own new file mode 100644 index 0000000..4b8711d --- /dev/null +++ b/examples/common/array.own @@ -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 \ No newline at end of file diff --git a/examples/common/destructuring_assignment.own b/examples/common/destructuring_assignment.own new file mode 100644 index 0000000..11b07c4 --- /dev/null +++ b/examples/common/destructuring_assignment.own @@ -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) \ No newline at end of file diff --git a/examples/common/loops.own b/examples/common/loops.own new file mode 100644 index 0000000..3c69fa7 --- /dev/null +++ b/examples/common/loops.own @@ -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 "" \ No newline at end of file diff --git a/examples/common/map.own b/examples/common/map.own new file mode 100644 index 0000000..cea1482 --- /dev/null +++ b/examples/common/map.own @@ -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 \ No newline at end of file diff --git a/examples/common/operators.own b/examples/common/operators.own index dae4e8a..80b57c5 100644 --- a/examples/common/operators.own +++ b/examples/common/operators.own @@ -1,7 +1,7 @@ use "std" +// Ternary operator a = 0 b = 1 - print (a ? "text1" : "text2") + "\n" -print (b ? "text3" : "text4") + "\n" +print (b ? "text3" : "text4") + "\n" \ No newline at end of file diff --git a/examples/common/pattern_matching.own b/examples/common/pattern_matching.own new file mode 100644 index 0000000..87acb77 --- /dev/null +++ b/examples/common/pattern_matching.own @@ -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 + ", []]" +} diff --git a/examples/common/thread.own b/examples/common/thread.own new file mode 100644 index 0000000..d71883d --- /dev/null +++ b/examples/common/thread.own @@ -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) +} + diff --git a/examples/functions/factorial.own b/examples/functions/factorial.own index 560aab4..3370015 100644 --- a/examples/functions/factorial.own +++ b/examples/functions/factorial.own @@ -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 diff --git a/examples/game/agar.own b/examples/game/agar.own index c75b731..5998989 100644 --- a/examples/game/agar.own +++ b/examples/game/agar.own @@ -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)