Comments (1)
The same program but with operators added in.
//! A dino program that reads a line of text as input, sorts it, and then outputs the
//! sorted string.
fn main() {
// Continue forever until the user quits the program with Ctrl-C
while true {
print_bstr(b"Enter some text: ");
let input = read_line();
let output = merge_sort(input);
print_bstr(output);
}
}
fn merge_sort(input: bstr) -> bstr {
let input_len = bstr_len(input);
// Base case: return a single character (or nothing)
if input_len <= 1 {
return input;
}
// Split the string and sort each half
let left = input[0..(input_len / 2)];
let right = input[(input_len / 2)..input_len];
let sorted_left = merge_sort(left);
let sorted_right = merge_sort(right);
// Merge the two halves together to get a single sorted string
merge(sorted_left, sorted_right)
}
/// Merges two sorted strings into a single sorted string
fn merge(left: bstr, right: bstr) -> bstr {
let left_len = bstr_len(left);
let right_len = bstr_len(right);
let left_i = 0;
let right_i = 0;
let output = b"";
// Continue until one of the strings runs out of characters
while left_i < left_len && right_i < right_len {
let left_byte = left[left_i];
let right_byte = right[right_i];
if left_byte < right_byte {
output += left_byte
left_i += 1;
} else { // left_byte >= right_byte
output += right_byte;
right_i += 1;
}
}
// Append any remaining characters on to the string
// Only one of these loops will run
while left_i < left_len {
output += left[left_i];
left_i += 1;
}
while right_i < right_len {
output += right[right_i];
right_i += 1;
}
output
}
While this adds the ability to dispatch on operators, it still does not technically add method dispatch. For that, we would also need bstr_len(x)
to become x.len()
.
from dino.
Related Issues (20)
- Advanced patterns
- Next Steps
- Diagnostics API
- Error Recovery in Parser HOT 2
- Parser Combinators
- Implement casting for numeric types
- Support arbitrary blocks in expr
- Remove distinction between NamedTy and Ty
- Support enums
- Basic Pattern Matching
- Figure out design for Package HOT 1
- Compiled Regular Expression State Machines
- Support Brainfuck Interpreter Sample Program
- Type inference and method/field dispatch HOT 1
- Prevent variables from being used before they're defined
- Block edge case: statements that are valid expressions
- Allow assignment by indexing
- Add x.py script for running all build steps
- Build std using compiler
- Use new cargo environment variables in integration tests
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dino.