An xBASIC interpreter library written in Rust. xBASIC is a language loosely based off of QBasic.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Stephen 0e92e5ce49 Another bug fix(arithmetic assign) 2 dias atrás
benches Give IO ownership to XBasic object 1 semana atrás
examples Functions 1 mês atrás
src Another bug fix(arithmetic assign) 2 dias atrás
tests Another bug fix(arithmetic assign) 2 dias atrás
.gitignore Initial commit - does hello world 1 mês atrás
Cargo.lock cut release 5 dias atrás
Cargo.toml cut release 5 dias atrás
LICENSE.md Add license 1 mês atrás
README.md Add repository 2 semanas atrás
rustfmt.toml Initial commit - does hello world 1 mês atrás

README.md

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 = 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.