Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stregasgate/gravity
Gravity for Swift
https://github.com/stregasgate/gravity
gravity-language swift-language swift-linux swift-mac swift-package swift-windows
Last synced: about 22 hours ago
JSON representation
Gravity for Swift
- Host: GitHub
- URL: https://github.com/stregasgate/gravity
- Owner: STREGAsGate
- License: mit
- Created: 2022-12-14T19:59:51.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-08T21:30:06.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T16:40:44.196Z (about 1 month ago)
- Topics: gravity-language, swift-language, swift-linux, swift-mac, swift-package, swift-windows
- Language: Swift
- Homepage:
- Size: 834 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
⚠️ Under Heavy Development! Expect sweeping changes followed by a squash.
# Gravity for Swift
[![Windows](https://github.com/STREGAsGate/Gravity/actions/workflows/Windows.yml/badge.svg)](https://github.com/STREGAsGate/Gravity/actions/workflows/Windows.yml) [![macOS](https://github.com/STREGAsGate/Gravity/actions/workflows/macOS.yml/badge.svg)](https://github.com/STREGAsGate/Gravity/actions/workflows/macOS.yml) [![Linux](https://github.com/STREGAsGate/Gravity/actions/workflows/Linux.yml/badge.svg)](https://github.com/STREGAsGate/Gravity/actions/workflows/Linux.yml) [![WebAssembly](https://github.com/STREGAsGate/Gravity/actions/workflows/SwiftWasm.yml/badge.svg)](https://github.com/STREGAsGate/Gravity/actions/workflows/SwiftWasm.yml)
# What is Gravity for Swift?
Gravity is a powerful, dynamically typed, lightweight, embeddable programming language. It is a class-based concurrent scripting language with a modern Swift like syntax.Gravity for Swift is a Swift Package that allows you to use the Gravity language with your Swift projects.
# Getting Started
This README does not cover the Gravity language.
Before jumping in you should familiarize yourself with [Gravity's documentation](http://gravity-lang.org).## Adding Gravity for Swift to your Project
You can add Gravity for Swift to a package:
```swift
let package = Package(
name: "MyThing",
dependencies: [
// Add Gravity as a package dependency so it's available to your target
.package(url: "https://github.com/STREGAsGate/Gravity.git", branch: "master"),
],
targets: [
// Add Gravity to your target dependencies
// This will make Gravity available to `import Gravity`
.executableTarget(name: "MyThing", dependencies: ["Gravity"]),
]
)
```
If you are using an ***Xcode Project*** you can add gravity by selecting your project in the navigator, your project at the top of the targets list, and finally the ***Package Dependencies*** tab.## Running a Gravity script
```swift
import Gravity// The Gravity object handles everything.
let gravity = Gravity()// Compile script from a URL
let bundleURL = Bundle.module.resourceURL!
let scriptURL = bundleURL.appendingPathComponent("File.gravity")
try gravity.compile(scriptURL)
// Execute the script's func main()
try gravity.runMain()
```A Gravity script can also be compiled from a string.
```swift
try gravity.compile("func main() {}")
```Some actions must be done in a specific order.
For example, you cannot call `runMain()` before calling `compile(script)`.# Obtaining Values
You can retrieve a value from the script in various ways.### Global Variables
A global variable is any variable in the root of a script.
```swift
/* --- Gravity Script --- */
var myVar = 10 // <- This is a global variable
func main() {}
```
You can obtain the value of a global variable using `gravity.getVar("myVar")`.
```swift
// The GravityValue type is the universal return type for Gravity
let myVarGravity: GravityValue = gravity.getVar("myVar")
// Make sure it's an Int
assert(myVarGravity.valueType == .int)
// Ask for the Int
let myVar: Int = myVarGravity.getInt()// The Int can also be obtained directly by declarting myVar as an Int
let myVar: Int = gravity.getVar("myVar")
```### Return Values
All Closures in Gravity return a value, but you can ignore the value if you know it's empty
```swift
// Ignoring the return value
try gravity.runMain()
// Storing the return value
let result = try gravity.runMain()
```# C99 Access
You can access the unmodifed c99 Gravity source directly via the Swift module GravityC.
```swift
import GravityC
```
Note: Using the GravityC module requires significant knowledge of Swift's Unsafe API and is not recommended.# Strega's Gate
[![Twitter](https://img.shields.io/twitter/follow/stregasgate?style=social)](https://twitter.com/stregasgate) [![YouTube](https://img.shields.io/youtube/channel/subscribers/UCBXFkK2B4w9856wBJfCGufg?label=Subscribe&style=social)](https://youtube.com/stregasgate) [![Reddit](https://img.shields.io/reddit/subreddit-subscribers/stregasgate?style=social)](https://www.reddit.com/r/stregasgate/) [![Discord](https://img.shields.io/discord/641809158051725322?label=Hang%20Out&logo=Discord&style=social)](https://discord.gg/5JdRJhD)Check out what I'm working on at various places or come say hi on discord!