Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it work with Zig #425

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,7 @@ binding/GravityObjC/GravityObjC.xcodeproj/project.xcworkspace/xcshareddata/IDEWo
gravity.xcodeproj/xcuserdata/marco.xcuserdatad/xcschemes/gravity.xcscheme
*.xcscheme
*.xcscheme

## Zig
**/zig-cache
**/zig-out
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: c
os:
- osx
- linux
- windows
- osx
- linux
compiler:
- gcc
- clang
- zig
script:
- make
- test/unittest/run_all.sh
- zig build
71 changes: 44 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,57 +41,74 @@ class Vector {
func main() {
// initialize a new vector object
var v1 = Vector(1,2,3);

// initialize a new vector object
var v2 = Vector(4,5,6);

// call + function in the vector object
var v3 = v1 + v2;

// returns string "[1,2,3] + [4,5,6] = [5,7,9]"
return "\(v1) + \(v2) = \(v3)";
}
```
```

## Features
* multipass compiler
* dynamic typing
* classes and inheritance
* higher-order functions and classes
* lexical scoping
* coroutines (via fibers)
* nested classes
* closures
* garbage collection
* operator overriding
* powerful embedding API
* built-in unit tests
* built-in JSON serializer/deserializer
* **optional semicolons**

- multipass compiler
- dynamic typing
- classes and inheritance
- higher-order functions and classes
- lexical scoping
- coroutines (via fibers)
- nested classes
- closures
- garbage collection
- operator overriding
- powerful embedding API
- built-in unit tests
- built-in JSON serializer/deserializer
- **optional semicolons**

## Special thanks

Gravity was supported by a couple of open-source projects. The inspiration for closures comes from the elegant <a href="http://www.lua.org" target="_blank">Lua</a> programming language; specifically from the document <a href="http://www.cs.tufts.edu/~nr/cs257/archive/roberto-ierusalimschy/closures-draft.pdf">Closures in Lua</a>. For fibers, upvalues handling and some parts of the garbage collector, my gratitude goes to <a href="http://journal.stuffwithstuff.com" target="_blank">Bob Nystrom</a> and his excellent <a href="https://github.com/munificent/wren">Wren</a> programming language. A very special thanks should also go to my friend **Andrea Donetti** who helped me debugging and testing various aspects of the language.

## Documentation

The <a href="https://marcobambini.github.io/gravity/#/README">Getting Started</a> page is a guide for downloading and compiling the language. There is also a more extensive <a href="https://gravity-lang.org">language documentation</a>. Official [wiki](https://github.com/marcobambini/gravity/wiki) is used to collect related projects and tools.

## Where Gravity is used
* Gravity is the core language built into Creo (https://creolabs.com)
* Gravity is the scripting language for the Untold game engine (https://youtu.be/OGrWq8jpK14?t=58)

- Gravity is the core language built into Creo (https://creolabs.com)
- Gravity is the scripting language for the Untold game engine (https://youtu.be/OGrWq8jpK14?t=58)

## Community

Seems like a good idea to make a group chat for people to discuss Gravity.<br> [![Join the chat at https://gitter.im/gravity-lang/](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gravity-lang/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

## Contributing

Contributions to Gravity are welcomed and encouraged!<br>
More information is available in the official [CONTRIBUTING](CONTRIBUTING.md) file.
* <a href="https://github.com/marcobambini/gravity/issues/new">Open an issue</a>:
* if you need help
* if you find a bug
* if you have a feature request
* to ask a general question
* <a href="https://github.com/marcobambini/gravity/pulls">Submit a pull request</a>:
* if you want to contribute

- <a href="https://github.com/marcobambini/gravity/issues/new">Open an issue</a>:
- if you need help
- if you find a bug
- if you have a feature request
- to ask a general question
- <a href="https://github.com/marcobambini/gravity/pulls">Submit a pull request</a>:
- if you want to contribute

## License

Gravity is available under the permissive MIT license.

## Let's get ziggy

I made a thin wrapper for gravity in zig, you can fetch it like any other zig packages,
you can pass an optional parameter to the build options: "shared"

- If set to true, the "gravity" artifact will be dynamically linked
- If set to false, it will be statically linked
- If not set, both of them will be created: "gravity" and "gravity_s", "gravity" is dynamic, "gravity_s" is static
36 changes: 36 additions & 0 deletions bootstrap.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const std = @import("std");
const builtin = @import("builtin");

extern fn c_main(argc: c_int, argv: [*c]const [*c]const u8) c_int;

const DEBUG = builtin.mode == .Debug;
const BACK_ALLOCATOR = std.heap.c_allocator;

pub fn main() u8 {
var gpa = std.heap.GeneralPurposeAllocator(.{}){ .backing_allocator = BACK_ALLOCATOR };
defer if (DEBUG) {
_ = gpa.deinit();
};
const allocator = if (DEBUG) gpa.allocator() else BACK_ALLOCATOR;

var args_list = std.ArrayList([*c]const u8).init(allocator);
defer {
for (args_list.items) |arg| {
allocator.free(std.mem.span(arg));
}
args_list.deinit();
}

var args_it = std.process.argsWithAllocator(allocator) catch return 1;
defer args_it.deinit();

while (args_it.next()) |arg| {
args_list.append(@ptrCast(allocator.dupeZ(u8, arg) catch return 1)) catch return 1;
}

args_list.ensureTotalCapacity(args_list.items.len + 1) catch return 1;
args_list.append(null) catch unreachable;

const argv: [*c]const [*c]const u8 = @ptrCast(args_list.allocatedSlice());
return @intCast(c_main(@intCast(args_list.items.len), argv) & 0xFF);
}
131 changes: 131 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const std = @import("std");

const GRAVITY_SRC_PATH: []const u8 = "src/";

const COMPILER_DIR: []const u8 = GRAVITY_SRC_PATH ++ "compiler/";
const RUNTIME_DIR: []const u8 = GRAVITY_SRC_PATH ++ "runtime/";
const SHARED_DIR: []const u8 = GRAVITY_SRC_PATH ++ "shared/";
const UTILS_DIR: []const u8 = GRAVITY_SRC_PATH ++ "utils/";
const OPT_DIR: []const u8 = GRAVITY_SRC_PATH ++ "optionals/";
const SRC: []const []const u8 = &.{
// COMPILER_DIR/*.c
COMPILER_DIR ++ "gravity_ast.c",
COMPILER_DIR ++ "gravity_codegen.c",
COMPILER_DIR ++ "gravity_compiler.c",
COMPILER_DIR ++ "gravity_ircode.c",
COMPILER_DIR ++ "gravity_lexer.c",
COMPILER_DIR ++ "gravity_optimizer.c",
COMPILER_DIR ++ "gravity_optimizer.c",
COMPILER_DIR ++ "gravity_parser.c",
COMPILER_DIR ++ "gravity_semacheck1.c",
COMPILER_DIR ++ "gravity_semacheck2.c",
COMPILER_DIR ++ "gravity_symboltable.c",
COMPILER_DIR ++ "gravity_token.c",
COMPILER_DIR ++ "gravity_visitor.c",
// RUNTIME_DIR/*.c
RUNTIME_DIR ++ "gravity_core.c",
RUNTIME_DIR ++ "gravity_vm.c",
// SHARED_DIR/*.c
SHARED_DIR ++ "gravity_hash.c",
SHARED_DIR ++ "gravity_memory.c",
SHARED_DIR ++ "gravity_value.c",
// UTILS_DIR/*.c
UTILS_DIR ++ "gravity_debug.c",
UTILS_DIR ++ "gravity_json.c",
UTILS_DIR ++ "gravity_utils.c",
// OPT_DIR/*.c
OPT_DIR ++ "gravity_opt_env.c",
OPT_DIR ++ "gravity_opt_file.c",
OPT_DIR ++ "gravity_opt_json.c",
OPT_DIR ++ "gravity_opt_math.c",
};
const INCLUDE: []const []const u8 = &.{
"-I", COMPILER_DIR,
"-I", RUNTIME_DIR,
"-I", SHARED_DIR,
"-I", UTILS_DIR,
"-I", OPT_DIR,
};
const CFLAGS: []const []const u8 = @as([]const []const u8, &.{
"-std=gnu99",
"-fgnu89-inline",
"-fPIC",
"-fno-sanitize=undefined",
});

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const lib_s = b.addStaticLibrary(.{
.name = "gravity_s",
.target = target,
.optimize = optimize,
.link_libc = true,
});
inline for (INCLUDE) |I| {
if (comptime std.mem.eql(u8, I, "-I"))
continue;
lib_s.addIncludePath(b.path(I));
lib_s.installHeadersDirectory(b.path(I), ".", .{});
}
lib_s.linkSystemLibrary("m");
switch (target.result.os.tag) {
.windows => lib_s.linkSystemLibrary("Shlwapi"),
.openbsd, .freebsd, .netbsd, .dragonfly => {},
else => if (!target.result.isDarwin()) lib_s.linkSystemLibrary("rt"),
}
lib_s.addCSourceFiles(.{
.root = b.path("."),
.files = SRC,
.flags = INCLUDE ++ CFLAGS ++ @as([]const []const u8, &.{"-DBUILD_GRAVITY_API"}),
});

const lib = b.addSharedLibrary(.{
.name = "gravity",
.target = target,
.optimize = optimize,
.link_libc = true,
});
inline for (INCLUDE) |I| {
if (comptime std.mem.eql(u8, I, "-I"))
continue;
lib.addIncludePath(b.path(I));
lib.installHeadersDirectory(b.path(I), ".", .{});
}
lib.linkSystemLibrary("m");
switch (target.result.os.tag) {
.windows => lib.linkSystemLibrary("Shlwapi"),
.openbsd, .freebsd, .netbsd, .dragonfly => {},
else => if (!target.result.isDarwin()) lib.linkSystemLibrary("rt"),
}
lib.addCSourceFiles(.{
.root = b.path("."),
.files = SRC,
.flags = INCLUDE ++ CFLAGS ++ @as([]const []const u8, &.{"-DBUILD_GRAVITY_API"}),
});

b.installArtifact(lib);
b.installArtifact(lib_s);

const exe = b.addExecutable(.{
.name = "gravity",
.root_source_file = b.path("bootstrap.zig"),
.target = target,
.optimize = optimize,
});
exe.linkLibrary(lib_s);
exe.addCSourceFiles(.{
.root = b.path("."),
.files = &.{"src/cli/gravity.c"},
.flags = CFLAGS ++ @as([]const []const u8, &.{"-DZIG_BOOTSTRAP"}),
});
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
run_cmd.addArgs(b.args orelse &.{});

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
12 changes: 12 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.{
.name = "gravity",
.version = "0.8.5-build+1eb6d5ab98b9df4909513612a2bb9238a8304422",

.dependencies = .{},

.paths = .{
"src/",
"build.zig",
"build.zig.zon",
},
}
Loading