From 35626e55a51169a07befc3f9555fbd0022e2053b Mon Sep 17 00:00:00 2001 From: x86y Date: Tue, 17 Oct 2023 00:28:36 +0400 Subject: [PATCH] chore: slight cleanup --- Beacon.xcodeproj/project.pbxproj | 6 +++ Beacon/Utilities/Tokenizer.swift | 8 ++-- Beacon/Views/ContentView.swift | 79 -------------------------------- Beacon/Views/HistoryView.swift | 74 ++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 83 deletions(-) create mode 100644 Beacon/Views/HistoryView.swift diff --git a/Beacon.xcodeproj/project.pbxproj b/Beacon.xcodeproj/project.pbxproj index a3ab6b5..5935e69 100644 --- a/Beacon.xcodeproj/project.pbxproj +++ b/Beacon.xcodeproj/project.pbxproj @@ -38,6 +38,8 @@ F379CEC72ADDB24D00164F76 /* LexBQN.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC52ADDB24D00164F76 /* LexBQN.swift */; }; F379CEC92ADDD2A700164F76 /* Tokenizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC82ADDD2A700164F76 /* Tokenizer.swift */; }; F379CECA2ADDD2A700164F76 /* Tokenizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC82ADDD2A700164F76 /* Tokenizer.swift */; }; + F379CECC2ADDD42C00164F76 /* HistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CECB2ADDD42C00164F76 /* HistoryView.swift */; }; + F379CECD2ADDD42C00164F76 /* HistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CECB2ADDD42C00164F76 /* HistoryView.swift */; }; F3877C8E29952A6600E2FCB5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F3877C8D29952A6600E2FCB5 /* Assets.xcassets */; }; F38F03A92A323F8500F66354 /* libksim.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F38F03A82A323F8500F66354 /* libksim.a */; }; F3AC4D262A4F6C3100B4FECD /* curl.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = F3AC4D252A4F6C3000B4FECD /* curl.xcframework */; }; @@ -113,6 +115,7 @@ F379CEC22ADD94BC00164F76 /* LexK.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LexK.swift; sourceTree = ""; }; F379CEC52ADDB24D00164F76 /* LexBQN.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LexBQN.swift; sourceTree = ""; }; F379CEC82ADDD2A700164F76 /* Tokenizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tokenizer.swift; sourceTree = ""; }; + F379CECB2ADDD42C00164F76 /* HistoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryView.swift; sourceTree = ""; }; F3877C8B29952A5400E2FCB5 /* ContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; F3877C8D29952A6600E2FCB5 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; F38F03A82A323F8500F66354 /* libksim.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libksim.a; path = Beacon/libs/libksim.a; sourceTree = ""; }; @@ -221,6 +224,7 @@ F30A7B892A370B7800503966 /* BuffersView.swift */, F3E1DFF32A31F9D800B4A553 /* ConfigView.swift */, F3DF82892A4EF33A00F6CD19 /* DashboardView.swift */, + F379CECB2ADDD42C00164F76 /* HistoryView.swift */, ); path = Views; sourceTree = ""; @@ -459,6 +463,7 @@ F32D9E8429966FCD007BC97C /* Beacon-Bridging-Header.h in Sources */, F379CEC72ADDB24D00164F76 /* LexBQN.swift in Sources */, F379CEC42ADD94BC00164F76 /* LexK.swift in Sources */, + F379CECD2ADDD42C00164F76 /* HistoryView.swift in Sources */, F30A7B8B2A370B7800503966 /* BuffersView.swift in Sources */, F31BCE7F2A348B7500A6116D /* HelpView.swift in Sources */, F32D9E8529966FCD007BC97C /* ArraygroundApp.swift in Sources */, @@ -479,6 +484,7 @@ F312CF8829951C89008EC197 /* Beacon-Bridging-Header.h in Sources */, F379CEC62ADDB24D00164F76 /* LexBQN.swift in Sources */, F379CEC32ADD94BC00164F76 /* LexK.swift in Sources */, + F379CECC2ADDD42C00164F76 /* HistoryView.swift in Sources */, F30A7B8A2A370B7800503966 /* BuffersView.swift in Sources */, F31BCE7E2A348B7500A6116D /* HelpView.swift in Sources */, F3B198CE299517DA00FE664F /* ArraygroundApp.swift in Sources */, diff --git a/Beacon/Utilities/Tokenizer.swift b/Beacon/Utilities/Tokenizer.swift index 3bdb22d..276ebb9 100644 --- a/Beacon/Utilities/Tokenizer.swift +++ b/Beacon/Utilities/Tokenizer.swift @@ -53,14 +53,14 @@ struct Token: Identifiable { let type: TokenType } -func tokenize(_ str: String, _ parseKOutput: [String]) -> [[Token]] { +func tokenize(_ str: String, _ lexed: [String]) -> [[Token]] { var tokens: [Token] = [] var i = 0 - while i < parseKOutput.count { - if let type = TokenType(rawValue: parseKOutput[i]) { + while i < lexed.count { + if let type = TokenType(rawValue: lexed[i]) { var j = i + 1 - while j < parseKOutput.count && parseKOutput[j] == "" { + while j < lexed.count && lexed[j] == "" { j += 1 } let value = str[str.index(str.startIndex, offsetBy: i) ..< str.index(str.startIndex, offsetBy: j)] diff --git a/Beacon/Views/ContentView.swift b/Beacon/Views/ContentView.swift index 6dedf0e..052ee43 100644 --- a/Beacon/Views/ContentView.swift +++ b/Beacon/Views/ContentView.swift @@ -10,73 +10,6 @@ import MobileCoreServices import SwiftUI import UIKit -struct HistoryView: View { - var index: Int - var historyItem: Entry - var curBuffer: String - var onMySubmit: (String) -> Void - @Binding var input: String - @Binding var ephemerals: [Int: [String]] - @Binding var editType: Behavior - @ObservedObject var viewModel: HistoryModel - - var body: some View { - VStack(alignment: .leading) { - if editType == Behavior.inlineEdit { - TextField("Source", text: Binding( - get: { - if index < viewModel.history[curBuffer, default: []].count { - return self.viewModel.history[curBuffer, default: []][index].src - } else { - return "" - } - }, - set: { - if index < viewModel.history[curBuffer, default: []].count { - self.viewModel.history[curBuffer, default: []][index].src = $0 - ephemerals[index, default: []].append($0) - } - } - )) - .onSubmit { - onMySubmit(self.viewModel.history[curBuffer, default: []][index].src) - for k in ephemerals.keys { - self.viewModel.history[curBuffer, default: []][k].src = ephemerals[k, default: []].first! - } - ephemerals = [:] // reset all virtual textfield edits - } - .font(Font.custom("BQN386 Unicode", size: 18)) - .foregroundColor(.blue) - } else if editType == Behavior.duplicate { - VStack(spacing: 1) { - let tokens = historyItem.lang == .k - ? tokenize(historyItem.src, parseK(historyItem.src)) - : tokenize(historyItem.src, parseBQN(code: historyItem.src)) - ForEach(Array(tokens.enumerated()), id: \.offset) { _, line in - HStack(spacing: 0) { - ForEach(line, id: \.id) { token in - Text(token.value) - .foregroundColor(tokenToColor(token.type)) - .font(Font.custom("BQN386 Unicode", size: 18)) - .onTapGesture { - self.input = historyItem.src - } - } - }.frame(maxWidth: .infinity, alignment: .leading) - } - } - } - Text("\(trimLongText(historyItem.out))") - .font(Font.custom("BQN386 Unicode", size: 18)) - .foregroundColor(historyItem.out.starts(with: "Error:") || historyItem.out.starts(with: "\"Error:") ? .red : .primary) - .multilineTextAlignment(.leading) - .onTapGesture { - self.input = historyItem.out - } - }.frame(maxWidth: .infinity, alignment: .leading) - } -} - struct ContentView: View { @State var input: String = "" @State var ephemerals: [Int: [String]] = [:] @@ -204,17 +137,5 @@ struct ContentView: View { viewModel.load(Buffers.get()) kinit() repl_init() - /* for filename in modules { - print(e(input: "•Import \"\(Bundle.main.resourcePath!)/bqn-libs/\(filename)\"")) - } */ - // isFocused = true } } - -/* - struct ContentView_Preview: PreviewProvider { - static var previews: some View { - ContentView() - } - } - */ diff --git a/Beacon/Views/HistoryView.swift b/Beacon/Views/HistoryView.swift new file mode 100644 index 0000000..6139ab9 --- /dev/null +++ b/Beacon/Views/HistoryView.swift @@ -0,0 +1,74 @@ +// +// HistoryView.swift +// Arrayground +// + +import Foundation +import SwiftUI + +struct HistoryView: View { + var index: Int + var historyItem: Entry + var curBuffer: String + var onMySubmit: (String) -> Void + @Binding var input: String + @Binding var ephemerals: [Int: [String]] + @Binding var editType: Behavior + @ObservedObject var viewModel: HistoryModel + + var body: some View { + VStack(alignment: .leading) { + if editType == Behavior.inlineEdit { + TextField("Source", text: Binding( + get: { + if index < viewModel.history[curBuffer, default: []].count { + return self.viewModel.history[curBuffer, default: []][index].src + } else { + return "" + } + }, + set: { + if index < viewModel.history[curBuffer, default: []].count { + self.viewModel.history[curBuffer, default: []][index].src = $0 + ephemerals[index, default: []].append($0) + } + } + )) + .onSubmit { + onMySubmit(self.viewModel.history[curBuffer, default: []][index].src) + for k in ephemerals.keys { + self.viewModel.history[curBuffer, default: []][k].src = ephemerals[k, default: []].first! + } + ephemerals = [:] // reset all virtual textfield edits + } + .font(Font.custom("BQN386 Unicode", size: 18)) + .foregroundColor(.blue) + } else if editType == Behavior.duplicate { + VStack(spacing: 1) { + let tokens = historyItem.lang == .k + ? tokenize(historyItem.src, parseK(historyItem.src)) + : tokenize(historyItem.src, parseBQN(code: historyItem.src)) + ForEach(Array(tokens.enumerated()), id: \.offset) { _, line in + HStack(spacing: 0) { + ForEach(line, id: \.id) { token in + Text(token.value) + .foregroundColor(tokenToColor(token.type)) + .font(Font.custom("BQN386 Unicode", size: 18)) + .onTapGesture { + self.input = historyItem.src + } + } + }.frame(maxWidth: .infinity, alignment: .leading) + } + } + } + Text("\(trimLongText(historyItem.out))") + .font(Font.custom("BQN386 Unicode", size: 18)) + .foregroundColor(historyItem.out.starts(with: "Error:") || historyItem.out.starts(with: "\"Error:") ? .red : .primary) + .multilineTextAlignment(.leading) + .onTapGesture { + self.input = historyItem.out + } + }.frame(maxWidth: .infinity, alignment: .leading) + } +}