-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Builtin lines function part II - Functionality (#566)
- Loading branch information
Showing
16 changed files
with
313 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use heraclitus_compiler::prelude::*; | ||
use crate::docs::module::DocumentationModule; | ||
use crate::modules::expression::expr::Expr; | ||
use crate::modules::types::{Type, Typed}; | ||
use crate::translate::module::TranslateModule; | ||
use crate::utils::metadata::{ParserMetadata, TranslateMetadata}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct LinesInvocation { | ||
path: Box<Option<Expr>>, | ||
} | ||
|
||
impl Typed for LinesInvocation { | ||
fn get_type(&self) -> Type { | ||
Type::Array(Box::new(Type::Text)) | ||
} | ||
} | ||
|
||
impl SyntaxModule<ParserMetadata> for LinesInvocation { | ||
syntax_name!("Lines Invocation"); | ||
|
||
fn new() -> Self { | ||
LinesInvocation { | ||
path: Box::new(None) | ||
} | ||
} | ||
|
||
fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
token(meta, "lines")?; | ||
token(meta, "(")?; | ||
let tok = meta.get_current_token(); | ||
let mut path = Expr::new(); | ||
syntax(meta, &mut path)?; | ||
token(meta, ")")?; | ||
if path.get_type() != Type::Text { | ||
let msg = format!("Expected value of type 'Text' but got '{}'", path.get_type()); | ||
return error!(meta, tok, msg); | ||
} | ||
self.path = Box::new(Some(path)); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TranslateModule for LinesInvocation { | ||
fn translate(&self, meta: &mut TranslateMetadata) -> String { | ||
let name = format!("__AMBER_ARRAY_{}", meta.gen_array_id()); | ||
let temp = format!("__AMBER_LINE_{}", meta.gen_value_id()); | ||
let path = (*self.path).as_ref() | ||
.map(|p| p.translate_eval(meta, false)) | ||
.unwrap_or_default(); | ||
let quote = meta.gen_quote(); | ||
let dollar = meta.gen_dollar(); | ||
let indent = TranslateMetadata::single_indent(); | ||
let block = [ | ||
format!("{name}=()"), | ||
format!("while IFS= read -r {temp}; do"), | ||
format!("{indent}{name}+=(\"${temp}\")"), | ||
format!("done <{path}"), | ||
].join("\n"); | ||
meta.stmt_queue.push_back(block); | ||
format!("{quote}{dollar}{{{name}[@]}}{quote}") | ||
} | ||
} | ||
|
||
impl LinesInvocation { | ||
pub fn surround_iter(&self, meta: &mut TranslateMetadata, name: &str) -> (String, String) { | ||
let path = (*self.path).as_ref() | ||
.map(|p| p.translate(meta)) | ||
.unwrap_or_default(); | ||
let prefix = format!("while IFS= read -r {name}; do"); | ||
let suffix = format!("done <{path}"); | ||
(prefix, suffix) | ||
} | ||
} | ||
|
||
impl DocumentationModule for LinesInvocation { | ||
fn document(&self, _meta: &ParserMetadata) -> String { | ||
"".to_string() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ pub mod mv; | |
pub mod nameof; | ||
pub mod exit; | ||
pub mod len; | ||
pub mod lines; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { lines } from "std/text" | ||
import { split_lines } from "std/text" | ||
|
||
// Output | ||
// line: hello | ||
// line: world | ||
|
||
main { | ||
for line in lines("hello\nworld") { | ||
for line in split_lines("hello\nworld") { | ||
echo "line: " + line | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Output | ||
// [one] | ||
// [two three four] | ||
// [] | ||
// [five] | ||
// [ six ] | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust $ echo -e 'one\ntwo three four' >{tmpdir}/numbers1.txt $ | ||
trust $ echo -e '\nfive\n six ' >{tmpdir}/numbers2.txt $ | ||
|
||
// Inefficient for large files. | ||
let lines = lines("{tmpdir}/numbers1.txt") + lines("{tmpdir}/numbers2.txt") | ||
for line in lines { | ||
echo "[{line}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Output | ||
// [one] | ||
// [two three four] | ||
// [] | ||
// [five] | ||
// [ six ] | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust $ echo -e 'one\ntwo three four' >{tmpdir}/numbers1.txt $ | ||
|
||
// Inefficient for large files. | ||
let lines = lines("{tmpdir}/numbers1.txt") + ["", "five", " six "] | ||
for line in lines { | ||
echo "[{line}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Output | ||
// [one] | ||
// [two three four] | ||
// [] | ||
// [five] | ||
// [ six ] | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust $ echo -e '\nfive\n six ' >{tmpdir}/numbers2.txt $ | ||
|
||
// Inefficient for large files. | ||
let lines = ["one", "two three four"] + lines("{tmpdir}/numbers2.txt") | ||
for line in lines { | ||
echo "[{line}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Output | ||
// [one] | ||
// [two three four] | ||
// [] | ||
// [five] | ||
// [ six ] | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust $ echo -e 'one\ntwo three four' >{tmpdir}/numbers1.txt $ | ||
trust $ echo -e '\nfive\n six ' >{tmpdir}/numbers2.txt $ | ||
|
||
// Inefficient for large files. | ||
let lines = lines("{tmpdir}/numbers1.txt") | ||
lines += lines("{tmpdir}/numbers2.txt") | ||
for line in lines { | ||
echo "[{line}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Output | ||
// [one] | ||
// [two three four] | ||
// [] | ||
// [five] | ||
// [ six ] | ||
|
||
fun append_ref(ref inner: [Text], path: Text): Null { | ||
inner += lines(path) | ||
} | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust $ echo -e 'one\ntwo three four' >{tmpdir}/numbers1.txt $ | ||
trust $ echo -e '\nfive\n six ' >{tmpdir}/numbers2.txt $ | ||
|
||
// Inefficient for large files. | ||
let lines = lines("{tmpdir}/numbers1.txt") | ||
append_ref(lines, "{tmpdir}/numbers2.txt") | ||
for line in lines { | ||
echo "[{line}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Output | ||
// [one] | ||
// [two three four] | ||
// [] | ||
// [five] | ||
// [ six ] | ||
// 0 [one] | ||
// 1 [two three four] | ||
// 2 [] | ||
// 3 [five] | ||
// 4 [ six ] | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust $ echo -e 'one\ntwo three four\n\nfive\n six ' >{tmpdir}/numbers.txt $ | ||
|
||
// Efficient for large files. | ||
for line in lines("{tmpdir}/numbers.txt") { | ||
echo "[{line}]" | ||
} | ||
for index, line in lines("{tmpdir}/numbers.txt") { | ||
echo "{index} [{line}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Output | ||
// [one] | ||
// [two three four] | ||
// [] | ||
// [five] | ||
// [ six ] | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust $ echo -e 'one\ntwo three four\n\nfive\n six ' >{tmpdir}/numbers.txt $ | ||
|
||
// Inefficient for large files. | ||
let lines = lines("{tmpdir}/numbers.txt") | ||
for line in lines { | ||
echo "[{line}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Output | ||
// [one] | ||
// [two three four] | ||
// [] | ||
// [five] | ||
// [ six ] | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust $ echo -e 'one\ntwo three four\n\nfive\n six ' >{tmpdir}/numbers.txt $ | ||
|
||
// Inefficient for large files. | ||
let lines = ["a", "b", "c"] | ||
lines = lines("{tmpdir}/numbers.txt") | ||
for line in lines { | ||
echo "[{line}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
Oops, something went wrong.