diff --git a/Structures.md b/Structures.md new file mode 100644 index 0000000..b5e9b92 --- /dev/null +++ b/Structures.md @@ -0,0 +1,102 @@ +Structures allow coders to define their own types. They are similar to lists, but they do have a few key differences: + +- Each field of a set can be called at any time +- You can define functions within the structure, which can be called by using `!struct.fun`. +- The fields of a structure are unordered + +Let's have a look at how you can create a structure. + +## The Basics +For our example, we will be making a point structure. This will store a point on the Cartesian plane. + +Let's create the framework for the struct: +``` +struct -point + +endstruct +``` +All this does is create a new structure, named point. + +## Defining Fields +A field is, to put it simply, the parts that make up a structure. To define a field, use `init &fieldname -type`. + +For our example, we want to store two variables that store a double: one for the x-position, and one for the y-position. So our fields will be xpos and ypos. + +``` +struct -point + init &xpos -double + init &ypos -double + +endstruct +``` +Now we have a complete structure! + +## Defining a variable as a structure +We have a point, but how can we set a variable as a point? It is pretty simple: just use `init &myPoint -point`. + +To modify the xpos and ypos of the `myPoint` variable, you can use `set &myPoint.xpos $value` and `set &myPoint.ypos $value`. +``` +struct -point + init &xpos -double + init &ypos -double + +endstruct + +init &myPoint -point +set &myPoint.xpos 3 +set &myPoint.ypos 4 +``` +This stores the point (3, 4). + +And now you know the basics of making a structure! You know how to define a structure, its fields, and how to define a variable as a structure. But there is more you can do... + +## Member Functions +One trick you can do is to define functions within the structure. Typically these will be getting some value related to the structure. + +For our example, we will find the distance of the point from the origin. Let's first build the framework. + +``` +struct -point + init &xpos -double + init &ypos -double + + fun -double !modulus + + endfun +endstruct +``` + +The distance from the origin, by the Pythagorean Theorem, is $$\sqrt{x^2+y^2}$$. We can simply use `$xpos` and `$ypos` to find the respective values when the function is called. + +We will import the math function to use the square root function. + +``` +struct -point + init &xpos -double + init &ypos -double + + fun -double !modulus + multiply $xpos $xpos &x2 + multiply $ypos $ypos &y2 + add $x2 $y2 &z2 + pusharg $z2 + !math:sqrt &z + return $z + endfun +endstruct +``` + +Now, if we run `!myPoint.modulus`, we can get the modulus of the vector represented by `($myPoint.xpos, $myPoint.ypos)`. + +And that's all you need to know! + +## Other examples of structures + +- A user account for a messaging service +``` +struct -user + init -string &username + init -string &password + init -bool &isBanned +endfun +``` \ No newline at end of file