diff --git a/binding.c b/binding.c index 1614350..f2dcf3e 100644 --- a/binding.c +++ b/binding.c @@ -201,6 +201,27 @@ ReturnObject *stratWrapper(struct scopeobject *scope) return createReturnObject(RT_RETURN, ret); } +ReturnObject *srandWrapper(struct scopeobject *scope) +{ + ValueObject *arg1 = getArg(scope, "seed"); + int seed = getInteger(arg1); + + srand(seed); + + return createReturnObject(RT_DEFAULT, NULL); +} + +ReturnObject *randWrapper(struct scopeobject *scope) +{ + ValueObject *arg1 = getArg(scope, "max"); + unsigned int max = getInteger(arg1); + + unsigned int val = (rand() % max); + + ValueObject *ret = createIntegerValueObject(val); + return createReturnObject(RT_RETURN, ret); +} + void loadLibrary(ScopeObject *scope, IdentifierNode *target) { char *name = NULL; @@ -213,7 +234,25 @@ void loadLibrary(ScopeObject *scope, IdentifierNode *target) name = resolveIdentifierName(target, scope); if (!name) goto loadLibraryAbort; - if (!strcmp(name, "STDIO")) { + if (!strcmp(name, "STDLIB")) { + lib = createScopeObject(scope); + if (!lib) goto loadLibraryAbort; + + loadBinding(lib, "MIX", "seed", &srandWrapper); + loadBinding(lib, "BLOW", "max", &randWrapper); + + id = createIdentifierNode(IT_DIRECT, (void *)copyString("STDLIB"), NULL, NULL, 0); + if (!id) goto loadLibraryAbort; + + if (!createScopeValue(scope, scope, id)) goto loadLibraryAbort; + + val = createArrayValueObject(lib); + if (!val) goto loadLibraryAbort; + lib = NULL; + + if (!updateScopeValue(scope, scope, id, val)) goto loadLibraryAbort; + deleteIdentifierNode(id); + } else if (!strcmp(name, "STDIO")) { lib = createScopeObject(scope); if (!lib) goto loadLibraryAbort; diff --git a/test/1.4-Tests/13-Bindings/4-stdlib/1-srand/test.lol b/test/1.4-Tests/13-Bindings/4-stdlib/1-srand/test.lol new file mode 100644 index 0000000..0f8ffb3 --- /dev/null +++ b/test/1.4-Tests/13-Bindings/4-stdlib/1-srand/test.lol @@ -0,0 +1,12 @@ +HAI 1.4 + CAN HAS STDLIB? + I IZ STDLIB'Z MIX YR 0 MKAY + I HAS A val1 ITZ I IZ STDLIB'Z BLOW YR 10 MKAY + I IZ STDLIB'Z MIX YR 1 MKAY + I HAS A val2 ITZ I IZ STDLIB'Z BLOW YR 10 MKAY + I IZ STDLIB'Z MIX YR 2 MKAY + I HAS A val3 ITZ I IZ STDLIB'Z BLOW YR 10 MKAY + VISIBLE val1 + VISIBLE val2 + VISIBLE val3 +KTHXBYE diff --git a/test/1.4-Tests/13-Bindings/4-stdlib/2-rand/test.lol b/test/1.4-Tests/13-Bindings/4-stdlib/2-rand/test.lol new file mode 100644 index 0000000..35bb835 --- /dev/null +++ b/test/1.4-Tests/13-Bindings/4-stdlib/2-rand/test.lol @@ -0,0 +1,9 @@ +HAI 1.4 + CAN HAS STDLIB? + I HAS A val1 ITZ I IZ STDLIB'Z BLOW YR 10 MKAY + I HAS A val2 ITZ I IZ STDLIB'Z BLOW YR 10 MKAY + I HAS A val3 ITZ I IZ STDLIB'Z BLOW YR 10 MKAY + VISIBLE val1 + VISIBLE val2 + VISIBLE val3 +KTHXBYE