| 1 | module main |
| 2 | |
| 3 | struct Point { |
| 4 | mut: |
| 5 | x int |
| 6 | y int |
| 7 | } |
| 8 | |
| 9 | struct Rect { |
| 10 | origin Point |
| 11 | width int |
| 12 | height int |
| 13 | } |
| 14 | |
| 15 | enum Color { |
| 16 | red |
| 17 | green |
| 18 | blue |
| 19 | } |
| 20 | |
| 21 | enum Direction { |
| 22 | up |
| 23 | down |
| 24 | left |
| 25 | right |
| 26 | } |
| 27 | |
| 28 | fn add(a int, b int) int { |
| 29 | return a + b |
| 30 | } |
| 31 | |
| 32 | fn multiply(a int, b int) int { |
| 33 | return a * b |
| 34 | } |
| 35 | |
| 36 | fn (p Point) sum() int { |
| 37 | return p.x + p.y |
| 38 | } |
| 39 | |
| 40 | fn (p Point) scale(factor int) Point { |
| 41 | return Point{ |
| 42 | x: p.x * factor |
| 43 | y: p.y * factor |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn (r Rect) area() int { |
| 48 | return r.width * r.height |
| 49 | } |
| 50 | |
| 51 | fn max(a int, b int) int { |
| 52 | if a > b { |
| 53 | return a |
| 54 | } |
| 55 | return b |
| 56 | } |
| 57 | |
| 58 | fn abs(n int) int { |
| 59 | if n < 0 { |
| 60 | return -n |
| 61 | } |
| 62 | return n |
| 63 | } |
| 64 | |
| 65 | fn fibonacci(n int) int { |
| 66 | if n <= 1 { |
| 67 | return n |
| 68 | } |
| 69 | mut a := 0 |
| 70 | mut b := 1 |
| 71 | for i := 2; i <= n; i++ { |
| 72 | c := a + b |
| 73 | a = b |
| 74 | b = c |
| 75 | } |
| 76 | return b |
| 77 | } |
| 78 | |
| 79 | fn color_name(c Color) string { |
| 80 | if int(c) == 0 { |
| 81 | return 'red' |
| 82 | } else if int(c) == 1 { |
| 83 | return 'green' |
| 84 | } else if int(c) == 2 { |
| 85 | return 'blue' |
| 86 | } |
| 87 | return 'unknown' |
| 88 | } |
| 89 | |
| 90 | fn collatz_steps(start int) int { |
| 91 | mut n := start |
| 92 | mut steps := 0 |
| 93 | for n > 1 { |
| 94 | if n % 2 == 0 { |
| 95 | n = n / 2 |
| 96 | } else { |
| 97 | n = n * 3 + 1 |
| 98 | } |
| 99 | steps = steps + 1 |
| 100 | } |
| 101 | return steps |
| 102 | } |
| 103 | |
| 104 | fn sum_range(start int, end int) int { |
| 105 | mut total := 0 |
| 106 | for i := start; i < end; i++ { |
| 107 | total = total + i |
| 108 | } |
| 109 | return total |
| 110 | } |
| 111 | |
| 112 | fn is_even(n int) bool { |
| 113 | return n % 2 == 0 |
| 114 | } |
| 115 | |
| 116 | fn count_even(limit int) int { |
| 117 | mut c := 0 |
| 118 | for i := 0; i < limit; i++ { |
| 119 | if is_even(i) { |
| 120 | c = c + 1 |
| 121 | } |
| 122 | } |
| 123 | return c |
| 124 | } |
| 125 | |
| 126 | fn direction_dx(d Direction) int { |
| 127 | if int(d) == 2 { |
| 128 | return -1 |
| 129 | } else if int(d) == 3 { |
| 130 | return 1 |
| 131 | } |
| 132 | return 0 |
| 133 | } |
| 134 | |
| 135 | fn direction_dy(d Direction) int { |
| 136 | if int(d) == 0 { |
| 137 | return -1 |
| 138 | } else if int(d) == 1 { |
| 139 | return 1 |
| 140 | } |
| 141 | return 0 |
| 142 | } |
| 143 | |
| 144 | __global g_val int |
| 145 | |
| 146 | fn fib_recursive(n int) int { |
| 147 | if n <= 1 { |
| 148 | return n |
| 149 | } |
| 150 | return fib_recursive(n - 1) + fib_recursive(n - 2) |
| 151 | } |
| 152 | |
| 153 | fn factorial_recursive(n int) int { |
| 154 | if n <= 1 { |
| 155 | return 1 |
| 156 | } |
| 157 | return n * factorial_recursive(n - 1) |
| 158 | } |
| 159 | |
| 160 | fn gcd(a int, b int) int { |
| 161 | if b == 0 { |
| 162 | return a |
| 163 | } |
| 164 | return gcd(b, a % b) |
| 165 | } |
| 166 | |
| 167 | fn power(base int, exp int) int { |
| 168 | mut result := 1 |
| 169 | for i := 0; i < exp; i++ { |
| 170 | result = result * base |
| 171 | } |
| 172 | return result |
| 173 | } |
| 174 | |
| 175 | fn swap_point(mut p Point) { |
| 176 | tmp := p.x |
| 177 | p.x = p.y |
| 178 | p.y = tmp |
| 179 | } |
| 180 | |
| 181 | fn scale_point(mut p Point, factor int) { |
| 182 | p.x = p.x * factor |
| 183 | p.y = p.y * factor |
| 184 | } |
| 185 | |
| 186 | fn nested_return(x int) int { |
| 187 | if x < 10 { |
| 188 | return 100 |
| 189 | } else { |
| 190 | if x < 20 { |
| 191 | return 200 |
| 192 | } else { |
| 193 | return 300 |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | fn sum_many(a int, b int, c int, d int, e int, f int, g int, h int) int { |
| 199 | return a + b + c + d + e + f + g + h |
| 200 | } |
| 201 | |
| 202 | fn clamp(x int, lo int, hi int) int { |
| 203 | if x < lo { |
| 204 | return lo |
| 205 | } else if x > hi { |
| 206 | return hi |
| 207 | } |
| 208 | return x |
| 209 | } |
| 210 | |
| 211 | fn print_str(s string) { |
| 212 | C.puts(s.str) |
| 213 | } |
| 214 | |
| 215 | fn print_int(n int) { |
| 216 | C.printf(c'%d\n', n) |
| 217 | } |
| 218 | |
| 219 | fn main() { |
| 220 | // 1. Basic arithmetic |
| 221 | x := 10 |
| 222 | y := 20 |
| 223 | z := add(x, y) |
| 224 | |
| 225 | if z > 25 { |
| 226 | print_str('big') |
| 227 | } else { |
| 228 | print_str('small') |
| 229 | } |
| 230 | |
| 231 | // 2. For loop with sum |
| 232 | mut sum := 0 |
| 233 | for i := 0; i < 5; i++ { |
| 234 | sum = sum + i |
| 235 | } |
| 236 | if sum == 10 { |
| 237 | print_str('sum ok') |
| 238 | } |
| 239 | |
| 240 | // 3. Struct and method |
| 241 | p := Point{ |
| 242 | x: 3 |
| 243 | y: 4 |
| 244 | } |
| 245 | ps := p.sum() |
| 246 | if ps == 7 { |
| 247 | print_str('struct ok') |
| 248 | } |
| 249 | |
| 250 | // 4. While loop |
| 251 | mut count := 3 |
| 252 | for count > 0 { |
| 253 | count = count - 1 |
| 254 | } |
| 255 | if count == 0 { |
| 256 | print_str('loop ok2') |
| 257 | } |
| 258 | |
| 259 | // 5. Print the sum result |
| 260 | print_int(z) |
| 261 | |
| 262 | // 6. Nested function calls |
| 263 | result := add(multiply(3, 4), multiply(5, 6)) |
| 264 | if result == 42 { |
| 265 | print_str('nested ok') |
| 266 | } |
| 267 | |
| 268 | // 7. Multiple comparisons and boolean logic |
| 269 | a := 15 |
| 270 | b := 25 |
| 271 | if a < b && b < 30 { |
| 272 | print_str('logic ok') |
| 273 | } |
| 274 | |
| 275 | if a == 15 || b == 99 { |
| 276 | print_str('or ok') |
| 277 | } |
| 278 | |
| 279 | // 8. Negation and abs |
| 280 | neg := -42 |
| 281 | pos := abs(neg) |
| 282 | if pos == 42 { |
| 283 | print_str('abs ok') |
| 284 | } |
| 285 | |
| 286 | // 9. Nested if/else |
| 287 | val := 50 |
| 288 | if val < 0 { |
| 289 | print_str('negative') |
| 290 | } else if val == 0 { |
| 291 | print_str('zero') |
| 292 | } else if val < 100 { |
| 293 | print_str('medium') |
| 294 | } else { |
| 295 | print_str('large') |
| 296 | } |
| 297 | |
| 298 | // 10. Chained arithmetic |
| 299 | chain := (10 + 20) * 3 - 5 |
| 300 | if chain == 85 { |
| 301 | print_str('chain ok') |
| 302 | } |
| 303 | |
| 304 | // 11. Bitwise operations |
| 305 | bw := 0xFF & 0x0F |
| 306 | if bw == 15 { |
| 307 | print_str('bit and ok') |
| 308 | } |
| 309 | |
| 310 | bw2 := 0xA0 | 0x05 |
| 311 | if bw2 == 0xA5 { |
| 312 | print_str('bit or ok') |
| 313 | } |
| 314 | |
| 315 | bw3 := 1 << 4 |
| 316 | if bw3 == 16 { |
| 317 | print_str('shift ok') |
| 318 | } |
| 319 | |
| 320 | // 12. Struct method returning struct |
| 321 | p2 := Point{ |
| 322 | x: 2 |
| 323 | y: 3 |
| 324 | } |
| 325 | p3 := p2.scale(10) |
| 326 | if p3.x == 20 && p3.y == 30 { |
| 327 | print_str('scale ok') |
| 328 | } |
| 329 | |
| 330 | // 13. Nested struct |
| 331 | r := Rect{ |
| 332 | origin: Point{ |
| 333 | x: 1 |
| 334 | y: 2 |
| 335 | } |
| 336 | width: 10 |
| 337 | height: 5 |
| 338 | } |
| 339 | if r.area() == 50 { |
| 340 | print_str('rect ok') |
| 341 | } |
| 342 | if r.origin.x == 1 && r.origin.y == 2 { |
| 343 | print_str('nested struct ok') |
| 344 | } |
| 345 | |
| 346 | // 14. Fibonacci |
| 347 | fib := fibonacci(10) |
| 348 | if fib == 55 { |
| 349 | print_str('fib ok') |
| 350 | } |
| 351 | |
| 352 | // 15. Max function |
| 353 | m := max(33, 77) |
| 354 | if m == 77 { |
| 355 | print_str('max ok') |
| 356 | } |
| 357 | |
| 358 | // 16. Multiple mutable updates |
| 359 | mut acc := 1 |
| 360 | for j := 1; j <= 5; j++ { |
| 361 | acc = acc * j |
| 362 | } |
| 363 | if acc == 120 { |
| 364 | print_str('factorial ok') |
| 365 | } |
| 366 | |
| 367 | // 17. Comparison operators |
| 368 | if 10 >= 10 { |
| 369 | print_str('ge ok') |
| 370 | } |
| 371 | if 9 <= 10 { |
| 372 | print_str('le ok') |
| 373 | } |
| 374 | if 10 != 11 { |
| 375 | print_str('ne ok') |
| 376 | } |
| 377 | |
| 378 | // 18. Modulo |
| 379 | rem := 17 % 5 |
| 380 | if rem == 2 { |
| 381 | print_str('mod ok') |
| 382 | } |
| 383 | |
| 384 | // 19. Mutable struct fields |
| 385 | mut mp := Point{ |
| 386 | x: 10 |
| 387 | y: 20 |
| 388 | } |
| 389 | mp.x = 100 |
| 390 | mp.y = mp.x + 50 |
| 391 | if mp.x == 100 && mp.y == 150 { |
| 392 | print_str('mut struct ok') |
| 393 | } |
| 394 | |
| 395 | // 20. Division |
| 396 | div := 100 / 3 |
| 397 | if div == 33 { |
| 398 | print_str('div ok') |
| 399 | } |
| 400 | |
| 401 | // 21. Enum values |
| 402 | c1 := Color.red |
| 403 | c2 := Color.blue |
| 404 | if int(c1) == 0 && int(c2) == 2 { |
| 405 | print_str('enum ok') |
| 406 | } |
| 407 | |
| 408 | // 22. Enum in function |
| 409 | name := color_name(Color.green) |
| 410 | C.printf(c'color: %s\n', name.str) |
| 411 | |
| 412 | // 23. Bool return value |
| 413 | if is_even(42) { |
| 414 | print_str('even ok') |
| 415 | } |
| 416 | if !is_even(7) { |
| 417 | print_str('odd ok') |
| 418 | } |
| 419 | |
| 420 | // 24. Nested loops |
| 421 | mut total := 0 |
| 422 | for i := 0; i < 3; i++ { |
| 423 | for j := 0; j < 4; j++ { |
| 424 | total = total + 1 |
| 425 | } |
| 426 | } |
| 427 | if total == 12 { |
| 428 | print_str('nested loops ok') |
| 429 | } |
| 430 | |
| 431 | // 25. Break in loop |
| 432 | mut found := -1 |
| 433 | for i := 0; i < 100; i++ { |
| 434 | if i * i > 50 { |
| 435 | found = i |
| 436 | break |
| 437 | } |
| 438 | } |
| 439 | if found == 8 { |
| 440 | print_str('break ok') |
| 441 | } |
| 442 | |
| 443 | // 26. Continue in loop |
| 444 | mut odd_sum := 0 |
| 445 | for i := 0; i < 10; i++ { |
| 446 | if is_even(i) { |
| 447 | continue |
| 448 | } |
| 449 | odd_sum = odd_sum + i |
| 450 | } |
| 451 | if odd_sum == 25 { |
| 452 | print_str('continue ok') |
| 453 | } |
| 454 | |
| 455 | // 27. Enum as direction |
| 456 | dx := direction_dx(Direction.right) |
| 457 | dy := direction_dy(Direction.down) |
| 458 | if dx == 1 && dy == 1 { |
| 459 | print_str('direction ok') |
| 460 | } |
| 461 | |
| 462 | // 28. Compound assignment operators |
| 463 | mut ca := 10 |
| 464 | ca += 5 |
| 465 | ca -= 3 |
| 466 | ca *= 2 |
| 467 | if ca == 24 { |
| 468 | print_str('compound ok') |
| 469 | } |
| 470 | |
| 471 | // 29. XOR |
| 472 | xr := 0xFF ^ 0x0F |
| 473 | if xr == 0xF0 { |
| 474 | print_str('xor ok') |
| 475 | } |
| 476 | |
| 477 | // 30. Right shift |
| 478 | rs := 256 >> 3 |
| 479 | if rs == 32 { |
| 480 | print_str('rshift ok') |
| 481 | } |
| 482 | |
| 483 | // 31. Collatz conjecture (complex loop logic) |
| 484 | steps := collatz_steps(27) |
| 485 | if steps == 111 { |
| 486 | print_str('collatz ok') |
| 487 | } |
| 488 | |
| 489 | // 32. count_even (function call in loop condition body) |
| 490 | ev := count_even(10) |
| 491 | if ev == 5 { |
| 492 | print_str('count ok') |
| 493 | } |
| 494 | |
| 495 | // 33. Struct with zero fields |
| 496 | origin := Point{} |
| 497 | if origin.x == 0 && origin.y == 0 { |
| 498 | print_str('zero struct ok') |
| 499 | } |
| 500 | |
| 501 | // 34. Multiple early returns |
| 502 | m2 := max(max(10, 20), max(15, 5)) |
| 503 | if m2 == 20 { |
| 504 | print_str('multi ret ok') |
| 505 | } |
| 506 | |
| 507 | // 35. sum_range |
| 508 | sr := sum_range(1, 11) |
| 509 | if sr == 55 { |
| 510 | print_str('range sum ok') |
| 511 | } |
| 512 | |
| 513 | // 36. Recursive fibonacci |
| 514 | rf := fib_recursive(10) |
| 515 | if rf == 55 { |
| 516 | print_str('rec fib ok') |
| 517 | } |
| 518 | |
| 519 | // 37. Recursive factorial |
| 520 | fact := factorial_recursive(6) |
| 521 | if fact == 720 { |
| 522 | print_str('rec fact ok') |
| 523 | } |
| 524 | |
| 525 | // 38. GCD |
| 526 | g := gcd(48, 18) |
| 527 | if g == 6 { |
| 528 | print_str('gcd ok') |
| 529 | } |
| 530 | |
| 531 | // 39. Power |
| 532 | pw := power(2, 10) |
| 533 | if pw == 1024 { |
| 534 | print_str('power ok') |
| 535 | } |
| 536 | |
| 537 | // 40. Heap allocation (&Point{}) |
| 538 | hp := &Point{ |
| 539 | x: 42 |
| 540 | y: 84 |
| 541 | } |
| 542 | if hp.x == 42 && hp.y == 84 { |
| 543 | print_str('heap ok') |
| 544 | } |
| 545 | |
| 546 | // 41. Mut struct parameter (swap) |
| 547 | mut sp := Point{ |
| 548 | x: 10 |
| 549 | y: 20 |
| 550 | } |
| 551 | swap_point(mut sp) |
| 552 | if sp.x == 20 && sp.y == 10 { |
| 553 | print_str('swap ok') |
| 554 | } |
| 555 | |
| 556 | // 42. Mut struct parameter (scale) |
| 557 | mut sc := Point{ |
| 558 | x: 5 |
| 559 | y: 3 |
| 560 | } |
| 561 | scale_point(mut sc, 10) |
| 562 | if sc.x == 50 && sc.y == 30 { |
| 563 | print_str('mut param ok') |
| 564 | } |
| 565 | |
| 566 | // 43. Nested return (all branches return) |
| 567 | nr1 := nested_return(5) |
| 568 | nr2 := nested_return(15) |
| 569 | nr3 := nested_return(25) |
| 570 | if nr1 == 100 && nr2 == 200 && nr3 == 300 { |
| 571 | print_str('nested ret ok') |
| 572 | } |
| 573 | |
| 574 | // 44. Global variable |
| 575 | g_val = 100 |
| 576 | g_val += 50 |
| 577 | g_val -= 20 |
| 578 | if g_val == 130 { |
| 579 | print_str('global ok') |
| 580 | } |
| 581 | |
| 582 | // 45. Many arguments (8 args) |
| 583 | sm := sum_many(1, 2, 3, 4, 5, 6, 7, 8) |
| 584 | if sm == 36 { |
| 585 | print_str('8 args ok') |
| 586 | } |
| 587 | |
| 588 | // 46. Infinite loop with break |
| 589 | mut inf := 0 |
| 590 | for { |
| 591 | inf++ |
| 592 | if inf == 10 { |
| 593 | break |
| 594 | } |
| 595 | } |
| 596 | if inf == 10 { |
| 597 | print_str('inf loop ok') |
| 598 | } |
| 599 | |
| 600 | // 47. Assert |
| 601 | assert 2 + 2 == 4 |
| 602 | assert 10 > 5 |
| 603 | print_str('assert ok') |
| 604 | |
| 605 | // 48. Double negation |
| 606 | dn := !!true |
| 607 | if dn { |
| 608 | print_str('double neg ok') |
| 609 | } |
| 610 | |
| 611 | // 49. Clamp |
| 612 | cl1 := clamp(5, 0, 10) |
| 613 | cl2 := clamp(-5, 0, 10) |
| 614 | cl3 := clamp(15, 0, 10) |
| 615 | if cl1 == 5 && cl2 == 0 && cl3 == 10 { |
| 616 | print_str('clamp ok') |
| 617 | } |
| 618 | |
| 619 | // 50. Heap struct method call |
| 620 | hp2 := &Point{ |
| 621 | x: 7 |
| 622 | y: 8 |
| 623 | } |
| 624 | hs := hp2.sum() |
| 625 | if hs == 15 { |
| 626 | print_str('heap method ok') |
| 627 | } |
| 628 | } |
| 629 | |