#scripting-language #user-defined #function #calling #logic #compute #limit

bin+lib xbasic

A library that allows adding a scripting language onto your project with ease. This lets your users write their own arbitrary logic.

11 releases

0.3.2 May 24, 2021
0.3.1 Jan 12, 2021
0.2.1 Nov 23, 2020
0.1.5 Nov 21, 2020

#500 in Programming languages

MIT license

80KB
2.5K SLoC

xBASIC

xBASIC is designed to be a very simple language. The goal of this library is to make it easy to add user-defined logic to your Rust project.

Features:

  • Gracefully handles bad or malicious code.
  • Allows setting a compute time limit.
  • Allows calling xBASIC functions from Rust
  • Allows calling Rust functions from xBASIC
  • Allows defining custom IO, which PRINT and INPUT work with
  • Entirely safe rust

TODO:

  • Arrays
  • Better standard library(string lengths, type conversions, etc)

A basic interpreter is also included in this project. It can be started with cargo run. It possesses a REPL, but can also run files.

Syntax

As mentioned already, xBASIC is a very simple language. Like other BASIC languages, it is designed to be easy for beginners to understand.

Printing

print "hello world" // "hello world"
print "hello" "world" // "hello world"
print 5 "+" 3 "=" 5 + 3 // "5 + 3 = 8"

PRINT will automatically concatenate multiple expressions together.

Reading

input a // Read in a line, store it in a
input b // Read in another line, store it in b

Variables

a = 3
print a // "3"
print b // "0"

All variables are implicitly initialized to 0.

Comparisons

a = 3
print a = 3 // "true"
print not a = 3 // \"false\"
print a < 3 // "false"
print a > 3 // "false"
print a <= 3 // "true"
print a >= 3 // "true"

Note that a = 3 is an assignment when used as a statement, and a comparison when used as an expression.

IF statements

if 3 = 3 then
    print "hello world" // runs
end if

if not a then
    print "hello world" // runs, because a is implicitly 0
end if

if b = 2 then
elseif b = 3 then
elseif b = 4 then
else
end if

Truthy values are true and all numbers other than 0. It is an error to attempt to evaluate the truthiness of a string.

WHILE statements

a = 0
while a < 10
    print a // prints 0 1 2 3 4 5 6 7 8 9, each on a newline
    a += 1
wend

FOR statements

for x = 10 to 20
    print x // prints 10 11 12 13 14 15 16 17 18 19 20, each on a newline
next x

Functions

print a("hello", "world") // "helloworld"

function a(b, c)
    return b + c
end function

Functions can be defined before or after they are called. Return values can be specified with return, otherwise they will implicitly return 0. Recursion is also allowed.

For more examples, check out the examples directory.

Dependencies

~3.5MB
~73K SLoC