By figosdev
Summary: This is the second in a two-part series on fig
This is the second in a two-part series on fig, an educational language that I use for other practical purposes.
Because I designed fig for computerphobes, you may decide it has a few odd qualities. One example is that it uses a "plus" command instead of + and a "minus" command instead of -. It also has an inline Python feature, which can be used to import Python functions into fig programs, as well as allow more traditional expressions to be evaluated, if you prefer:
python
import math
evaluated = math.sqrt(p ** int(s / 2.5))
fig
Fig has its own function definition command, which you can use to create functions with Python-like scope:
function greet whom
now = "Hello, " plus whom ; print
fig
Fig is the standard command that in Python would be unindenting, and it is named for "return to fig programming" after inline Python. In most instances, you can use "next" if you prefer:
function greet whom
now = "Hello, " plus whom ; print
next
You can use this as a wrapper around a python feature like this:
function pi
python
from math import pi as ppi ; return ppi
fig
fig
r = 50
circumference = pi times 2 times r ; print
In fig, the only mandatory punctuation in syntax is "quotes for strings" and # hashes for comments. Decimal points work the same way they do in Python.
These characters are optional, and for the most part can be freely used to help visually organise your code:
= ( ) | ; : ,
The vertical bar can be used to introduce students to Bash. With the exception of a few commands that start on their own line -- such as for, next, forin, ifmore, ifless, iftrue, ifequal, else, break, while, wend -- nearly all fig commands share a line with a variable on the far left:
height = 60 ; plus 5 ; str ; len ; print
height is 60, it adds 5 to become 65, is converted to a string: "65" then the length is found, and 2 is printed to the screen.
As in Python, there are a few ways to create arrays:
titles = "" ; arr ; times 100
This will create an array of 100 string items. you can add items like this:
titles plus "A History of Red Hat: From Cottage Industry to Corporation"
Each time a variable is used on the left, it is set to 0-- arrays are the exception:
now 5 print # prints 5
now print # prints 0
now 5 arr
now print # prints [5]
You can create an array using split:
ingredients = "milk eggs sugar flour water" ; split ingredients " "
Unlike Python, split and join both put the variable to split or join first -- then the thing to split or join it with:
ingredients = "milk eggs sugar flour water" ; split ingredients " "
ingredients = join ingredients " " ; print
Arrays are 1-based (apologies purists, it's an educational language) and for loops are started with count-variable, start-var, stop-var, step-var:
ingredients = "milk eggs sugar flour water" ; split ingredients " "
howmany = ingredients ; len
for each, 1, howmany, 1
now = each ; print
next
Forin loops are started with value-tracking-variable, array:
ingredients = "milk eggs sugar flour water" ; split ingredients " "
forin each, ingredients
now = each ; print
next
(nextin can be used instead of next)
Files can be opened with arropen, and webpages with arrcurl:
urls = arropen "urls.txt"
buffer = arr ; mid 1 0 # zero-length array
newline = 10 ; chr
forin each urls
iftrue each
geteach arrcurl each ; join geteach newline
buffer plus geteach
fig
nextin
Failed downloads create an error, so add error trapping (no, this is not how you should do this in python -- it is just a simple introduction to error trapping)
try
geteach arrcurl each ; join geteach newline
buffer plus geteach
except
geteach ""
resume
Finally, you can open geany using fig:
openleafpad = "leafpad &" shell
Or get the output of shell commands into an array:
textfiles = "ls *.txt" arrshell
To compile your fig program, give it a .fig extension and run it this way:
$ fig46.py program.fig
$ ./program.fig.py
To get help on a command, type part of it into the help feature:
$ fig46.py help
print
It will return all commands containing "print" in the name. hit enter instead to list all commands.
This will not work:
$ fig46.py help print
This will:
$ echo print | fig46.py help
All commands:
$ echo | fig46.py help
Fig is based on Python 2 -- as much as the Python Foundation is trying to kill Python 2, I think it is a much friendlier language than the latest Python.
PyPy is Python 2 compatible, and is the interpreter fig will most likely to be paired with in the future. There are two factors being waited on: PyPy is planning to support Pygame (an optional component for fig that lets it do real graphics) and Python 2 is still supported by many distros, not least of all Debian, Devuan and Refracta.
The Python Foundation will officially drop Python 2 on January 1. If you love it anyway, try
PyPy.
To make fig use PyPy instead, change the two lines that contain the string "env python2" to "env pypy" and that should do it. You can download fig 4.6
here.
Happy Coding!
⬆
Licence:
Creative Commons cc0 1.0 (public domain)