Ошибка «Недостаточно пространства сегмента» в VMEmulator, вызванная получателем mwthod в Jack - PullRequest
0 голосов
/ 29 апреля 2020

Я делаю проект для nand2tetris. Мы пишем программу на Джеке и тестируем ее на VMEmulator. Класс выглядит так:

class List {
    field int data;
    field List next;

    /* Creates a new List object. */
    constructor List new(int car, List cdr) {
        let data = car;
        let next = cdr;
        return this;
    }

    /* Disposes this List by recursively disposing its tail. */
    method void dispose() {
        if (~(next = null)) {
            do next.dispose();
        }
        // Use an OS routine to recycle the memory held by this object.
        do Memory.deAlloc(this);
        return;
    }

    /* Prints the list*/

    method void print() {
        do Output.printString(" -> ");
        do Output.printInt(data);
        if (~(next =  null)) {
            do next.print();
        }
        return;
    }

    /* Inserts the argument in the right position of the list (ascending order)*/
    method void insertInOrder(int ins){
        var List prev, curr, insert;
        let prev = this;
        let curr = prev.getnext();
        while (ins > prev.getdata()){
            if (ins < curr.getdata()){
                let insert = List.new(ins, curr);
                do prev.setnext(insert);
            }
            else{
                let prev = prev.getnext();
                let curr = prev.getnext();
            }
        }
        return;
    }

    /* Searches the argument in the list, if found, it returns the corresponding List object*/
    method List find(int toFind){
        var List temp;
        var List equal;
      var boolean found;
        let temp = this;
        let found = false;
        while (~(next = null)){
            if(toFind = temp.getdata()){
                let equal = temp;
                let found = true;
            }
            let temp = temp.getnext();
        }
        if (found){
            return equal;
        }
        else{
            return null;
        }
  }

    method List getnext(){
        return next;
    }

    method void setnext(List object){
        let next = object;
        return;
    }

    method int getdata(){
        return data;
    }

}

Он имеет одну личную переменную data и указатель next. Поэтому я написал метод getter и setter для возврата этих значений. Другие методы хороши, только метод getdata() неверен. Когда он проходит через VMEmulator, он показывает ошибку Out of segment space in List.getdata.3. Это показывает в VMEmulator.

0function    List.getdata0
1push        argument0
2pop         pointer0
3push        this 0
4return

ошибка в 4-й строке return. Когда я меняю код Джека, та же самая ошибка все еще на 4-й строке.

В чем проблема в моем методе получения?

...