-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Brian Downs <[email protected]>
- Loading branch information
1 parent
8aa6b1d
commit 3db570d
Showing
6 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters