Skip to content

Commit

Permalink
add IO module
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Downs <[email protected]>
  • Loading branch information
briandowns committed Oct 19, 2023
1 parent 8aa6b1d commit 3db570d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/docs/standard-lib/io.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
layout: default
title: IO
nav_order: 24
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!
```
7 changes: 7 additions & 0 deletions examples/io.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import IO;


IO.print("asdf134\n");
IO.println(IO.stderr);
IO.println(IO.stdin);
IO.println(IO.stdout);
52 changes: 52 additions & 0 deletions src/optionals/io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#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, "fprint", fprintIO);
defineNative(vm, &module->values, "println", printlnIO);

pop(vm);
pop(vm);

return OBJ_VAL(module);
}
9 changes: 9 additions & 0 deletions src/optionals/io.h
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/optionals/optionals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions src/optionals/optionals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3db570d

Please sign in to comment.