Skip to content

Commit

Permalink
WIP: pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyash committed May 14, 2017
1 parent 5aa502c commit ea088d2
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 46 deletions.
2 changes: 2 additions & 0 deletions ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ char *NGS_AST_NODE_TYPES_NAMES[] = {
"guard",
"try_catch",
"throw",
"commands_pipeline",
"commands_pipe",
"command",
"break",
"continue",
Expand Down
2 changes: 2 additions & 0 deletions ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ typedef enum {
GUARD_NODE,
TRY_CATCH_NODE,
THROW_NODE,
COMMANDS_PIPELINE_NODE,
COMMANDS_PIPE_NODE,
COMMAND_NODE,
BREAK_NODE,
CONTINUE_NODE,
Expand Down
35 changes: 28 additions & 7 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,34 @@ void compile_main_section(COMPILATION_CONTEXT *ctx, ast_node *node, char **buf,
OPCODE(*buf, OP_THROW);
break;

case COMMAND_NODE:
case COMMANDS_PIPELINE_NODE:

OPCODE(*buf, OP_PUSH_NULL); // Placeholder for return value
// commands
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
// options
compile_main_section(ctx, node->first_child->next_sibling->next_sibling, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_MAKE_CMDS_PIPELINE);

OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 1);
compile_identifier(ctx, buf, idx, node->first_child->name, OP_FETCH_LOCAL, OP_FETCH_UPVAR, OP_FETCH_GLOBAL);
OPCODE(*buf, OP_CALL);
POP_IF_DONT_NEED_RESULT(*buf);

break;

case COMMANDS_PIPE_NODE:

// name
compile_main_section(ctx, node->first_child, buf, idx, allocated, NEED_RESULT);
// options
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);

OPCODE(*buf, OP_MAKE_CMDS_PIPE);
POP_IF_DONT_NEED_RESULT(*buf);
break;

case COMMAND_NODE:
// argv
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 0); // Make array with zero elements
OPCODE(*buf, OP_MAKE_ARR);
Expand All @@ -988,16 +1014,11 @@ void compile_main_section(COMPILATION_CONTEXT *ctx, ast_node *node, char **buf,
compile_main_section(ctx, ptr, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_ARR_APPEND);
}
//options
// options
compile_main_section(ctx, node->first_child->next_sibling->next_sibling->next_sibling, buf, idx, allocated, NEED_RESULT);

OPCODE(*buf, OP_MAKE_CMD);

OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 1);
compile_identifier(ctx, buf, idx, node->first_child->name, OP_FETCH_LOCAL, OP_FETCH_UPVAR, OP_FETCH_GLOBAL);
OPCODE(*buf, OP_CALL);
POP_IF_DONT_NEED_RESULT(*buf);

break;

case BREAK_NODE:
Expand Down
56 changes: 34 additions & 22 deletions lib/stdlib.ngs
Original file line number Diff line number Diff line change
Expand Up @@ -3346,21 +3346,6 @@ TEST Argv({"-a1": [], "-a2": [1,2,3]}) == ["-a2", 1, 2, 3]

# --- $() ---

