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: 5 months 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 (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-08T21:30:06.000Z (over 3 years ago)
- Last Synced: 2025-03-13T23:13:28.987Z (about 1 year 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
[](https://github.com/STREGAsGate/Gravity/actions/workflows/Windows.yml) [](https://github.com/STREGAsGate/Gravity/actions/workflows/macOS.yml) [](https://github.com/STREGAsGate/Gravity/actions/workflows/Linux.yml) [](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
[](https://twitter.com/stregasgate) [](https://youtube.com/stregasgate) [](https://www.reddit.com/r/stregasgate/) [](https://discord.gg/5JdRJhD)
Check out what I'm working on at various places or come say hi on discord!

