kuroko-lang / kuroko

Dialect of Python with explicit variable declaration and block scoping, with a lightweight and easy-to-embed bytecode compiler and interpreter.
https://kuroko-lang.github.io/
MIT License
431 stars 25 forks source link

print won't print without an argument #22

Closed notwa closed 2 years ago

notwa commented 2 years ago
print()  # prints newline in python, nothing in kuroko
print("")  # prints in both

# also...
print("hello", end=" ")
print(end="world")  # this only works in python
print()
print("hello", end=" ")
print("", end="world")  # this works in both

the case with print(end="blah") not printing anything had me especially confused when I was porting a script, as I have a (bad?) habit of using that form.

this is with Kuroko 1.2.3 (commit 650f324) and a tiny patch to build with MSYS2 (MSYSTEM=MSYS).

klange commented 2 years ago

Ah, looks like a pretty straightforward case of just completely neglecting argc == 0... I could have sworn this was being handled at some point, but likely before print become a normal function.

diff --git a/src/builtins.c b/src/builtins.c
index ba8a711..ef0f46e 100644
--- a/src/builtins.c
+++ b/src/builtins.c
@@ -512,6 +512,11 @@ KRK_FUNC(print,{
            endLen = AS_STRING(endVal)->length;
        }
    }
+   if (!argc) {
+       for (size_t j = 0; j < endLen; ++j) {
+           fputc(end[j], stdout);
+       }
+   }
    for (int i = 0; i < argc; ++i) {
        KrkValue printable = argv[i];
        if (IS_STRING(printable)) { /* krk_printValue runs repr */

As for the MSYS patch, I will take a look, but I suspect a more direct fix would be to change the bit in the Makefile that's checking for mingw so that MSYS takes the same path. Native Windows builds without cygwin or the like should work well.

notwa commented 2 years ago

ah, I wasn't suggesting the patch so much as I was including it for reproducibility.

taking the other branch in the Makefile almost works; the pthread library is named differently here:

-WINLIBS= -l:libwinpthread.a
+WINLIBS= -l:libpthread.a

and gcc still warns about ignoring the "hidden" visibility attribute. edit: also, setenv is undefined for MSYSTEM=MINGW64. is this supposed to be ifndef _WIN32?

also, I have a few other small issues to report relating to built-ins. would you prefer if I made individual github issues like this, or something else?

klange commented 2 years ago

is this supposed to be ifndef _WIN32?

No, that's for Sortix, which explicitly does not have putenv, though I suppose what I was trying to do does not work as I had intended... (I mistakenly believed it would exist as a macro... lots of stuff in libc is actually defined as "this should exist as a macro", but...)

klange commented 2 years ago

also, I have a few other small issues to report relating to built-ins. would you prefer if I made individual github issues like this, or something else?

Let's do a separate issue for each function.

klange commented 2 years ago

Closing as fixed in 21b459919b98c084a9ab44da4ebc6ad99d6e09dd