# TODO: better exceptions (use correct type, not string)
# TODO: c_waitpid() - handle signals
# TODO: split to methods for easy behaviour modification
# TODO: capture stderr
# TODO: provide additional communication channel on additional fd?
# TODO: handle built-in commands: do not execve,
# INFO: bash - redir.c
doc Execute an external command. Waits for the command to finish unless "&" is specified.
doc c - Command type instance, typically constructed by NGS when parsing the contents of $(...) .
doc %EX - if $(test -f /myfile) { ... }
doc %EX - # or
doc %EX - p = $(node main.js serve &)
doc %EX - YOUR_TESTS_OF_NODE_SERVER_HERE
doc %EX - p.kill()
doc %RET - Process
F '$()'(c:Command) {

# Special case: $($cmd)
Expand Down Expand Up @@ -3465,6 +3450,32 @@ F '$()'(c:Command) {
process
}

# TODO: better exceptions (use correct type, not string)
# TODO: c_waitpid() - handle signals
# TODO: split to methods for easy behaviour modification
# TODO: capture stderr
# TODO: provide additional communication channel on additional fd?
# TODO: handle built-in commands: do not execve,
# INFO: bash - redir.c
doc Execute an external command. Waits for the command to finish unless "&" is specified.
doc c - Command type instance, typically constructed by NGS when parsing the contents of $(...) .
doc %EX - if $(test -f /myfile) { ... }
doc %EX - # or
doc %EX - p = $(node main.js serve &)
doc %EX - YOUR_TESTS_OF_NODE_SERVER_HERE
doc %EX - p.kill()
doc %RET - Process
F '$()'(cp:CommandsPipeline) {

cp.commands.len() !=1 throws NotImplemented("CommandsPipeline can have exactly one command")

# XXX: can not be printed because it's circular - StackDepthFail in inspect()
# cp.commands.cp = cp

('$()')(cp.commands[0])

}

TEST $(true).Bool()
TEST $(false).Bool() == false
TEST "$(/bin/echo -n abc)" == 'abc'
Expand All @@ -3482,14 +3493,14 @@ F '$()'(c:Command) {


doc Get command standard output. Similar to bash.
F ``(c:Command) ('$()')(c).Str()
F ``(cp:CommandsPipeline) ('$()')(cp).Str()

doc Get command standard output and parse() it.
doc %RET - structured data when the output can be parsed.
doc %EX - ``aws ec2 describe-instances`` is Arr # true
doc %EX - ``aws ec2 describe-instances``.InstanceId.map(X[0..3]).join(",") # i-3, i-9, i-8, i-a, i-7, ...
F ````(c:Command) {
p = ('$()')(c)
F ````(cp:CommandsPipeline) {
p = ('$()')(cp)
p.stdout.parse({'process': p})
}

Expand Down Expand Up @@ -3558,11 +3569,11 @@ F unparse(data, hints:Hash) {
1
}

doc Returns c, wihout any processing. Convenient way to pass ready-to-run Command as an argument
doc %EX - F run_when_needed(c:Command) { ... $(c) ... }
doc Returns cp, wihout any processing. Convenient way to pass ready-to-run Command as an argument
doc %EX - F run_when_needed(cp:CommandsPipeline) { ... $(cp) ... }
doc %EX - ...
doc %EX - run_when_needed(%(ls >/tmp/my_ls))
F '%()'(c:Command) c
F '%()'(cp:CommandsPipeline) cp

doc Wait for the process and return lines of its stdout.
doc %RET - Arr of Str
Expand Down Expand Up @@ -4046,9 +4057,10 @@ doc Inspect Int
doc %RET - Arr with one element, Str
F inspect(i:Int) ["Int: $i"]

# TODO: escape special characters
doc Inspect Str
doc %RET - Arr with one element, Str
F inspect(s:Str) ["String: $s"]
F inspect(s:Str) ["String(${s.len()}): $s"]

doc Inspect Arr
doc %RET - Arr of Str
Expand Down
52 changes: 44 additions & 8 deletions syntax.leg
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ imm-array-literal =
CAPTURE_LOCATION($$);
}

imm-array-words = acc:command-node (
imm-array-words = acc:command-words-node (
inline_space?
(
array-command-word {
Expand Down Expand Up @@ -1138,7 +1138,7 @@ imm-hash-literal =
CAPTURE_LOCATION($$);
}

imm-hash-words = acc:command-node (
imm-hash-words = acc:command-words-node (
space?
(
hash-command-word {
Expand Down Expand Up @@ -1627,12 +1627,48 @@ optional-function-call-arguments =
}

subshell =
"$(" cmd:command-words ")" { $$ = cmd; }
| "%(" cmd:command-words ")" { cmd->first_child->name = ngs_strdup("%()"); $$ = cmd; }
| "``" cmd:command-words "``" { cmd->first_child->name = ngs_strdup("````"); $$ = cmd; }
| "`" cmd:command-words "`" { cmd->first_child->name = ngs_strdup("``"); $$ = cmd; }
"$(" cmd:commands-pipeline ")" { $$ = cmd; }
| "%(" cmd:commands-pipeline ")" { cmd->first_child->name = ngs_strdup("%()"); $$ = cmd; }
| "``" cmd:commands-pipeline "``" { cmd->first_child->name = ngs_strdup("````"); $$ = cmd; }
| "`" cmd:commands-pipeline "`" { cmd->first_child->name = ngs_strdup("``"); $$ = cmd; }

command-words = acc:command-node
commands-pipeline = acc:commands-pipeline-node c1:command-words {
append_ast_node_child(acc->first_child->next_sibling, c1);
}
(
inline_space?
p:commands-pipe {
append_ast_node_child(acc->first_child->next_sibling, p);
}
c:command-words {
append_ast_node_child(acc->first_child->next_sibling, c);
}
)* {
$$ = acc;
CAPTURE_LOCATION($$);
}

commands-pipeline-node = {
MAKE_NODE(ret, COMMANDS_PIPELINE_NODE);
MAKE_IDENTIFIER_NODE(f, "$()");
append_ast_node_child(ret, f);
MAKE_NODE(commands, ARR_LIT_NODE);
append_ast_node_child(ret, commands);
MAKE_NODE(options, HASH_LIT_NODE);
append_ast_node_child(ret, options);
$$ = ret;
}

commands-pipe = <"|"> {
MAKE_NODE(ret, COMMANDS_PIPE_NODE);
MAKE_STRING_NODE(name, yytext);
append_ast_node_child(ret, name);
MAKE_NODE(options, HASH_LIT_NODE);
append_ast_node_child(ret, options);
$$ = ret;
}

command-words = acc:command-words-node
(
inline_space?
o:command-option {
Expand Down Expand Up @@ -1662,7 +1698,7 @@ command-words = acc:command-node
CAPTURE_LOCATION($$);
}

command-node = {
command-words-node = {
MAKE_NODE(ret, COMMAND_NODE);
MAKE_IDENTIFIER_NODE(f, "$()");
append_ast_node_child(ret, f);
Expand Down
2 changes: 1 addition & 1 deletion vim/syntax/ngs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ syn keyword ngsRepeat for
" bootstrap types
syn keyword ngsType NotImplemented ReadFail RequreFail MainFail
" other types
syn keyword ngsType Any ArgvMatcher ArgvMatcherDecorator Arr ArrIter ArrLike BasicType Bool Box CLib Closure Command ConstIter Counter CSym DelimStr Eachable Eachable1 Eachable2 EmptyBox ExecutableNotFound FFI FullBox Fun Hash HashIter HashLike Int Iter KillFail KV NormalType NormalTypeInstance NoData Null Num Path Pipe PipeCreateFail Pred Process ProcessFail Range RangeIter Real Seq Stats Str Table TableMeta TableMetaNotIfCol TtyCheckFail Type
syn keyword ngsType Any ArgvMatcher ArgvMatcherDecorator Arr ArrIter ArrLike BasicType Bool Box CLib Closure Command CommandsPipe CommandsPipeline ConstIter Counter CSym DelimStr Eachable Eachable1 Eachable2 EmptyBox ExecutableNotFound FFI FullBox Fun Hash HashIter HashLike Int Iter KillFail KV NormalType NormalTypeInstance NoData Null Num Path Pipe PipeCreateFail Pred Process ProcessFail Range RangeIter Real Seq Stats Str Table TableMeta TableMetaNotIfCol TtyCheckFail Type
syn keyword ngsType Range NumRange PredRange
syn keyword ngsType Diff ArrDiff HashDiff
syn keyword ngsType Presence PartialPresence Present Absent ExactPresence
Expand Down
38 changes: 30 additions & 8 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ char *opcodes_names[] = {
/* 49 */ "TRY_END",
/* 50 */ "ARR_REVERSE",
/* 51 */ "THROW",
/* 52 */ "MAKE_CMD",
/* 53 */ "SET_CLOSURE_NAME",
/* 54 */ "SET_CLOSURE_DOC",
/* 55 */ "HASH_SET",
/* 56 */ "HASH_UPDATE",
/* 57 */ "PUSH_KWARGS_MARKER",
/* 58 */ "MAKE_REDIR",
/* 59 */ "SUPER",
/* 52 */ "MAKE_CMDS_PIPELINE",
/* 53 */ "MAKE_CMDS_PIPE",
/* 54 */ "MAKE_CMD",
/* 55 */ "SET_CLOSURE_NAME",
/* 56 */ "SET_CLOSURE_DOC",
/* 57 */ "HASH_SET",
/* 58 */ "HASH_UPDATE",
/* 59 */ "PUSH_KWARGS_MARKER",
/* 60 */ "MAKE_REDIR",
/* 61 */ "SUPER",
};


Expand Down Expand Up @@ -2349,6 +2351,8 @@ void vm_init(VM *vm, int argc, char **argv) {
NULL
);

MKTYPE(CommandsPipeline);
MKTYPE(CommandsPipe);
MKTYPE(Command);
MKTYPE(Redir);

Expand Down Expand Up @@ -4155,6 +4159,24 @@ METHOD_RESULT vm_run(VM *vm, CTX *ctx, IP ip, VALUE *result) {
// The place that Exception is created is the right place to set backtrace, not where it is thrown
// so not setting *result backtrace property here.
goto exception;
case OP_MAKE_CMDS_PIPELINE:
EXPECT_STACK_DEPTH(2);
command = make_normal_type_instance(vm->CommandsPipeline);
POP_NOCHECK(v);
set_normal_type_instance_attribute(command, make_string("options"), v);
POP_NOCHECK(v);
set_normal_type_instance_attribute(command, make_string("commands"), v);
PUSH_NOCHECK(command);
goto main_loop;
case OP_MAKE_CMDS_PIPE:
EXPECT_STACK_DEPTH(2);
command = make_normal_type_instance(vm->CommandsPipe);
POP_NOCHECK(v);
set_normal_type_instance_attribute(command, make_string("options"), v);
POP_NOCHECK(v);
set_normal_type_instance_attribute(command, make_string("name"), v);
PUSH_NOCHECK(command);
goto main_loop;
case OP_MAKE_CMD:
EXPECT_STACK_DEPTH(3);
command = make_normal_type_instance(vm->Command);
Expand Down
4 changes: 4 additions & 0 deletions vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ typedef struct {

VALUE Backtrace;

VALUE CommandsPipeline;
VALUE CommandsPipe;
VALUE Command;
VALUE Redir;

Expand Down Expand Up @@ -323,6 +325,8 @@ enum opcodes {
OP_TRY_END,
OP_ARR_REVERSE,
OP_THROW,
OP_MAKE_CMDS_PIPELINE,
OP_MAKE_CMDS_PIPE,
OP_MAKE_CMD,
OP_SET_CLOSURE_NAME,
OP_SET_CLOSURE_DOC,
Expand Down

0 comments on commit ea088d2

Please sign in to comment.