Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add read builtin #93

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/builtin/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ int builtin_which(Interpreter *interpreter, size_t argc, SlashValue *argv);
int builtin_cd(Interpreter *interpreter, size_t argc, SlashValue *argv);
int builtin_vars(Interpreter *interpreter, size_t argc, SlashValue *argv);
int builtin_exit(Interpreter *interpreter, size_t argc, SlashValue *argv);
int builtin_read(Interpreter *interpreter, size_t argc, SlashValue *argv);


#endif /* BUILTIN_H */
6 changes: 6 additions & 0 deletions include/lib/str_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ void str_view_to_cstr(StrView view, char *cstr);

StrView str_view_arena_copy(Arena *arena, StrView to_copy);


#define str_view_to_buf_cstr(__str_view) \
char buf[(__str_view).size + 1]; \
memcpy(buf, (__str_view).view, (__str_view).size); \
buf[(__str_view).size] = 0;

#endif /* STR_VIEW_H */
56 changes: 56 additions & 0 deletions src/builtin/read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2024 Nicolai Brand (https://lytix.dev)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <stdio.h>

#include "interpreter/interpreter.h"
#include "interpreter/scope.h"
#include "interpreter/value/slash_value.h"
#include "interpreter/value/slash_str.h"
#include "interactive/prompt.h"


int builtin_read(Interpreter *interpreter, size_t argc, SlashValue *argv)
{
/* Usage: takes one argument, variable. */

if (argc == 0) {
fprintf(stderr, "read: no argument received");
return 1;
}
if (argc > 1) {
fprintf(stderr, "read: too many arguments received, expected one");
return 1;
}
SlashValue arg = argv[0];
if (!IS_TEXT_LIT(arg)) {
fprintf(stderr, "read: expected argument to be text, not '%s'", arg.T->name);
return 1;
}

Prompt prompt;
prompt_init(&prompt, ">>>");
prompt_run(&prompt);
StrView input = (StrView){ .view = prompt.buf, .size = prompt.buf_len };

SlashObj *str = gc_new_T(interpreter, &str_type_info);
slash_str_init_from_view(interpreter, (SlashStr *)str, &input);
var_define(interpreter->scope, &arg.text_lit, &AS_VALUE(str));

prompt_free(&prompt);
return 0;
}
53 changes: 36 additions & 17 deletions src/builtin/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,46 @@
#include "nicc/nicc.h"


static void hashmap_get_keys_as_str_views(HashMap *map, StrView *return_ptr)
{
size_t count = 0;
for (int i = 0; i < N_BUCKETS(map->size_log2); i++) {
struct hm_bucket_t *bucket = &map->buckets[i];
for (int j = 0; j < HM_BUCKET_SIZE; j++) {
struct hm_entry_t entry = bucket->entries[j];
if (entry.key == NULL)
continue;

return_ptr[count++] = (StrView){ .view = entry.key, .size = entry.key_size };
if (count == map->len)
return;
}
}
}


int builtin_vars(Interpreter *interpreter, size_t argc, SlashValue *argv)
{
(void)interpreter;
(void)argc;
(void)argv;
/// for (Scope *scope = interpreter->scope; scope != NULL; scope = scope->enclosing) {
/// HashMap map = scope->values;
/// char *keys[map.len];
/// SlashValue *values[map.len];
/// hashmap_get_keys(&map, (void **)keys);
/// hashmap_get_values(&map, (void **)values);

/// for (size_t i = 0; i < map.len; i++) {
/// // char *var = vars[i];
/// SlashValue *value = values[i];
/// putchar('=');
/// TraitPrint print_func = trait_print[value->type];
/// print_func(value);
/// putchar('\n');
/// }
///}
for (Scope *scope = interpreter->scope; scope != NULL; scope = scope->enclosing) {
HashMap map = scope->values;
StrView keys[map.len];
SlashValue *values[map.len];
hashmap_get_keys_as_str_views(&map, keys);
hashmap_get_values(&map, (void **)values);

for (size_t i = 0; i < map.len; i++) {
StrView key = keys[i];
SlashValue *value = values[i];
str_view_to_buf_cstr(key); // creates temporary buf variable
printf("%s", buf);
putchar('=');
VERIFY_TRAIT_IMPL(print, *value, "print not defined for type '%s'", value->T->name);
value->T->print(*value);
putchar('\n');
}
}

return 0;
}
3 changes: 2 additions & 1 deletion src/builtin/which.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
Builtin builtins[] = { { .name = "cd", .func = builtin_cd },
{ .name = "vars", .func = builtin_vars },
{ .name = "which", .func = builtin_which },
{ .name = "exit", .func = builtin_exit } };
{ .name = "exit", .func = builtin_exit },
{ .name = "read", .func = builtin_read } };


