- Add function declarations, calls, and return statements - Implement lexical scoping with Environment class and closures - Convert print from statement to standard library function - Add assert() function to standard library for testing - Add time() function for microsecond precision benchmarking - Create StdLib class and BuiltinFunction wrapper for standard library - Implement first-class functions and higher-order functions - Add function parameter support (tested up to 100 parameters) - Support alphanumeric identifiers in variable and function names - Add underscore support in variable names and identifiers - Implement string + number and number + string concatenation - Add boolean + string and string + boolean concatenation - Support string multiplication (string * number) - Fix decimal truncation issue by using std::stod for all number parsing - Add comprehensive number formatting with proper precision handling - Support huge numbers (epoch timestamps) without integer overflow - Clean number display (no trailing zeros on integers) - Add basic error handling with program termination on errors - Add comprehensive test suite covering all features - Add escape sequence support (\n, \t, \", \\) - Add comprehensive documentation and language reference - Update development roadmap with completed features
8.3 KiB
8.3 KiB
Bob Language Reference
Overview
Bob is a dynamically typed programming language with a focus on simplicity and expressiveness. It features automatic type conversion, closures, and a clean syntax inspired by modern programming languages.
Table of Contents
- Data Types
- Variables
- Operators
- Functions
- Control Flow
- Standard Library
- Error Handling
- Examples
- Language Nuances
Data Types
Numbers
- Integers:
42,-10,0 - Floats:
3.14,2.718,-1.5 - Automatic conversion: Numbers are stored as doubles internally
Strings
- Literal strings:
"Hello, World!" - Empty strings:
"" - Escape sequences: Not currently supported
Booleans
- True:
true - False:
false
None
- Null value:
none(represents absence of value)
Variables
Declaration
var name = "Bob";
var age = 25;
var isActive = true;
Assignment
var x = 10;
x = 20; // Reassignment
Scoping
- Global scope: Variables declared at top level
- Local scope: Variables declared inside functions
- Shadowing: Local variables can shadow global variables
- No
globalkeyword: Unlike Python, Bob doesn't require explicit global declaration
Variable Behavior
var globalVar = 100;
func testScope() {
var localVar = 50; // Local variable
return localVar + globalVar; // Can access global
}
Operators
Arithmetic Operators
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Modulo:
%
Comparison Operators
- Equal:
== - Not equal:
!= - Greater than:
> - Less than:
< - Greater than or equal:
>= - Less than or equal:
<=
String Operators
Concatenation
Bob supports bidirectional string + number concatenation with automatic type conversion:
// String + Number
"Hello " + 42; // → "Hello 42"
"Pi: " + 3.14; // → "Pi: 3.14"
// Number + String
42 + " items"; // → "42 items"
3.14 + " is pi"; // → "3.14 is pi"
String Multiplication
"hello" * 3; // → "hellohellohello"
3 * "hello"; // → "hellohellohello"
Note: String multiplication requires whole numbers.
Number Formatting
Bob automatically formats numbers to show only significant digits:
"Count: " + 2.0; // → "Count: 2" (no trailing zeros)
"Pi: " + 3.14; // → "Pi: 3.14" (exact precision)
"Integer: " + 42; // → "Integer: 42" (no decimal)
Functions
Function Declaration
func add(a, b) {
return a + b;
}
Function Call
var result = add(2, 3); // result = 5
Parameters
- Any number of parameters supported
- No default parameters (not implemented)
- No keyword arguments (not implemented)
Return Values
- Explicit return:
return value; - Implicit return: Functions return
noneif no return statement - Early return: Functions can return from anywhere
Closures
Bob supports lexical closures with variable capture:
var outerVar = "Outer";
func makeGreeter(greeting) {
return greeting + " " + outerVar;
}
var greeter = makeGreeter("Hello");
// greeter captures outerVar from its lexical scope
Nested Functions
func outer() {
func inner() {
return 42;
}
return inner();
}
Control Flow
Current Status
Control flow statements are NOT implemented yet:
ifstatementswhileloopsforloopselseclauses
Planned Features
- Conditional execution
- Looping constructs
- Logical operators (
and,or,not)
Standard Library
Print Function
print("Hello, World!");
print(42);
print(true);
Usage: print(expression)
- Prints the result of any expression
- Automatically converts values to strings
- Adds newline after output
Assert Function
assert(condition, "optional message");
Usage:
assert(true)- passes silentlyassert(false)- throws error and stops executionassert(condition, "message")- includes custom error message
Behavior:
- Terminates program execution on failure
- No exception handling mechanism (yet)
- Useful for testing and validation
Error Handling
Current Error Types
- Division by zero:
DivisionByZeroError - Type errors:
Operands must be of same type - String multiplication:
String multiplier must be whole number - Assertion failures:
Assertion failed: condition is false
Error Behavior
- No try-catch: Exception handling not implemented
- Program termination: Errors stop execution immediately
- Error messages: Descriptive error messages printed to console
Common Error Scenarios
// Division by zero
10 / 0; // Error: DivisionByZeroError
// Type mismatch
"hello" - "world"; // Error: Cannot use '-' on two strings
// Invalid string multiplication
"hello" * 3.5; // Error: String multiplier must be whole number
// Undefined variable
undefinedVar; // Error: Undefined variable
Examples
Basic Calculator
func add(a, b) {
return a + b;
}
func multiply(a, b) {
return a * b;
}
var result = add(5, multiply(3, 4));
print("Result: " + result); // Result: 17
String Processing
func greet(name) {
return "Hello, " + name + "!";
}
func repeat(str, count) {
return str * count;
}
var greeting = greet("Bob");
var repeated = repeat("Ha", 3);
print(greeting + " " + repeated); // Hello, Bob! HaHaHa
Variable Scoping Example
var globalCounter = 0;
func increment() {
var localCounter = 1;
globalCounter = globalCounter + localCounter;
return globalCounter;
}
print("Before: " + globalCounter); // Before: 0
increment();
print("After: " + globalCounter); // After: 1
Language Nuances
Type System
- Dynamic typing: Variables can hold any type
- Automatic conversion: Numbers and strings convert automatically
- No type annotations: Types are inferred at runtime
Memory Management
- Reference counting: Uses
std::shared_ptrfor automatic memory management - No manual memory management: No
deleteorfreeneeded - Garbage collection: Automatic cleanup of unused objects
Performance Characteristics
- Interpreted: Code is executed by an interpreter
- AST-based: Abstract Syntax Tree for execution
- No compilation: Direct interpretation of source code
Syntax Rules
- Semicolons: Required at end of statements
- Parentheses: Required for function calls
- Curly braces: Required for function bodies
- Case sensitive:
varandVarare different
Comparison with Other Languages
vs Python
- Similar: Dynamic typing, functions, closures
- Different: No
globalkeyword, automatic string conversion, different error handling
vs JavaScript
- Similar: Automatic type conversion, string concatenation
- Different: No
undefined, different syntax, nonull
vs Lua
- Similar: Dynamic typing, functions
- Different: No
localkeyword, different scoping rules
Limitations
- No control flow: No if/while/for statements
- No logical operators: No
and/or/not - No exception handling: No try-catch blocks
- No modules: No import/export system
- No classes: No object-oriented features
- No arrays/lists: No built-in collection types
- No dictionaries: No key-value data structures
Future Features
- Control flow statements
- Logical operators
- Exception handling
- Collection types (arrays, dictionaries)
- Modules and imports
- Object-oriented programming
- Standard library expansion
Getting Started
Running Bob Code
# Compile the interpreter
make
# Run a Bob file
./build/bob your_file.bob
# Run the comprehensive test suite
./build/bob test_bob_language.bob
File Extension
.bob: Standard file extension for Bob source code
Interactive Mode
- Not implemented: No REPL (Read-Eval-Print Loop) yet
- File-based: All code must be in
.bobfiles
This documentation covers Bob language version 1.0. For the latest updates, check the ROADMAP.md file.