mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 06:38:45 +01:00
Implement IO.read
This commit is contained in:
@ -78,4 +78,9 @@ class IO {
|
||||
IO.writeString_(obj.toString)
|
||||
return obj
|
||||
}
|
||||
|
||||
static read(prompt) {
|
||||
IO.write(prompt)
|
||||
return IO.read
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,8 +3,12 @@
|
||||
#if WREN_USE_LIB_IO
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
// TODO: this is a somewhat arbitrary limit, do something smarter?
|
||||
#define MAX_READ_LEN 1024
|
||||
|
||||
// This string literal is generated automatically from io.wren. Do not edit.
|
||||
static const char* libSource =
|
||||
"class IO {\n"
|
||||
@ -87,6 +91,11 @@ static const char* libSource =
|
||||
" IO.writeString_(obj.toString)\n"
|
||||
" return obj\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" static read(prompt) {\n"
|
||||
" IO.write(prompt)\n"
|
||||
" return IO.read\n"
|
||||
" }\n"
|
||||
"}\n";
|
||||
|
||||
static void ioWriteString(WrenVM* vm)
|
||||
@ -96,6 +105,18 @@ static void ioWriteString(WrenVM* vm)
|
||||
printf("%s", s);
|
||||
}
|
||||
|
||||
static void ioRead(WrenVM* vm)
|
||||
{
|
||||
char buffer[MAX_READ_LEN];
|
||||
char* result = fgets(buffer, MAX_READ_LEN, stdin);
|
||||
|
||||
if (result == NULL) {
|
||||
// TODO: handle error here
|
||||
}
|
||||
|
||||
wrenReturnString(vm, buffer, strlen(buffer));
|
||||
}
|
||||
|
||||
static void ioClock(WrenVM* vm)
|
||||
{
|
||||
wrenReturnDouble(vm, (double)clock() / CLOCKS_PER_SEC);
|
||||
@ -106,6 +127,7 @@ void wrenLoadIOLibrary(WrenVM* vm)
|
||||
wrenInterpret(vm, "Wren IO library", libSource);
|
||||
wrenDefineStaticMethod(vm, "IO", "writeString_", 1, ioWriteString);
|
||||
wrenDefineStaticMethod(vm, "IO", "clock", 0, ioClock);
|
||||
wrenDefineStaticMethod(vm, "IO", "read", 0, ioRead);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user