|
| 1 | +# The previous examples introduced objects, freezing, regions and cowns. |
| 2 | +# This example is a showcase of most expressions that FrankenScript |
| 3 | +# supports. It can be used as a reference for writing your own scripts. |
| 4 | +# |
| 5 | +# A list of built-in function can be found in the `docs` folder. Here is |
| 6 | +# a link to the rendered version on GitHub: |
| 7 | +# <https://github.com/fxpl/frankenscript/blob/main/docs/builtin.md> |
| 8 | + |
| 9 | +# This is how you construct the objects discussed in example 01 |
| 10 | +a = {} |
| 11 | +x = "a new string" |
| 12 | +x = Region() |
| 13 | +x = Cown(None) |
| 14 | + |
| 15 | +# FrankenScript has a few built-in and frozen objects: |
| 16 | +x = True # The boolean value `true` |
| 17 | +x = False # The boolean value `false` |
| 18 | +x = None # The null value, similar to Python's None |
| 19 | + |
| 20 | +# FrankenScript supports functions with arguments and return values. |
| 21 | +def id(x): |
| 22 | + return x |
| 23 | + |
| 24 | +# A function can be called like this. Each function call adds a new frame |
| 25 | +# to the diagram. The frame holds all variables known to the current scope. |
| 26 | +# The frame is deleted when the function returns. |
| 27 | +id(a) |
| 28 | + |
| 29 | +# Function objects are hidden from the mermaid to make it cleaner. But they're |
| 30 | +# still normal objects that can be used in assignments like this: |
| 31 | +a.id = id |
| 32 | + |
| 33 | +# This can be used to simulate method calls. Calling a function on stored in a |
| 34 | +# field will pass the object in as the first argument. |
| 35 | +a = a.id() # a is passed in as the first argument |
| 36 | + |
| 37 | +# The move keyword can be used for destructive reads. The previous location |
| 38 | +# is reassigned to None. |
| 39 | +b = move a |
| 40 | + |
| 41 | +# FrankenScript has two comparison operators. These check for object identity: |
| 42 | +res = b == None |
| 43 | +res = b != None |
| 44 | + |
| 45 | +# Boolean values can be used in if statements: |
| 46 | +if res: |
| 47 | + pass() # A built-in function for empty blocks |
| 48 | +else: |
| 49 | + unreachable() # A built-in function for unreachable branches |
| 50 | + |
| 51 | +# The else block is optional: |
| 52 | +if res: |
| 53 | + pass() |
| 54 | + |
| 55 | +# Boolean expressions can also be used in while loops: |
| 56 | +while res == True: |
| 57 | + res = False |
| 58 | + |
| 59 | +# For loops can be used to iterate over all fields of an object. |
| 60 | +a = {} |
| 61 | +a.field = "value" |
| 62 | +for key, value in b: |
| 63 | + pass() |
0 commit comments