In today’s sandwich-making adventure, I improved the comment parser. Not you can use comments inline. For example:
# this is a comment pHello #this is an inline comment pWorld # <- Will print World!
# symbol turns the rest of the line into a comment, so be careful! Everything before the comment symbol will be evaluated.
I also added the ability to import things from other files with the
i opcode, like so:
#one.txt pHello world! itwo.txt# now everything from two.txt will be imported into one.txt p$v #v is defined in two.txt
And the external file would look like this:
#two.txt (external) lvsome variable
So the output would be “some variable”. Note that the file is evaluated before importing, like in Python. This means that any print statements or math operations in the file that are not enclosed in a variable or a function will be executed like a normal program.
As always, the code is up on tildegit, so clone a copy and get started with sandwich! PRs can be made on Gitea or sent to my tilde.team email (karx AT tilde DOT team) as patches.
Today, I added functions! functions are a neat little tool that let you “pipe” math operation outputs into other instructions. For example, you could assign the output to a variable, like below:
#Declare a function fxa3-3 #Call the function and assign it to variable lv*x #will print out 6 p$v
Or you could print it out directly:
#Declare a function fxs30-3 #Call it and print it out #Will print out 27 p*x
You can also use variables inside a function, like so:
#Declare a variable lv3 #Declare a function that will use the variable fxs30-$v #Prints out 27 p*x
Note that functions will only get evaluated when they’re called. This means that if you reassign the variable
v in between the function declaration and the function call, the function will use the new value of the variable.
Also, functions are re-evaluated for each call. So if they depend on an outside variable and the variable changes frequently, the result of the function will also change frequently.
Nested function calls also work:
#Declare a function fxa2-2 #Declare a function that calls the other one fya*x-4 #Will print out 8 p*y
Also note that the only operations you can perform inside a function are the math functions, which are
The code is up on tildegit, so clone a copy and get started with sandwich!
Today (well, actually, yesterday. I just forgot to write this until just now), i did three things in the
sandwich development process.
the first thing i did was make the separator a
\n instead of a space. because of this change, you can now print strings with spaces in them. for example, to print “Hello, world!” in the previous version, you had to do this:
but they would actually print on separate lines. this is not what i wanted, so i changed it to look like this:
subsequent instructions would go on their own lines, rather than being next to other instructions.
explicit variable calls and comments
before, variables would get automatically get converted. however, this can be very annoying. this is what it looked like:
but what if you wanted to print
h? you’d have to set the variable
h to be
h, then print it out, then set it back to what it was before. to combat this, i’ve changed this to look like the following:
variables are now denoted by a
$ before its name.
i also added comments, which can be used like so:
pHello, world! # the interpreter will skip the next line (and this line) #pThis will not print
readme, license, code comments
i added a readme and a (un)license, so newcomers will know how to use the language. i also added comments to the code, so people readding the source code can tell what each peice of code does.
as always, please try to contribute to sandwich by sending a patch to my email or making a pull request on tildegit. instructions are in the README.