Inform 6 compiler does not give error when article property uses single quotes

I recently struck an issue where Windows Frotz crashed partway through printing the contents of a container. Unfortunately, I didn’t take note of the actual error, but it was indicating that a routine did not exist or could not be run or something like that.

After much hair tearing, I realised that I had:
article 'some',
instead of:
article "some",
In other words, single quotes instead of double quotes. This is easy to miss amongst hundreds of lines of source code.

(For what it’s worth, this was one of those situations where the object was singular, so the pluralname attribute wasn’t appropriate, but the article was “some”.)

Is it possible for a future version of the compiler to detect this error at compilation time?

2 Likes

There’s no way for the compiler to detect this because there’s no way to declare what kind of value a property is supposed to hold. It’s legal for properties to hold dict words. In the standard library, only the name property contains dict words, but there are library extensions that define others.

In fact you could define a property which contains either a string or dict word, and check its value before using it to handle it properly. (This is not practical in Z-code, because Z-code dict word values aren’t distinguishable. Works fine in Glulx though.)

(Footnote: the compiler does know the type of the name property, because that one is hard-wired in the compiler. So you can write with name "lamp" and the compiler will figure out that you meant to use single quotes. All other properties are defined by the library, however.)

4 Likes

Thanks. I suspected that would be the case, but it never hurts to ask.

1 Like

I doo agree stronger typed inform would have been really handy sometimes. I’ve only recently started a Inform 6 project again and the times i’ve got numbers and strings and objects confused is astounding (doing nothing very complex). Something like Python/mypy optional type annotations would be awesome. Then again, this is the real retro experience :smile:

2 Likes

Optional type annotations are an interesting idea.

The compiler already does better type-checking (warnings, not errors) for operators like . and has. (E.g., for the expression (O has A), O should be an object and A should be an attribute.)

Doing the same type-checking for global variables and properties wouldn’t be hard. The hard part is extending the syntax for declaring these things with type annotations.

1 Like

Is there a formal definition of the syntax anywhere? If there is, you could do a pre-processing pass where it checks the syntax prior to compilation. I know the compilation does some of that anyway, but the pre-processor could detect the things that the compiler doesn’t currently detect.

Just a thought.

1 Like

The other fun part is that lots of library properties have an implicit type of “string or routine”.

Is nothing or 0 always permitted? Or can you declare that as part of the type? What about sentinel values like -1? (The library defines a special value NULL = -1 but the compiler doesn’t know about that.)

Lots of fun questions.

2 Likes

The only pre-processing pass is to pick up compiler options like !% FOO=1. That’s very simple parsing – it has no notion of the language syntax.

Writing a whole new language parser for the preprocessor is not in scope, as they say.

1 Like

It could be a separate tool though, like Typescript is for Javascript.

1 Like

My estimation is that writing a whole new language processor would be about 12.75 times as much work as adding type annotations to the existing compiler.

This number arrived at by the highly scientific method of looking at the problem and saying “you’ve got to be kidding me”.

(Didn’t vaporware or somebody start doing this once?)

1 Like

Aren’t there tools where you can feed it a text file and a formal syntax definition (defined in another file) and it parses the file and spits out any syntax errors?

1 Like

There were Typed Inform:

There’s also a language server for Inform 6, although last time I checked there were some bugs.

I suppose it would be possible to add non-obtrusive type annotations with special comments, like JSDoc, but it will have to be well thought out, as said above. Something like:

!! @type Number
Global score = 0;

I’m also currently writing an Inform 6 grammar to enable syntax highlighting in Borogove, because that’s what CodeMirror, the code editor it uses, wants. (No error checking, only highlighting) And it’s a pain: Inform 6’s syntax is ambiguous at place and it allows naming variables like keywords, amongst others. So my grammar will sometimes be approximate, as long the highlighting it gives is correct.

I believe Inform 6 works better with a hand-written parser than a grammar/syntax definition, but I might be wrong.

1 Like

It should be possible, but your original question was a semantic analysis, not a syntax one, so it wouldn’t solve preventing putting a dictionary word in the article property.

1 Like

Typed Inform was solving a different problem – it’s not what I’m thinking of here. Sorry, this is just a vague memory. Not important.

Inform 6’s syntax is ambiguous at place and it allows naming variables like keywords, amongst others.

Yep.

The proposal here is to let the library declare the article property with a type:

Property article [String] "a";

(That’s Python-style. The comment style you suggested would also work. Lots of other possibilities.)

Ignoring the question of how it’s spelled, this declaration would let the compiler type-check constant values in an article property

Object foo
  with article 'word';

Warning: expected String but found Dictionary word
2 Likes

It actually is a syntax issue. A dictionary word is defined using single quotes, but the article property requires a string, which is defined using double quotes.

1 Like

I disagree. Syntax-wise, it’s a valid program. It’s absolutely legal to put a dictionary word in a property (which happens to be named article here).

The compiler/analyser would have to know that the article property is supposed to hold a string, and that means we go into semantic analysis territory.

So if you feed the hypothetical tool (which actually exist, it’s the compiler) a formal syntax definition and your source, it won’t complain. (Except if you hardcode article to be a dict word in the syntax definition, but I don’t think it would be a good idea.)

That’s partly solved if types annotations are added to the compiler or something else, but i still think there’ll be a bit of semantic analysis.

Anyway.

1 Like

But it’s not a valid program. That’s the point. It crashed the interpreter.

1 Like

The difference between syntax and semantics is merely semantic, so let’s please not go too far down a rabbit hole. :)

1 Like

Syntax is spelling form. Semantic is diction usage.

As I’m sure you’re aware, 0 is considered a valid routine address in Z-code, and calling it yields a return value of 1 without actually executing any code or examining the contents of address 0. Is this the case in Glulx as well?

However, Inform doesn’t consider 0 to belong to the Routine class, so testing if it’s a routine doesn’t reveal that it can be called as one.

Of course, if you put a 0 as a placeholder for say a before routine, it won’t be called but will instead be considered the same as if the object didn’t provide the property at all.

1 Like