An open API service indexing awesome lists of open source software.

https://github.com/washed07/onyx

The Onyx Programming Language
https://github.com/washed07/onyx

compiler language llvm

Last synced: 6 months ago
JSON representation

The Onyx Programming Language

Awesome Lists containing this project

README

          

# Onyx Programming Language

## CURRENTLY IN EARLY DEVELOPMENT, EXPECT BUGS

Onyx is a modern compiled programming language that compiles to LLVM IR and then to native machine code. It features C-style syntax with modern language constructs, built-in string operations, and seamless integration with LLVM's optimization pipeline.

## Features

- **C-style syntax** with modern improvements
- **Strong type system** with type inference
- **LLVM backend** for optimized machine code generation
- **Built-in string operations** including concatenation
- **Standard library** with I/O operations
- **Control flow** structures (if/elif/else, while, for, break, continue)
- **Function definitions** with optional type hints
- **Mathematical intrinsics** (sqrt, sin, cos, etc.)
- **Cross-platform support** (Windows, Linux, macOS)

## Installation

### Prerequisites

- **LLVM 17+** with development headers
- **Clang** compiler
- **CMake 3.20+**
- **C++17 compatible compiler**

### Building from Source

1. **Clone the repository:**

```bash
git clone
cd Onyx
```

2. **Create build directory:**

```bash
mkdir build
cd build
```

3. **Configure with CMake:**

```bash
cmake ..
```

4. **Build the compiler:**

```bash
cmake --build . --config Release
```

5. **The compiler executable will be available at:**
```
bin/Onyx.exe (Windows)
bin/Onyx (Linux/macOS)
```

## Language Syntax

### Basic Types

```onyx
int // 32-bit signed integer
short // 16-bit signed integer
long // 64-bit signed integer
float // 32-bit floating point
double // 64-bit floating point
real // 128-bit floating point
char // 8-bit character
str // String type
bool // Boolean (true/false)
void // No return value
```

### Variable Declarations

```onyx
// Type-first declaration
int x = 42;
str message = "Hello, World!";

// Type annotation with colon
x: int = 42;
message: str = "Hello, World!";

// Type inference (requires initializer)
auto value = 3.14; // inferred as float
```

### Function Definitions

```onyx
// Basic function
func add(int a, int b) int {
return a + b;
}

// Function with optional return type hint
func greet(str name) {
outputln("Hello " + name + "!");
}

// Function with type hints on parameters
func calculate(x: float, y: float) float {
return x * y + 10.0;
}
```

### Control Flow

#### Conditional Statements

```onyx
// Basic if statement
if (condition) {
// code
}

// If-elif-else chain
if (x > 0) {
outputln("Positive");
} elif (x < 0) {
outputln("Negative");
} else {
outputln("Zero");
}

// Optional parentheses
if condition {
// code
}

// Optional return type hints
if (x > 0) int {
return 1;
}
```

#### Loops

```onyx
// While loop
while (condition) {
// code
}

// For loop (C-style)
for (int i = 0; i < 10; i++) {
output(i);
}

// Break and continue
while (true) {
if (condition1) {
continue;
}
if (condition2) {
break;
}
}
```

### Operators

#### Arithmetic Operators

```onyx
+ - * / % // Basic arithmetic
** // Power (if implemented)
++ -- // Increment/decrement (prefix and postfix)
```

#### Comparison Operators

```onyx
== != // Equality
< <= > >= // Relational
```

#### Logical Operators

```onyx
&& || // Short-circuit logical AND/OR
& | // Bitwise AND/OR
! // Logical NOT
^ // Bitwise XOR
~ // Bitwise NOT
<< >> // Bit shifts
```

#### Assignment Operators

```onyx
= // Basic assignment
+= -= *= /= // Compound assignment
```

### String Operations

```onyx
str greeting = "Hello";
str name = "World";
str message = greeting + " " + name + "!"; // String concatenation

// Automatic type conversion in string context
int number = 42;
str result = "The answer is " + number; // number auto-converted to string
```

