Sinisterly
Learn the Stuck Programming Language - Thread 1: Introduction - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: Learn the Stuck Programming Language - Thread 1: Introduction (/Thread-Learn-the-Stuck-Programming-Language-Thread-1-Introduction)

Pages: 1 2


Learn the Stuck Programming Language - Thread 1: Introduction - Shebang - 08-12-2015

Syntax();
Learn the Stuck Programming Language - Thread 1: Introduction
Click here for Thread 2
Click here for Thread 3



Table of Contents
  1. Introduction
  2. Types of Data
  3. Basic Operators
  4. Conditional Operators
  5. User Input
  6. Putting It All Together

Chapter 1: Introduction

Hello, and welcome to the Stuck programming language! I'm guessing you probably haven't heard of it, because it's a language I've been working on for about 2 weeks when I'm not at work. The purpose of the Stuck language is to be able to make concise programs with ease. I originally had the idea for a hobby I participate in called code-golfing, whereby a bunch of individuals are given a programming task and are asked to solve it in the least amount of code possible.

Stuck makes this possible by having all of the built-in functions as one-character commands that are applied via a stack. A stack is essentially a register of values, and Stuck makes it easy to do modifications to the stack and make some actual, useful programs!

If you would like to follow along, download the Stuck Python interpreter from here.

Chapter 2: Types of Data

As I originally wrote the interpreter for Stuck in Python, pretty much all data types in Python work in Stuck. However, there are only a few types of data which can be kept inline with the rest of the program. These are string and number literals. What I mean by "inline" with the program is that they can be mixed in with all of the 1-character operations and the interpreter can easily pick them out.

