Skip to content

Commit

Permalink
chore: store tokenization for perf
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Oct 17, 2023
1 parent 784a9a8 commit 8727bf3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Beacon/Models/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Entry: Hashable, Codable, Identifiable {
var src: String
var out: String
var lang: Language
var tokens: [[Token]]
}

enum Buffers {
Expand Down Expand Up @@ -40,7 +41,10 @@ class HistoryModel: ObservableObject {
@Published var history: [String: [Entry]] = ["default": []]

func addMessage(with src: String, out: String, lang: Language, for key: String) {
let entry = Entry(src: src, out: out, lang: lang)
let tokenize = lang == .k
? tokenize(src, parseK(src))
: tokenize(src, parseBQN(code: src))
let entry = Entry(src: src, out: out, lang: lang, tokens: tokenize)
if var entries = history[key] {
entries.append(entry)
history[key] = entries
Expand Down
4 changes: 2 additions & 2 deletions Beacon/Utilities/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func tokenToColor(_ type: TokenType) -> Color {
}
}

enum TokenType: String {
enum TokenType: String, Codable {
case regC = "0"
case fnsC = "1"
case mopC = "2"
Expand All @@ -47,7 +47,7 @@ enum TokenType: String {
case endL = "E"
}

struct Token: Identifiable {
struct Token: Hashable, Codable, Identifiable {
var id = UUID()
let value: String
let type: TokenType
Expand Down
5 changes: 1 addition & 4 deletions Beacon/Views/HistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ struct HistoryView: View {
.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
ForEach(Array(historyItem.tokens.enumerated()), id: \.offset) { _, line in
HStack(spacing: 0) {
ForEach(line, id: \.id) { token in
Text(token.value)
Expand Down

0 comments on commit 8727bf3

Please sign in to comment.