-
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.
Merge pull request #680 from briandowns/feature/fmt_module
add IO module
- Loading branch information
Showing
21 changed files
with
128 additions
and
16 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: 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! | ||
``` |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: JSON | ||
nav_order: 9 | ||
nav_order: 10 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Log | ||
nav_order: 10 | ||
nav_order: 11 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Math | ||
nav_order: 11 | ||
nav_order: 12 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Object | ||
nav_order: 12 | ||
nav_order: 13 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Path | ||
nav_order: 13 | ||
nav_order: 14 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Process | ||
nav_order: 14 | ||
nav_order: 15 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Queue | ||
nav_order: 15 | ||
nav_order: 16 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Random | ||
nav_order: 16 | ||
nav_order: 17 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Socket | ||
nav_order: 17 | ||
nav_order: 18 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Sqlite | ||
nav_order: 18 | ||
nav_order: 19 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Stack | ||
nav_order: 19 | ||
nav_order: 20 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: System | ||
nav_order: 20 | ||
nav_order: 21 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Term | ||
nav_order: 21 | ||
nav_order: 22 | ||
parent: Standard Library | ||
--- | ||
|
||
|
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
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 | ||
--- | ||
|
||
|
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,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); | ||
} |
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