An xBASIC interpreter library written in Rust. xBASIC is a language loosely based off of QBasic.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.4 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. # xBASIC
  2. xBASIC is designed to be a very simple language. The goal of this library is to make it easy to add
  3. user-defined logic to your Rust project.
  4. Features:
  5. - Gracefully handles bad or malicious code.
  6. - Allows setting a compute time limit.
  7. - Allows calling xBASIC functions from Rust
  8. - Allows calling Rust functions from xBASIC
  9. - Allows defining custom IO, which `PRINT` and `INPUT` work with
  10. - Entirely safe rust
  11. TODO:
  12. - Arrays
  13. - Better standard library(string lengths, type conversions, etc)
  14. 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.
  15. ## Syntax
  16. As mentioned already, xBASIC is a very simple language. Like other BASIC languages, it is designed to be easy
  17. for beginners to understand.
  18. ### Printing
  19. ```
  20. print "hello world" // "hello world"
  21. print "hello" "world" // "hello world"
  22. print 5 "+" 3 "=" 5 + 3 // "5 + 3 = 8"
  23. ```
  24. `PRINT` will automatically concatenate multiple expressions together.
  25. ### Reading
  26. ```
  27. input a // Read in a line, store it in a
  28. input b // Read in another line, store it in b
  29. ```
  30. ### Variables
  31. ```
  32. a = 3
  33. print a // "3"
  34. print b // "0"
  35. ```
  36. All variables are implicitly initialized to 0.
  37. ### Comparisons
  38. ```
  39. a = 3
  40. print a = 3 // "true"
  41. print not a = 3 // \"false\"
  42. print a < 3 // "false"
  43. print a > 3 // "false"
  44. print a <= 3 // "true"
  45. print a >= 3 // "true"
  46. ```
  47. Note that `a = 3` is an assignment when used as a statement, and a comparison when used as an expression.
  48. ### IF statements
  49. ```
  50. if 3 = 3 then
  51. print "hello world" // runs
  52. end if
  53. if not a then
  54. print "hello world" // runs, because a is implicitly 0
  55. end if
  56. if b = 2 then
  57. elseif b = 3 then
  58. elseif b = 4 then
  59. else
  60. end if
  61. ```
  62. Truthy values are `true` and all numbers other than 0. It is an error to attempt to evaluate the truthiness of a string.
  63. ### WHILE statements
  64. ```
  65. a = 0
  66. while a < 10
  67. print a // prints 0 1 2 3 4 5 6 7 8 9, each on a newline
  68. a += 1
  69. wend
  70. ```
  71. ### FOR statements
  72. ```
  73. for x = 10 to 20
  74. print x // prints 10 11 12 13 14 15 16 17 18 19 20, each on a newline
  75. next x
  76. ```
  77. ### Functions
  78. ```
  79. print a("hello", "world") // "helloworld"
  80. function a(b, c)
  81. return b + c
  82. end function
  83. ```
  84. Functions can be defined before or after they are called. Return values can be specified with `return`,
  85. otherwise they will implicitly return 0. Recursion is also allowed.
  86. For more examples, check out the `examples` directory.