[personal profile] tangaroa

I was experimenting with Nim earlier this year.

Useful links:

Using nimrod 0.9.2 since apparently freebsd has not caught up to the name change.

Nim's ability to compile to C provides an easy way to tell how the language features are implemented without having to open up the compiler source code and try to follow through.

While loop

nimc
while doStuff(limit : int) = 
  var i:int = 0; 
  while (i < limit):
    i = i + 1
N_NIMCALL(void, dostuff_67002)(NI limit) {
        NI i;
        nimfr("doStuff", "nim1.nim")  # Frame name, for debugging
        nimln(4, "nim1.nim");  # Line number and source file
        i = 0;
        nimln(5, "nim1.nim");
        while (1) {
                NI TMP120;
                nimln(5, "nim1.nim");
                if (!(i < limit)) goto LA1;
                nimln(6, "nim1.nim");
                nimln(6, "nim1.nim");
                TMP120 = addInt(i, 1);
                i = (NI64)(TMP120);
        } LA1: ;
        popFrame();
}

Most of the lines are functions, probably macros, that define the frame ID and line number. They do not affect the program logic.

while and for loops are both implemented as while loops internally, which makes me question the documentation that implies certain things can only be done in for loops. Loops are implemented as while(1) loops with the condition on a separate line, and a goto is used to exit the loop.

Iterator (without yield)

There is no purpose to making an iterator that does not yield. I just wanted to see what the C would look like. Lessons learned:

  • The iterator is implemented inline with its calling code. No frame is created for it.
  • The iterator code is repeated (copied and pasted) if the iterator is called from more than one location.
  • The iterator is optimized out of the program if it is not called.
  • The calling code inside the for loop is optimized out if the iterator does not yield anything.
nimc
iterator doStuff(limit : int) : int = 
  var i:int = 0; 
  while (i < limit):
    i = i + 1
    
for i in doStuff(2): 
  echo("Hello world " & $i) # never called
while (1) {
        NI TMP120;
        nimln(5, "nim1.nim");
        if (!(i_67014 < 2)) goto LA1; # 2 is passed into "limit"
        nimln(6, "nim1.nim");
        nimln(6, "nim1.nim");
        TMP120 = addInt(i_67014, 1);
        i_67014 = (NI64)(TMP120);
} LA1: ;

Iterator (yielding)

nimc
iterator doStuff(limit : int) : int = 
  var i:int = 0; 
  while (i < limit):
    i = i + 1
    yield i

for i in doStuff(2): 
  echo("Hello world " & $i)
while (1) {
        NI TMP120;
        NimStringDesc* LOC2;
        NimStringDesc* LOC3;
        nimln(5, "nim1.nim");
        if (!(i_67014 < 2)) goto LA1; # 2 is passed into "limit"
        nimln(6, "nim1.nim");
        nimln(6, "nim1.nim");
        TMP120 = addInt(i_67014, 1);
        i_67014 = (NI64)(TMP120);
        nimln(4, "nim1.nim");
        i_67012 = i_67014; # Return value? 
        nimln(11, "nim1.nim"); # calling code is inlined inside the loop
        nimln(11, "nim1.nim");
        LOC2 = 0;
        nimln(11, "nim1.nim");
        LOC3 = 0;
        LOC3 = nimIntToStr(i_67012);
        LOC2 = rawNewString(LOC3->Sup.len + 12);
appendString(LOC2, ((NimStringDesc*) &TMP121));
appendString(LOC2, LOC3);
        printf("%s\012", (LOC2)->data);
} LA1: ;

Let's see how it handles parallel processing.

nim2.nim(1, 17) Error: cannot open 'threadpool'

That's where my notes end. Attempting to rebuild the code now produces the error:

nim2.nim(12, 22) Error: 'spawn'ed function cannot have a 'var' parameter

Are you kidding me? From a chat log "var for non-parameters means mutable, while it also mean by-reference for parameters." So spawn takes constants only? That makes no sense. Maybe Nim's definition of mutable is different.

From another conversation:

spawn is not createThread. createThread really creates a thread and as such will guarantee it will return. spawn simply waits for a thread in the thread pool to become available for the task.

createThread is not a drop-in replacement for spawn. I will need to look up some examples.

From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.
Page generated Apr. 30th, 2017 03:10 am
Powered by Dreamwidth Studios