![]() |
|
Learn the Stuck Programming Language - Thread 2: Diving Deeper - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Coding (https://sinister.ly/Forum-Coding--71) +--- Thread: Learn the Stuck Programming Language - Thread 2: Diving Deeper (/Thread-Learn-the-Stuck-Programming-Language-Thread-2-Diving-Deeper) |
Learn the Stuck Programming Language - Thread 2: Diving Deeper - Shebang - 08-12-2015 Syntax();
Learn the Stuck Programming Language - Thread 2: Diving Deeper Click here for Thread 1 Click here for Thread 3 Table of Contents
Chapter 1: Preface Firstly, if you haven't read the first thread, or you need to download the Stuck interpreter, go here. Now, if you've been through the first thread, then you've experienced the reverse Polish notation that is used to perform operations, working with inline string and number definitions, taking user input and more. Now, we get to the fun stuff - making more complex programs! I'll be running through all of the built-ins I've made so far, which work off of a plugin system (I will cover making plugins later). Also, we will be checking out the beautiful looping construct, which will help greatly! As a quick note, if you would ever like to see how the stack is modified after each step, at the end of your Stuck program, append a -d. This enables the debug mode, which will show you how each step of the application changes the stack. Chapter 2: Basic Built-ins Here's a list of all of the basic built-in functions that will help with organizing and modifying the stack.
A few notes concerning some commands listed above, which should clear up functionality:
Chapter 3: Math Built-ins A great use of Stack is to perform calculations, so of course there has to be a math library. To see more of the basic operations, refer to the first thread.
I will be implementing logarithms, permutations and combinations at some point, I just haven't gotten around to it. For those of you unaware of the Cartesian product, this is it's basic function: Code: Let's say the stack is [[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]], and we use X for Cartesian product
The output would be the 120 different combinations:
[(1, 1, 1, 1, 1), (1, 1, 1, 1, 2), (1, 1, 1, 1, 3), (1, 1, 1, 1, 4), (1, 1, 1, 1, 5), (1, 1, 1, 2, 1), (1, 1, 1, 2, 2),
(1, 1, 1, 2, 3), (1, 1, 1, 2, 4), (1, 1, 1, 2, 5), (1, 1, 1, 3, 1), (1, 1, 1, 3, 2), (1, 1, 1, 3, 3), (1, 1, 1, 3, 4),
(1, 1, 1, 3, 5), (1, 1, 1, 4, 1), (1, 1, 1, 4, 2), (1, 1, 1, 4, 3), (1, 1, 1, 4, 4), (1, 1, 1, 4, 5), (1, 1, 2, 1, 1),
(1, 1, 2, 1, 2), (1, 1, 2, 1, 3), (1, 1, 2, 1, 4), (1, 1, 2, 1, 5), (1, 1, 2, 2, 1), (1, 1, 2, 2, 2), (1, 1, 2, 2, 3),
(1, 1, 2, 2, 4), (1, 1, 2, 2, 5), (1, 1, 2, 3, 1), (1, 1, 2, 3, 2), (1, 1, 2, 3, 3), (1, 1, 2, 3, 4), (1, 1, 2, 3, 5),
(1, 1, 2, 4, 1), (1, 1, 2, 4, 2), (1, 1, 2, 4, 3), (1, 1, 2, 4, 4), (1, 1, 2, 4, 5), (1, 1, 3, 1, 1), (1, 1, 3, 1, 2),
(1, 1, 3, 1, 3), (1, 1, 3, 1, 4), (1, 1, 3, 1, 5), (1, 1, 3, 2, 1), (1, 1, 3, 2, 2), (1, 1, 3, 2, 3), (1, 1, 3, 2, 4),
(1, 1, 3, 2, 5), (1, 1, 3, 3, 1), (1, 1, 3, 3, 2), (1, 1, 3, 3, 3), (1, 1, 3, 3, 4), (1, 1, 3, 3, 5), (1, 1, 3, 4, 1),
(1, 1, 3, 4, 2), (1, 1, 3, 4, 3), (1, 1, 3, 4, 4), (1, 1, 3, 4, 5), (1, 2, 1, 1, 1), (1, 2, 1, 1, 2), (1, 2, 1, 1, 3),
(1, 2, 1, 1, 4), (1, 2, 1, 1, 5), (1, 2, 1, 2, 1), (1, 2, 1, 2, 2), (1, 2, 1, 2, 3), (1, 2, 1, 2, 4), (1, 2, 1, 2, 5),
(1, 2, 1, 3, 1), (1, 2, 1, 3, 2), (1, 2, 1, 3, 3), (1, 2, 1, 3, 4), (1, 2, 1, 3, 5), (1, 2, 1, 4, 1), (1, 2, 1, 4, 2),
(1, 2, 1, 4, 3), (1, 2, 1, 4, 4), (1, 2, 1, 4, 5), (1, 2, 2, 1, 1), (1, 2, 2, 1, 2), (1, 2, 2, 1, 3), (1, 2, 2, 1, 4),
(1, 2, 2, 1, 5), (1, 2, 2, 2, 1), (1, 2, 2, 2, 2), (1, 2, 2, 2, 3), (1, 2, 2, 2, 4), (1, 2, 2, 2, 5), (1, 2, 2, 3, 1),
(1, 2, 2, 3, 2), (1, 2, 2, 3, 3), (1, 2, 2, 3, 4), (1, 2, 2, 3, 5), (1, 2, 2, 4, 1), (1, 2, 2, 4, 2), (1, 2, 2, 4, 3),
(1, 2, 2, 4, 4), (1, 2, 2, 4, 5), (1, 2, 3, 1, 1), (1, 2, 3, 1, 2), (1, 2, 3, 1, 3), (1, 2, 3, 1, 4), (1, 2, 3, 1, 5),
(1, 2, 3, 2, 1), (1, 2, 3, 2, 2), (1, 2, 3, 2, 3), (1, 2, 3, 2, 4), (1, 2, 3, 2, 5), (1, 2, 3, 3, 1), (1, 2, 3, 3, 2),
(1, 2, 3, 3, 3), (1, 2, 3, 3, 4), (1, 2, 3, 3, 5), (1, 2, 3, 4, 1), (1, 2, 3, 4, 2), (1, 2, 3, 4, 3), (1, 2, 3, 4, 4),
(1, 2, 3, 4, 5)]Chapter 4: SciPy Built-ins I'll be the first to admit, this module is.. sparse. I haven't gotten around to implementing a lot of SciPy's functionality, because I need to think of how exactly I want everything to work. I have so far only implemented two functions:
Since there's only two functions so far, I'll show implementations. These are full programs that you can run yourself to check, by the way! Make sure you have SciPy and NumPy installed. Code: "[[1,4,0],[0,1,3],[2,0,1]]"~İ -> [[0.04, -0.16, 0.48], [0.24, 0.04, -0.12], [-0.08, 0.32, 0.04]]
"[[1,4,0],[0,1,3],[2,0,1]]"~Ɗ -> 25
"[[1,4,0],[0,1,3],[2,0,1]]"~İƊ -> 0.04Chapter 5: The Loop Construct Now, we reach my favourite part, learning about the almighty loop construct! This little guy, :, is probably the most useful feature implemented to date, and it was only added a day or two ago! A side effect of this is that it's not heavily tested and probably buggy, but in the tests I've done it works great! The looping construct works by taking the top value from the stack. This value should be a string literal, which contains a valid Stuck program. Then, if the second-top value is a list, it will modify that. If it is not a list, every value on the stack will be modified. For every value of the list that the : is accessing, it initializes the stack with each value, and executes the code individually, finally returning the each of the results in the appropriate format. As of right now, this can be nested once. What I mean by this, is you can have up to one nesting of a loop inside a loop. For example: Code: 5R"R": will produce a stack with [[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]] as the top value.
5R"R''R'':": will produce a stack with [[[1]], [[1], [1, 2]], [[1], [1, 2], [1, 2, 3]], [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]], [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]] as the top value.Note that if you are nesting a loop, you must use double apostrophes ('') rather than quotes (") on the inside. Chapter 6: Putting it All Together The program I will be creating as an example is one that generates the tetrahedral number sequence. As you can see from the this, the easy way to calculate the nth tetrahedral number is n(n+1)(n+2)/6. Since we need to use the value for n three times, we'll take n via user-input, then duplicate it twice. Code: i__For one term, we need to add one, and for another, we need to add two. So, we can add 1 to the top element, swap the top 2, then add 2 to the new top element. We know we need to multiply these together, and instead of using * twice, we'll use the product character. Then, we can divide by 6. Code: i__1+;2+Π6/There's the basic code for calculating the Nth tetrahedral number. However, we want to generate the sequence, so we'll take the input as the max number to generate. We can convert this to a range of 1 to n, then for each k in there, we'll calculate the kth tetrahedral number. So, we will wrap this code we already have in quotes (removing the user input at the start), then use the looping construct to apply this. Code: iR"__1+;2+Π6/":However, we can make this prettier. As of now, this will wait to calculate every individual value before printing the stack. If we want to see the progression of calculations, we can put in a p at the end of the loop string to print each value out as it is calulated. This will also suppress printing the stack at the end. Code: iR"__1+;2+Π6/p":For a finishing touch, why don't we try to make it a little user friendly? If you want to have an input prompt, you can push a string to the stop of the stack, print it, then pop it off the stack so it doesn't interfere. I'll put the finished program below, and show how it would execute. Code: "Calculate the tetrahedral numbers up to:"pyiR"__1+;2+Π6/p":
After execution:
Calculate the tetrahedral numbers up to:
(input here, for example 6)
1.0
4.0
10.0
20.0
35.0
56.0There you go! You've made a very simple, but useful program in Stuck! Stay tuned for the next thread, where I go into creating plugins for Stuck. |