Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add IO module #680

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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!
```
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/json.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: JSON
nav_order: 9
nav_order: 10
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/log.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Log
nav_order: 10
nav_order: 11
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/math.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Math
nav_order: 11
nav_order: 12
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/object.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Object
nav_order: 12
nav_order: 13
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/path.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Path
nav_order: 13
nav_order: 14
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/process.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Process
nav_order: 14
nav_order: 15
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/queue.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Queue
nav_order: 15
nav_order: 16
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/random.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Random
nav_order: 16
nav_order: 17
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/socket.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Socket
nav_order: 17
nav_order: 18
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/sqlite.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Sqlite
nav_order: 18
nav_order: 19
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/stack.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Stack
nav_order: 19
nav_order: 20
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/system.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: System
nav_order: 20
nav_order: 21
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/term.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Term
nav_order: 21
nav_order: 22
parent: Standard Library
---

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/standard-lib/unittest.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: UnitTest
nav_order: 22
nav_order: 23
parent: Standard Library
---

Expand Down Expand Up @@ -508,4 +508,4 @@ SomeServiceClass(successObj).print(); // Success!

const errorObj = mock(ExternalApi, {"someExternalCall": 500});
SomeServiceClass(errorObj).print(); // <nothing>
```
```
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/uuid.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: UUID
nav_order: 23
nav_order: 24
parent: Standard Library
---

Expand Down
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);
51 changes: 51 additions & 0 deletions src/optionals/io.c
Original file line number Diff line number Diff line change
@@ -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);
}
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