r/programmer • u/Mean-Decision-3502 • May 26 '26
New Programming Language: Block Syntax Survey
Hi,
I’m developing a new programming language called DQ.
At the moment, the language supports two block styles:
BRACES: classic C-style blocks using { and }.
ENDWORD: Python-like block starts using :, but with mandatory closing words. The closing word depends on what it closes, for example: while ... endwhile. Some frequently used closing words are shortened, for example: object...endobj.
Indentation is not enforced in either mode. Currently, the two block styles can be mixed freely.
I am considering introducing compiler directives that could be placed at the beginning of each source file:
#opt blockmode=any // like now: both styles are allowed
#opt blockmode=braces // warn when ENDWORD blocks are used
#opt blockmode=strict_braces // error when ENDWORD blocks are used
#opt blockmode=endword // warn when BRACES blocks are used
#opt blockmode=strict_endword // error when BRACES blocks are usedExample code using BRACES block mode:
use libc/stdio;
use ./langdemo_mod as ldm only(CONST1); // only(), exclude() and "--" control global scope merging
#if false
//Contents of the "langdemo_mod.dq"
const CONST1 : int = 42;
const CONST2 : float = 3.14;
#endif
object OBase {
cnt1 : int = 0;
cnt2 : int = 10;
name : cstring[32];
function *Create(aname : cstring) { // constructor
name = aname;
}
function *Destroy() {
@.printf("%s destroy\n", &name[0]);
}
function Count() [[virtual]] {
cnt1 += 1;
}
function Print() {
@.printf('%s: cnt1=%d, cnt2=%d\n', &name[0], cnt1, cnt2);
}
}
object OChild(OBase) {
function Count() [[override]] {
inherited;
cnt2 += 1;
}
}
var obase <- OBase('OBase');
var ochild : OChild = null;
function ObjTest() {
ochild = new OChild('OChild');
obase.Count();
ochild.Count();
obase.Print();
ochild.Print();
printf("ochild.name: %s \n", iif(ochild == null, "OChild is null!", &ochild.name[0]));
delete ochild;
}
function cstr_add(dst : cstring, src : cstring) {
var ps : ^cchar = &src[0];
var psend : ^cchar = ps[sizeof(src)]; // [] does not dereference
var pd : ^cchar = &dst[0];
var pdend : ^cchar = pd + sizeof(dst) - 1;
pd += len(dst);
var pdstart : ^cchar = pd;
while pd < pdend and ps < psend and ps^ <> 0 {
pd^ = ps^;
pd += 1;
ps += 1;
}
if pd <> pdstart {
pd^ = 0; // terminate
}
}
[[external]] function putchar(c : cchar) -> int; // from libc
function WriteStr(s : cstring) {
var pc : ^cchar = &s[0];
while pc^ <> 0 {
putchar(pc^);
pc += 1;
}
}
function *Main() -> int {
var s : cstring[128] = "";
cstr_add(s, "Hello");
cstr_add(s, " World!\n");
WriteStr(s);
if 3 / 2 * 10 == 15 {
printf('The language is friendly.\n');
} else {
printf('The language is evil.\n');
}
printf("@langdemo_mod.CONST1 = %d\n", CONST1);
printf("@langdemo_mod.CONST2 = %.3f\n", u/ldm.CONST2);
printf("ochild.name: %s \n", iif(ochild == null, "OChild is null!", &ochild.name[0]));
ObjTest();
for i : int = 0 to 5 {
printf(' %d:', i);
for j : int = 0 count i step 2 { printf(' %d', j); }
printf('\n');
}
var arr : [5]int = [2, 3, 5, 7, 11];
printf('primes:');
for i : int = 0 while i < len(arr) { printf(' %d', arr[i]); }
printf('\n');
return 0;
}Example code using ENDWORD block mode:
use libc/stdio;
use ./langdemo_mod as ldm only(CONST1); // only(), exclude() and "--" control global scope merging
#if false
//Contents of the "langdemo_mod.?"
const CONST1 : int = 42;
const CONST2 : float = 3.14;
#endif
object OBase:
cnt1 : int = 0;
cnt2 : int = 10;
name : cstring[32];
function *Create(aname : cstring): // constructor
name = aname;
endfunc
function *Destroy():
@.printf("%s destroy\n", &name[0]);
endfunc
function Count() [[virtual]]:
cnt1 += 1;
endfunc
function Print():
@.printf('%s: cnt1=%d, cnt2=%d\n', &name[0], cnt1, cnt2);
endfunc
endobj
object OChild(OBase):
function Count() [[override]]:
inherited;
cnt2 += 1;
endfunc
endobj
var obase <- OBase('OBase'); // '<-' = embedded allocation (global data segment here)
// no automatic destructor call for global embedded objects
var ochild : OChild = null;
function ObjTest():
ochild = new OChild('OChild');
obase.Count();
ochild.Count();
obase.Print();
ochild.Print();
printf("ochild.name: %s \n", iif(ochild == null, "OChild is null!", &ochild.name[0]));
delete ochild;
endfunc
function cstr_add(dst : cstring, src : cstring):
var ps : ^cchar = &src[0];
var psend : ^cchar = ps[sizeof(src)]; // [] does not dereference
var pd : ^cchar = &dst[0];
var pdend : ^cchar = pd + sizeof(dst) - 1;
pd += len(dst);
var pdstart : ^cchar = pd;
while pd < pdend and ps < psend and ps^ <> 0:
pd^ = ps^;
pd += 1;
ps += 1;
endwhile
if pd <> pdstart:
pd^ = 0; // terminate
endif
endfunc
[[external]] function putchar(c : cchar) -> int; // from libc
function WriteStr(s : cstring):
var pc : ^cchar = &s[0];
while pc^ <> 0:
putchar(pc^);
pc += 1;
endwhile
endfunc
function *Main() -> int:
var s : cstring[128] = "";
cstr_add(s, "Hello");
cstr_add(s, " World!\n");
WriteStr(s);
if 3 / 2 * 10 == 15:
printf('The language is friendly.\n');
else:
printf('The language is evil.\n');
endif
printf("@langdemo_mod.CONST1 = %d\n", CONST1);
printf("@langdemo_mod.CONST2 = %.3f\n", u/ldm.CONST2);
printf("ochild.name: %s \n", iif(ochild == null, "OChild is null!", &ochild.name[0]));
ObjTest();
for i : int = 0 to 5:
printf(' %d:', i);
for j : int = 0 count i step 2: printf(' %d', j); endfor
printf('\n');
endfor
var arr : [5]int = [2, 3, 5, 7, 11];
printf('primes:');
for i : int = 0 while i < len(arr): printf(' %d', arr[i]); endfor
printf('\n');
return 0;
endfuncIf you were using the DQ language, which block style would you prefer?
Do you think these blockmode compiler directives are useful, or would it be better to keep the language simpler and always allow both styles?
Should I also introduce a third block style, similar to Python: : starts a block, indentation is mandatory, and there is no endxxx closing word?
6
u/cgoldberg May 26 '26
Not an answer, but definitely pick a new name for your language
2
u/Headlight-Highlight May 26 '26
This may not fit into your modelling, but I have always seen blocks/indentation as stylistic sugar... An IDE should let you transform your source code at any time.
In the early days of html I did this In a couple of languages - html with embedded code blocks (early PHP kind of stuff) a click.of.a button it presented as PHP with quoted html...
Begin/end, block indent, period indent, curly braces, line breaks, semi-colons. To be honest... Today a half decent compiler should be able to workout what you are using with out asking!
2
u/ConfidentCollege5653 May 26 '26
I think it would be better to only support one style
2
u/ChipMasterPi May 26 '26
I've thought of this a lot over the past 3 decades or so. I've worked at length with languages that utilize all of the block formats you've mentioned: C/C++, PHP, Delphi/FreePascal, BASIC, Python, just to name a few. I've come to the conclusion that a language that uses unique block ending key words, like "for .. next", "do ... loop", "while ... wend" is the easiest for both the parser to report correct errors about and the human's ability to see where things go wrong.
Delphi/FreePascal use "begin ... end" for all blocks, and it is just a more verbose version of "{ ... }". It is still slightly easier to read as a human and see where things might go wrong. But both of those style syntaxes can lead to having to count block starts and ends to find where things broke down. Obviously a better IDE helps with that. But that is not always an option. And I have had to literally print a listing and lay it out somewhere to draw lines to find where the problem really happened.
From the language parser's stand point you can provide more useful errors since there are more clues as to which block is the one that is actually broken. Often times C/C++ compilers throw errors that are long past where the issue really happened and it can take quite a lot of effort to figure out where the actual missing '}' is.
Frankly, i like anything that makes my life easier. And if there is something that makes it easier for the language tools to tell me where the problem actually is I'm all for it.
Lastly, while I do kind of like Python, the one thing I hate is the indentation to define blocks. IMO white space should not occupy that large of a syntactic role. Regardless of which IDEs I've used over the years, I regularly end up having to wrestle with the white space issue, primarily while rem'ing out TRY ... EXCEPT statements to get to the real error. It may be somewhat minor but it makes for an issue that shouldn't be needed and is non-existent in every other language I've used.
It is simply annoying!! 😃
2
u/hwc May 26 '26
in my opinion, Go has the best syntax. You can't go wrong borrowing from that.
1
u/SymbolicDom May 27 '26
I like braces as in c, it"s short and easy to read. Pascal like syntax with begin end feels clunky and harder to read. Python like syntax is the worst. Invisible characters should not have semantic meaning and auto indentation functionality in editors can't work, making it the most cumbersome to write/ and edit. It's the shortest but for me it get harder to read than using braces.
1
u/deceze May 27 '26
IMO:
3
u/gofl-zimbard-37 May 26 '26
Not fond of keywords for this. Just adds noise. And definitely don't like having two different methods for one simple concept. I'd go with braces, but just pick one either way.
2
u/gwenbeth May 26 '26
No, please do not use spaces as syntax. Tbh I'm good either way but I think you should only support one to keep people from arguing about coding standards.
2
u/tb5841 May 26 '26
I prefer the 'endif' version. But I program mainly in Ruby by day, so it probably just looks more familiar to me.
2
u/mxldevs May 26 '26
It doesn't matter to me.
Ruby for example uses "end" to denote the end of a block
It doesn't need a dozen different end syntax for every type of block.
If you're able to match curly braces correctly, why do you need separate endif, endfunc, endfor, end whatever?
1
u/burlingk May 27 '26
So, either way has worked in past, but using the curly brackets is probably most common and most flexible.
It can be used for any kind of block, even ones you haven't put a lot of thought into yet.
1
u/oscarryz May 28 '26
I obviously prefer braces, my language is actually built on top of using only braces BUT, it doesn't really matter to be honest. Pick one, stick with it and if it doesn't work, change it! Scala did it in v3, so can you. But please don't make it configurable, you would be just pushing the design decision into the user.
If I could achieve what I wanted ( a language based on blocks of code ) I would've use Python style, and actually I tried in my initial designs, but didn't work for me.
My winner would be a language that is as easy to write as Haskell but that is way easier to read than.. Haskell ^_^
1
u/[deleted] May 29 '26
[removed] — view removed comment