A number literal is any sort of integer/long/float. A string literal is anything surrounded by double quotes (").

Chapter 3: Basic Operators

Being a 1-character language, it makes sense for the first things to be implemented to be math operations. Here's a brief list of the simple operators you're probably already used to from other languages:
  • + adds the top two values on the stack.
  • - subtracts the top two values on the stack.
  • * multiplies the top two values on the stack.
  • / divides the top two values on the stack.
  • % takes the modulus of the top two values on the stack.
  • ^ raises the second-top value on the stack to the power of the top value on the stack.
  • \ square roots the top value of the stack.
  • # squares the top value of the stack.
  • { bit-shifts the second-top value on the stack to the left n positions, where n is the top value on the stack.
  • } bit-shifts the second-top value on the stack to the right n positions, where n is the top value on the stack.

Also a quick thing to note is that the programs work in reverse-Polish notations, so if you wanted to perform 2 * 3, you would need to write 2 3 *. Here are some examples of each command:

Code:
2 3 * -> 6 6 4 - -> 2 2 3 + -> 5 10 2.5 / -> 4 4 2 % -> 0 2 5 ^ -> 32 25 \ -> 5 3 # -> 9 2 3 { -> 16 12 1 } -> 6

Note that while I have kept spaces between everything, it is OK if there is a number connected to an operator with no spaces separating. So, 2 3* is the same as 2 3 *. You must keep seperate number literals separated by at least a space though!

Chapter 4: Conditional Operators

One of the most important pieces of a programming language is the ability to compare two objects and make decisions based on those comparisons. As such, I have implemented the following comparators, and a ternary operator to deal with these values. In this instance, 1 is used as a truthy value, while 0 is used as a falsy value. This is only necessary if you want to have a boolean inline with the program, you may still have Booleans that appear after comparisions in standard form or through input.
  • < compares the top two values on the stack, to see if the second-top value is less than the top value.
  • > compares the top two values on the stack, to see if the second-top value is greater than the top value.
  • = compares the top two values on the stack, to see if they are equal.
  • ? takes the top three values from the stack. If the third-top value is true, it keeps the second top value. Otherwise, it keeps the top value.

Usage of these should be fairly straightforward, but I'll give some examples:

Code:
2 3 < -> true 3 3 > -> false 5 5 = -> false 1 "Yes" "No" ? -> "Yes" 0 "True" "False" ? -> "False"

Note that once again, while I have kept everything separated by at least one space, you only need a space between number literals. The program 1"Yes""No"? would still yield "Yes", just like 1 "Yes" "No" ?.

Chapter 5: User Input

An extremely important aspect of programs is being able to have the user running the program able to interact without requiring changes to the source code. There are just three ways of entering user input in Stuck.
  • i reads a number literal and places it on the top of the stack.
  • s reads a string literal and places it on the top of the stack.
  • t reads the whole line of input, seperates by a '|' character, and places each individual item on the stack.

If you wanted to take input as a list of values, you would want to use t either like a|b|c or [a,b,c], as either will be evaluated similarly. I'm not going to provide any examples for this, as I feel it's very straightforward.

Chapter 6: Putting It All Together

Well, you've learned some basic math operations, user input, and conditional operators, now to make something that's got a purpose! For this example, we'll make a program that takes two coordinates from the user, and if the length of the line connecting the two is greater than 5, we will print "Long Enough!", otherwise we will print "Not Long Enough!". Now, while there is a print command in Stuck, the language implicitly prints the stack once all the calculations are complete, so we will use that to our advantage. There are a number of ways we can do this, I will choose the simplest in this instance.

I will be using a couple of commands we didn't learn above, which are:
  • @ rotates the stack forwards one place.
  • u rotates the stack backwards one place.
  • ; swaps the top two elements on the stack.


So, we need to take in two sets of coordinates, or four number literals. The easy way of doing this is repeatedly using i, a total of 4 times.

Code:
iiii

Now, these will be arranged in the stack as [x1, y1, x2, y2]. Ideally to make these calculations we want to have x1 and x2 together, and y1 and y2 together. We can do this by first swapping the top two elements with ;, then rotating the stack forwards with @.

Code:
iiii;@

Now our stack will be arranged as [x2, x1, y1, y2]. Now we can take the square of the top 2 values subtracted, so we need to subtract (-) them and then square them (#). Then, we need to position the other pair at the top to do the same thing, with @. Then, we can repeat the same steps as before.

Code:
iiii;@-#@-#

Now, we must sum the two values left on the stack together, which are essentially [(y1-y2)^2, (x1-x2)^2], and then square root to get the length.

Code:
iiii;@-#@-#+\

The final operation of this program is to check whether this value is greater than 5, and leave the appropriate string on the stack. Our complete program will look like this:

Code:
iiii;@-#@-#+\5>"Long Enough!""Not Long Enough!"?

Note, we could have also written it like this to keep it even shorter:

Code:
iiii;@-#@-#+\5>"""Not "?"Long Enough!"

Even then, we could've taken input via t, which would mean it only needs to be this:

Code:
t;@-#@-#+\5>"""Not "?"Long Enough!"

And, I didn't mention it before, but there's actually a builtin that calculates the length for you Smile So we could have actually had this:

Code:
t`5>"""Not "?"Long Enough!"

There you have it, you've made your first program in Stuck Biggrin



Stay tuned for the next thread, where I go through more of the builtins.


RE: Learn the Stuck Programming Language - Thread 1: Introduction - Akane - 08-12-2015

This is actually an excellent thread. A framework like this would come in pretty handy when you want short, simple, and optimised code. I'm pretty annoyed at the fact that it's not very readable, however.

Oh, and any comments on the optimisation?


RE: Learn the Stuck Programming Language - Thread 1: Introduction - Shebang - 08-12-2015

(08-12-2015, 02:05 AM)Akane Wrote: This is actually an excellent thread. A framework like this would come in pretty handy when you want short, simple, and optimised code. I'm pretty annoyed at the fact that it's not very readable, however.

Oh, and any comments on the optimisation?

I'm not sure how I could make the thread more readable, if there's any tips I'd love to hear them Smile

What do you mean by 'the optimization'?


RE: Learn the Stuck Programming Language - Thread 1: Introduction - Eclipse - 08-12-2015

(08-12-2015, 02:59 AM)Shebang Wrote: I'm not sure how I could make the thread more readable, if there's any tips I'd love to hear them Smile

What do you mean by 'the optimization'?

I think she meant the language, not the thread.


RE: Learn the Stuck Programming Language - Thread 1: Introduction - Akane - 08-12-2015

(08-12-2015, 02:59 AM)Shebang Wrote: I'm not sure how I could make the thread more readable, if there's any tips I'd love to hear them Smile

What do you mean by 'the optimization'?

I meant the readability of the language, not the thread, as @Eclipse suggested.

Stuck is basically a bunch of shortcuts for Python, so what I'm asking is if there's any effect on the speed?


RE: Learn the Stuck Programming Language - Thread 1: Introduction - Shebang - 08-12-2015

(08-12-2015, 07:44 AM)eclipse Wrote: I think she meant the language, not the thread.

Oh, that makes sense. Tongue

(08-12-2015, 09:21 AM)Akane Wrote: I meant the readability of the language, not the thread, as @Eclipse suggested.

Stuck is basically a bunch of shortcuts for Python, so what I'm asking is if there's any effect on the speed?

Well, firstly, in regards to the readability, due to the purpose of the language being to create extremely short and useful tools, that will be sacrificed. However, I would argue that once you become used to the language, it's very easy to understand. If you're interested in a few, even less readable languages, check out Befunge-93 and Malbolge, the latter of which actually gained a little bit of fame after being featured in an episode of Elementary.

When it comes to speed of execution, it should be essentially just as fast as Python, mostly. I did recently implement a looping construct, which is going to add a little overhead on execution because of how I'm doing it, so it will likely be a bit slower. However, to the user, this will usually not be perceptible.


RE: Learn the Stuck Programming Language - Thread 1: Introduction - Equinox - 08-12-2015

Not bad. I did a quick scan through the thread, I'll have to put more reading into this when I have a bit more free time (well, I'm free now, but working on my desktop). Seems really interesting, though since it uses Python as the interpreter, it would be as slow (if not, for whatever reason, slower than) Python. Is there going to be other interpreters in other languages for this, or will you always stick with Python?


RE: Learn the Stuck Programming Language - Thread 1: Introduction - Shebang - 08-12-2015

(08-12-2015, 02:41 PM)Equinox Wrote: Not bad. I did a quick scan through the thread, I'll have to put more reading into this when I have a bit more free time (well, I'm free now, but working on my desktop). Seems really interesting, though since it uses Python as the interpreter, it would be as slow (if not, for whatever reason, slower than) Python. Is there going to be other interpreters in other languages for this, or will you always stick with Python?

I'm going to be developing likely just in Python. However, I'm totally open to anyone who wants to further the project to create an interpreter in another language. I would like to point out that this should work (almost) fully under PyPy, which could be used for speeding up longer programs.

One of the big things that I implemented that I haven't got too far into is the plugin system, which means anyone can develop a plugin (with the proper format), and drop it in the plugins folder and use it right away. I did that because I knew that eventually I would end up not spending a lot of time on it, so development doesn't have to end.

Also, be sure to check out the next thread, it will be much more interesting Smile


RE: Learn the Stuck Programming Language - Thread 1: Introduction - DarkKnight - 08-12-2015

Nice tutorial mate, could come in handy.


RE: Learn the Stuck Programming Language - Thread 1: Introduction - Shebang - 08-12-2015

(08-12-2015, 03:48 PM)DarkKnight Wrote: Nice tutorial mate, could come in handy.

If you'd like to read more, I posted the second thread earlier Smile