Skip to content

Commit

Permalink
Add !args and !kwargs
Browse files Browse the repository at this point in the history
The names might change.
  • Loading branch information
gvx committed Oct 22, 2013
1 parent 812f17d commit cb3daec
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 19 deletions.
79 changes: 79 additions & 0 deletions vm/eva.c
Expand Up @@ -401,6 +401,85 @@ Error find_file_(Stack *S, Stack *scope_arr)
return Nothing;
}

int init_argv(int argc, char** argv, V v_eva)
{
V args = new_list();
V kwargs = new_dict();
int argi;
for (argi = 0; argi < argc; argi++)
{
char *arg = argv[argi];
int len = strlen(arg);
if (!valid_utf8(len, arg))
{
handle_error(UnicodeError, NULL);
return 1;
}
if (arg[0] == '-')
{
if (len == 1) // -
{
goto add_arg;
}
if (arg[1] == '-')
{
if (len == 2) // --
{
goto add_arg;
}
char *eq;
if ((eq = strchr(arg, '='))) // --arg=val
{
*eq = '\0';
V key = get_ident(arg + 2);
V value = a_to_string(eq + 1);
set_hashmap(toHashMap(kwargs), key, value);
}
else // --arg
{
V key = get_ident(arg + 2);
V current = get_hashmap(toHashMap(kwargs), key);
if (!current)
{
current = intToV(0);
}
if (isInt(current))
{
set_hashmap(toHashMap(kwargs), key, intToV(toInt(current) + 1));
}
}
}
else // -arg
{
char key[] = {'\0', '\0'};
int i;
for (i = 1; i < len; i++)
{
key[0] = arg[i];
V keyv = get_ident(key);
V current = get_hashmap(toHashMap(kwargs), keyv);
if (!current)
{
current = intToV(0);
}
if (isInt(current))
{
set_hashmap(toHashMap(kwargs), keyv, intToV(toInt(current) + 1));
}
}
}
}
else //arg
{
add_arg:
push(toStack(args), a_to_string(arg));
}
}
set_hashmap(toHashMap(v_eva), get_ident("args"), args);
set_hashmap(toHashMap(v_eva), get_ident("kwargs"), kwargs);
return 0;
}

CFunc eva[] = {
{"encode", encode},
{"decode", decode},
Expand Down
1 change: 1 addition & 0 deletions vm/eva.h
Expand Up @@ -4,5 +4,6 @@
#include "lib.h"

void init_module_path();
int init_argv(int, char**, V);

#endif
5 changes: 4 additions & 1 deletion vm/lib.c
Expand Up @@ -3,6 +3,7 @@
#include "persist.h"
#include "mersenne.h"
#include "blob.h"
#include "eva.h"

#include <time.h>
#include <sys/time.h>
Expand Down Expand Up @@ -2463,7 +2464,7 @@ void open_lib(CFunc lib[], HashMap* hm)
}
}

void open_std_lib(HashMap* hm)
V open_std_lib(HashMap* hm)
{
open_lib(stdlib, hm);
char** k;
Expand All @@ -2489,4 +2490,6 @@ void open_std_lib(HashMap* hm)
struct timespec tm;
clock_gettime(CLOCK_MONOTONIC, &tm);
init_random(tm.tv_sec ^ (tm.tv_sec >> 32) ^ tm.tv_nsec ^ (tm.tv_nsec >> 32));

return v_eva;
}
2 changes: 1 addition & 1 deletion vm/lib.h
Expand Up @@ -24,7 +24,7 @@ V new_cfunc(CFuncP);
V v_true;
V v_false;
void open_lib(CFunc[], HashMap*);
void open_std_lib(HashMap*);
V open_std_lib(HashMap*);

#define require(x) if (stack_size(S) < (x)) return StackEmpty;

Expand Down
4 changes: 1 addition & 3 deletions vm/run.c
Expand Up @@ -11,10 +11,8 @@ bool vm_silent = false;
bool vm_debug = false;
bool vm_persist = false;

void run(V file_name, Stack *S)
void run(V file_name, V global, Stack *S)
{
V global = new_global_scope();
open_std_lib(&toScope(global)->hm);
Error e = Nothing;
V file = load_file(file_name, global);
if (file == NULL)
Expand Down
2 changes: 1 addition & 1 deletion vm/run.h
@@ -1,3 +1,3 @@
#include "stack.h"

void run(V, Stack*);
void run(V, V, Stack*);
17 changes: 4 additions & 13 deletions vm/vu.c
Expand Up @@ -26,7 +26,6 @@ int main(int argc, char *argv[])
{0, 0, 0, 0},
};
char opt;
int i;
while ((opt = getopt_long(argc, argv, "+hdvsp", options, NULL)) != -1)
{
switch (opt)
Expand Down Expand Up @@ -60,20 +59,12 @@ int main(int argc, char *argv[])
init_path();
init_module_path();
init_errors();
V global = new_global_scope();
V v_eva = open_std_lib(&toScope(global)->hm);
Stack *S = new_stack();
init_argv(argc - optind, argv + optind, v_eva);

for (i = argc - 1; i > optind; i--)
{
if (!valid_utf8(strlen(argv[i]), argv[i]))
{
handle_error(UnicodeError, NULL);
clear_stack(S);
return 1;
}
pushS(a_to_string(argv[i]));
}

run(find_file(get_ident(argv[optind])), S);
run(find_file(get_ident(argv[optind])), global, S);
clear_stack(S);
}
return 0;
Expand Down

0 comments on commit cb3daec

Please sign in to comment.