Project 11>Strings-- calling string constructor

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Project 11>Strings-- calling string constructor

kraftwerk1611
Hi,

I have a question about the right way to call String.new() constructor.

The book says on page 235 that it should be like String.new(length).
Does it translate to following vm code

push length
call String.new 1

And does '1' above represent the 'this' ?
Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

cadet1620
Administrator
This post was updated on .
kraftwerk1611 wrote
I have a question about the right way to call String.new() constructor.

The book says on page 235 that it should be like String.new(length).
Does it translate to following vm code

push length
call String.new 1

And does '1' above represent the 'this' ?
There is no 'this' involved in the call to a constructor.  The '1' is the number of parameters

Creating the string constant is the same as creating any other String object.
do Output.printString("abc");
is functionally equivalent to
let str = String.new(3);
do str.appendChar(97);
do str.appendChar(98);
do str.appendChar(99);
do Output.printString(str);
The confusing bit is that because the compiler requires that appendChar() return the String that was passed to it, the compiler can shortcut popping the return value and pushing 'str' for the next call when it is creating a String constant.
                            // stack after command
push constant 3             // 3
call String.new 1           // str
push constant 97            // 97   str
call String.append 2        // str
push constant 98            // 98   str
call String.append 2        // str
push constant 99            // 99   str
call String.append 2        // str
call Output.printString 1

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

kraftwerk1611
Thank you for this explaining it.
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

froycard
this piece of code doesn't work...
""
push 3                      // 3
call String.new 1           // str
push 97                     // 97   str
call String.append 1        // str
push 98                     // 98   str
call String.append 1        // str
push 99                     // 99   str
call String.append 1        // str
call Output.printString 1
""
PUSH 3 is a invalid VM command, since 3 is a integer constant, you have to tell me VM Emulator where it's that 3 comes from, and it is from the constant segment, thus the correct way is:

push constant 3

*** this applies to the rest of the code where a integer constant is found
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

cadet1620
Administrator
Your good eyes, my lazy typing...

I edited the post.
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

froycard
Thanks for your answer anyway...
but the VM emulator throws me an error on this line:


it seems Memory.alloc is the culprit and VM emulator shows this error:
"A built-in function tried to access memory outside the heap..."

Any hints or help would be appreciated... thanks
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

cadet1620
Administrator
I see that you have written a minimal test.vm program to experiment with constant allocation.

The problem with this is that when your String.new() calls Memory.alloc(), the heap has not yet been initialized.  There are two things that you can do.

The safest is to rename your test to Main.vm and wrap you code in a function
function Main.main 1
push constant 3
call String.new 1
push constant 97
call String.appendChar 2
push constant 98
call String.appendChar 2
push constant 99
call String.appendChar 2
call Output.printString 1
pop temp 0
push constant 0
return
[Note the "2" in the appendChar calls; yet another evil type in my hand written VM code.  This code is TESTED.  I'll update my post above...]

This way, Sys.init() gets called which initializes the rest of the OS then calls Main.main.

When you load a VM files that does not contain Main.main, the VM Emulator doesn't automatically call Sys.init.

The second option that should work is to call Memory.init() as the first thing in test.vm.  Warning you can't call Output.printString() because other OS classes that it depends on have not been initialized!
call Memory.init 0
push constant 3
call String.new 1
push constant 97
call String.appendChar 2
...

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

froycard
Awesome,,, now it works... I need to continue reading about this to finish project 11... thanks for your help mr Mark
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

froycard
In reply to this post by cadet1620
cadet1620 wrote
function Main.main 1
push constant 3
call String.new 1
push constant 97
call String.appendChar 2
push constant 98
call String.appendChar 2
push constant 99
call String.appendChar 2
call Output.printString 1
pop temp 0
push constant 0
return
[Note the "2" in the appendChar calls; yet another evil type in my hand written VM code.  This code is TESTED.  I'll update my post above...]

...


--Mark
I have a doubt, why did you use 2 for calling appendChar? 2 is not the number of arguments you have to pass to the subroutine: appendChar, and it seems it only takes one argument: the char.
[excuse me, but I am new taking this course]
and thanks beforehand

Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

cadet1620
Administrator
froycard wrote
cadet1620 wrote
function Main.main 1
push constant 3
call String.new 1
push constant 97
call String.appendChar 2
...
I have a doubt, why did you use 2 for calling appendChar? 2 is not the number of arguments you have to pass to the subroutine: appendChar, and it seems it only takes one argument: the char.
[excuse me, but I am new taking this course]
and thanks beforehand
    var String a_string;
    let a_string = String.new(3);     // constructor call -- no this -- only 1 argument (3)
    do a_string.appendChar(97);       // method call -- requires this -- 2 arguments (a_string's address, 97)
appendChar is a method in object String. Therefore, the compiler always pushes the object's pointer (the method's 'this') as the first argument and the 97 as the second argument.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Project 11>Strings-- calling string constructor

froycard

Yeah, you are right! I forgot about it... geezzz
Thanks again