### Built-in Functions

#### I/O Operations

```onyx
output(value); // Print value without newline
outputln(value); // Print value with newline
char c = input(); // Read single character
str line = inputln(); // Read entire line
quit(); // Exit program immediately
```

#### Mathematical Functions

```onyx
// Single argument functions
sqrt(x) sin(x) cos(x) tan(x)
asin(x) acos(x) atan(x)
sinh(x) cosh(x) tanh(x)
exp(x) exp2(x) log(x) log10(x) log2(x)
floor(x) ceil(x) round(x) trunc(x)
abs(x)

// Two argument functions
pow(x, y) copysign(x, y)
minnum(x, y) maxnum(x, y)
minimum(x, y) maximum(x, y)

// Three argument functions
fma(x, y, z) // Fused multiply-add: x * y + z

// Bit manipulation (integer types only)
ctpop(x) // Count population (number of 1 bits)
ctlz(x) // Count leading zeros
cttz(x) // Count trailing zeros
```

## Example Programs

### Hello World

```onyx
func main() int {
outputln("Hello, World!");
return 0;
}
```

### Fibonacci Sequence

```onyx
func fib(int n) int {
if (n <= 1) int {
return n;
}
return fib(n - 1) + fib(n - 2);
}

func main() int {
for (int i = 0; i < 10; i++) {
outputln("fib(" + str(i) + ") = " + str(fib(i)));
}
return 0;
}
```

### Interactive Program

```onyx
func main() int {
output("What's your name? ");
str name = inputln();
outputln("Hello " + name + "!");

output("Enter a number: ");
int num = int(inputln());
output("Solid Choice !" + str(num));

return 0;
}
```

## Compiling Programs

### Basic Compilation

1. **Create an Onyx source file** (e.g., `hello.ox`):

```onyx
func main() int {
outputln("Hello, World!");
input(); // Prevents immediate exit
return 0;
}
```

2. **Compile the program:**

```bash
./bin/Onyx hello.ox
```

3. **Run the executable:**
```bash
./hello # Linux/macOS
hello.exe # Windows
```

### Compilation Process

The Onyx compiler follows a multi-stage compilation process:

1. **Lexical Analysis** - Source code → Tokens
2. **Parsing** - Tokens → Abstract Syntax Tree (AST)
3. **Semantic Analysis** - AST validation and symbol resolution
4. **IR Generation** - AST → LLVM Intermediate Representation
5. **Code Generation** - LLVM IR → Machine Code
6. **Linking** - Combines with runtime libraries

### Output Files

During compilation, several intermediate files are generated:

- `program.ll` - LLVM IR representation
- `program.exe` - Final executable (Windows)
- `program` - Final executable (Linux/macOS)

## Language Specification

### Comments

```onyx
// Single-line comments only
// Multi-line comments not yet supported
```

### Literals

```onyx
// Integer literals
42
-17

// Floating-point literals
3.14
2.5e-3
1.0E+10

// String literals
"Hello, World!"
"Escape sequences: \n \t \\ \""

// Character literals
'A'
'\n'
'\t'

// Boolean literals
true
false
```

### Identifiers

- Start with letter or underscore
- Followed by letters, digits, or underscores
- Case-sensitive

### Reserved Keywords

```
func if elif else while for
return break continue true false null
```

## Development Status

Onyx is currently in active development. Features and syntax may change as the language evolves.

### Current Limitations

- Limited standard library
- No module system yet
- No generic types
- Limited error messages
- No package manager

### Planned Features

- Module system
- Generic types
- Improved error reporting
- Extended standard library
- Package manager
- IDE integration

## Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Submit a pull request

## License

MIT License - see LICENSE file for details.

## Support

For questions, bug reports, or feature requests, please open an issue on the project repository.

---

_Onyx Programming Language - A modern compiled language with LLVM backend_