defano / wyldcard

A clone of Apple's HyperCard and HyperTalk scripting language.
https://github.com/defano/wyldcard/wiki
MIT License
122 stars 11 forks source link

Added command "addThree" not working - but not flagged as wrong either #72

Closed Jimw338 closed 5 years ago

Jimw338 commented 5 years ago

I tried adding an "AddThree" command which is like add, but takes 4 parameters (3 "expressions" and 1 "container"). I modified the grammar (in both the the "commandStmnt" and "commandName" classes), the HyperTalkTreeVisitor file, and added a AddThreeCmd.java file. Right now just copies the function of add, but ignores the second and third parameters.

The grammar addition is:

    | 'addThree' expression ',' expression ',' expression ',' expression        # addThreeCmdStmnt

The addition to HyperTalkTreeVisitor is:

    public Object visitAddThreeCmdStmnt(HyperTalkParser.AddThreeCmdStmntContext ctx) {
        return new AddThreeCmd(ctx,
                               (Expression) visit(ctx.expression(0)),
                               (Expression) visit(ctx.expression(1)),
                               (Expression) visit(ctx.expression(2)),
                               (Expression) visit(ctx.expression(3))
        );
    }

It doesn't work, but doesn't complain that it's wrong either. The syntax coloring also doesn't work. It acts like it just skips over the line. The test script I have:

on mouseup
    put 4 into a
    put 5 into b
    put 7 into c
    put 8 into d
    add a to d -- d will equal 12
    put d -- puts 12
    wait 1 second
    addThree a, b, c, d -- d should equal (a+d) = (4+12) = 16
    put d -- still gives 12 - "addThree" didn't execute
end mouseup

Is there something in the generated grammar or another "list of commands" that would cause it to be skipped in both the SyntaxParser to "ignore" the statement entirely?

Attached is my zip file of the project (I really need to look how to make commits).

wyldcard-master-unworking-addThreeCmd.zip

defano commented 5 years ago

Looks like you did everything right, but got bit by a poorly documented side effect of my technique for making the language case insensitive.

In your grammar, you have a lexer rule that matches a camel-case string:

| 'addThree' expression ',' expression ',' expression ',' expression

Unfortunately, in order to support case insensitive parsing, I use a special input stream that works only if all the tokens in the language are specified in lowercase. See: com.defano.wyldcard.runtime.interpreter.CaseInsensitiveInputStream.

Here's what I think is happening: When you execute addThree a, b, c, d in your mouseUp handler, addThree is not not matching your Antlr rule because of the way CaseInsensitiveInputStream works. Instead, that statement is being interpreted as a HyperTalk message with four arguments, equivalent to saying send "addThree a, b, c, d". I bet if you added an on addThree handler to your script, you'd see it fire.

Bottom line: Change your grammar rule to look like this and try it again:

| 'addthree' expression ',' expression ',' expression ',' expression
Jimw338 commented 5 years ago

That worked!