Skip to content

Commit 0c32317

Browse files
author
Brandon Sneed
committed
moar stuff
1 parent f0208cb commit 0c32317

13 files changed

Lines changed: 432 additions & 35 deletions

File tree

.swiftpm/xcode/xcshareddata/xcschemes/segmentcli.xcscheme

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@
9999
</CommandLineArgument>
100100
<CommandLineArgument
101101
argument = "repl"
102+
isEnabled = "NO">
103+
</CommandLineArgument>
104+
<CommandLineArgument
105+
argument = "repl -r /Users/brandonsneed/test.js"
102106
isEnabled = "YES">
103107
</CommandLineArgument>
104108
<CommandLineArgument

Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ let package = Package(
1515
.package(url: "https://github.com/segmentio/analytics-swift.git", branch: "bsneed/cli_additions"),
1616
.package(url: "https://github.com/swiftcsv/SwiftCSV.git", from: "0.6.1"),
1717
.package(url: "https://github.com/AlwaysRightInstitute/Mustache", from: "1.0.0"),
18-
.package(url: "https://github.com/antitypical/Result.git", from: "5.0.0")
18+
.package(url: "https://github.com/antitypical/Result.git", from: "5.0.0"),
19+
.package(url: "https://github.com/bsneed/SwiftJS.git", branch: "main")
1920
],
2021
targets: [
2122
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -27,6 +28,7 @@ let package = Package(
2728
"ColorizeSwift",
2829
"SwiftCSV",
2930
"Result",
31+
"SwiftJS",
3032
.product(name: "mustache", package: "Mustache"),
3133
.product(name: "Segment", package: "analytics-swift")]),
3234
.testTarget(

Sources/segmentcli/Commands/Import.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77

88
import Foundation
99
import SwiftCLI
10+
import JavaScriptCore
11+
import mustache
1012

1113
class ImportCommand: Command {
1214
let name = "import"
1315
let shortDescription = "Import CSV data into Segment from"
1416

15-
@Param var csvFile: String
16-
@Param var configFile: String
1717
@Param var writeKey: String
18+
@Param var csvFile: String
1819

19-
/*
20-
@Key("-csv", "--csv", description: "CSV file to read from")
21-
var csvFile: String?
20+
let jsContext = JSContext()!
2221

23-
@Key("-csvConfig", "--csvConfig", description: "A config file describing how to interpret CSV columns to event data")
24-
var configFile: String?
25-
*/
2622
func execute() throws {
23+
let generate = Mustache(importer_js)
24+
let result = generate(name: "", writeKey: writeKey, csvFile: csvFile)
25+
26+
runJS(script: result, context: jsContext)
2727
}
2828
}

Sources/segmentcli/Commands/REPL.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,29 @@
88
import Foundation
99
import SwiftCLI
1010
import Segment
11+
//import SwiftJS
12+
import JavaScriptCore
1113

14+
class REPLCommand: Command {
15+
let name: String = "repl"
16+
let shortDescription = "Segment virtual development environment"
17+
18+
@Key("-r", "--runscript", "Runs the supplied script in the REPL")
19+
var scriptFile: String?
20+
21+
let jsContext = JSContext()!
22+
23+
func execute() throws {
24+
if let scriptFile = scriptFile {
25+
runJSFile(path: scriptFile, context: jsContext)
26+
} else {
27+
runJSInteractive(context: jsContext)
28+
}
29+
}
30+
31+
}
32+
33+
/*
1234
class REPLCommand: Command {
1335
let name: String = "repl"
1436
let shortDescription = "Segment virtual development environment"
@@ -38,6 +60,7 @@ class REPLCommand: Command {
3860
}
3961

4062
}
63+
*/
4164

4265
/*
4366
guard let writeKey = args[0] as? String else { return NilAny }

Sources/segmentcli/Commands/Scaffold.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class ScaffoldCommand: Command {
6363

6464
if plugin && useSwift {
6565
generateSwiftPlugin()
66+
} else if importer {
67+
generateCSVImporterScript()
6668
}
6769
}
6870

@@ -95,4 +97,35 @@ class ScaffoldCommand: Command {
9597

9698
print("\n")
9799
}
100+
101+
func generateCSVImporterScript() {
102+
guard let scaffoldName = scaffoldName else {
103+
exitWithError("Could not determine a plugin name to use.")
104+
return
105+
}
106+
107+
let filename = scaffoldName + ".js"
108+
109+
print("Generating a Swift Plugin from template...")
110+
111+
for file in importer_templates_js {
112+
if fileManager.fileExists(atPath: filename) {
113+
let overwrite = Input.readBool(prompt: "\(filename) exists. Overwrite? [y/N]: ", defaultValue: false)
114+
if overwrite == false {
115+
exitWithError(code: .commandFailed)
116+
}
117+
}
118+
let generate = Mustache(file)
119+
let result = generate(name: scaffoldName, filename: filename)
120+
do {
121+
try result.write(toFile: filename, atomically: true, encoding: .utf8)
122+
print("Created \(filename).")
123+
} catch {
124+
exitWithError("Unable to write \(filename)")
125+
}
126+
}
127+
128+
print("\n")
129+
130+
}
98131
}

Sources/segmentcli/LoxCore/Lox.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,22 @@ public final class Lox {
4343
}
4444

4545
public static func runFile(path: String) throws {
46-
let url = URL(fileURLWithPath: path)
47-
let code = try String(contentsOf: url)
48-
run(code)
46+
//let runQueue = DispatchQueue(label: "segmentcli.run")
4947

50-
if hadError {
51-
exit(65)
52-
}
53-
if hadRuntimeError {
54-
exit(70)
48+
//runQueue.async {
49+
let url = URL(fileURLWithPath: path)
50+
let code = try String(contentsOf: url)
51+
run(code)
52+
53+
if hadError {
54+
exit(65)
55+
}
56+
if hadRuntimeError {
57+
exit(70)
58+
}
59+
//}
60+
while true {
61+
RunLoop.main.run(until: Date.distantPast)
5562
}
5663
}
5764

Sources/segmentcli/LoxCore/Parser.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,17 @@ final class Parser {
308308
let (parameters, body) = try functionBody(kind: kind)
309309
return Stmt.Function(name: name, parameters: parameters, body: body)
310310
}
311-
311+
/*
312+
private func array() throws -> Expr {
313+
var items: Array<Token> = []
314+
if !check(.rightBracket) {
315+
repeat {
316+
items.append(try consume(., message: "Expect parameter name."))
317+
} while match(.comma)
318+
}
319+
try consume(.rightParen, message: "Expect ')' after parameters.")
320+
}
321+
*/
312322
private func block() throws -> Array<Stmt> {
313323
var statements = Array<Stmt>()
314324

@@ -344,6 +354,10 @@ final class Parser {
344354
let right = try unary()
345355
return Expr.Unary(op: op, right: right)
346356
}
357+
358+
/*if match(.number, .string) {
359+
return try function(kind: "array")
360+
}*/
347361

348362
return try call()
349363
}
@@ -417,6 +431,12 @@ final class Parser {
417431
try consume(.rightParen, message: "Expect ')' after expression.")
418432
return Expr.Grouping(expression: expr)
419433
}
434+
435+
if match(.leftBracket) {
436+
let expr = try expression()
437+
try consume(.rightBracket, message: "Expect ']' after expression.")
438+
return Expr.Grouping(expression: expr)
439+
}
420440

421441
throw error(token: peek(), message: "Expect expression.")
422442
}

