I am designing a new programming language.
I am designing a syntax to implement queries. Currently this is how it works.
The database is accessible through entity object.
on this sample I have “entity.manufacturers” and “entity.products” table
To get manufacturer with an ID of 1:
var @manufacturer = entity.manufacturers where id = 1;
To query all products for @manufacturer:
// Get all manufacturers products
var @products = entity.products under @manufacturer;
// No need to specify what links both since system already knows how entity is related
Currently I am using “under” as a keyword to get all records for @manufacturer. I want it to make sense by just reading the code.
If you can give suggestions on a better syntax or keywords on how to implement this that will be awesome.
7
Designing good concrete syntax is ridiculously hard. And unfortunately, while the best syntax in the world can’t save a lame language, bad syntax can (has) doom(ed) good languages.
If this is a toy language, “full speed ahead” say I! But if you want to create a practical language for people to use, I have the following observations based on my personal experiences. YMMV:
- focus on the semantics and abstract syntax of your language. This is the core of a language, and how people will learn and understand it. Figure out how elements of the language can be defined, combined, and abstracted. Absolutely do not let concrete syntax dictate the semantics or abstract syntax of your language.
- keywords, braces, semicolons — these are all totally irrelevant as far as abstract syntax and semantics are concerned. There can be multiple concrete syntaxes for the exact same language.
- implementing features as libraries is usually preferable to implementing them as syntax
- syntax is hard to fix
- syntax is hard to extend
- syntax is hard to compose
- syntax is not accessible at run-time
So my answer to your question is: design your abstract syntax and semantics first. Without seeing those, it’s very hard to come up with a good concrete syntax. Once you have those, a concrete syntax should be much easier to derive.
Or you could just go with Lisp. 😉
Also, since you mentioned I want it to make sense by just reading the code
, I should point out that designing a syntax that reads like plain English, yet is also simple, unambiguous and powerful has never been done (to my knowledge). I do not believe this is possible (although I hope I am wrong).
2
I don’t think under
is the right word here as it makes to many assumptions about the nature of the relationship between two entities, when all you really need to indicate is fact that they are related. In many entity relationship, one entity may be thought of as under another, but this is certainly not the case for all entity relationships.
I’d use the word by
instead, as in
var @products = entity.products by @manufacturer;
Or on
, as in
var @products = entity.products on @manufacturer;
Since it hints that you are creating a simplified join syntax.
1