This repository contains basic information about the Barebone programming language, some code for basic calculation and a Barebone compiler program in GUI, for the final presentation topic in my Principles of Programming Languages class.
The goals are:
-
Presents about the Barebone programming language.
-
Builds a program that compiles code in Barebone to perform addition, subtraction, multiplication, division and comparison operation.
-
Builds the program in GUI.
Table of Contents
Barebone is a procedural programming language with these features :
-
Barebone does not have subroutines such as functions and procedures.
-
Code can only contain statements. Each statement must ends with a semicolon
;
. -
The only data type is non-negative integer.
-
Variables are global. When first mentioned in a statement, the variable is set to zero before executing the statement.
-
A loop can be nested in another loop.
-
There are no standard I/O.
-
Case-insensitive. For example,
X
andincr
are the same asx
andINCR
, respectively. -
Keywords are
clear
,decr
,do
,end
,incr
,not
andwhile
. -
Variable names can only contain ASCII alphanumeric or underscore
_
. Variable names do not start with a digit. Variable names are case-insensitive and cannot be the same as any keyword.
There are three statements in Barebone:
-
clear X;
assign the variableX
to zero. -
incr X;
increaseX
by one. -
decr X;
decreaseX
by one if it is greater than zero.
and a loop structure
while X not 0 do ... end;
that works in two steps
-
Exits the loop if
X
is zero, otherwise goes to step 2. -
Sequentially executes statements in the
...
until the statementend;
. Goes to step 1.
Each space can be replaced by a newline or a tab (four whitespaces) for readability, for example while X not 0 do decr X end;
is the same as
while X not 0 do
decr X;
end;
or
while X not 0 do
decr X;
end;
It is recommended to add newlines and tabs at right places for readability.
Example of a valid code snippet:
clear Y;
clear T;
while X not 0 do
incr Y;
incr T;
decr X;
end;
while T not 0 do
incr X;
decr T;
end;
Files mentioned in this section can be found in app/src/main/resources/snippets/
.
-
Branching structure
if X != 0 then X := 0 else X := 1
intoggle.txt
. It can be calledinvert X
. -
Addition operation
Z := X + Y
in fileadd.txt
. -
Subtraction operation
Z := X - Y
in filesubtract.txt
. -
Multiplication operation
Z := X * Y
in filemultiply.txt
. -
Integer division operation
Z := floor(X / Y)
in fileint_divide.txt
. -
Comparison operation
if X < Y then Z := 0 else Z := 1
in fileless_than.txt
, can be rewritten in pseudo-code as:
Z := 0
T_X := X
T_Y := Y
while T_Y != 0 do
Z := Z + 1
T_Y := T_Y - 1
while T_X != 0 do
if Z > 0 then Z := Z - 1
T_X := T_X - 1
T_X := T_X + 1
while Z != 0 do
T_X := 0
Z := 0
while T_X != 0 do
Z := Z + 1
T_X := T_X - 1
- Other comparison operations can be found in
not_less_than.txt
,greater_than.txt
,not_greater_than.txt
,equals.txt
,not_equals.txt
.
In place of clear X
, we can use this code:
while X not 0 do
decr X;
end;
A signed integer X_NEG
equals zero if
For example, an integer
-
X
as the absolute value of$X$ and -
X_NEG
as the sign variable.
We can change the sign of
if X_NEG != 0 then X_NEG := 0 else X_NEG := 1
Barebonesim (Barebone + Simulator) is a Barebone compiling simulation Java program in GUI. Barebonesim is built with Gradle 7.5.1.
Oracle JDK 1.8+ is required. Check the version of Java using this command
java -version
If Java is not available, download it following the instruction on java.com and run the command above to confirm the version.
Clone this repo.
git clone https://github.com/hnthap/barebonesim.git -depth 1
In the top level directory of this repo (containing the file gradlew
), run this command
./gradlew run
This window will appear :
The main window insists of
-
Menu bar ("Thanh menu") contains basic command to interact with file and code,
-
Toolbar ("Thanh công cụ") contains buttons (from left to right) to run code, open file, save file, check for errors and "beautify" code, create snippets and close file.
-
Code area ("Cửa sổ soạn thảo") to write and change source code.
-
Input window ("Cửa sổ Input", on the left of the Code area) to work with input variables.
-
Output window ("Cửa sổ Output", on the right of the Code area) to work with the result of variables after executing code.
-
Current file's absolute path is displayed below the Code area ("Thanh hiển thị địa chỉ").
Code can be written directly in the Code area. Click on 🗁
in Toolbar to open a file. Save the code by clicking on 💾
in Toolbar.
While coding, check for errors and "beautify" code by clicking on ❀
(the flower icon) in Toolbar.
To save your coding time, use existing snippets that perform basic operations (addtion, subtraction, multiplication, division, comparison, Boolean toggling, changing a variable's sign).
Using Input window (on the left of the Code area), you can assign starting values for some variables.
-
To add new variable, select
[+]
(the plus sign). Enter the variable's name and value and select OK. -
To change the value of a variable, select
[✎]
(the pencil icon). Choose the variable to change, select OK and enter its new value, select OK. -
To remove a variable from the list, select
[-]
(the minus sign). Choose the variable to remove and select OK.
These are starting values of variables before the code is executed.
Alternatively, these variables can be added from a CSV file. Click on
From CSV
button and choose the file. CSV file follows RFC 4180 and does NOT contain the header row.An example for a valid CSV file :
X,12 Y,20 Z,0
After starting values of variables and the code are completed, execute (run) the code by clicking on ▶
(the triangle icon) in Toolbar.
If an error is catched in code (invalid variable name, infinite loop, etc.) or it's executed beyond the time limit (
If successfully executed, the values of variables after executing will be displayed on Output window (on the right of the Code area).
Save results to a CSV file by clicking on
Extract to CSV
on Output window.Bring results on Output window to Input window by clicking on
From Result
on Input window.
If the code and starting values of variables are changed after execution, update the Output window by execute again.
For example, to perform A := Z * T
with specified values of Z
and T
:
-
On Menu bar, select File → New File and create new file.
-
After a new file is created and opened, on Menu bar, select Snippets → Snippets.
-
On "Snippets" window, choose
Z := X * Y
and select OK. -
On "Snippet Settings" window, rename variables as below and select OK.
Remember to name temporary variables so that they don't have the same name with any variables in use. Two variables in the same snippet cannot have the same name. Temporary variable names should start with "T" or "T_".
Temporary variables can be reused in other operations. After an operation perform by a snippet, all temporary variables will be set to zero.
-
On "Here You Have" window, select all new code and press Ctrl + C (or equivalent shortcut to copy text) and select OK.
-
In the Code area, press Ctrl + V (or equivalent shortcut to paste text).
-
Click on
[+]
on Input window (on the left of the Code area), then enter the name and starting value ofZ
(equals$12$ for example).Do the same thing for
T
(equals$15$ for example).The Input window will be displayed like this:
-
Click on
▶
in Toolbar to execute your code. The main window can be like below:On the Output window (on the right of the Code area), we see that
A
equals$180$ , which is what we need.
Code is compiled and executed arcording to the following steps:
-
Starting values of variables (on the Input window, on the left of the Code area) are stored in a
VariableContainer
object. It stores all "input" variables by their names and values. -
Parser converts code into a list of abstract syntax trees. Each of them represents a statement. (A loop is also a statement.)
-
While parsing, if a syntax error is detected, Parser will stop and report the error.
-
If the parsing process is success, the list of abstract syntax trees will be stored in Compiler and be executed sequentially in another thread. All changes in variables' values will be applied in a copy of the
VariableContainer
object in the first step. -
If the code is executed beyond the time limit (timeout), Compiler will stop and report the error. (The time limit is
$1000 ;\textrm{ms}$ by default and can be modified.) -
If executed successfully, final values in the
VariableContainer
will be displayed on Output window (on the right of the Code area).
All steps are performed in Barebonesim's runtime. There is no executable file created.
Conceptually, Barebone only supports incr
, decr
, clear
and while
statements, other than that there is no more statements. Some Barebone compilers additionally support read
and print
statements to enter and print a variable's value.
Although, to emphasize the simplicity of Barebone, Barebonesim does not support standard I/O. Instead, the results can be displayed on Output window.
Barebonesim does not yet support code commenting.
If you have any suggestion, feel free to fork this repo and create a pull request. Any contributions are greatly appreciated. I may be slow to respond to pull requests, but I'll try to respond later.
Distributed under the MIT License. See LICENSE
for more information.