Skip to content
Eric Brown edited this page Mar 20, 2024 · 11 revisions

Hello World

As with any programming language, let's start with a simple hello world example. Create hello_world.pb with the following text inside:

print "Hello, World!"

Note: PBPL (Peanut Butter Programming Language) is case-insensitive for keywords. The only time case matters is for string literals.

Then to compile and run the program, use cpbpl and pbpl respectively.

cpbpl hello_world.pb
pbpl hello_world.nut  # Notice the change in file extension.

After compiling, the file extension changes from .pb to .nut. The .nut file is the compiled bytecode format for the Peanut Butter Virtual Machine, similar to how a .class file works in Java.

Comments

To write a comment, use parentheses.

(This is a comment)
(This is another comment)
print "Hello, World!"  (This prints out "Hello, World!")

Variables

To declare a variable, use the let keyword.

let a be 5.  (This assigns 5 to the variable 'a')

In declarations, you use let and be to create the variable and a period to finish off a statement. If you want to reassign a variable, use set and to.

set a to 7. (Changes a's value to 7)

Data Types

There are three different data types in Peanut Butter: String, Boolean, and Integer. (Floating point numbers are overrated anyways, right?)

let string be "Hello". (Can also use single quotes as in 'Hello')
let integer be 123.
let boolean be true.  (Or false) 

Variables can change their type at any time as well.

let string be "I am a string".
set string to 45.

Conditionals

To use an if statement, use the if and otherwise keywords.

let a be 4.
if a:
	print 'a is truthy'.
otherwise:
	print 'a is falsey'.

Note the indentation is very significant. You must use tabs (which are superior to spaces) and the indentation for the if and otherwise must line up.

Boolean expressions are evaluated based on the following rules:

  1. Booleans evaluate to true if they are true and false otherwise.
  2. Strings evaluate to true if they are not empty and false otherwise.
  3. Integers evaluate to true if they are not equal to 0 and false otherwise.

Basically, the same rules for C-like languages.

If you want to chain conditionals together, you have to place the second statement in the otherwise block of the first one like so:

let a be 4.
if a is 3:
	print 'a is 3'.
otherwise:
	if a is 4:
		print 'a is 4'.
	otherwise:
		print 'You should not see this'.

Be careful of the indentation.

To compare values, you can use greater than, less than, or is for equality.

print 4 greater than 3.  (Prints out 'true')
print 3 less than 4.  (Prints out 'false')
print 4 is 3.  (Prints out 'false')

Loops

The only kind of loops in Peanut Butter are while loops.

let x be 10.
while x greater than -1:  (Counts down from 10 to 0)
	print x.
	set x to x minus 1.

To chain expressions together use and and or.

Math Operations

Several math operations are supported in PBPL:

  1. mod for modulo or remainder division
  2. over for regular or integer division
  3. plus for addition
  4. minus for subtraction
  5. times for multiplication
print 'Hello, ' plus 'World!'. (Prints 'Hello, World!')
print 5 times 3 mod 10 minus 2. (Prints 3)

Notice the order of operations are specified from left to right as you would read it in English. Essentially every operator has the same precedence and left-to-right associativity.

Clone this wiki locally