Compare commits

..

4 Commits

6 changed files with 63 additions and 12 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ static enum bshell_status process_record(
bshell_runtime_push_scope(rt, p->f_block); bshell_runtime_push_scope(rt, p->f_block);
bshell_variable *item = bshell_runtime_define_var(rt, "_"); bshell_variable *item = bshell_runtime_define_var(rt, "_");
bshell_variable_set_value(item, &in); bshell_variable_set_value(item, in);
fx_value out = bshell_runtime_eval(rt); fx_value out = bshell_runtime_eval(rt);
bshell_runtime_pop_scope(rt); bshell_runtime_pop_scope(rt);
fx_value_unset(&in); fx_value_unset(&in);
+1 -5
View File
@@ -56,11 +56,7 @@ static enum bshell_status process_record(
continue; continue;
} }
bshell_verb_ref(verb); bshell_pipeline_write_value(pipeline, *v, false);
bshell_pipeline_write_value(
pipeline,
fx_value_ref_copy_return(v),
false);
break; break;
} while (1); } while (1);
+26
View File
@@ -171,6 +171,32 @@ void print_scriptblock(bshell_scriptblock *block, size_t depth)
} }
} }
void print_function(bshell_function *func)
{
const char *name = bshell_function_get_name(func);
const fx_array *params
= bshell_function_get_positional_parameters(func);
bshell_scriptblock *body = bshell_function_get_body(func);
printf("%s(", name);
size_t i = 0;
const fx_iterator *it = fx_iterator_begin(params);
fx_foreach(v, it)
{
if (i > 0) {
printf(", ");
}
printf("$");
fx_value_to_string(v, fx_stdout, NULL);
i++;
}
fx_iterator_unref(it);
printf("):\n");
print_scriptblock(body, 1);
}
void print_instruction( void print_instruction(
size_t offset, size_t offset,
bshell_instruction instr, bshell_instruction instr,
+2
View File
@@ -1,6 +1,7 @@
#ifndef DEBUG_H_ #ifndef DEBUG_H_
#define DEBUG_H_ #define DEBUG_H_
#include <bshell/command/function.h>
#include <bshell/runtime/opcode.h> #include <bshell/runtime/opcode.h>
#include <bshell/runtime/script-block.h> #include <bshell/runtime/script-block.h>
#include <stdbool.h> #include <stdbool.h>
@@ -12,6 +13,7 @@ extern void print_lex_token(struct bshell_lex_token *tok);
extern void print_ast_node(struct bshell_ast_node *node); extern void print_ast_node(struct bshell_ast_node *node);
extern void print_ast_node_recursive(struct bshell_ast_node *node); extern void print_ast_node_recursive(struct bshell_ast_node *node);
extern void print_scriptblock(bshell_scriptblock *block, size_t depth); extern void print_scriptblock(bshell_scriptblock *block, size_t depth);
extern void print_function(bshell_function *func);
extern void print_instruction( extern void print_instruction(
size_t offset, size_t offset,
bshell_instruction instr, bshell_instruction instr,
+27 -6
View File
@@ -107,15 +107,34 @@ int main(int argc, const char **argv)
printf("----\n"); printf("----\n");
bshell_scriptblock_clear_text(block); bshell_scriptblock_clear_text(block);
bshell_compile(node, block); switch (node->n_type) {
print_scriptblock(block, 0); case BSHELL_AST_FUNC: {
bshell_function *func = NULL;
fx_value result = bshell_runtime_eval_global(rt, block); bshell_compile_function(node, &func);
if (result.v_type != NULL) { print_function(func);
format_value_default(&result, fx_stdout); bshell_runtime_define_function(rt, func);
bshell_function_unref(func);
break;
} }
default: {
bshell_compile(node, block);
print_scriptblock(block, 0);
fx_value result = bshell_runtime_eval_global(rt, block);
if (result.v_type != NULL) {
format_value_default(&result, fx_stdout);
}
break;
}
}
bshell_ast_node_destroy(node);
} }
bshell_runtime_destroy(rt);
bshell_scriptblock_unref(block);
bshell_parse_ctx_cleanup(&parse); bshell_parse_ctx_cleanup(&parse);
bshell_lex_ctx_cleanup(&lex); bshell_lex_ctx_cleanup(&lex);
@@ -129,5 +148,7 @@ int main(int argc, const char **argv)
bshell_file_close(file); bshell_file_close(file);
} }
bshell_cleanup_all_verbs();
return 0; return 0;
} }
+6
View File
@@ -0,0 +1,6 @@
func test-function($name) {
echo "Hello, $name!"
}
$x = "Jonh"
test-function $x