by hzhou321 · 8y ago · 0 comments
I tried creating my own languages before, some 15 years ago. While I had fun writing compilers, I missed a lot more writing actual programs. One day I suddenly realized I don't really need new language, what I needed is simply context dependent syntax and code block organization, which can easily implemented on the spot. It become MyDef and I have been programing with it entirely for more than 10 years now and wondering how others don't go down the same path.
Actually in a broad sense, Knuth's literate programing adventure is very similar to mine. He also wondered how others were not as excited as he was.
So rather than a new language, try a new dimension.
Here is a short example:
page: test, basic_frame
module: c
$local int A[3]
$for i=0:3
A[i] = i
$dump A[0], A[1], A[2]
Despite the look, if you know c, you recognize it as c. And as you get familiar, you see it exactly as C. `basic_frame` is merely a custom template so one doesn't have to always write the main function signature. $for is shorthand for `for-loop`, `$dump` is simply printf with simple type translations.Now that you had this meta layer, there is no limit on how you want to dress up your code. Examples:
$local A[3]: int
$sumcode(3) A[i]=i+3
$foreach A
$dump $(t)
or page: test, basic_frame
$call declare
$call init
$call report
subcode: declare
$local pn_A[3] = {1, 2, 3}
subcode: init
$foreach pn_A
$(t) = $(t)^2
subcode: report
$(set:print_to=stderr)
$foreach pn_A
$print "%d: %d", $(i), $(t)
There are lots of idioms and conventions, but it is not a problem as long as you recognize them only as clothing.Lastly, when dressing up the code makes little sense or you simply don't know better way of dressing them, you can simply write vanila C (or any languages that you are writing). MyDef never tried to compile your language.