Sources/segmentcli/LoxCore/Runtime/Interpreter.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class Interpreter: ExprVisitor, StmtVisitor {
3434

3535
internal let globals = Environment()
3636
private var locals: Dictionary<Expr, Int> = [:]
37-
private var environment: Environment
37+
internal var environment: Environment
3838

3939
init() {
4040
environment = globals
@@ -191,10 +191,12 @@ final class Interpreter: ExprVisitor, StmtVisitor {
191191

192192
case .leftParen, .rightParen: fallthrough
193193
case .leftBrace, .rightBrace: fallthrough
194+
case .leftBracket, .rightBracket: fallthrough
194195
case .comma: fallthrough
195196
case .dot: fallthrough
196197
case .plus: fallthrough
197198
case .semicolon: fallthrough
199+
case .colon: fallthrough
198200
case .slash: fallthrough
199201
case .star: fallthrough
200202
case .bangEqual: fallthrough
@@ -205,6 +207,8 @@ final class Interpreter: ExprVisitor, StmtVisitor {
205207
case .identifier: fallthrough
206208
case .string: fallthrough
207209
case .number: fallthrough
210+
case .array: fallthrough
211+
case .dictionary: fallthrough
208212
case .and: fallthrough
209213
case .Class: fallthrough
210214
case .Else: fallthrough
@@ -307,14 +311,18 @@ final class Interpreter: ExprVisitor, StmtVisitor {
307311

308312
case .leftParen, .rightParen: fallthrough
309313
case .leftBrace, .rightBrace: fallthrough
314+
case .leftBracket, .rightBracket: fallthrough
310315
case .comma: fallthrough
311316
case .dot: fallthrough
312317
case .semicolon: fallthrough
318+
case .colon: fallthrough
313319
case .bang: fallthrough
314320
case .equal: fallthrough
315321
case .identifier: fallthrough
316322
case .string: fallthrough
317323
case .number: fallthrough
324+
case .array: fallthrough
325+
case .dictionary: fallthrough
318326
case .and: fallthrough
319327
case .Class: fallthrough
320328
case .Else: fallthrough

Sources/segmentcli/LoxCore/Scanner.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ final class Scanner {
5151
addToken(type: .leftBrace)
5252
case "}":
5353
addToken(type: .rightBrace)
54+
case "[":
55+
addToken(type: .leftBracket)
56+
case "]":
57+
addToken(type: .rightBracket)
5458
case ",":
5559
addToken(type: .comma)
5660
case ".":
@@ -61,6 +65,8 @@ final class Scanner {
6165
addToken(type: .plus)
6266
case ";":
6367
addToken(type: .semicolon)
68+
case ":":
69+
addToken(type: .colon)
6470
case "*":
6571
addToken(type: .star)
6672

0 commit comments

Comments
 (0)