A very stripped back BASIC like language. It contains only what you need to create a turing complete program. No synactic sugar, nothing unnessary. You should be able to learn it in less than 5 minutes.
All variables are stored on the stack. There is no heap or garbage collection. Memory is freed when variables go out of scope. All variables are passed by val to functions, and assignment means the variable will be copied. Very Basic doesn't ever use references.
Example:
a = array(1,2,3)
' b is copy of 'a'
b = a
b[0] = 5
print a[0] ' prints 1
print b[0] ' prints 5
Other things to note:
- Very Basic is not object orientated, there are no classes, inheritance or interfaces
- Very Basic does not have closures. Â You cannot declare a function within a function.
- Functions are first class citizens and can be passed as parameters to functions (via the funtion name).
- Very Basic is not designed for speed or memory efficiency
- Very Basic is a dynamically typed language, like Python or Javascript
On linux you may need to install the following dependencies
sudo apt install libfontconfig libfontconfig1-dev
sudo apt install pkg-config
then simply
git clone https://github.com/malworthy/verybasic.git
cd verybasic_rust
cargo build
if <expression> then <statement> [else <statement>][elseif <expression> then <statement>] end
while <expression> <statement> end
' EXAMPLE:
if x == 1 then
print("x is one")
elseif x == 2 then
print("x is two")
else
print("x is something else")
end
Note: The expression evaluated for if and while must return a boolean. For example if 1 then print("hello") end
won't work.
Example:
x = 0
while x < 10
print("X is " + x)
x = x + 1
end
- step is optional
- the step must be a number, an expression is not allowed
- the for loop variable will only exist in scope of the loop
Example:
for x = 1 to 10
print(x)
next
for x = 1 to 10 step 2
print(x)
next
for x = 10 to 0 step - 1
print(x)
next
Functions will return the result of the last expression executed. All functions must return a value. An empty function body will be a compile error.
Example:
function addNumbers(a,b)
a+b
end
' prints '2'
print(addNumbers(1,1))
To exit a function, use the keyword exit as below.
' the function below returns zero if x is less than zero. Otherwise returns the value of x.
function foo(x)
if x < 0 then
0 exit
end if
x
end
The function keyword can be shortened to fn
fn foo(x)
if x < 0 then
0 exit
end if
x
end
A method is called using the "dot" syntax.
Example:
array.push(123)
Method are really just functions, but can mutate the variable you are calling the method on. Any function can be called using dot syntax. "1,2,3".split(",")
is the same as split("1,2,3",",")
match [expression] when [operator] [Expression] then [statements] when [Expression[,Expression,...]] then [statements] when [Expression] to [Expression] then [statements] ... else [expression] end
Example:
x = match i
when 0 then 1 ' match single value
when 2 to 3 then 2 ' match range
when 4,5,6 then 7 ' match multiples
when <=6 then 5 ' match using operators
when < 8 then 6
when >=5 then 4
when > 3 then 3
else 7 ' must supply else
end
There are 4 datatypes. String, Number, Boolean and Array. Arrays can hold any datatype
x = "hello" ' string
x = 123 ' number floating point
x = true ' boolean
x = array(1,1,1) ' create an array of 3 elements with the number 1
Arrays are acually vectors
Example:
x = array(4,5,6) ' create an array with 3 elements
print(x[0]) ' prints 4
print(x[1]) ' prints 5
print(x[1]) ' prints 6
x[0] = 10 ' change first element from 4 to 10
x.push(15) ' adds an element to the end of the array
Standard operators:
==
+
-
/
*
>
>=
<
<=
<>
not
and
or
^ (to the power of)
mod (remainder)
in [expression],[expresssion],...
Example:
if x in 1,2,3 then print("x is 1 2 or 3");
Use a single quote for comments. No block comments.
' This is a comment
Any character that is not a letter, number or operator is treated as whitespace.
Example below. Semi-colon and other symbols are just ignored.
print("hello, world");
!#$ print("goodbye")
The following short-cuts are available:
function
can be replace withfn
end
can be replaced with;
fn dbl_num(x) x*2;
if dbl_num(2)==4 then print("doubled");
Very Basic supports string interpolation. Any expression between '{' and '}' will be interpreted and inserted into to the string.
See example below:
print ("The result is {1+1}")
String interpolation is just syntatic sugar. The above string in converted to the following:
"The result is " + str(1+1) + ""
Returns a one character string using ascii encoding. Invalid ascii value will return an empty string.
returns command line arguments as an array.
NOTE: The first argument will be the full path of vbas.exe, 2nd argument name of the script.
clears the console
will query the filesystem for all files that match a particular pattern. Uses Unix shell style patterns.
Returns the largest integer not greater than number
reads input from the console
Prints a string to the console.
returns a random number between 0 and 1
round a number to [precision] decimal places
returns the number of seconds since the unix epoch
reads a value from the settings. value returns will always be converted to a string. Will return an empty string if the setting doesn't exist.
saves a key/pair value to a config file. config file default name is [name of script].json.
pause the program for a specified number of milliseconds
Converts any value to a string, optionally applying formatting to numbers.
- Nx format to x decimal places, use thousands separator e.g.
str(123456.456,"N2") => 123,456.46
- Fx format to x decimal places, don't use thousands separator e.g.
str(123456.456,"F2") => 123456.46
returns the square root of num
Converts a string to a number. Will return zero if string cannot be converted to a number, or if the data type is not a string.
appends text to a file. If the file doesn't exist it will be created.
returns an array of all lines in a text file
creates a new file writes text to it. Will overwrite any existing file.
creates a new array, optionally populating with elements
creates a new array of a specified size, all elements will default to zero, or optionally to the specified value
return the index of item in the array. if item is not found it returns -1
returns the largest element in the array
returns a new array with val added to the end of an array
return a new array with all elements ordered at random
returns a sorted array
adds a value to the end of an array
return a new sliced array. This does not mutate the array.
Example:
array.filter(">", 7) ' all elements greater than 7
array.filter("<", 5, ">", 10) ' all elements less than 5 or greater than 10
get ascii value of the first character of a string. Will return 0 if string is empty or parameter is not a string
returns the index of string2 found in string1, using a 1 based index. If not found, then it returns zero.
- start = start position to search
- compare = if this value is 1, then it does a case insenstive comparison
returns the lower case value of a string
Returns a substring containing a specified number of characters from the beginning (left side) of a string.
returns part of a string using a 1 based index. e.g mid("hello",3) returns "llo", mid("hello",3, 2) return "lo"
returns the right most characters of a string
replace part of a string
splits a string based on a delimiter and returns an array of its part. If remove_empty is true then any empty elements are removed from the array.
returns the upper case value of a string
Dates are stored as strings in ISO8601 format. There is no native datetime format. Functions that use dates will convert the string value to a date internally, and then convert the date back to a IOS8601 format string for any dates returned.
returns the current date using the local timezone
valid intervals are:
- s = second
- n = minute
- h = hour
- d = day
- w = week
- m = month
- y = year
Very Basic has the ability to do basic 2D graphics. You can draw to a canvas and then display the canvas in a window, or save it as a image file.
sets all pixels on the canvas to white
creates a new canvas for drawing. You must call this before calling any other graphics functions.
sets the colour at x,y coordinates on the canvas. Colour can either be a pre-defined colour string or a hex code (e.g. #ADFF2F)
returns a hex code for the specified red, green and blue values.
display a window showing the canvas. Size of window will be the same as the canvas
the following pre-defined string values for colours are available:
- darkblue
- blue
- purple
- yellow
- pink
- red
- green
- black
- white
Other colours can be specified using Hex Codes. A hex code starts with '#' and must be 6 digits.
Any function call prefixed with a '@' will be a system call.
e.g. @ls("-l)
will be the bash command "ls -l".
on windows, to run the 'dir' command, use @cmd("/c","dir")
@notepad()
will open notepad