Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Space Cadets
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
James Ellison
Space Cadets
Commits
2aebae68
Verified
Commit
2aebae68
authored
7 months ago
by
James Ellison
Browse files
Options
Downloads
Patches
Plain Diff
Week 2
parent
8a0cfdcf
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
challenge-ii/barebones.rio
+213
-0
213 additions, 0 deletions
challenge-ii/barebones.rio
with
213 additions
and
0 deletions
challenge-ii/barebones.rio
0 → 100644
+
213
−
0
View file @
2aebae68
fn remove_leading_whitespace(input) {
result := ""
leading := true
for char in input {
when char {
' ' => {
when leading {
false => {
result += char
}
}
},
else => {
leading = false
result += char
}
}
}
return result
}
fn split_by(input, splitter) {
lines := []
line := ""
for char in input {
when char {
splitter => {
lines += line
line = ""
},
else => {
line += char
}
}
}
lines += line
return lines
}
fn filter_out(input, filter) {
result := ""
for char in input {
when char {
filter => {},
else => { result += char }
}
}
return result
}
fn split(input) {
splitterated := []
for line in split_by(input, '\n') {
splitterated += line | remove_leading_whitespace() | filter_out(';') | split_by(' ')
}
return splitterated
}
fn index_of(arr, value) {
var_idx := 0
for v in arr {
when v {
value => {
return var_idx
}
}
var_idx += 1
}
return 99999
}
fn parse_int(str) {
result := 0
for c in str {
result *= 10
when c {
'0' => {},
'1' => {result += 1},
'2' => {result += 2},
'3' => {result += 3},
'4' => {result += 4},
'5' => {result += 5},
'6' => {result += 6},
'7' => {result += 7},
'8' => {result += 8},
'9' => {result += 9},
}
}
return result
}
fn interpret_line(line_idx, lines) {
line := lines[line_idx]
when line[0] {
"clear" => {
variable_idx := index_of(variable_names, line[1])
when variable_idx {
99999 => {
variable_values += 0
variable_names += line[1]
},
else => {
variable_values[variable_idx] = 0
}
}
}
"incr" => {
variable_idx := index_of(variable_names, line[1])
variable_values[variable_idx] = variable_values[variable_idx] + 1
}
"decr" => {
variable_idx := index_of(variable_names, line[1])
variable_values[variable_idx] = variable_values[variable_idx] - 1
}
"while" => {
variable_name := line[1]
variable_idx := index_of(variable_names, variable_name)
comparison_value := parse_int(line[3])
start_idx := line_idx
end_idx := line_idx + 1
depth := 0
while true {
cond_a := lines[end_idx][0] == "end"
cond_b := depth == 0
when cond_a and cond_b {
true => {
break
}
}
when lines[end_idx][0] {
"while" => {
depth += 1
}
"end" => {
depth -= 1
}
}
end_idx += 1
}
while variable_values[variable_idx] != comparison_value {
idx := start_idx + 1
while idx < end_idx {
idx = interpret_line(idx, lines)
}
}
return end_idx
}
"end" => {}
}
return line_idx + 1
}
input := "clear X;
incr X;
incr X;
clear Y;
incr Y;
incr Y;
incr Y;
clear Z;
while X not 0 do;
clear W;
while Y not 0 do;
incr Z;
incr W;
decr Y;
end;
while W not 0 do;
incr Y;
decr W;
end;
decr X;
end;"
variable_names := []
variable_values := []
lines := split(input)
lines_length := 0
for line in lines {
lines_length += 1
}
line_idx := 0
while line_idx < lines_length {
line_idx = interpret_line(line_idx, lines)
}
variable_idx := 0
for variable_name in variable_names {
d := print(variable_name, ": ", variable_values[variable_idx])
variable_idx += 1
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment