| 1 | <h2>Full example</h2> |
| 2 | <pre><code>// http://chaiscript.com/examples.html#ChaiScript_Language_Examples |
| 3 | // Source: https://gist.github.com/lefticus/cf058f2927fef68d58e0#file-chaiscript_overview-chai |
| 4 | |
| 5 | // ChaiScript supports the normal kind of control blocks you've come to expect from |
| 6 | // C++ and JavaScript |
| 7 | |
| 8 | |
| 9 | if (5 > 2) { |
| 10 | print("Yup, 5 > 2"); |
| 11 | } else if (2 > 5) { |
| 12 | // never gonna happen |
| 13 | } else { |
| 14 | // really not going to happen |
| 15 | } |
| 16 | |
| 17 | var x = true; |
| 18 | |
| 19 | while (x) |
| 20 | { |
| 21 | print("x was true") |
| 22 | x = false; |
| 23 | } |
| 24 | |
| 25 | for (var i = 1; i < 10; ++i) |
| 26 | { |
| 27 | print(i); // prints 1 through 9 |
| 28 | } |
| 29 | |
| 30 | |
| 31 | // function definition |
| 32 | |
| 33 | def myFunc(x) { |
| 34 | print(x); |
| 35 | } |
| 36 | |
| 37 | // function definition with function guards |
| 38 | def myFunc(x) : x > 2 && x < 5 { |
| 39 | print(to_string(x) + " is between 2 and 5") |
| 40 | } |
| 41 | |
| 42 | def myFunc(x) : x >= 5 { |
| 43 | print(t_string(x) + " is greater than or equal to 5") |
| 44 | } |
| 45 | |
| 46 | myFunc(3) |
| 47 | |
| 48 | // ChaiScript also supports in string evaluation, which C++ does not |
| 49 | |
| 50 | print("${3 + 5} is 8"); |
| 51 | |
| 52 | // And dynamic code evaluation |
| 53 | |
| 54 | var value = eval("4 + 2 + 12"); |
| 55 | |
| 56 | // value is equal to 18</code></pre> |