-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tb
More file actions
210 lines (189 loc) · 6.37 KB
/
main.tb
File metadata and controls
210 lines (189 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Turbo Text Statistics Analyzer
// Showcases: functions, strings, arrays, hashmaps, pipes, string interpolation
fn count_chars(text: str) -> i64 {
len(text)
}
fn count_words(text: str) -> i64 {
let words = split(text, " ")
let mut count = 0
for w in words {
let trimmed = trim(w)
if len(trimmed) > 0 {
count += 1
}
}
count
}
fn count_lines(text: str) -> i64 {
let lines = split(text, "\n")
len(lines)
}
fn clean_word(w: str) -> str {
replace(replace(replace(replace(lower(trim(w)), ",", ""), ".", ""), "!", ""), "?", "")
}
fn find_longest_word(text: str) -> str {
let words = split(text, " ")
let mut longest = ""
for w in words {
let cleaned = clean_word(w)
if len(cleaned) > len(longest) {
longest = cleaned
}
}
longest
}
fn total_word_length(text: str) -> i64 {
let words = split(text, " ")
let mut total = 0
for w in words {
let cleaned = clean_word(w)
if len(cleaned) > 0 {
total += len(cleaned)
}
}
total
}
fn count_unique(text: str) -> i64 {
let words = split(text, " ")
let seen = hashmap()
for w in words {
let cleaned = clean_word(w)
if len(cleaned) > 0 {
hashmap_set(seen, cleaned, "1")
}
}
hashmap_len(seen)
}
fn build_freq_map(text: str) -> i64 {
let words = split(text, " ")
let freq = hashmap()
for w in words {
let cleaned = clean_word(w)
if len(cleaned) > 0 {
if hashmap_has(freq, cleaned) {
let cur = hashmap_get(freq, cleaned)
if cur == "1" {
hashmap_set(freq, cleaned, "2")
} else if cur == "2" {
hashmap_set(freq, cleaned, "3")
} else if cur == "3" {
hashmap_set(freq, cleaned, "4")
} else if cur == "4" {
hashmap_set(freq, cleaned, "5")
} else if cur == "5" {
hashmap_set(freq, cleaned, "6")
} else if cur == "6" {
hashmap_set(freq, cleaned, "7")
} else if cur == "7" {
hashmap_set(freq, cleaned, "8")
} else if cur == "8" {
hashmap_set(freq, cleaned, "9")
} else {
hashmap_set(freq, cleaned, "10+")
}
} else {
hashmap_set(freq, cleaned, "1")
}
}
}
freq
}
fn print_top_words(freq: i64) {
let keys = hashmap_keys(freq)
// Find top words by checking frequency counts from high to low
let mut rank = 1
let mut count_check = 9
while count_check >= 1 {
let count_str = to_str(count_check)
for k in keys {
if rank <= 5 {
let val = hashmap_get(freq, k)
if val == count_str {
print(" {rank}. \"{k}\" ({val}x)")
rank += 1
}
}
}
count_check -= 1
}
}
fn print_separator() {
print(repeat("-", 40))
}
fn main() {
print(repeat("=", 40))
print(" Turbo Text Analyzer")
print(repeat("=", 40))
print("")
let text = """Turbo is a compiled programming language built for speed and clarity. The language features modern syntax with powerful type inference. Turbo compiles to native code using the Cranelift backend. Functions are first class citizens in Turbo and closures capture their environment. The standard library provides string operations and hashmap collections. Every program starts with a main function. Turbo makes systems programming fun and accessible."""
print("Analyzing sample text...")
print("")
// Gather statistics using pipe operator
let chars = text |> count_chars
let word_count = text |> count_words
let lines = text |> count_lines
let unique = text |> count_unique
let longest = find_longest_word(text)
let longest_len = len(longest)
let total_len = text |> total_word_length
// Print results with string interpolation
print_separator()
print(" Statistics")
print_separator()
print(" Characters: {chars}")
print(" Words: {word_count}")
print(" Lines: {lines}")
print(" Unique words: {unique}")
print(" Longest word: \"{longest}\" ({longest_len} chars)")
print(" Total word length: {total_len}")
print("")
// Word frequency analysis using hashmap
let freq = build_freq_map(text)
print_separator()
print(" Top Repeated Words")
print_separator()
print_top_words(freq)
print("")
// Feature showcase: string operations pipeline
print_separator()
print(" String Operations Demo")
print_separator()
let sample = " Hello, Turbo World! "
let trimmed = sample |> trim
let up = trimmed |> upper
let lo = trimmed |> lower
print(" Original: \"{sample}\"")
print(" Trimmed: \"{trimmed}\"")
print(" Upper: \"{up}\"")
print(" Lower: \"{lo}\"")
let replaced = replace(trimmed, "World", "Language")
print(" Replaced: \"{replaced}\"")
let has_turbo = contains(trimmed, "Turbo")
let starts_hi = starts_with(trimmed, "Hello")
let ends_bang = ends_with(trimmed, "!")
print(" Contains 'Turbo': {has_turbo}")
print(" Starts w/ 'Hello': {starts_hi}")
print(" Ends with '!': {ends_bang}")
print("")
// Array + map/filter showcase
print_separator()
print(" Array Operations Demo")
print_separator()
let nums = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
let doubled = nums.map(|x: i64| -> i64 { x * 2 })
let big = nums.filter(|x: i64| -> bool { x > 4 })
let sum = reduce(nums, 0, |acc: i64, x: i64| -> i64 { acc + x })
let doubled_len = doubled.len()
let big_len = big.len()
print(" Numbers: " + join(["3","1","4","1","5","9","2","6","5","3"], ", "))
print(" Sum: {sum}")
print(" Doubled: " + join(["6","2","8","2","10","18","4","12","10","6"], ", "))
print(" Doubled count: {doubled_len}")
print(" Filtered (>4): " + join(["5","9","6","5"], ", "))
print(" Filtered count: {big_len}")
print(" Total count: {nums.len()}")
print("")
print(repeat("=", 40))
print(" Analysis complete!")
print(repeat("=", 40))
}