Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7ac3bef

Browse files
committedMar 17, 2024·
start work on time builtin
1 parent 29d5524 commit 7ac3bef

File tree

8 files changed

+76
-9
lines changed

8 files changed

+76
-9
lines changed
 

‎examples/take-screenshot.slash

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ mkdir -p $dir
44
var file_name = $dir + "/" + (date +%Y-%m-%d-%H%M%S) + ".png"
55

66
if $1 == "-a" {
7-
gnome-screenshot -a -f $file_name
7+
notify-send -t 2000 "Taking screenshot ..."
8+
gnome-screenshot -a -f $file_name && echo "done"
89
} else {
910
scrot -F $file_name
1011
}

‎include/builtin/builtin.h

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ int builtin_vars(Interpreter *interpreter, size_t argc, SlashValue *argv);
5252
int builtin_exit(Interpreter *interpreter, size_t argc, SlashValue *argv);
5353
int builtin_read(Interpreter *interpreter, size_t argc, SlashValue *argv);
5454
int builtin_dot(Interpreter *interpreter, size_t argc, SlashValue *argv);
55+
int builtin_time(Interpreter *interpreter, size_t argc, SlashValue *argv);
5556

5657

5758
#endif /* BUILTIN_H */

‎include/interpreter/scope.h

-1
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,4 @@ ScopeAndValue var_get(Scope *scope, StrView *key);
6363
ScopeAndValue var_get_or_runtime_error(Scope *scope, StrView *key);
6464

6565

66-
6766
#endif /* SCOPE_H */

‎include/interpreter/value/slash_list.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131

3232
#define SLASH_LIST_STARTING_CAP 8
33-
#define SLASH_LIST_GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity)*2)
33+
#define SLASH_LIST_GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity) * 2)
3434

