Skip to content

patds20/sqbra-interpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo

SQBRA Interpreter (Version 2.2.3 - Rapid Red Panda)

Interpreter for the SquareBracket Programming Language

SquareBracket is a simple programming language designed to make the execution of small algorithms and math scripts easy. It has a syntax that is easy to learn and intuitive commands to perform mathematical operations, create variables, and control program flow. It is designed to be extensible and many more functions will be added in future versions to make it more powerful. With its focus on simplicity and ease of use, SquareBracket is a good choice for anyone looking to quickly and easily create and execute mathematical algorithms and scripts. Small bugs may still occur. The interpreter is lightweight and completely implemented in C++.

Usage

The SquareBracket Interpreter can be used with the following syntax:

sqbra.exe [flags] [path to the code file] [input values (separated with spaces)]

Here's what each part of the syntax means:

  • [flags]: Optional flags that can be used to modify the behavior of the interpreter. See the "Flags" section below for more information.
  • <path to the code file>: The path to the code file that contains your code.
  • <input values>: Optional input values that can be passed to the program. These values will be available in the args list when the program runs.

Flags

The following flags are available:

  • -v: Outputs the current version of the interpreter.
  • -c: Only checks the code for syntax errors.
  • -pt: Checks for syntax errors and prints the parsed tree.
  • -I: Indicates that input values are given and writes them to the args list.

Examples

Here are some examples of how to use the SquareBracket Interpreter:

Example 1: Running a SquareBracket program with input values

If you have a SquareBracket program saved in a file called program.sqbr and you want to pass the input values 1.32 and 54.624 to the program, you can use the following command:

sqbra.exe -I program.sqbr 1.32 54.624

This will run the program.sqbr file with the input values passed in as arguments.

Example 2: Checking a SquareBracket program for syntax errors

If you want to check a SquareBracket program called program.sqbr for syntax errors, you can use the following command:

sqbra.exe -c program.sqbr

This will check the program.sqb file for syntax errors and output any errors that are found.

Example 3: Printing the parsed tree of a SquareBracket program

If you want to check a SquareBracket program called program.sqbr for syntax errors and print out the parsed tree, you can use the following command:

sqbra.exe -pt program.sqbr

This will check the program.sqbr file for syntax errors, print out the parsed tree, and output any errors that are found.

Syntax of the Language:

Variables and Assignments

  • cvar <variable_name> <initial_value>: Create a new variable with the given name and assign it the given initial value.
  • mvar <variable1> <variable2> ... <variableN> <initial_value>: Create multiple variables at once with the same initial value.
  • set <variable_name> (<expression>): Assign the value of the given expression to the variable with the given name.
  • clist name length: create a list with the given name and length. Example: clist my_list 5.
  • cmat name length1 length2: create a matrix with the given name and size. Example: cmat my_matrix 5 5.
  • ldef name [1.23,1.65,1.87,23.4]: create a predefined list.
  • mdef name [[1,2],[3,4]]: create a predefined matrix.

Control Flow Statements

  • loop n do [ ... ]: loop n times and execute the code in the indented block. The variable n is decremented each time.
  • sloop n do [ ... ]: same as loop, but changes to the loop variable inside the code block do not affect the loop.
  • autoloop n expr do [ ... ]: assign variable n the value of expression expr at the beginning and then loop n times and execute the code in the indented block. The variable n is decremented each time.
  • if (statement) [ ... ]: execute the code in the indented block if the statement is true. The statement can use the following operators: = (equal), != (not equal), >= (greater or equal), <= (smaller or equal), > (greater), < (smaller). Example: if (x = 0) [set y 1].
  • elif (statement) [ ... ]: execute the code in the indented block if the previous if statement is false and the current statement is true. The statement can use the following operators: = (equal), != (not equal), >= (greater or equal), <= (smaller or equal), > (greater), < (smaller).
  • else [ ... ]: execute the code in the indented block if the previous if statement is false.
  • while (statement) [ ... ]: execute the code in the indented block as long as the statement is true. The statement has the same syntax as in the if command.
  • funct identifier [ ... ]: declare a void function without return value. The end of the corresponding code must be marked with a closing squarebracket in a single line.
  • call identifier: execute a specific function without return value.

Trigonometric Functions

  • sin <target> <source>: Calculates the sine of the source and assigns the result to the target.
  • cos <target> <source>: Calculates the cosine of the source and assigns the result to the target.
  • tan <target> <source>: Calculates the tangent of the source and assigns the result to the target.
  • asin <target> <source>: Calculates the inverse sine of the source and assigns the result to the target.
  • acos <target> <source>: Calculates the inverse cosine of the source and assigns the result to the target.
  • atan <target> <source>: Calculates the inverse tangent of the source and assigns the result to the target.
  • sec <target> <source>: Calculates the secant of the source and assigns the result to the target.

I/O Statements

  • print ["string"]: print a string and break a new line. Example: print ["Hello, world!"].
  • printb ["string"]: same as print, but does not break a new line. Example: printb ["Hello, world!"].
  • printv variable: prints the value of a variable and does not break a new line. Example: printv x.
  • printm matrix: prints the cells of a matrix and does not break a new line. Example: prints mymat.
  • newl: break a new line. Example: newl.
  • input variable ["string"]: ask for user input and save it in the variable. The optional string is used as a prompt. Example: input x ["Please enter a number: "].
  • readf listname ["file.csv"]: read a csv-file containing only a list. Example: readf list ["file.csv"]
  • writef listname ["file.csv"]: write a list in a csv file. Example: writef list ["file.csv"]

List Operators

  • push var list: append the value of var at the end of the list.
  • pop var list: remove the last value of the list and save it in var.
  • chsl list length: change the size of an existing list.
  • getl variable name: get the length of a list and save it in the variable. Example: getl len mylist.
  • getdim rows columns name: get the dimensions of a matrix and save them in the variables. Example: getdim x y mymatrix.
  • list[index]: access an element of a list. Example: set my_list[0] 42.

Basic Math Functions

  • floor x: round down the number x. Example: floor var1.
  • ceil x: round up the number x. Example: ceil var1.
  • round x precision: round the number x to the given number of precision digits. Example: round 3.14159 3.
  • xroot target source rt: calculate var^(1/rt). Example: xroot var1 (2+x) 2 calculates the square root of (2+x) and saves the result in var1.
  • random target max min: set target to a random double with interval [max,min]. Example: random x 0 10.
  • log target source exp: calculate the logarithm of source with exponent exp and save the result in target

Syntax & Structure

All commands must be in a single line. Indentation is optional but renders the code more structured and clear.


Example:

funct printHello [ while (var1 != 0) [ print ["Hello!"] set var1 (var1-2) ] ]

Please take a look at the example programs in the corresponding directory.

Data Structures:

In SquareBracket, the only data structures available are doubles, lists and matrices of doubles. You can create a new list with the "clist" command followed by the name and the length of the list. Matrices can be created with the "cmat" command, the name and two expressions for the length in both dimensions. You can access elements of the list by indexing the list with square brackets, for example: "my_list[index]" or matrices with "my_matrix[i1][i2]". You can also use the "getl" command to get the length of a list and assign it to a variable.