Skip to content

Commit

Permalink
Initializes the project
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Sep 13, 2023
0 parents commit 1716117
Show file tree
Hide file tree
Showing 27 changed files with 2,441 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: ultimaweapon
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "deps/llvm"]
path = deps/llvm
url = https://github.com/llvm/llvm-project.git
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "Stage 0 (std)",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/stage0/target/debug/pluto",
"args": [
"${workspaceFolder}/std"
],
"cwd": "${workspaceFolder}",
"env": {
"RUST_BACKTRACE": "1"
},
"preLaunchTask": "stage0"
}
],
"version": "2.0.0"
}
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"editor.rulers": [100],
"files.exclude": {
"deps": true
},
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
}
14 changes: 14 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "stage0",
"type": "cargo",
"command": "build",
"group": "build",
"options": {
"cwd": "${workspaceFolder}/stage0"
}
}
]
}
38 changes: 38 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.

Subject to the terms and conditions of this license, each copyright holder and contributor hereby
grants to those receiving rights under this license a perpetual, worldwide, non-exclusive,
no-charge, royalty-free, irrevocable (except for failure to satisfy the conditions of this license)
patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer this
software, where such license applies only to those patent claims, already acquired or hereafter
acquired, licensable by such copyright holder or contributor that are necessarily infringed by:

(a) their Contribution(s) (the licensed copyrights of copyright holders and non-copyrightable
additions of contributors, in source or binary form) alone; or

(b) combination of their Contribution(s) with the work of authorship to which such Contribution(s)
was added by such copyright holder or contributor, if, at the time the Contribution is added, such
addition causes such combination to be necessarily infringed. The patent license shall not apply to
any other combinations which include the Contribution.

Except as expressly stated above, no rights or licenses from any copyright holder or contributor is
granted under this license, whether expressly, by implication, estoppel or otherwise.

DISCLAIMER

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
126 changes: 126 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Pluto

Pluto is an experimental OOP language inspired by Rust with the following goals:

