diff --git a/README.md b/README.md index 009d3f162d638c5de93211a43a402b144bfb86a3..3bde3e3d6401d51e8a489e171ac988d08294debe 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ core functionality is complete. Features below: - [x] Array Expressions (**[]**) - [x] Array-based Command Substitution (**@[]**) - [x] String-based Command Substitution (**$()**) -- [ ] Array Methods (**@split(var, ' ')**) -- [ ] String Methods (**$join(array, ', ')**) +- [x] Array Methods (**@split(var, ' ')**) +- [x] String Methods (**$join(array, ', ')**) - [x] Array Splicing - [ ] Maps - [x] For Loops @@ -57,8 +57,8 @@ If the command is executed without any arguments, it will simply list all availa ### Using Variables -Variables may be called with ith **$** sigil, where the value that follows may be a local or global value. -They may also be optionally be defined using a braced syntax, which is useful in the event that you need the value +Variables may be called with the **$** sigil, where the value that follows may be a local or global value. +They may also be optionally defined using a braced syntax, which is useful in the event that you need the value integrated alongside other characters that do not terminate the variable parsing. ```ion @@ -225,6 +225,24 @@ echo @array[3..] echo @array[..] ``` +### Methods + +There are two types of methods -- string-based and array-based methods. The type that a method returns is denoted +by the sigil that is used to invoke the method. Currently, there are only two supported methods: **$join()** and +**@split**. + +```ion +let results = [ 1 2 3 4 5] +echo $join(results) @join # Both of these effectively do the same thing +echo $join(results, ', ') # You may provide a custom pattern instead + +let line = "one two three four five" +echo @split(line) # Splits a line by whitespace + +let row = "one,two,three,four,five" +echo @split(row, ',') # Splits by commas +``` + ### Commands Commands may be written line by line or altogether on the same line with semicolons separating them. diff --git a/examples/methods.ion b/examples/methods.ion new file mode 100644 index 0000000000000000000000000000000000000000..8db2673362d29e4cf3778c15bca6b29f8507fb71 --- /dev/null +++ b/examples/methods.ion @@ -0,0 +1,7 @@ +let array = [ one two three four ] +let space_string = $join(array) +let comma_string = $join(array, ', ') +echo $space_string +echo $comma_string +echo @split(space_string) +echo @split(comma_string, ', ') diff --git a/examples/methods.out b/examples/methods.out new file mode 100644 index 0000000000000000000000000000000000000000..128c4c4c7e452a974ba8a90f30593b3ebb21ed34 --- /dev/null +++ b/examples/methods.out @@ -0,0 +1,4 @@ +one two three four +one, two, three, four +one two three four +one two three four diff --git a/src/parser/shell_expand/mod.rs b/src/parser/shell_expand/mod.rs index dc5bf1745c896f0d1d97e7e4f0c57e0751f367dd..c764e01e6e79b95ae749de39885c31913fd3ef2a 100644 --- a/src/parser/shell_expand/mod.rs +++ b/src/parser/shell_expand/mod.rs @@ -1,3 +1,5 @@ +// TODO: Handle Runtime Errors + extern crate permutate; mod braces; diff --git a/src/parser/shell_expand/words.rs b/src/parser/shell_expand/words.rs index f97033c45fd528852370ea9f129925b261042dc9..e27cf66115e7a45667f696176c459c33864cbeac 100644 --- a/src/parser/shell_expand/words.rs +++ b/src/parser/shell_expand/words.rs @@ -158,6 +158,11 @@ impl<'a> WordIterator<'a> { } self.read += 1; } + } else if character == b')' { + // If no pattern is supplied, the default is a space. + let variable = &self.data[start..self.read]; + self.read += 1; + return WordToken::StringMethod(method, variable, " "); } self.read += 1; } @@ -221,6 +226,11 @@ impl<'a> WordIterator<'a> { } self.read += 1; } + } else if character == b')' { + // If no pattern is supplied, the default is a space. + let variable = &self.data[start..self.read]; + self.read += 1; + return WordToken::ArrayMethod(method, variable, " "); } self.read += 1; } diff --git a/src/parser/statements.rs b/src/parser/statements.rs index 3079031ac89d210d6ae7d90bad96a61dfa21f1e0..56cce713eb6371451d1113e728008d8c7ca5d60c 100644 --- a/src/parser/statements.rs +++ b/src/parser/statements.rs @@ -1,4 +1,6 @@ -// TODO: Rewrite this in the same style as shell_expand::words. +// TODO: +// - Rewrite this in the same style as shell_expand::words. +// - Validate syntax in methods use std::u16; use std::io::{self, Write};