The "base language" is nearly done and I'm soon to begin work on the "metaprogramming part" of the language soon. As a result, I would like to "finalize" the general declaration syntax for this language.
For this syntax, I had a few main "goals" however, I've come to realize that having all of them is most likely impossible. The goals being:
(1) A grammar that is fast to parse (i.e. LALR(1), LL(1) or at least very small lookahead)
(2) Obvious declaration kind (no need to determine kind during semantic stage)
(3) Consistent syntax between kinds of declarations
(4) Consistent syntax between procedures, lambdas, and procedure types
(5) Minimal amount of "keywords" and "operators"
(6) Not fucking LISP
Initially I had been using a Jai-like syntax for Odin until very recently. Its syntax does have problems such as inconsistencies in declarations and its restrictiveness compared to other syntax families. So I've been experimenting with a Pascal/Oberon/Go/Rust/Swift/etc. like syntax which uses "prefixes" for declarations, which I'll call Prefix-like.
n.b. Most "modern" language seem to use the Prefix-like style but that does necessarily mean it's good.
Jai-like syntax fails on (3), why Prefix-like fails on (4), if both adhere to (1) and (2):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | // Declaration ordering
// Variable
// Variable Inferred Type
// Multiple Variables
// Constant
// Constant Inferred Type
// Type
// Procedure
// Jai-like
foo: int = 123;
foo := 123;
a, b, c: int;
bar: int : 123;
bar :: 123;
blah :: type int; // `type` could be removes if it is a record type which has a keyword prefix
doop :: proc() {}
// Prefix-like
var foo int = 123;
var foo = 123;
var a, b, c int;
const bar int = 123;
const bar = 123;
type blah int;
proc doop() {}
|
As shown above, Jai-like syntax is highly irregular when it comes to declarations whilst, prefix-like is very consistent with the prefix-keyword describing the declaration kind. Prefix-like also allows the ability add different kinds of declarations with ease, e.g. `let`, `import`, `macro`. Jai-like could do the same but it would not be consistent.
Another advantage Prefix-like syntax is the ability to do grouped declarations (Pascal/Go):
1
2
3
4
5
6
7
8
9
10
11
12
13 | var x int;
var {
a int;
b f32;
c string;
}
import "sys/windows.odin";
import {
"atomic.odin";
"fmt.odin";
"hash.odin";
"math.odin";
}
|
and from Go, the ability to have "naturally" occurring enumerations:
| // Go code
type Food int
const (
FOOD_APPLE Food = iota // 0
FOOD_BANANA // 1
FOOD_CABBAGE // 2
FOOD_DRAGONFRUIT // 3
)
|
This is one of my favourite features that occurs from the prefix-like syntax.
n.b. For more information about how Go's constant system and enumerations work, I recommend these webpages:
*
https://blog.golang.org/constants
*
https://splice.com/blog/iota-elegant-constants-golang/
Enumerations are still possible in Jai-like syntax but only through the addition of a `enum` type. This `enum` type can have the added bonus that you can namespace the values to that type (Fruit.APPLE), have extra type information, and string values for debugging purposes.
One of the nice advantages of Jai-like is that (4) can be adhered to:
| // Jai-like
foo :: proc(x: int) {} // procedure declaration
foo := proc(x: int) {}; // named-lambda declaration
proc(x: int) // procedure type
// Prefix-like
proc foo(x int) {} // procedure declaration
var foo = proc(x int) {}; // named-lambda declaration
proc(x int) // procedure type
|
Another "advantage" that I personally find pleasing to the eye about Jai-like is that the "entity's name" is first.
---
There are more things do discuss between these two syntax "families" however, I don't want this post to be enormous :P
I would like to know your opinions about the subject and if there are any other good declaration syntaxes out there that may be a better fit for this language or even ways to improve Jai-like. For the mean time, I will stick with the recently introduced prefix-style until I finally decide upon the best syntax for this language -- I don't want to bike-shed too much ;)
Regards,
Bill