* [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/
@ 2011-03-30 12:58 Petteri Räty
0 siblings, 0 replies; 4+ messages in thread
From: Petteri Räty @ 2011-03-30 12:58 UTC (permalink / raw
To: gentoo-commits
commit: ee97a0f09065b4a3117fb1312dfb8b2667015220
Author: Petteri Räty <petsku <AT> petteriraty <DOT> eu>
AuthorDate: Mon Mar 28 19:35:29 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Wed Mar 30 12:51:00 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=ee97a0f0
Use function objects to simplify code
Using function objects we can make the special assignment operator
implementations share code.
---
bashast/libbashWalker.g | 30 ++++++++++--------------------
src/core/interpreter.h | 10 ++++++++++
2 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 6de281f..f7ed4ab 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -81,44 +81,34 @@ arithmetics returns[int value]
|^(POST_DECR libbash_name=name){ $value = walker->post_decr(libbash_name); }
|^(EQUALS libbash_name=name l=arithmetics) { walker->set_value(libbash_name, l); }
|^(MUL_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->multiply(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::multiply, libbash_name, l);
}
|^(DIVIDE_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->divide(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::divide, libbash_name, l);
}
|^(MOD_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->mod(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::mod, libbash_name, l);
}
|^(PLUS_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->plus(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::plus, libbash_name, l);
}
|^(MINUS_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->minus(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::minus, libbash_name, l);
}
|^(LSHIFT_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->left_shift(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::left_shift, libbash_name, l);
}
|^(RSHIFT_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->right_shift(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::right_shift, libbash_name, l);
}
|^(AND_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->bitwiseand(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::bitwiseand, libbash_name, l);
}
|^(XOR_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->bitwisexor(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::bitwisexor, libbash_name, l);
}
|^(OR_ASSIGN libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name,
- walker->bitwiseor(walker->resolve<int>(libbash_name), l));
+ walker->assign(&interpreter::bitwiseor, libbash_name, l);
}
| NUMBER { $value = walker->parse_int($NUMBER);}
| DIGIT { $value = walker->parse_int($DIGIT);}
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 1d6ced9..81e0837 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -29,6 +29,7 @@
#include <memory>
#include <string>
+#include <functional>
#include <antlr3basetree.h>
@@ -289,6 +290,15 @@ public:
return value;
}
+ /// \brief assign with an operator (for example multiply)
+ /// \param a function object to do an operation while assigning
+ /// \param the name of the variable
+ /// \param the value to assign
+ void assign(std::function<int(int,int)> f, const std::string& name, int value)
+ {
+ set_value(name,f(resolve<int>(name), value));
+ }
+
/// \brief resolve any variable
/// \param variable name
/// \return the value of the variable, call default constructor if
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/
@ 2011-04-04 16:09 Petteri Räty
0 siblings, 0 replies; 4+ messages in thread
From: Petteri Räty @ 2011-04-04 16:09 UTC (permalink / raw
To: gentoo-commits
commit: 8600327ca12c34900887989dd7d0d93b6a366f01
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Mon Apr 4 12:45:44 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Mon Apr 4 16:00:58 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=8600327c
Refactor walker grammar
String related rules are too complex in walker grammar. Now we use
a wildcard instead of listing all possible characters. Some names
are modified to have a clearer meaning.
---
bashast/libbashWalker.g | 62 +++++++++++-----------------------------------
src/core/interpreter.h | 4 +++
2 files changed, 19 insertions(+), 47 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index d0f89bf..ac8a040 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -61,54 +61,22 @@ var_def:
};
string_expr returns[std::string libbash_value]:
- ^(STRING libbash_string=string_expr) { $libbash_value = libbash_string; }
- |^(DOUBLE_QUOTED_STRING (libbash_string=dqstr { $libbash_value += libbash_string; })*)
- |^(STRING libbash_string=dqstr) { $libbash_value = libbash_string; };
+ ^(STRING(
+ (DOUBLE_QUOTED_STRING) => ^(DOUBLE_QUOTED_STRING (libbash_string=double_quoted_string { $libbash_value += libbash_string; })*)
+ |libbash_string=any_string { $libbash_value = libbash_string; }
+ ));
-//A rule for filenames/strings
-res_word_str returns[std::string libbash_value]
-@after {
- $libbash_value = walker->get_string($res_word_str.start);
-}:
- CASE|DO|DONE|ELIF|ELSE|ESAC|FI|FOR|FUNCTION|IF|IN|SELECT|THEN|UNTIL|WHILE|TIME;
-
-//Parts of strings, no slashes, no reserved words
-ns_str_part_no_res returns[std::string libbash_value]
-options{ backtrack=true; }
-@after {
- $libbash_value = walker->get_string($ns_str_part_no_res.start);
-}:
- num|name|EQUALS|PCT|PCTPCT|MINUS|DOT|DOTDOT|COLON|BOP|UOP|TEST_EXPR|'_'|TILDE|INC|DEC|MUL_ASSIGN|DIVIDE_ASSIGN|MOD_ASSIGN|PLUS_ASSIGN|MINUS_ASSIGN|LSHIFT_ASSIGN|RSHIFT_ASSIGN|AND_ASSIGN|XOR_ASSIGN|OR_ASSIGN|ESC_CHAR|CARET|OTHER;
-
-//Parts of strings, no slashes
-ns_str_part returns[std::string libbash_value]:
- ns_str_part_no_res { $libbash_value = $ns_str_part_no_res.libbash_value; }
- |res_word_str {$libbash_value = $res_word_str.libbash_value; };
-
-//Any allowable part of a string, including slashes, no pounds
-str_part returns[std::string libbash_value]:
- libbash_string=ns_str_part { $libbash_value = libbash_string; }
- |SLASH { $libbash_value = "/"; };
-
-//Any allowable part of a string, with pounds
-str_part_with_pound returns[std::string libbash_value]
-@after {
- $libbash_value = walker->get_string($str_part_with_pound.start);
-}:
- str_part|POUND|POUNDPOUND;
+//double quoted string rule, allows expansions
+double_quoted_string returns[std::string libbash_value]:
+ (var_ref) => libbash_string=var_ref { $libbash_value = libbash_string; }
+ |(ARITHMETIC_EXPRESSION) => ^(ARITHMETIC_EXPRESSION value=arithmetics) { $libbash_value = boost::lexical_cast<std::string>(value); }
+ |libbash_string=any_string { $libbash_value = libbash_string; };
-//Allowable parts of double quoted strings
-dq_str_part returns[std::string libbash_value]
-@after {
- $libbash_value = walker->get_string($dq_str_part.start);
+any_string returns[std::string libbash_value]
+@declarations {
+ pANTLR3_BASE_TREE any_token;
}:
- BLANK|EOL|AMP|LOGICAND|LOGICOR|LESS_THAN|GREATER_THAN|PIPE|SQUOTE|SEMIC|COMMA|LPAREN|RPAREN|LLPAREN|RRPAREN|DOUBLE_SEMIC|LBRACE|RBRACE|TICK|LEQ|GEQ|str_part_with_pound;
-
-//double quoted string rule, allows expansions
-dqstr returns[std::string libbash_value]:
- dq_str_part { $libbash_value = $dq_str_part.libbash_value; }
- | libbash_string=var_ref { $libbash_value = libbash_string; }
- | ^(ARITHMETIC_EXPRESSION value=arithmetics) { $libbash_value = boost::lexical_cast<std::string>(value); };
+ any_token=. { $libbash_value = walker->get_string(any_token); };
//Allowable variable names in the variable expansion
var_name returns[std::string libbash_value]
@@ -117,7 +85,7 @@ var_name returns[std::string libbash_value]
}:
num|name|TIMES|AT;
-var_exp returns[std::string libbash_value]:
+var_expansion returns[std::string libbash_value]:
^(USE_DEFAULT var_name libbash_word=word) {
libbash_value = walker->do_default_expansion($var_name.libbash_value, libbash_word);
};
@@ -133,7 +101,7 @@ word returns[std::string libbash_value]:
//variable reference
var_ref returns[std::string libbash_value]:
^(VAR_REF libbash_name=name) { $libbash_value = walker->resolve<std::string>(libbash_name); }
- |^(VAR_REF libbash_string=var_exp) { $libbash_value = libbash_string; };
+ |^(VAR_REF libbash_string=var_expansion) { $libbash_value = libbash_string; };
// shell arithmetic
arithmetics returns[int value]
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 2fc8703..5e097ea 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -104,6 +104,10 @@ public:
static std::string get_string(pANTLR3_BASE_TREE node)
{
pANTLR3_COMMON_TOKEN token = node->getToken(node);
+ // The tree walker may send null pointer here, so return an empty
+ // string if that's the case.
+ if(!token->start)
+ return "";
// Use reinterpret_cast here because we have to cast C code.
// The real type here is int64_t which is used as a pointer.
return std::string(reinterpret_cast<const char *>(token->start),
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/
@ 2011-06-11 8:52 Petteri Räty
0 siblings, 0 replies; 4+ messages in thread
From: Petteri Räty @ 2011-06-11 8:52 UTC (permalink / raw
To: gentoo-commits
commit: cb8d8bb66e2d51597ef32ce311fc25631936a99a
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Fri Jun 10 03:31:53 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Fri Jun 10 08:25:07 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=cb8d8bb6
Core: move helper functions to grammar file
Functions related to ANTLR are moved to grammar file.
---
bashast/libbashWalker.g | 44 +++++++++++++++++++++++++++++++++++---------
src/core/interpreter.cpp | 15 ---------------
src/core/interpreter.h | 17 -----------------
3 files changed, 35 insertions(+), 41 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 608d85c..5587bfa 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -115,6 +115,32 @@ options
{
return boost::xpressive::regex_match(target, pattern);
}
+
+ /// \brief parse the text value of a tree to integer
+ /// \param the target tree
+ /// \return the parsed value
+ static int parse_int(ANTLR3_BASE_TREE* tree)
+ {
+ return tree->getText(tree)->toInt32(tree->getText(tree));
+ }
+
+ /// \brief a helper function that get the string value
+ /// of the given pANTLR3_BASE_TREE node.
+ /// \param the target tree node
+ /// \return the value of node->text
+ static std::string get_string(pANTLR3_BASE_TREE node)
+ {
+ pANTLR3_COMMON_TOKEN token = node->getToken(node);
+ // The tree walker may send null pointer here, so return an empty
+ // string if that's the case.
+ if(!token->start)
+ return "";
+ // Use reinterpret_cast here because we have to cast C code.
+ // The real type here is int64_t which is used as a pointer.
+ // token->stop - token->start + 1 should be bigger than 0.
+ return std::string(reinterpret_cast<const char *>(token->start),
+ boost::numeric_cast<unsigned>(token->stop - token->start + 1));
+ }
}
start: list EOF;
@@ -128,8 +154,8 @@ variable_definitions
:^(VARIABLE_DEFINITIONS (LOCAL { local = true; })? var_def[local]*);
name_base returns[std::string libbash_value]
- :NAME { $libbash_value = walker->get_string($NAME); }
- |LETTER { $libbash_value = walker->get_string($LETTER); }
+ :NAME { $libbash_value = get_string($NAME); }
+ |LETTER { $libbash_value = get_string($LETTER); }
|'_' { $libbash_value="_"; };
name returns[std::string libbash_value, unsigned index]
@@ -146,8 +172,8 @@ name returns[std::string libbash_value, unsigned index]
num returns[std::string libbash_value]
options{ k=1; }
- :DIGIT { $libbash_value = walker->get_string($DIGIT); }
- |NUMBER { $libbash_value = walker->get_string($NUMBER); };
+ :DIGIT { $libbash_value = get_string($DIGIT); }
+ |NUMBER { $libbash_value = get_string($NUMBER); };
var_def[bool local]
@declarations {
@@ -295,7 +321,7 @@ basic_pattern[boost::xpressive::sregex& pattern, bool greedy, bool& do_append]
|(MATCH_ANY_EXCEPT|MATCH_ANY) =>
^((MATCH_ANY_EXCEPT { negation = true; } | MATCH_ANY { negation = false; })
((CHARACTER_CLASS) => ^(CHARACTER_CLASS n=NAME) {
- std::string class_name = walker->get_string(n);
+ std::string class_name = get_string(n);
if(class_name == "word")
pattern_str += "A-Za-z0-9_";
else if(class_name == "ascii")
@@ -331,7 +357,7 @@ any_string returns[std::string libbash_value]
@declarations {
pANTLR3_BASE_TREE any_token;
}
- :any_token=. { $libbash_value = walker->get_string(any_token); };
+ :any_token=. { $libbash_value = get_string(any_token); };
//Allowable variable names in the variable expansion
var_name returns[std::string libbash_value, unsigned index]
@@ -586,7 +612,7 @@ common_condition returns[bool status]
}
// -eq, -ne, -lt, -le, -gt, or -ge for arithmetic. -nt -ot -ef for files
:^(NAME left_str=string_expr right_str=string_expr) {
- $status = internal::test_binary(walker->get_string($NAME), left_str.libbash_value, right_str.libbash_value, *walker);
+ $status = internal::test_binary(get_string($NAME), left_str.libbash_value, right_str.libbash_value, *walker);
}
// -o for shell option, -z -n for string, -abcdefghkprstuwxOGLSN for files
|^(op=LETTER string_expr) {
@@ -1010,7 +1036,7 @@ arithmetics returns[int value]
walker->resolve<int>($primary.libbash_value, $primary.index) | l,
$primary.index);
}
- | NUMBER { $value = walker->parse_int($NUMBER);}
- | DIGIT { $value = walker->parse_int($DIGIT);}
+ | NUMBER { $value =parse_int($NUMBER);}
+ | DIGIT { $value = parse_int($DIGIT);}
| ^(VAR_REF libbash_string=var_expansion) { $value = boost::lexical_cast<int>(libbash_string); }
;
diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 87799e9..d1eb411 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -38,7 +38,6 @@
#include "core/bash_ast.h"
#include "core/unset_exception.h"
-#include "libbashWalker.h"
interpreter::interpreter(): _out(&std::cout), _err(&std::cerr), _in(&std::cin), bash_options(
{
@@ -86,20 +85,6 @@ interpreter::interpreter(): _out(&std::cout), _err(&std::cerr), _in(&std::cin),
define("IFS", " \t\n");
}
-std::string interpreter::get_string(pANTLR3_BASE_TREE node)
-{
- pANTLR3_COMMON_TOKEN token = node->getToken(node);
- // The tree walker may send null pointer here, so return an empty
- // string if that's the case.
- if(!token->start)
- return "";
- // Use reinterpret_cast here because we have to cast C code.
- // The real type here is int64_t which is used as a pointer.
- // token->stop - token->start + 1 should be bigger than 0.
- return std::string(reinterpret_cast<const char *>(token->start),
- boost::numeric_cast<unsigned>(token->stop - token->start + 1));
-}
-
std::shared_ptr<variable> interpreter::resolve_variable(const std::string& name) const
{
if(name.empty())
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index b7274ca..38b4c94 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -28,7 +28,6 @@
#include <memory>
#include <string>
-#include <antlr3basetree.h>
#include <boost/utility.hpp>
#include <boost/xpressive/xpressive.hpp>
#include <boost/numeric/conversion/cast.hpp>
@@ -38,8 +37,6 @@
#include "cppbash_builtin.h"
typedef std::unordered_map<std::string, std::shared_ptr<variable>> scope;
-struct libbashWalker_Ctx_struct;
-typedef struct libbashWalker_Ctx_struct * plibbashWalker;
///
/// \class interpreter
@@ -175,20 +172,6 @@ public:
_out = &std::cout;
}
- /// \brief parse the text value of a tree to integer
- /// \param the target tree
- /// \return the parsed value
- static int parse_int(ANTLR3_BASE_TREE* tree)
- {
- return tree->getText(tree)->toInt32(tree->getText(tree));
- }
-
- /// \brief a helper function that get the string value
- /// of the given pANTLR3_BASE_TREE node.
- /// \param the target tree node
- /// \return the value of node->text
- static std::string get_string(pANTLR3_BASE_TREE node);
-
/// \brief resolve string/int variable, local scope will be
/// checked first, then global scope
/// \param variable name
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/
@ 2011-06-11 8:52 Petteri Räty
0 siblings, 0 replies; 4+ messages in thread
From: Petteri Räty @ 2011-06-11 8:52 UTC (permalink / raw
To: gentoo-commits
commit: 21daf9a6dc8459eaf648f61e6116c63b5c97442b
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Thu Jun 9 15:01:59 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Sat Jun 11 08:51:16 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=21daf9a6
Walker: reimplement positional parameter
We don't need to define $1, $2 ... because $* is all of them. The
numbers are actually indexes of $*. This makes it easier to implement
shift and $#.
---
bashast/libbashWalker.g | 5 ++++-
src/core/interpreter.cpp | 26 ++++++--------------------
2 files changed, 10 insertions(+), 21 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 44df1a9..2648ecb 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -338,7 +338,10 @@ var_name returns[std::string libbash_value, unsigned index]
@init {
$var_name.index = 0;
}
- :libbash_string=num { $libbash_value = libbash_string; }
+ :libbash_string=num {
+ $index = boost::lexical_cast<unsigned>(libbash_string);
+ $libbash_value = ($index != 0 ? "*" : "0");
+ }
|name {
$libbash_value = $name.libbash_value;
$index = $name.index;
diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 4acfbb0..4c8c0d5 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -105,22 +105,12 @@ std::shared_ptr<variable> interpreter::resolve_variable(const std::string& name)
if(name.empty())
return std::shared_ptr<variable>();
- // positional parameter
- if(isdigit(name[0]) && !local_members.empty())
+ BOOST_REVERSE_FOREACH(auto& frame, local_members)
{
- auto iter_local = local_members.back().find(name);
- if(iter_local != local_members.back().end())
+ auto iter_local = frame.find(name);
+ if(iter_local != frame.end())
return iter_local->second;
}
- else
- {
- BOOST_REVERSE_FOREACH(auto& frame, local_members)
- {
- auto iter_local = frame.find(name);
- if(iter_local != frame.end())
- return iter_local->second;
- }
- }
auto iter_global = members.find(name);
if(iter_global == members.end())
@@ -286,14 +276,10 @@ void interpreter::define_function_arguments(scope& current_stack,
{
std::map<unsigned, std::string> positional_args;
- for(auto i = 0u; i != arguments.size(); ++i)
- {
- const std::string& name = boost::lexical_cast<std::string>(i + 1);
- current_stack[name].reset(new variable(name, arguments[i]));
- positional_args[i] = arguments[i];
- }
+ for(auto i = 1u; i <= arguments.size(); ++i)
+ positional_args[i] = arguments[i - 1];
- define("*", positional_args);
+ current_stack["*"].reset(new variable("*", positional_args, true));
}
void interpreter::call(const std::string& name,
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-06-11 8:53 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-06-11 8:52 [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/ Petteri Räty
-- strict thread matches above, loose matches on Subject: below --
2011-06-11 8:52 Petteri Räty
2011-04-04 16:09 Petteri Räty
2011-03-30 12:58 Petteri Räty
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox