Skip to content

Commit

Permalink
Merge pull request #35 from wrapl/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rajamukherji authored Aug 16, 2018
2 parents 45fc25a + 29ed109 commit e769327
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
41 changes: 41 additions & 0 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

static stringmap_t ContextCache[1] = {STRINGMAP_INIT};
static ml_value_t *DefaultString;
static ml_type_t *ContextT;

context_t *context_find(const char *Path) {
return stringmap_search(ContextCache, Path);
}

context_t *context_push(const char *Path) {
context_t *Context = new(context_t);
Context->Type = ContextT;
Context->Parent = CurrentContext;
Context->Path = Path;
Context->Name = Path;
Expand All @@ -26,12 +28,15 @@ context_t *context_push(const char *Path) {
stringmap_insert(Context->Locals, "DEFAULT", Default);
target_t *BuildDir = target_file_check(Path[0] == '/' ? Path + 1 : Path, 0);
stringmap_insert(Context->Locals, "BUILDDIR", BuildDir);
stringmap_insert(Context->Locals, "PATH", BuildDir);
stringmap_insert(Context->Locals, "_", Context);
stringmap_insert(ContextCache, Context->Name, Context);
return Context;
}

context_t *context_scope(const char *Name) {
context_t *Context = new(context_t);
Context->Type = ContextT;
Context->Parent = CurrentContext;
Context->Path = CurrentContext->Path;
Context->Name = concat(CurrentContext->Name, ":", Name, NULL);
Expand Down Expand Up @@ -61,6 +66,42 @@ void context_symb_set(context_t *Context, const char *Name, ml_value_t *Value) {
stringmap_insert(Context->Locals, Name, Value);
}

static ml_value_t *context_get_local(void *Data, int Count, ml_value_t **Args) {
context_t *Context = (context_t *)Args[0];
const char *Name = ml_string_value(Args[1]);
return ml_property(Context, Name, (ml_getter_t)context_symb_get, (ml_setter_t)context_symb_set, NULL, NULL);
}

static ml_value_t *context_get_parent(void *Data, int Count, ml_value_t **Args) {
context_t *Context = (context_t *)Args[0];
return (ml_value_t *)Context->Parent ?: MLNil;
}

static ml_value_t *context_path(void *Data, int Count, ml_value_t **Args) {
context_t *Context = (context_t *)Args[0];
return ml_string(Context->Path, -1);
}

static ml_value_t *context_get_subdir(void *Data, int Count, ml_value_t **Args) {
context_t *Context = (context_t *)Args[0];
const char *Name = ml_string_value(Args[1]);
const char *Path = concat(Context->Path, "/", Name, NULL);
return (ml_value_t *)context_find(Path) ?: MLNil;
}

static ml_value_t *context_get_scope(void *Data, int Count, ml_value_t **Args) {
context_t *Context = (context_t *)Args[0];
const char *Name = ml_string_value(Args[1]);
const char *Path = concat(Context->Path, ":", Name, NULL);
return (ml_value_t *)context_find(Path) ?: MLNil;
}

void context_init() {
DefaultString = ml_string("DEFAULT", -1);
ContextT = ml_class(MLAnyT, "context");
ml_method_by_name(".", 0, context_get_local, ContextT, MLStringT, NULL);
ml_method_by_name("parent", 0, context_get_parent, ContextT, NULL);
ml_method_by_name("path", 0, context_path, ContextT, NULL);
ml_method_by_name("/", 0, context_get_subdir, ContextT, MLStringT, NULL);
ml_method_by_name("@", 0, context_get_scope, ContextT, MLStringT, NULL);
}
1 change: 1 addition & 0 deletions context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
typedef struct context_t context_t;

struct context_t {
const ml_type_t *Type;
context_t *Parent;
const char *Path, *Name, *FullPath;
struct target_t *Default;
Expand Down
10 changes: 5 additions & 5 deletions rabs.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static ml_value_t *rabs_ml_get(void *Data, const char *Name) {
target_depends_auto(Target);
return Value;
} else {
return stringmap_search(Globals, Name) ?: ml_error("NameError", "%s undefined", Name);
return stringmap_search(Globals, Name) ?: MLNil; //ml_error("NameError", "%s undefined", Name);
}
}

Expand Down Expand Up @@ -106,18 +106,18 @@ ml_value_t *subdir(void *Data, int Count, ml_value_t **Args) {
//printf("FileName = %s\n", FileName);
FileName = vfs_resolve(FileName);
target_t *ParentDefault = CurrentContext->Default;
context_push(Path);
context_t *Context = context_push(Path);
targetset_insert(ParentDefault->Depends, CurrentContext->Default);
load_file(FileName);
context_pop();
return MLNil;
return (ml_value_t*)Context;
}

ml_value_t *scope(void *Data, int Count, ml_value_t **Args) {
ML_CHECK_ARG_COUNT(2);
ML_CHECK_ARG_TYPE(0, MLStringT);
const char *Name = ml_string_value(Args[0]);
context_scope(Name);
context_t *Context = context_scope(Name);
ml_value_t *Result = ml_call(Args[1], 0, NULL);
if (Result->Type == MLErrorT) {
printf("Error: %s\n", ml_error_message(Result));
Expand All @@ -127,7 +127,7 @@ ml_value_t *scope(void *Data, int Count, ml_value_t **Args) {
exit(1);
}
context_pop();
return MLNil;
return (ml_value_t *)Context;
}

ml_value_t *include(void *Data, int Count, ml_value_t **Args) {
Expand Down

0 comments on commit e769327

Please sign in to comment.