static void which_internal(WhichResult *mutable_result, char *PATH, char *command)
Expand Down
12 changes: 10 additions & 2 deletions src/interpreter/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Stmt *stmt_alloc(Arena *ast_arena, StmtType type)

Expr *expr_copy(Arena *arena, Expr *to_copy)
{
if (to_copy == NULL)
return NULL;

Expr *copy = expr_alloc(arena, to_copy->type);
switch (to_copy->type) {
case EXPR_UNARY: {
Expand Down Expand Up @@ -197,7 +200,9 @@ Expr *expr_copy(Arena *arena, Expr *to_copy)

Stmt *stmt_copy(Arena *arena, Stmt *to_copy)
{
/* Need expr_copy as well :-( */
if (to_copy == NULL)
return NULL;

Stmt *copy = stmt_alloc(arena, to_copy->type);
switch (to_copy->type) {
case STMT_VAR: {
Expand Down Expand Up @@ -298,9 +303,12 @@ Stmt *stmt_copy(Arena *arena, Stmt *to_copy)
case STMT_ABRUPT_CONTROL_FLOW: {
((AbruptControlFlowStmt *)copy)->ctrlf_type =
((AbruptControlFlowStmt *)to_copy)->ctrlf_type;
if (((AbruptControlFlowStmt *)to_copy)->return_expr != NULL)
if (((AbruptControlFlowStmt *)to_copy)->return_expr != NULL) {
((AbruptControlFlowStmt *)copy)->return_expr =
expr_copy(arena, ((AbruptControlFlowStmt *)to_copy)->return_expr);
} else {
((AbruptControlFlowStmt *)copy)->return_expr = NULL;
}
break;
}
case STMT_ENUM_COUNT:
Expand Down
13 changes: 7 additions & 6 deletions src/interpreter/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,8 @@ static SlashValue eval_call(Interpreter *interpreter, CallExpr *expr)

SlashValue return_value = NoneSingleton;
ExecResult result = exec_block_body(interpreter, function.body);
if (result.type != RT_NORMAL) {
if (result.type == RT_RETURN && result.return_expr != NULL)
return_value = eval(interpreter, result.return_expr);
}
interpreter->scope = function_scope->enclosing;
scope_destroy(function_scope);
return return_value;
Expand All @@ -500,8 +499,8 @@ static SlashValue eval_call(Interpreter *interpreter, CallExpr *expr)
static void exec_expr(Interpreter *interpreter, ExpressionStmt *stmt)
{
SlashValue value = eval(interpreter, stmt->expression);
// if (stmt->expression->type == EXPR_CALL)
// return;
if (stmt->expression->type == EXPR_CALL)
return;

TraitPrint trait_print = value.T->print;
assert(trait_print != NULL);
Expand Down Expand Up @@ -557,8 +556,10 @@ static void exec_cmd(Interpreter *interpreter, CmdStmt *stmt)
path.value->T->name);

WhichResult which_result = which(stmt->cmd_name, AS_STR(*path.value)->str);
if (which_result.type == WHICH_NOT_FOUND)
REPORT_RUNTIME_ERROR("Command not found");
if (which_result.type == WHICH_NOT_FOUND) {
str_view_to_buf_cstr(stmt->cmd_name); // creates temporary buf variable
REPORT_RUNTIME_ERROR("Command '%s' not found", buf);
}

if (which_result.type == WHICH_EXTERN) {
exec_program_stub(interpreter, stmt, which_result.path);
Expand Down
Loading