diff --git a/docs/docs/standard-lib/io.md b/docs/docs/standard-lib/io.md new file mode 100644 index 00000000..f3a81781 --- /dev/null +++ b/docs/docs/standard-lib/io.md @@ -0,0 +1,43 @@ +--- +layout: default +title: IO +nav_order: 9 +parent: Standard Library +--- + +# IO +{: .no_toc } + +## Table of contents +{: .no_toc .text-delta } + +1. TOC +{:toc} + +--- + +## IO + +To make use of the IO module an import is required. + +```cs +import IO; +``` + +### IO.print(...values) -> Nil + +Prints a given list of values to stdout. + +```cs +IO.print(0); +// 0 +``` + +### IO.println(...values) -> Nil + +Prints a given list of values to stdout with an appended newline character. + +```cs +IO.println("Dictu!"); +// Dictu! +``` diff --git a/docs/docs/standard-lib/json.md b/docs/docs/standard-lib/json.md index c537b3bf..0197676c 100644 --- a/docs/docs/standard-lib/json.md +++ b/docs/docs/standard-lib/json.md @@ -1,7 +1,7 @@ --- layout: default title: JSON -nav_order: 9 +nav_order: 10 parent: Standard Library --- diff --git a/docs/docs/standard-lib/log.md b/docs/docs/standard-lib/log.md index 3c0044c7..e14f0cc5 100644 --- a/docs/docs/standard-lib/log.md +++ b/docs/docs/standard-lib/log.md @@ -1,7 +1,7 @@ --- layout: default title: Log -nav_order: 10 +nav_order: 11 parent: Standard Library --- diff --git a/docs/docs/standard-lib/math.md b/docs/docs/standard-lib/math.md index c97425ff..d7d94ae7 100644 --- a/docs/docs/standard-lib/math.md +++ b/docs/docs/standard-lib/math.md @@ -1,7 +1,7 @@ --- layout: default title: Math -nav_order: 11 +nav_order: 12 parent: Standard Library --- diff --git a/docs/docs/standard-lib/object.md b/docs/docs/standard-lib/object.md index c2fd6cc9..7cfc057b 100644 --- a/docs/docs/standard-lib/object.md +++ b/docs/docs/standard-lib/object.md @@ -1,7 +1,7 @@ --- layout: default title: Object -nav_order: 12 +nav_order: 13 parent: Standard Library --- diff --git a/docs/docs/standard-lib/path.md b/docs/docs/standard-lib/path.md index 56aa67dc..54f560c3 100644 --- a/docs/docs/standard-lib/path.md +++ b/docs/docs/standard-lib/path.md @@ -1,7 +1,7 @@ --- layout: default title: Path -nav_order: 13 +nav_order: 14 parent: Standard Library --- diff --git a/docs/docs/standard-lib/process.md b/docs/docs/standard-lib/process.md index 8471f0f5..caf36dd1 100644 --- a/docs/docs/standard-lib/process.md +++ b/docs/docs/standard-lib/process.md @@ -1,7 +1,7 @@ --- layout: default title: Process -nav_order: 14 +nav_order: 15 parent: Standard Library --- diff --git a/docs/docs/standard-lib/queue.md b/docs/docs/standard-lib/queue.md index ed823177..4b3c6870 100644 --- a/docs/docs/standard-lib/queue.md +++ b/docs/docs/standard-lib/queue.md @@ -1,7 +1,7 @@ --- layout: default title: Queue -nav_order: 15 +nav_order: 16 parent: Standard Library --- diff --git a/docs/docs/standard-lib/random.md b/docs/docs/standard-lib/random.md index 977f613e..8e56d8f0 100644 --- a/docs/docs/standard-lib/random.md +++ b/docs/docs/standard-lib/random.md @@ -1,7 +1,7 @@ --- layout: default title: Random -nav_order: 16 +nav_order: 17 parent: Standard Library --- diff --git a/docs/docs/standard-lib/socket.md b/docs/docs/standard-lib/socket.md index e1a873fa..8c7d9bc3 100644 --- a/docs/docs/standard-lib/socket.md +++ b/docs/docs/standard-lib/socket.md @@ -1,7 +1,7 @@ --- layout: default title: Socket -nav_order: 17 +nav_order: 18 parent: Standard Library --- diff --git a/docs/docs/standard-lib/sqlite.md b/docs/docs/standard-lib/sqlite.md index e476d7ae..df381a1f 100644 --- a/docs/docs/standard-lib/sqlite.md +++ b/docs/docs/standard-lib/sqlite.md @@ -1,7 +1,7 @@ --- layout: default title: Sqlite -nav_order: 18 +nav_order: 19 parent: Standard Library --- diff --git a/docs/docs/standard-lib/stack.md b/docs/docs/standard-lib/stack.md index 39f62319..3dddf28d 100644 --- a/docs/docs/standard-lib/stack.md +++ b/docs/docs/standard-lib/stack.md @@ -1,7 +1,7 @@ --- layout: default title: Stack -nav_order: 19 +nav_order: 20 parent: Standard Library --- diff --git a/docs/docs/standard-lib/system.md b/docs/docs/standard-lib/system.md index bfdbed9f..05238970 100644 --- a/docs/docs/standard-lib/system.md +++ b/docs/docs/standard-lib/system.md @@ -1,7 +1,7 @@ --- layout: default title: System -nav_order: 20 +nav_order: 21 parent: Standard Library --- diff --git a/docs/docs/standard-lib/term.md b/docs/docs/standard-lib/term.md index c4bf0cad..5d1b2f3d 100644 --- a/docs/docs/standard-lib/term.md +++ b/docs/docs/standard-lib/term.md @@ -1,7 +1,7 @@ --- layout: default title: Term -nav_order: 21 +nav_order: 22 parent: Standard Library --- diff --git a/docs/docs/standard-lib/unittest.md b/docs/docs/standard-lib/unittest.md index a801f0b3..dbe56202 100644 --- a/docs/docs/standard-lib/unittest.md +++ b/docs/docs/standard-lib/unittest.md @@ -1,7 +1,7 @@ --- layout: default title: UnitTest -nav_order: 22 +nav_order: 23 parent: Standard Library --- @@ -508,4 +508,4 @@ SomeServiceClass(successObj).print(); // Success! const errorObj = mock(ExternalApi, {"someExternalCall": 500}); SomeServiceClass(errorObj).print(); // -``` \ No newline at end of file +``` diff --git a/docs/docs/standard-lib/uuid.md b/docs/docs/standard-lib/uuid.md index 46199554..bbd9b367 100644 --- a/docs/docs/standard-lib/uuid.md +++ b/docs/docs/standard-lib/uuid.md @@ -1,7 +1,7 @@ --- layout: default title: UUID -nav_order: 23 +nav_order: 24 parent: Standard Library --- diff --git a/examples/io.du b/examples/io.du new file mode 100644 index 00000000..63eb8255 --- /dev/null +++ b/examples/io.du @@ -0,0 +1,7 @@ +import IO; + + +IO.print("asdf134\n"); +IO.println(IO.stderr); +IO.println(IO.stdin); +IO.println(IO.stdout); diff --git a/src/optionals/io.c b/src/optionals/io.c new file mode 100644 index 00000000..5ef8ad48 --- /dev/null +++ b/src/optionals/io.c @@ -0,0 +1,51 @@ +#include "io.h" + + +static Value printIO(DictuVM *vm, int argCount, Value *args) { + if (argCount == 0) { + runtimeError(vm, "print() takes 1 or more arguments (%d given)", argCount); + return EMPTY_VAL; + } + + for (int i = 0; i < argCount; ++i) { + printValue(args[i]); + } + + return NIL_VAL; +} + +static Value printlnIO(DictuVM *vm, int argCount, Value *args) { + if (argCount == 0) { + runtimeError(vm, "println() takes 1 or more arguments (%d given)", argCount); + return EMPTY_VAL; + } + + for (int i = 0; i < argCount; ++i) { + printValue(args[i]); + printf("\n"); + } + + return NIL_VAL; +} + +Value createIOModule(DictuVM *vm) { + ObjString *name = copyString(vm, "IO", 2); + push(vm, OBJ_VAL(name)); + ObjModule *module = newModule(vm, name); + push(vm, OBJ_VAL(module)); + + defineNativeProperty(vm, &module->values, "stdin", NUMBER_VAL(STDIN_FILENO)); + defineNativeProperty(vm, &module->values, "stdout", NUMBER_VAL(STDOUT_FILENO)); + defineNativeProperty(vm, &module->values, "stderr", NUMBER_VAL(STDERR_FILENO)); + + /** + * Define IO methods + */ + defineNative(vm, &module->values, "print", printIO); + defineNative(vm, &module->values, "println", printlnIO); + + pop(vm); + pop(vm); + + return OBJ_VAL(module); +} diff --git a/src/optionals/io.h b/src/optionals/io.h new file mode 100644 index 00000000..beb87c90 --- /dev/null +++ b/src/optionals/io.h @@ -0,0 +1,9 @@ +#ifndef _dictu_io_h +#define _dictu_io_h + +#include "optionals.h" +#include "../vm/vm.h" + +Value createIOModule(DictuVM *vm); + +#endif //_dictu_io_h diff --git a/src/optionals/optionals.c b/src/optionals/optionals.c index aebcc628..a0d307b6 100644 --- a/src/optionals/optionals.c +++ b/src/optionals/optionals.c @@ -5,6 +5,7 @@ BuiltinModules modules[] = { {"Argparse", &createArgParseModule, false}, {"Math", &createMathsModule, false}, {"Env", &createEnvModule, true}, + {"IO", &createIOModule, false}, {"JSON", &createJSONModule, false}, {"Log", &createLogModule, false}, {"Path", &createPathModule, false}, diff --git a/src/optionals/optionals.h b/src/optionals/optionals.h index c09d67bf..5a3dca30 100644 --- a/src/optionals/optionals.h +++ b/src/optionals/optionals.h @@ -6,6 +6,7 @@ #include "argparse/argparse.h" #include "math.h" #include "env/env.h" +#include "io.h" #include "system.h" #include "json.h" #include "log.h"