- Compiled to native code.
- [Non-fragile](https://en.wikipedia.org/wiki/Fragile_binary_interface_problem) and stable ABI.
- GC using reference counting.
- Runtime reflection.
- Error handling using exception.
- No null value (except a pointer).
- Option type.

Pluto borrowed most of the syntax from Rust except:

- Pluto is an OOP language like Java or C#.
- Easy to learn, especially for people who already know Java, C# or Rust.
- No lifetime, no borrow checker, no const VS mut.
- No borrowed and owned type like `str` and `String`.
- Use exception like Java or C# for error handling (no checked exception).
- Pluto was designed for application programming rather than systems programming.

The goal of Pluto is to be a modern OOP language with the productivity of Rust syntax.

## Different from Java or C#

The main different is Pluto compiled to native code instead of Java bytecode or Common Intermediate
Language, which can be run without a VM. The benefit with this are:

- Low memory footprint.
- Fast startup.
- Can be run on a client machine directly without a VM.
- Easy to interop with other languages.

## Current state

I'm currently writing the stage 0 compiler along side the `std` library. The goal of stage 0
compiler is to compile the `std` and `cli`. Once the first version of `cli` is fully working Pluto
will become a self-hosted language.

## Example

```
@pub
class Allocator;
impl Allocator {
@pub
fn Alloc(size: usize, align: usize): *u8 {
@cfg(unix)
let ptr = aligned_alloc(align, size);
@cfg(windows)
let ptr = _aligned_malloc(size, align);
if ptr == null {
@cfg(os != "windows")
abort();
@cfg(os == "windows")
asm("int 0x29", in("ecx") 7, out(!) _);
}
ptr
}
@pub
fn Free(ptr: *u8) {
@cfg(unix)
free(ptr);
@cfg(windows)
_aligned_free(ptr);
}
@cfg(unix)
@ext(C)
fn aligned_alloc(align: usize, size: usize): *u8;
@cfg(unix)
@ext(C)
fn free(ptr: *u8);
@cfg(windows)
@ext(C)
fn _aligned_malloc(size: usize, align: usize): *u8;
@cfg(windows)
@ext(C)
fn _aligned_free(ptr: *u8);
@cfg(unix)
@ext(C)
fn abort(): !;
}
```

## Build from source

### Prerequisites

- Git
- Rust
- C++ toolchain (e.g. MSVC, XCode, GCC)
- CMake

### Download the source

You need to clone this repository with submodules like this:

```sh
git clone --recurse-submodules https://github.com/ultimaweapon/pluto.git
```

### Build dependencies

#### Linux and macOS

Run the following command in the root of this repository:

```sh
CMAKE_BUILD_PARALLEL_LEVEL=2 ./build-deps.sh
```

## License

BSD-2-Clause Plus Patent License
15 changes: 15 additions & 0 deletions build-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh -e
dir=$(pwd)

# LLVM
cmake \
--install-prefix "$dir/lib/llvm" \
-B "$dir/deps/llvm/build" \
-Wno-dev \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DLLVM_ENABLE_ZSTD:BOOL=OFF \
-DLLVM_APPEND_VC_REV:BOOL=OFF \
"$dir/deps/llvm/llvm"

cmake --build "$dir/deps/llvm/build" --config Release
cmake --install "$dir/deps/llvm/build" --config Release
1 change: 1 addition & 0 deletions deps/llvm
Submodule llvm added at 7cbf1a
2 changes: 2 additions & 0 deletions stage0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/Cargo.lock
/target/
7 changes: 7 additions & 0 deletions stage0/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "pluto"
version = "0.1.0"
edition = "2021"

[dependencies]
thiserror = "1.0"
18 changes: 18 additions & 0 deletions stage0/src/ast/attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::Expression;
use crate::lexer::{AttributeName, Span};

/// An attribute.
pub struct Attribute {
name: AttributeName,
args: Option<Vec<Vec<Expression>>>,
}

impl Attribute {
pub fn new(name: AttributeName, args: Option<Vec<Vec<Expression>>>) -> Self {
Self { name, args }
}

pub fn span(&self) -> &Span {
self.name.span()
}
}
23 changes: 23 additions & 0 deletions stage0/src/ast/class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::Attribute;
use crate::lexer::{ClassKeyword, Identifier, Span};

/// A class.
pub struct Class {
attrs: Vec<Attribute>,
def: ClassKeyword,
name: Identifier,
}

impl Class {
pub fn new(attrs: Vec<Attribute>, def: ClassKeyword, name: Identifier) -> Self {
Self { attrs, def, name }
}

pub fn span(&self) -> &Span {
self.def.span()
}

pub fn name(&self) -> &Identifier {
&self.name
}
}
78 changes: 78 additions & 0 deletions stage0/src/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use super::Statement;
use crate::lexer::{
AsmKeyword, Equals, ExclamationMark, Identifier, IfKeyword, NullKeyword, StringLiteral,
UnsignedLiteral,
};

/// An expression.
pub enum Expression {
Value(Identifier),
Call(Call),
Equal(Equals, Equals),
NotEqual(ExclamationMark, Equals),
Unsigned(UnsignedLiteral),
String(StringLiteral),
Null(NullKeyword),
Asm(Asm),
If(If),
}

/// A function call.
pub struct Call {
path: Vec<Identifier>,
name: Identifier,
args: Vec<Vec<Expression>>,
}

impl Call {
pub fn new(path: Vec<Identifier>, name: Identifier, args: Vec<Vec<Expression>>) -> Self {
Self { path, name, args }
}
}

/// An inline assembly (e.g. `asm("nop")`).
pub struct Asm {
def: AsmKeyword,
inst: StringLiteral,
inputs: Vec<(AsmIn, Vec<Expression>)>,
outputs: Vec<(AsmOut, Identifier)>,
}

impl Asm {
pub fn new(
def: AsmKeyword,
inst: StringLiteral,
inputs: Vec<(AsmIn, Vec<Expression>)>,
outputs: Vec<(AsmOut, Identifier)>,
) -> Self {
Self {
def,
inst,
inputs,
outputs,
}
}
}

/// An input of the inline assembly (e.g. `in("rax")`).
pub enum AsmIn {
Register(StringLiteral),
}

/// An output of the inline assembly (e.h. `out("rax")`).
pub enum AsmOut {
Never(ExclamationMark),
}

/// An if expression.
pub struct If {
def: IfKeyword,
cond: Vec<Expression>,
body: Vec<Statement>,
}

impl If {
pub fn new(def: IfKeyword, cond: Vec<Expression>, body: Vec<Statement>) -> Self {
Self { def, cond, body }
}
}
Loading

0 comments on commit 1716117

Please sign in to comment.