untrusted icon indicating copy to clipboard operation
untrusted copied to clipboard

Line number error [SPOILER]

Open afluriach opened this issue 10 years ago • 8 comments

I'm getting syntax errors, but the error doesn't make sense for the given line number, and I'm pretty sure it is not a syntax error.

bug

afluriach avatar Aug 13 '14 16:08 afluriach

Here is a similar error:

dfs-error

afluriach avatar Aug 13 '14 16:08 afluriach

In fact, I'm pretty sure this solution is valid Javascript. Not sure why it is generating so many parse errors. Also, the parse errors seem inconsistent. Also, I did this solution to run once, but it appears to crash on me.move(). (difficult to debug though)

function Point(x,y)
{
    this.x = x;
    this.y = y;

    this.adj = function(dir)
    {
        switch(dir)
        {
            case 'left': return new Point(this.x-1, this.y);
            case 'right': return new Point(this.x+1, this.y);
            case 'up': return new Point(this.x, this.y-1);
            case 'down': return new Point(this.x, this.y+1);
        }
    };

    //get an integer that will uniquely represent this point
    this.hash = function()
    {
        return this.x + this.y*map.getWidth();
    };
}

function reverseDir(dir)
{
    switch(dir)
    {
        case 'left': return 'right';
        case 'right': return 'left';
        case 'up': return 'down';
        case 'down': return 'up';
    }
}

dirs = ['right', 'down', 'left', 'up'];

//the current best cost to get to this tile, and the direction we moved to get to it
function Tile(x,y,cost,fromDir)
{
    this.x = x;
    this.y = y;
    this.cost = cost;
    this.fromDir = fromDir;

    this.getPoint = function()
    {
        return new Point(this.x, this.y);
    };

    this.isAtPoint = function(point)
    {
        return this.x === point.x && this.y === point.y;
    };

    this.hash = function()
    {
        return this.x + this.y*map.getWidth();    
    };
}

var startTile = new Tile(1,1,0,'none');
var destPoint = new Point(map.getWidth() - 2, 8);
tiles = {};

tiles[startTile.hash()] = startTile;

function dfs(tile, dest)
{
    if(tile.hash() === dest.hash()) return;

    var crntCost = tile.cost;

    //update adjacent tiles as applicable and dfs on them
    for(var dirNum = 0; dirNum < dirs.length; ++dirNum)
    {
        var adjPoint = tile.getPoint().adj(dirs[dirNum]);
        var adjHash = adjPoint.hash();

        //if the tile is visitable and within map range
        //if the tile has not already been visited or the cost is lower
        var occupiable = map.getObjectTypeAt(adjPoint.x, adjPoint.y) !== 'block';
        var withinMapBounds = adjPoint.x >= 0 &&
                              adjPoint.x < map.getWidth() &&
                              adjPoint.y >= 0 &&
                              adjPoint.y < map.getHeight();
        var unvisitedOrBetter = !(adjHash in tiles) || tiles[adjHash].cost > crntCost + 1;
        if( occupiable && withinMapBounds && unvisitedOrBetter)
        {
            tiles[adjHash] = new Tile(adjPoint.x, adjPoint.y, crntCost+1, dirs[dirNum]);
            dfs(tiles[adjHash], dest);
        }
    }
}

//prepare an array of move instructions (move direction string). move backward from destination until we get to start.

//the node adjacent to the start node, cost 1, will represent the first move instruction. thus we know the dest nodes move instruction will be move instruction cost-1 and count down from there

function buildMoveList(destPoint)
{
    var crntTile = tiles[destPoint.hash()];
    var movelist = [];

    for(var inst = crntTile.cost -1; inst >= 0; --inst)
    {
        movelist[inst] = crntTile.fromDir;

        //move backwards
        crntTile = tiles[crntTile.getPoint.adj(reverseDir(crntTile.fromDir)).hash()];
    }

    return movelist;
}

//on first move:
//run DFS
//build move list

if(typeof moveInstList === 'undefined')
{
    dfs(startTile, destPoint);
    moveInstList = buildMoveList(destPoint);
    nextMoveInst = 0;
}

//iterate through move list
//then move down

if(nextMoveInst < moveInstList.length)
{
    me.move(moveInstList[nextMoveInst]);
}
else
{
    me.move('down');
}

afluriach avatar Aug 13 '14 16:08 afluriach

I'm taking a look at this now, but it may take some time to get to the bottom of this.

AlexNisnevich avatar Aug 14 '14 09:08 AlexNisnevich

It might be due to wrongly used curly brackets in new line. in functions, if's, for's and so on.

function name() {

not

function name() 
{

As this can make errors with silent semicolons (or however its called);

Setitch avatar Aug 14 '14 13:08 Setitch

From my understanding, Automatic Semicolon Insertion shouldn't occur during function, if, or for statements.

for (true) {

should evaluate the same as

for (true)
{

If he was doing this with do-while, continue, break, return, or throw statements, your case would be more valid.

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

Source

r2vq avatar Aug 14 '14 19:08 r2vq

I know i had several problems with it, also with different browsers - from that time, i always use proper {} in this language so no errors of this kind would ever happend to me. And i also think, that if you can fix something, you should - then error you had can (at least) point to its proper place not some random ones.

Setitch avatar Aug 15 '14 06:08 Setitch

Placing the brace on a new line is called Allman Style and is a valid style of indenting as long as you are consistent and avoid ASI issues like I mentioned above.

Personally, I prefer OTBS but using Allman style should not create a syntax error.

More info on the "holy war" here.

r2vq avatar Aug 15 '14 17:08 r2vq

Your code is valid JavaScript, but it doesn't adhere to Untrusted's 80-character-per-line limit, so some lines get split up when you paste it into the game. This seems to be the cause of most of your problems.

For example, the [Line 62] error is actually caused by invalid syntax on line 67:

The line numbers in the errors are sometimes wrong, which is a problem. However, I'm not sure if there's a good way to fix this, because we're currently getting these line numbers in an extremely fragile way (since JavaScript itself does not provide line numbers for exceptions that occur when you eval a multi-line string).

AlexNisnevich avatar Aug 19 '14 00:08 AlexNisnevich