3535
typedef struct slash_list_impl_t {
3636
SlashObj obj;

‎src/builtin/time.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) 2024 Nicolai Brand (https://lytix.dev)
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <stdio.h>
19+
#include <sys/resource.h>
20+
#include <time.h>
21+
22+
#include "interpreter/exec.h"
23+
#include "interpreter/interpreter.h"
24+
#include "interpreter/value/slash_str.h"
25+
#include "interpreter/value/slash_value.h"
26+
27+
28+
int builtin_time(Interpreter *interpreter, size_t argc, SlashValue *argv)
29+
{
30+
struct rusage start_usage;
31+
getrusage(RUSAGE_SELF, &start_usage);
32+
clock_t start_time = clock();
33+
34+
char *exec_argv[argc + 1]; // + 1 because last element is NULL
35+
exec_argv[argc] = NULL;
36+
gc_barrier_start(&interpreter->gc);
37+
38+
for (size_t i = 0; i < argc; i++) {
39+
SlashValue arg = argv[i];
40+
VERIFY_TRAIT_IMPL(to_str, arg, "Could not take 'to_str' of type '%s'", arg.T->name);
41+
SlashValue value_str_repr = arg.T->to_str(interpreter, arg);
42+
exec_argv[i] = AS_STR(value_str_repr)->str;
43+
}
44+
45+
gc_barrier_end(&interpreter->gc);
46+
int exit_code = exec_program(&interpreter->stream_ctx, exec_argv);
47+
48+
struct rusage end_usage;
49+
getrusage(RUSAGE_CHILDREN, &end_usage);
50+
clock_t end_time = clock();
51+
52+
double real_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
53+
double user_time = (double)(end_usage.ru_utime.tv_sec + end_usage.ru_utime.tv_usec / 1000000.0);
54+
double sys_time = (double)(end_usage.ru_stime.tv_sec + end_usage.ru_stime.tv_usec / 1000000.0);
55+
printf("real\t%.3f\n", real_time);
56+
printf("user\t%.3f\n", user_time);
57+
printf("sys\t%.3f\n", sys_time);
58+
59+
return exit_code;
60+
}

‎src/builtin/which.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
Builtin builtins[] = {
2929
{ .name = "cd", .func = builtin_cd }, { .name = "vars", .func = builtin_vars },
3030
{ .name = "which", .func = builtin_which }, { .name = "exit", .func = builtin_exit },
31-
{ .name = "read", .func = builtin_read }, { .name = ".", .func = builtin_dot }
31+
{ .name = "read", .func = builtin_read }, { .name = ".", .func = builtin_dot },
32+
{ .name = "time", .func = builtin_time }
3233
};
3334

3435

@@ -87,7 +88,8 @@ int builtin_which(Interpreter *interpreter, size_t argc, SlashValue *argv)
8788
return 1;
8889
}
8990
SlashStr *param_str = AS_STR(to_str(interpreter, param));
90-
ScopeAndValue path = var_get_or_runtime_error(interpreter->scope, &(StrView){ .view = "PATH", .size = 4 });
91+
ScopeAndValue path =
92+
var_get_or_runtime_error(interpreter->scope, &(StrView){ .view = "PATH", .size = 4 });
9193
if (!IS_STR(*path.value)) {
9294
fprintf(stderr, "which: PATH variable should be type '%s' not '%s'", str_type_info.name,
9395
path.value->T->name);

‎src/interpreter/interpreter.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,8 @@ static void exec_seq_var(Interpreter *interpreter, SeqVarStmt *stmt)
580580

581581
static void exec_cmd(Interpreter *interpreter, CmdStmt *stmt)
582582
{
583-
ScopeAndValue path = var_get_or_runtime_error(interpreter->scope, &(StrView){ .view = "PATH", .size = 4 });
583+
ScopeAndValue path =
584+
var_get_or_runtime_error(interpreter->scope, &(StrView){ .view = "PATH", .size = 4 });
584585
if (!IS_STR(*path.value))
585586
REPORT_RUNTIME_ERROR("PATH variable should be type '%s' not '%s'", str_type_info.name,
586587
path.value->T->name);
@@ -600,6 +601,7 @@ static void exec_cmd(Interpreter *interpreter, CmdStmt *stmt)
600601
return;
601602
}
602603

604+
gc_barrier_start(&interpreter->gc);
603605
size_t argc = stmt->arg_exprs->size;
604606
SlashValue argv[argc];
605607
LLItem *item = stmt->arg_exprs->head;
@@ -609,6 +611,7 @@ static void exec_cmd(Interpreter *interpreter, CmdStmt *stmt)
609611
item = item->next;
610612
}
611613
which_result.builtin(interpreter, argc, argv);
614+
gc_barrier_end(&interpreter->gc);
612615
}
613616
}
614617

@@ -846,7 +849,8 @@ static void exec_iter_loop_map(Interpreter *interpreter, IterLoopStmt *stmt, Sla
846849

847850
static void exec_iter_loop_str(Interpreter *interpreter, IterLoopStmt *stmt, SlashStr *iterable)
848851
{
849-
ScopeAndValue ifs_res = var_get_or_runtime_error(interpreter->scope, &(StrView){ .view = "IFS", .size = 3 });
852+
ScopeAndValue ifs_res =
853+
var_get_or_runtime_error(interpreter->scope, &(StrView){ .view = "IFS", .size = 3 });
850854
if (!IS_STR(*ifs_res.value))
851855
REPORT_RUNTIME_ERROR("$IFS has to be of type 'str', but got '%s'", ifs_res.value->T->name);
852856

‎src/interpreter/scope.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ ScopeAndValue var_get_or_runtime_error(Scope *scope, StrView *key)
147147
{
148148
ScopeAndValue sv = var_get(scope, key);
149149
if (sv.value == NULL) {
150-
str_view_to_buf_cstr(*key);
151-
REPORT_RUNTIME_ERROR("Variable '%s' is not defined", buf);
150+
str_view_to_buf_cstr(*key);
151+
REPORT_RUNTIME_ERROR("Variable '%s' is not defined", buf);
152152
}
153153
return sv;
154154
}

0 commit comments

Comments
 (0)
Please sign in to comment.