Skip to content

v0.0.2(#64) #70

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/SyntaxKit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ jobs:
restore-keys: |
${{ runner.os }}-mint-
- name: Install mint
if: steps.cache-mint.outputs.cache-hit != 'true'
if: steps.cache-mint.outputs.cache-hit == ''
run: |
git clone https://github.com/yonaskolb/Mint.git
cd Mint
Expand Down
6 changes: 6 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ identifier_name:
excluded:
- id
- no
type_name:
excluded:
- If
- Do
excluded:
- DerivedData
- .build
- Mint
- Examples
- Macros
indentation_width:
indentation_width: 2
file_name:
Expand All @@ -129,3 +134,4 @@ disabled_rules:
- closure_parameter_position
- trailing_comma
- opening_brace
- optional_data_string_conversion
14 changes: 14 additions & 0 deletions Examples/Completed/attributes/code.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@objc
class Foo {
@Published var bar: String = "bar"

@available(iOS 17.0, *)
func bar() {
print("bar")
}

@MainActor
func baz() {
print("baz")
}
}
7 changes: 7 additions & 0 deletions Examples/Completed/attributes/dsl.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Class("Foo") {
Variable(.var, name: "bar", type: "String", defaultValue: "bar").attribute("Published")
Function("bar") {
print("bar")
}.attribute("available", arguments: ["iOS 17.0", "*"])
Function("baz") {
}.attribute("objc")}.attribute("objc")
Empty file.
42 changes: 42 additions & 0 deletions Examples/Completed/concurrency/code.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
enum VendingMachineError: Error {
case invalidSelection
case insufficientFunds(coinsNeeded: Int)
case outOfStock
}

class VendingMachine {
var inventory = [
"Candy Bar": Item(price: 12, count: 7),
"Chips": Item(price: 10, count: 4),
"Pretzels": Item(price: 7, count: 11)
]
var coinsDeposited = 0


func vend(itemNamed name: String) throws {
guard let item = inventory[name] else {
throw VendingMachineError.invalidSelection
}


guard item.count > 0 else {
throw VendingMachineError.outOfStock
}


guard item.price <= coinsDeposited else {
throw VendingMachineError.insufficientFunds(coinsNeeded: item.price - coinsDeposited)
}


coinsDeposited -= item.price


var newItem = item
newItem.count -= 1
inventory[name] = newItem


print("Dispensing \(name)")
}
}
48 changes: 48 additions & 0 deletions Examples/Completed/concurrency/dsl.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Enum("VendingMachineError") {
Case("invalidSelection")
Case("insufficientFunds").associatedValue("coinsNeeded", type: "Int")
Case("outOfStock")
}

Class("VendingMachine") {
Variable(.var, name: "inventory", equals: Literal.dictionary(Dictionary(uniqueKeysWithValues: [
("Candy Bar", Item(price: 12, count: 7)),
("Chips", Item(price: 10, count: 4)),
("Pretzels", Item(price: 7, count: 11))
])))
Variable(.var, name: "coinsDeposited", equals: 0)

Function("vend"){
Parameter("name", labeled: "itemNamed", type: "String")
} _: {
Guard("let item = inventory[itemNamed]") else: {
Throw(
EnumValue("VendingMachineError", case: "invalidSelection")
)
}
Guard("item.count > 0") else: {
Throw(
EnumValue("VendingMachineError", case: "outOfStock")
)
}
Guard("item.price <= coinsDeposited") else: {
Throw(
EnumValue("VendingMachineError", case: "insufficientFunds"){
ParameterExp("coinsNeeded", value: Infix("-"){
VariableExp("item").property("price")
VariableExp("coinsDeposited")
})
}
)
}
Infix("-=", "coinsDeposited", VariableExp("item").property("price"))
Variable("newItem", equals: VariableExp("item"))
Infix("-=", "newItem.count", 1)
Assignment("inventory[itemNamed]", .ref("newItem"))
Call("print", "Dispensing \\(itemNamed)")
}
}




1 change: 1 addition & 0 deletions Examples/Completed/concurrency/syntax.json

Large diffs are not rendered by default.

154 changes: 154 additions & 0 deletions Examples/Completed/conditionals/code.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Simple if statement
let temperature = 25
if temperature > 30 {
print("It's hot outside!")
}

// If-else statement
let score = 85
if score >= 90 {
print("Excellent!")
} else if score >= 80 {
print("Good job!")
} else if score >= 70 {
print("Passing")
} else {
print("Needs improvement")
}

// MARK: - Optional Binding with If

// Using if let for optional binding
let possibleNumber = "123"
if let actualNumber = Int(possibleNumber) {
print("The string \"\(possibleNumber)\" has an integer value of \(actualNumber)")
} else {
print("The string \"\(possibleNumber)\" could not be converted to an integer")
}

// Multiple optional bindings
let possibleName: String? = "John"
let possibleAge: Int? = 30
if let name = possibleName, let age = possibleAge {
print("\(name) is \(age) years old")
}

// MARK: - Guard Statements
func greet(person: [String: String]) {
guard let name = person["name"] else {
print("No name provided")
return
}

guard let age = person["age"], let ageInt = Int(age) else {
print("Invalid age provided")
return
}

print("Hello \(name), you are \(ageInt) years old")
}

// MARK: - Switch Statements
// Switch with range matching
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
let naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")

// Switch with tuple matching
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("(0, 0) is at the origin")
case (_, 0):
print("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
print("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2):
print("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
print("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}

// Switch with value binding
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with a y value of \(y)")
case let (x, y):
print("somewhere else at (\(x), \(y))")
}

// MARK: - Fallthrough
// Using fallthrough in switch
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough
default:
description += " an integer."
}
print(description)

// MARK: - Labeled Statements
// Using labeled statements with break
let finalSquare = 25
var board = [Int](repeating: 0, count: finalSquare + 1)
board[03] = 8
board[06] = 11
board[09] = 9
board[10] = 2
board[14] = -10
board[19] = -11
board[22] = -2
board[24] = -8

var square = 0
var diceRoll = 0
while square != finalSquare {
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
break
case let newSquare where newSquare > finalSquare:
continue
default:
square += diceRoll
square += board[square]
}
}

// MARK: - For Loops
// For-in loop with enumerated() to get index and value
print("\n=== For-in with Enumerated ===")
for (index, name) in names.enumerated() {
print("\(index): \(name)")
}

// For-in loop with where clause
print("\n=== For-in with Where Clause ===")
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers where number % 2 == 0 {
print("Even number: \(number)")
}


Loading
Loading