summaryrefslogtreecommitdiffstats
path: root/src/parse
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-01-08 00:28:37 -0500
committerJesse Luehrs <doy@tozt.net>2022-01-08 00:28:37 -0500
commit489fd0db1bf7f1de31a043e315179e83b77921aa (patch)
treea4862b8f8572234305eae3a1128860a7a1d5117c /src/parse
parent9dd1d992bd2344d8824e69d8f47c9009a6caf021 (diff)
downloadnbsh-489fd0db1bf7f1de31a043e315179e83b77921aa.tar.gz
nbsh-489fd0db1bf7f1de31a043e315179e83b77921aa.zip
add basic variable parsing
Diffstat (limited to 'src/parse')
-rw-r--r--src/parse/ast.rs34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/parse/ast.rs b/src/parse/ast.rs
index 28593bb..71cb9af 100644
--- a/src/parse/ast.rs
+++ b/src/parse/ast.rs
@@ -142,6 +142,7 @@ struct Word {
word: String,
interpolate: bool,
quoted: bool,
+ var: bool,
}
impl Word {
@@ -150,22 +151,33 @@ impl Word {
matches!(rule, Rule::bareword | Rule::double_string);
let quoted =
matches!(rule, Rule::single_string | Rule::double_string);
- let mut word_str = s.to_string();
- if interpolate {
- word_str = strip_escape(&word_str);
+ let var = matches!(rule, Rule::var);
+ let word_str = if var {
+ let inner = s.strip_prefix('$').unwrap();
+ inner
+ .strip_prefix('{')
+ .map_or(inner, |inner| inner.strip_suffix('}').unwrap())
+ .to_string()
+ } else if interpolate {
+ strip_escape(s)
} else {
- word_str = strip_basic_escape(&word_str);
- }
+ strip_basic_escape(s)
+ };
Self {
word: word_str,
interpolate,
quoted,
+ var,
}
}
fn eval(self, _env: &Env) -> String {
- // TODO
- self.word
+ if self.var {
+ // TODO
+ format!("'value-of-${{{}}}'", self.word)
+ } else {
+ self.word
+ }
}
}
@@ -247,7 +259,10 @@ impl WordOrRedirect {
}
assert!(matches!(
word.as_rule(),
- Rule::bareword | Rule::single_string | Rule::double_string
+ Rule::var
+ | Rule::bareword
+ | Rule::single_string
+ | Rule::double_string
));
let word = Word::parse(word.as_str(), word.as_rule());
if let Some(prefix) = prefix {
@@ -381,6 +396,7 @@ macro_rules! w {
word: $word.to_string(),
interpolate: true,
quoted: false,
+ var: false,
}
};
($word:expr, $interpolate:expr) => {
@@ -388,6 +404,7 @@ macro_rules! w {
word: $word.to_string(),
interpolate: $interpolate,
quoted: false,
+ var: false,
}
};
($word:expr, $interpolate:expr, $quoted:expr) => {
@@ -395,6 +412,7 @@ macro_rules! w {
word: $word.to_string(),
interpolate: $interpolate,
quoted: $quoted,
+ var: false,
}
};
}