Ok, assuming I've not missed a 'scanf' library function somewhere, I decided
to write a reasonably useful 'read from console' function and get it small
as possible. Here's what I came up with based on failed experiments to
file_read from file_stdin()...
//!
//! Load standard module from /usr/lib/neko
//!
std = $loader.loadmodule("std",$loader);
//!
//! Smallest simplistic readline I managed to get.
//! CAN YOU SHRINK IT?! :)
//!
readLine = function( password ) {
read1 = function( acc ) {
switch( k = std.sys_getch( false )) {
13 => std.buffer_string( acc )
default => {
$print( if password "*" else std.sprintf("%c",k));
std.buffer_add_char( acc, k );
read1( acc ); }}}
read1( std.buffer_new()); }
//!
//! Test normal input -- No delete of course.
//!
$print( "Username: " );
$print( "\nValue: ", readLine( false ), "\n" );
//!
//! Get a password value
//!
$print( "Password: " );
$print( "\nValue: ", readLine( true ), "\n" );
The first challenge is can anybody make it smaller / more concise / better
Neko style etc etc so that I can learn some more and then maybe add
backspace a bit later. Unless you want to! ;) Sometimes little things like
this can really bring out the nuances of a language. I've thought of
otherways like mapping password to 0/1 and using it as an index into an
array of functions that return '*' or 'k' but it all seems like overkill.
I know somebody can make this slicker and better! There's the gauntlet :)
And now comes the change request, would it be possible for the buffer
functions that currently return void to return the 'buffer, that way my
recursive call "read1(acc)" could have been "read1( std.buffer_add( acc, k
))" which is more LISP/scheme like and make things more readable in that
paradigm.
I am sure that there are other functions that return void that could be
'improved'(?) by returning the target instead of void, allowing for chaining
etc. Comments ? Having seen the Neko source code, I can't see why those
return values could not be changed!
Can I also say that having used LISP, Scheme and Erlang I think that Neko
has loads of the good bits from all of them. Neko delights me more and more
every day as I play with things I read between the lines, for instance,
strings. It is not overly obvious from anything other than the BNF that you
can declare a string variable like so :-
var myString =
"this is line1
line2
another line here dude :)
the End!";
Neko continues to suprise and delight on a daily basis.
Awesome piece of work Nicolas!
:)
--
Neko : One VM to run them all
(http://nekovm.org)