Beer is an interpreter for the Beerlang programming language, written in TypeScript.
Beerlang is a dynamically-typed, interpreted programming language designed for simplicity and fun.
Stay tuned for these exciting additions to Beerlang!
Beerlang supports the following data types:
var a = 12;
and var b = 26;
assign the values 12 and 26 to variables a
and b
, respectively."
). For example, var breakfast = "break";
assigns the string "break" to the variable breakfast
. You can concatenate strings using the +
operator, as shown in the example print "Today's breakfast is " + breakfast + ".";
.true
and false
. For example, var t = true;
assigns the value true
to the variable t
, while var f = false;
assigns false
to the variable f
.nil
represents the absence of a value. For example, var notExists = nil;
assigns nil
to the variable notExists
. When you print notExists
, it will display nil
.var a = 12;
var b = 26;
print a + b;
var t = true;
var f = false;
var notExists = nil;
print notExists;
var breakfast = "break";
print "Today's breakfast is " + breakfast + ".";
Beerlang supports the following arithmetic operators:
var sum = 2 + 3;
assigns the value 5 to the variable sum
.var difference = 7 - 4;
assigns the value 3 to the variable difference
.var product = 5 * 6;
assigns the value 30 to the variable product
.var quotient = 10 / 2;
assigns the value 5 to the variable quotient
.Here's an example that demonstrates the usage of these operators:
var a = 2 + 3; // Addition
var b = 7 - 4; // Subtraction
var c = 5 * 6; // Multiplication
var d = 10 / 2; // Division
print a; // Output: 5
print b; // Output: 3
print c; // Output: 30
print d; // Output: 5
These operators can be used with variables, numeric literals, and expressions to perform arithmetic operations in Beerlang.
Beerlang supports the following control flow statements:
if
statementThe if
statement allows you to conditionally execute a block of code based on a Boolean expression.
Here's an example that demonstrates the usage of the if
statement:
var a = -10;
var condition = a > 3;
if (condition) {
print " I am true...";
} else {
print " I am false...";
}
while
loopThe while
loop allows you to repeatedly execute a block of code as long as a condition is true.
Here's an example that demonstrates the usage of the while
loop:
var i = 1;
while (i < 8) {
print i;
i = i + 1;
}
for
loopThe for
loop allows you to iterate over a range of values or elements in Beerlang.
for (var i =0; i < 10; i= i + 1){
print i;
}
These control flow statements allow you to make decisions, repeat actions, and iterate over values in Beerlang, adding flexibility and control to your programs.
In Beerlang, functions are first-class citizens, which means they can be assigned to variables, passed as arguments to other functions, and returned as values from other functions. You can define functions using the fun
keyword, followed by the function name and a pair of parentheses for the parameter list. The function body is enclosed in curly braces {}
.
Here's an example of a function that adds two numbers:
fun add(a, b) {
return a + b;
}
var result = add(3, 4);
print(result); // Output: 7
fun fib(n) {
if (n <= 1) return n;
return fib(n - 2) + fib(n - 1);
}
for (var i = 0; i < 20; i = i + 1) {
print fib(i);
}
Beerlang supports closures, which are functions that have access to variables from their outer (enclosing) scope. This allows functions to "remember" the values of variables even after they have finished executing. Closures can be useful for creating functions with private variables or for implementing callback functions.
Here's an example of a closure in Beerlang:
fun counter() {
var count = 0;
fun increment() {
count = count + 1;
print("Count: " + count);
}
return increment;
}
var counterFunc = counter();
counterFunc(); // Output: Count: 1
counterFunc(); // Output: Count: 2
In the example above, the counter
function returns the increment
function, which has access to the count
variable in its outer scope. Each time the increment
function is called, it increments the count
variable and prints the updated value.
Closures in Beerlang provide a powerful mechanism for creating functions with encapsulated state and behavior.
Beerlang supports object-oriented programming through the use of classes. A class is a blueprint for creating objects that share similar properties and behaviors. In Beerlang, you can define classes using the class
keyword, followed by the class name and a pair of curly braces {}
to enclose the class body.
In Beerlang, the init
method is used as a constructor for a class. It is called when a new instance of the class is created. The init
method initializes the properties of the class and performs any necessary setup.
Here's an example of a class definition in Beerlang:
class Breakfast {
init() {
this.name = "cake";
this.number = 2;
print "Cakes ready!";
}
eat() {
if (this.number > 0) {
print "Eating " + this.name + "...";
this.number = this.number - 1;
} else {
print "Nothing to eat.";
}
}
}
var breakfast = Breakfast();
var quickEat = breakfast.eat;
quickEat();
breakfast.eat();
quickEat();
Beerlang's class-based approach allows you to organize your code into reusable and modular components, making it easier to manage and maintain your programs.
In Beerlang, you can create classes that inherit properties and behaviors from other classes. This is known as inheritance and allows you to create a hierarchy of classes with shared characteristics.
To define a class that inherits from another class, you can use the <
keyword followed by the name of the parent class. The child class can then access and override the properties and methods of the parent class.
Here's an example that demonstrates inheritance in Beerlang:
class Doughnut {
cook() {
print "Fry until golden brown.";
}
}
class BostonCream < Doughnut {
cook() {
super.cook();
print "Pipe full of custard and coat with chocolate.";
}
}
BostonCream().cook();
Inheritance allows you to reuse code and create a more organized and modular class structure in Beerlang.
To use Beerlang, ensure that you have Bun.js and npm installed on your system. Then, follow these steps:
git clone https://github.com/Riley1101/Beerlang
>cd beerlang
bun install
bun run build
bun start
To execute a Beerlang script, use the following command:
bun beer myscript.beer
Replace myscript.beer
with the path to your Beerlang script file.
Checkout examples in /examples
Contributions to Beerlang are welcome! If you find any bugs, have feature requests, or want to contribute enhancements, please open an issue or submit a pull request on the
The next step in the development of Beerlang is to add more fun to it. This will involve introducing some funny syntax to make the language even more playful and unconventional. Stay tuned for these exciting additions to Beerlang!
Beerlang is open source and released under the MIT License.
Generated using TypeDoc