summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2014-12-06 20:07:57 (GMT)
committerAssaf Gordon <assafgordon@gmail.com>2014-12-06 20:07:57 (GMT)
commitdb127407269b38a10ab07c779148f7583b7fc350 (patch)
tree12f4121974acbcac54883d49609f8c8565a7730f
parentd470e6e18d64f0b7d232f2483c18fdbcecb18fd7 (diff)
downloadagnostic-db127407269b38a10ab07c779148f7583b7fc350.zip
agnostic-db127407269b38a10ab07c779148f7583b7fc350.tar.gz
agnostic-db127407269b38a10ab07c779148f7583b7fc350.tar.bz2
shell-descriptors: implement arithmetic ops + for.
-rw-r--r--src/node_modules/shell/shell_HTML_descriptor.js60
-rw-r--r--src/node_modules/shell/shell_descriptor.js55
2 files changed, 109 insertions, 6 deletions
diff --git a/src/node_modules/shell/shell_HTML_descriptor.js b/src/node_modules/shell/shell_HTML_descriptor.js
index f74cf25..d145bce 100644
--- a/src/node_modules/shell/shell_HTML_descriptor.js
+++ b/src/node_modules/shell/shell_HTML_descriptor.js
@@ -138,6 +138,11 @@ function Command(obj)
case "compound_currentshell":
html = CompoundCurrentshell(value);
break;
+
+ case "for_clause":
+ html = ForClause(value);
+ break;
+
default:
throw "Invalid Command type '" + type + '"';
}
@@ -147,6 +152,24 @@ function Command(obj)
return html ;
}
+/* ForClause() - process a 'for' statement */
+function ForClause(obj)
+{
+ var exit_code = 0 ;
+ ob_utils.VerifyAllowedKeys(obj, ["varname","wordlist","action"]);
+
+ var varname = obj["varname"];
+ /* Process the word-list, split into separate words */
+ var wordlist = Token(true,obj["wordlist"]);
+ /* The commands to exeute each iteration */
+ var action = obj["action"];
+
+ var exit_code;
+
+ return 'for ' + varname.toString() + ' in ' + wordlist + ' ; do ' +
+ Command(action) + ' ; done ';
+}
+
/*
Pipeline() - process commands in a pipeline (e.g. "seq 10 | wc -l").
*/
@@ -912,8 +935,14 @@ function ArithmeticExpression(obj)
case 'envvar':
return EnvVar(value);
- case 'arithmetics_op_list':
- return ArithmeticOpList(value);
+ case 'arithmetics_binary_op_list':
+ return ArithmeticBinaryOpList(value);
+
+ case 'arithmetic_unary_op_list':
+ return ArithmeticUnaryOp(value);
+
+ case 'arithmetics_ternary_op':
+ return ArithmeticTernaryOp(value);
case 'arithmetic':
return ArithmeticExpression(value);
@@ -926,11 +955,36 @@ function ArithmeticExpression(obj)
}
}
+/* Process the conditional ternary operator */
+function ArithmeticTernaryOp(obj)
+{
+ ob_utils.VerifyArray(obj);
+ var condition = obj[0];
+ var true_value = obj[1];
+ var false_value = obj[2];
+ var cond_desc = ArithmeticExpression(condition);
+
+ var true_value_desc = ArithmeticExpression(true_value);
+ var false_value_desc = ArithmeticExpression(false_value);
+ return cond_desc + "?" + true_value_desc + ":" + false_value_desc;
+}
+
+/* Process an unary operation */
+function ArithmeticUnaryOp(obj)
+{
+ ob_utils.VerifyArray(obj);
+ var operator = obj[0];
+ var operand = obj[1];
+ var value = ArithmeticExpression(operand);
+ return operand + value;
+}
+
+
/* Process an list of arithmetics operation, with the same precedence
(eg. a list of addition/substractions, or a list of multiplications/divisions).
During execution/emulation, 'value' should contain the accumulated value */
-function ArithmeticOpList(obj)
+function ArithmeticBinaryOpList(obj)
{
ob_utils.VerifyArray(obj);
var operand = obj[0];
diff --git a/src/node_modules/shell/shell_descriptor.js b/src/node_modules/shell/shell_descriptor.js
index 72ccab2..cd9f078 100644
--- a/src/node_modules/shell/shell_descriptor.js
+++ b/src/node_modules/shell/shell_descriptor.js
@@ -76,11 +76,31 @@ function Command(obj)
return CompoundSubshell(value);
case "compound_currentshell":
return CompoundCurrentshell(value);
+ case "for_clause":
+ return ForClause(value);
default:
throw "Invalid Command type '" + type + '"';
}
}
+/* ForClause() - process a 'for' statement */
+function ForClause(obj)
+{
+ var exit_code = 0 ;
+ ob_utils.VerifyAllowedKeys(obj, ["varname","wordlist","action"]);
+
+ var varname = obj["varname"];
+ /* Process the word-list, split into separate words */
+ var wordlist = Token(true,obj["wordlist"]);
+ /* The commands to exeute each iteration */
+ var action = obj["action"];
+
+ var exit_code;
+
+ return 'for ' + varname.toString() + ' in ' + wordlist + ' ; do ' +
+ Command(action) + ' ; done ';
+}
+
/*
Pipeline() - process commands in a pipeline (e.g. "seq 10 | wc -l").
@@ -856,8 +876,14 @@ function ArithmeticExpression(obj)
case 'envvar':
return EnvVar(value);
- case 'arithmetics_op_list':
- return ArithmeticOpList(value);
+ case 'arithmetics_binary_op_list':
+ return ArithmeticBinaryOpList(value);
+
+ case 'arithmetic_unary_op_list':
+ return ArithmeticUnaryOp(value);
+
+ case 'arithmetics_ternary_op':
+ return ArithmeticTernaryOp(value);
case 'arithmetic':
return ArithmeticExpression(value);
@@ -869,12 +895,35 @@ function ArithmeticExpression(obj)
throw "Uknown arithmetic operation '" + op + '"';
}
}
+/* Process the conditional ternary operator */
+function ArithmeticTernaryOp(obj)
+{
+ ob_utils.VerifyArray(obj);
+ var condition = obj[0];
+ var true_value = obj[1];
+ var false_value = obj[2];
+ var cond_desc = ArithmeticExpression(condition);
+
+ var true_value_desc = ArithmeticExpression(true_value);
+ var false_value_desc = ArithmeticExpression(false_value);
+ return cond_desc + "?" + true_value_desc + ":" + false_value_desc;
+}
+
+/* Process an unary operation */
+function ArithmeticUnaryOp(obj)
+{
+ ob_utils.VerifyArray(obj);
+ var operator = obj[0];
+ var operand = obj[1];
+ var value = ArithmeticExpression(operand);
+ return operand + value;
+}
/* Process an list of arithmetics operation, with the same precedence
(eg. a list of addition/substractions, or a list of multiplications/divisions).
During execution/emulation, 'value' should contain the accumulated value */
-function ArithmeticOpList(obj)
+function ArithmeticBinaryOpList(obj)
{
ob_utils.VerifyArray(obj);
var operand = obj[0];