Damian Walker

Personal Web Pages

Program Syntax

Thursday, 24th November 2016

I haven't yet settled on a name for my programming language, but I know what its programs will look like. I learned to program in BASIC, and I'm most comfortable with its syntax. Not the ancient form with line numbers and GOTO, but more the structured BASICs like QuickBASIC and OPL. The classic recursive factorial program will look something like this:

REM -- recursive factorial function
PROCEDURE factorial (n AS INTEGER) AS INTEGER
  IF n <= 1
    RETURN n
  END IF
  RETURN n * factorial (n - 1)
END PROCEDURE

REM -- main program
DECLARE i AS INTEGER
DECLARE f AS INTEGER
PRINT "Enter a number: "
INPUT i
LET f = factorial (i)
PRINT "Its factorial is ", f, ".", EOL

I like the idea of having no line terminator, but I also like the idea of being able to split long lines. So I've settled on the idea of making all statements start with a keyword. The LET keyword in assignments is mandatory, as would a CALL keyword if a procedure is called as a statement instead of a function. This means that the compiler will be able to tell if a line is a continuation line by the lack of an initial keyword.

I've recently ordered the book Modern Compiler Design by Grune et al. While I'm waiting for it to arrive, I'm starting to formalise the grammar using my distant memory of a compiler design assignment I did at university about twenty years ago. Doubtless the book will arrive and tell me that I'm doing it all wrong.

Post edited 15 Aug 2019 to format the code properly, now that I can.