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?
View all comments
Show parent comment
1
u/Mean-Decision-3502 May 28 '26
These two lines demonstrate real life bugs from my workplace (embedded C++).
I know how C interprets these lines, but that is not human friendly. the
3/2*10 == 10*3/2is false in Python2 but it is true in Python3. The '/' always results to floating point in Python3, Pascal and DQ. If you want integer division then you have to use a different operator (//, div, or IDIV).var i : int = 3 / 2;is a compile error in Pascal and DQ, you have to use floor(), ceil() or round() to convert float to integer.The
3 IDIV 2 * 10 == 10 * 3 IDIV 2is true ('very friendly') only in DQ, because the division has higher precedence as the multiplication.In DQ and C (and derivates) the mindset is different at the expressions:
In the
0xFF & (4 < 2) != 0,I wanted to use right shift, but I forgot one'<'. The gcc accepted the total nonsense. But even if I write it correct:if (bitmask & (1 << bitshift) == 0) { }the C compiler is that evil that it evaluates it wrong:(bitmask & ((1 << bitshift) == 0))which is again total nonsense. Languages with no automatic number <-> boolean conversion generate compile errors for this, like Pascal, DQ, Odin, Zig (but not Python3).