Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Oct 18, 2023
1 parent f8083e2 commit 3533d03
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 112 deletions.
18 changes: 6 additions & 12 deletions Beacon.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
F32D9E972996710A007BC97C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3877C8B29952A5400E2FCB5 /* ContentView.swift */; };
F361BF092AD2A74C00200F72 /* curl.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = F3AC4D252A4F6C3000B4FECD /* curl.xcframework */; };
F361BF0C2AD2A76700200F72 /* DashboardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3DF82892A4EF33A00F6CD19 /* DashboardView.swift */; };
F379CEC32ADD94BC00164F76 /* LexK.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC22ADD94BC00164F76 /* LexK.swift */; };
F379CEC42ADD94BC00164F76 /* LexK.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC22ADD94BC00164F76 /* LexK.swift */; };
F379CEC62ADDB24D00164F76 /* LexBQN.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC52ADDB24D00164F76 /* LexBQN.swift */; };
F379CEC72ADDB24D00164F76 /* LexBQN.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC52ADDB24D00164F76 /* LexBQN.swift */; };
F379CEC32ADD94BC00164F76 /* Lexer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC22ADD94BC00164F76 /* Lexer.swift */; };
F379CEC42ADD94BC00164F76 /* Lexer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379CEC22ADD94BC00164F76 /* Lexer.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 */; };
Expand Down Expand Up @@ -112,8 +110,7 @@
F3512E082A4CE18D00658F24 /* libk.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libk.a; path = Beacon/Libs/libk.a; sourceTree = "<group>"; };
F35FE0AD2A44387C00653849 /* WidgetKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WidgetKit.framework; path = System/Library/Frameworks/WidgetKit.framework; sourceTree = SDKROOT; };
F35FE0AF2A44387D00653849 /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = System/Library/Frameworks/SwiftUI.framework; sourceTree = SDKROOT; };
F379CEC22ADD94BC00164F76 /* LexK.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LexK.swift; sourceTree = "<group>"; };
F379CEC52ADDB24D00164F76 /* LexBQN.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LexBQN.swift; sourceTree = "<group>"; };
F379CEC22ADD94BC00164F76 /* Lexer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lexer.swift; sourceTree = "<group>"; };
F379CEC82ADDD2A700164F76 /* Tokenizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tokenizer.swift; sourceTree = "<group>"; };
F379CECB2ADDD42C00164F76 /* HistoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryView.swift; sourceTree = "<group>"; };
F3877C8B29952A5400E2FCB5 /* ContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -209,8 +206,7 @@
F3B198DD2995181F00FE664F /* bqnffi.h */,
F3E1DFF62A3216C000B4A553 /* k.h */,
F312CF8729951C49008EC197 /* Beacon-Bridging-Header.h */,
F379CEC22ADD94BC00164F76 /* LexK.swift */,
F379CEC52ADDB24D00164F76 /* LexBQN.swift */,
F379CEC22ADD94BC00164F76 /* Lexer.swift */,
F379CEC82ADDD2A700164F76 /* Tokenizer.swift */,
);
path = Utilities;
Expand Down Expand Up @@ -461,8 +457,7 @@
F31BCE792A33B33E00A6116D /* Utils.swift in Sources */,
F3E1DFF52A31F9D800B4A553 /* ConfigView.swift in Sources */,
F32D9E8429966FCD007BC97C /* Beacon-Bridging-Header.h in Sources */,
F379CEC72ADDB24D00164F76 /* LexBQN.swift in Sources */,
F379CEC42ADD94BC00164F76 /* LexK.swift in Sources */,
F379CEC42ADD94BC00164F76 /* Lexer.swift in Sources */,
F379CECD2ADDD42C00164F76 /* HistoryView.swift in Sources */,
F30A7B8B2A370B7800503966 /* BuffersView.swift in Sources */,
F31BCE7F2A348B7500A6116D /* HelpView.swift in Sources */,
Expand All @@ -482,8 +477,7 @@
F3DF828A2A4EF33A00F6CD19 /* DashboardView.swift in Sources */,
F3E1DFF42A31F9D800B4A553 /* ConfigView.swift in Sources */,
F312CF8829951C89008EC197 /* Beacon-Bridging-Header.h in Sources */,
F379CEC62ADDB24D00164F76 /* LexBQN.swift in Sources */,
F379CEC32ADD94BC00164F76 /* LexK.swift in Sources */,
F379CEC32ADD94BC00164F76 /* Lexer.swift in Sources */,
F379CECC2ADDD42C00164F76 /* HistoryView.swift in Sources */,
F30A7B8A2A370B7800503966 /* BuffersView.swift in Sources */,
F31BCE7E2A348B7500A6116D /* HelpView.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions Beacon/Models/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class HistoryModel: ObservableObject {

func addMessage(with src: String, out: String, lang: Language, for key: String) {
let tokenize = lang == .k
? tokenize(src, parseK(src))
: tokenize(src, parseBQN(code: src))
? tokenize(src, lexK(src))
: tokenize(src, lexBQN(src))
let entry = Entry(src: src, out: out, lang: lang, tokens: tokenize)
if var entries = history[key] {
entries.append(entry)
Expand Down
89 changes: 0 additions & 89 deletions Beacon/Utilities/LexK.swift

This file was deleted.

88 changes: 85 additions & 3 deletions Beacon/Utilities/LexBQN.swift → Beacon/Utilities/Lexer.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,94 @@
//
// LexBQN.swift
// Beacon
// LexK.swift
// Arrayground
//

import Foundation
import SwiftUI

func parseBQN(code: String) -> [String] {
func lexK(_ str: String) -> [String] {
let regC = "0"
let fnsC = "1"
let fns = "+-*%!&|<>=~,^#_?@."
let mopC = "2"
let mop = "/'\\"
let namC = "4"
let nam = "⎕⍞∆⍙ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
let digC = "5"
let dig = "0123456789¯∞"
let parC = "6"
let dfnC = "7"
let strC = "8"
let dmdC = "D"
let dmd = ":$"
let comC = "C"
let endL = "E"

var res = Array(repeating: "", count: str.count)
res[0] = regC

var i = 0
while i < str.count {
let p = charAt(str, i - 1)
let c = charAt(str, i)
let n = charAt(str, i + 1)

if dig.contains(c) || (c == "." && dig.contains(n)) {
res[i] = digC
while i < str.count, dig.contains(charAt(str, i)) || ".eEnNbiwW".contains(charAt(str, i)) {
i += 1
}
continue
} else if (c == " " && n == "/") || ((p == "\n" || p == "\0") && c == "/") {
res[i] = comC
while i < str.count, charAt(str, i) != "\n" {
i += 1
}
if charAt(str, i) == "\n" { res[i] = endL }
} else if fns.contains(c) || c.unicodeScalars.first!.value >= 0x80 {
res[i] = fnsC
} else if mop.contains(c) {
res[i] = mopC
if n == ":" {
i += 1
res[i] = mopC
}
} else if "{xyz}".contains(c) {
res[i] = dfnC
} else if "([])".contains(c) {
res[i] = parC
} else if dmd.contains(c) {
res[i] = dmdC
} else if nam.contains(c) {
res[i] = namC
while i < str.count, nam.contains(charAt(str, i)) || dig.contains(charAt(str, i)) {
i += 1
}
continue
} else if c == "\"" {
res[i] = strC
i += 1
while charAt(str, i) != "\"", charAt(str, i) != "\n" {
i += charAt(str, i) == "\\" ? 2 : 1
}
} else if !" \t".contains(c) {
res[i] = regC
}
i += 1
}

return res
}

func charAt(_ str: String, _ i: Int) -> String {
if i < 0 || i >= str.count {
return "\0"
}
let index = str.index(str.startIndex, offsetBy: i)
return String(str[index])
}

func lexBQN(_ code: String) -> [String] {
let regC = "0"
let fnsC = "1"
let fns = "!+-×÷⋆*√⌊⌈∧∨¬|=≠≤<>≥≡≢⊣⊢⥊∾≍⋈↑↓↕⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔«»⍎⍕"
Expand Down
7 changes: 2 additions & 5 deletions Beacon/Utilities/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Foundation
import SwiftUI

func tokenToColor(_ type: TokenType) -> Color {
func colorBQN(_ type: TokenType) -> Color {
switch type {
case .regC:
return Color.gray // '#D2D2D2'
Expand All @@ -33,7 +33,7 @@ func tokenToColor(_ type: TokenType) -> Color {
}
}

func tokenToColorK(_ type: TokenType, _ isDarkTheme: Bool) -> Color {
func colorK(_ type: TokenType, _ isDarkTheme: Bool) -> Color {
let colorHex: String
switch type {
case .regC, .namC:
Expand Down Expand Up @@ -63,13 +63,10 @@ extension Color {
init(hex: String) {
let scanner = Scanner(string: hex)
var rgb: UInt64 = 0

scanner.scanHexInt64(&rgb)

let red = Double((rgb & 0xFF0000) >> 16) / 255.0
let green = Double((rgb & 0x00FF00) >> 8) / 255.0
let blue = Double(rgb & 0x0000FF) / 255.0

self.init(red: red, green: green, blue: blue)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Beacon/Views/HistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct HistoryView: View {
HStack(spacing: 0) {
ForEach(line, id: \.id) { token in
let isDarkTheme = (scheme == .dark)
let col = historyItem.lang == Language.k ? tokenToColorK(token.type, isDarkTheme) : tokenToColor(token.type)
let col = historyItem.lang == Language.k ? colorK(token.type, isDarkTheme) : colorBQN(token.type)
Text(token.value)
.lineLimit(1) // HACK to stop long tokens from messing alignment
.foregroundColor(col)
Expand Down

0 comments on commit 3533d03

Please sign in to comment.