-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.c
56 lines (48 loc) · 1.76 KB
/
read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
}