Language Overview
Introduction
Twinkies is a modern, statically-typed programming language designed for hobbyist and educational use. It combines the familiarity of C syntax with modern language features like static type checking, automatic bounds checking, and a foreign function interface.
Design Philosophy
Twinkies was designed with these principles in mind:
- Familiarity - C-like syntax makes it easy to learn for developers coming from C, C++, or similar languages
- Safety - Static type checking and automatic bounds checking catch errors at compile time
- Flexibility - FFI and inline assembly allow low-level access when needed
- Simplicity - Clean, straightforward language design without unnecessary complexity
Key Features
Static Type System
Every variable and function must have an explicit type. The compiler performs comprehensive type checking to catch errors before runtime.
let x: int = 42; // ✓ Correct
let y: bool = true; // ✓ Correct
let z: int = "hello"; // ✗ Error: type mismatch
Automatic Array Bounds Checking
Arrays are automatically bounds-checked at compile-time (for constant indices) and runtime (for dynamic indices).
let arr: int[5];
arr[4] = 10; // ✓ Valid (0-4 are valid indices)
arr[10] = 20; // ✗ Compile-time error: out of bounds
Foreign Function Interface (FFI)
Call functions from external libraries (DLLs, shared objects) directly from Twinkies code.
extern "cdecl" from "kernel32.dll" {
func GetTickCount() -> int;
}
func main() -> int {
let ticks: int = GetTickCount();
print(ticks);
return 0;
}
Inline Assembly
Embed platform-specific assembly code directly in your programs.
func get_cpu_info() -> int {
let eax: int;
asm {
"cpuid"
: "=a" (eax)
: "a" (1)
: "ebx", "ecx", "edx", "cc"
};
return eax;
}
Module System
Organize code into modules with header files for clean separation of interface and implementation.
// math.tlh (header)
func factorial(n: int) -> int;
// math.tl (implementation)
func factorial(n: int) -> int {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// main.tl
#include "math.tlh"
func main() -> int {
print(factorial(5));
return 0;
}
Function Overloading
Define multiple functions with the same name but different parameter types.
func add(x: int) -> int {
return x + 1;
}
func add(x: int, y: int) -> int {
return x + y;
}
func add(flag: bool) -> int {
return flag ? 42 : 0;
}
Compilation Process
Twinkies uses a traditional multi-phase compiler architecture:
- Lexical Analysis - Converts source code into tokens
- Parsing - Builds an Abstract Syntax Tree (AST)
- Semantic Analysis - Type checking and symbol resolution
- IR Generation - Converts AST to three-address code
- Code Generation - Produces C or assembly output
Target Platforms
Twinkies generates code for:
- C - Standard C99 code (default, recommended)
- Assembly - x86-64 assembly (experimental)
The generated C code can be compiled with any standard C compiler (GCC, Clang, MSVC).
Type System Overview
Twinkies supports:
- Primitive Types:
int,int8,int16,int32,int64,float,double,bool,string,void,null - Array Types: Fixed-size arrays like
int[5] - Function Types: Functions with typed parameters and return values
Type conversions are automatic for numeric types (with warnings), but explicit for other conversions.
Error Handling
The compiler provides detailed error messages with:
- Line and column numbers
- Error context
- Helpful suggestions
- Color-coded output (errors, warnings, info, hints)
Next Steps
- Read Syntax and Basics to learn the language syntax
- Check out Examples Guide to see real code examples
- Try the Quick Reference for a syntax cheat sheet
