Balanced Ternary Fibonacci, in a new language.
Tags: sbtcvm, ternary, fibonacci
Published: 2018-08-06 17:28:48

---
title: Balanced Ternary Fibonacci, in a new language.
published: true
description: Explaining SBTCVM's new SSTNPL programming language, using the Fibonacci sequence.
tags: sbtcvm,ternary,fibonacci
---

##Overview:

SBTCVM is a balanced ternary (a variant of ternary that uses a negative digit instead of 2) virtual machine written in python. 

Recently, I and the rest of the SBTCVM developers have been hammering away at a new, 6.5Khz, modular, codebase called '**SBTCVM Gen 2**'. Its still in alpha, but its definitely making progress, and can already run moderately complex math programs, and print their output to a virtual TTY.

For those of you who saw my previous post on [Fibonacci in balanced ternary]( https://dev.to/thomasthespacefox/calculating-fibonacci-in-balanced-ternary), you probably already know, SBTCVM's old codebase only had an assembler. Well...

##SBTCVM's new SSTNPL (SBTCVM Simplified Ternary Numeric Programming Language)
{% gist 
https://gist.github.com/ThomasTheSpaceFox/90e8c74f8397f9e831015a325ac4d2bd %}
SO, Whats going on here? well, this is the new, abstracted, primarily-numeric, language, built for SBTCVM gen2, thats not a week old.

###basics:
SSTNPL is a language built primarily for mathematics. its driven by a seamless, named-integer-variable system. Its main goal is to abstract the complexities of the Virtual machine enough to provide a nice abstract language for learning Balanced Ternary mathematics, and doing calculations.

###var
var <name>=<value> is how we create variables for use & change later. (you must call var for every variable you use, regardless of whether it needs a starting value. Also most operations (other than a val <Value> followed by a 'set' statement), only take variable names as arguments.

value can have 2 forms:

prefix the value with '10x' if you wish to use decimal. i.e.
**10x-59**

otherwise you can use ternary in both **+0-** and **p0n** notations.

###Result printing:
here we have two strange commands:
**dumpd** and **dumpt**

dumpd dumps the specified integer in decimal form
dumpt does the same, but in ternary form.

newline and space are just TTY macros intended for formatting.

###end condition:

the conditional goto. you might notice that it takes 3 arguments. 2 operators that are comma-separated, and a label to actually jump to.

there are actually 3 types: if(equals), ifmore, and ifless. (this is no coincidence, as SBTCVM's new CPU also has these 3)

###math
add does just what you'd expect. It adds the two specified arguments. But how do we get our result?

thats the job of 'set' in this example, we 'set' num0 to the result of the previous operation.

swap, just switches the values of the two arguments.


And finally we just do 'goto loop' to return to the top of our loop at 'label loop', and then write a small exit routine to jump to in our conditional, and we have a program.


When we build this SSTNPL program into a memory image (.trom) and run it, via the **stnpcom.py** compiler and SBTCVM gen 2's curses VM frontend, the logged output looks like so:

{% gist https://gist.github.com/ThomasTheSpaceFox/8ee1d96c074a028efd83a6bd46ccb322 %}


SBTCVM's new codebase can be found here, and works in both python2 and python3:
{% github SBTCVM/SBTCVM-Gen2-9 %}