Posted to tcl by Stu at Tue Jun 04 14:23:35 GMT 2024view raw
- $cat README
- <?pragma elide on?>
- pTycl is a variation on Python syntax.
- Blocks:
- Braces are used to delimit multi-line blocks, like in C.
- Comments:
- Lines where the first non-whitespace characters are "//"
- are pTycl comments and will not appear in the output.
- Self:
- The character "$" will be replaced by "self" only in places
- where it is clear that "self" is what is wanted.
- Carets:
- The character "^" will be replaced by "return" only in places
- where it is clear that "return" is what is wanted.
- Mefods:
- Methods written with "mef" instead of "def" will be rewritten
- with "def" and "self" will be added as the first parameter.
- Use "maf" for "async def".
- Lambdas:
- pTycl:
- | a b c | some_code_here(a,b,c) |
- becomes Python:
- lambda a,b,c:some_code_here(a,b,c)
- Pragmas:
- Pragmas are of the form "<?pragma name args ...?>" and must be on a line by themselves.
- Available pragmas:
- elide Do not process or output lines. Default: off.
- comment Add a comment char "#" to the beginning of each line. Default: off.
- braces Turn on/off brace processing. Default: on.
- carets Turn on/off caret processing. Default: on.
- lambdas Turn on/off lamba processing. Default: on.
- m_fs Turn on/off mef/maf processing. Default: on.
- self Turn on/off self processing or set "self". Default: on, "self".
- Notes:
- The reference implementation of the preprocessor is the Tcl version.
- Input is processed line by line using regular expressions and string replacement.
- With the exception of multi-line blocks, all code can be written as normal Python.
- There is no actual parsing.
- This will work:
- if True {
- print('t')
- }
- This will not work:
- if True { print('t') }
- When writing such things on one line, use straight Python syntax:
- if True : print('t')
- Normal Python multi-line blocks will not work;
- pTycl and Python can't be mixed in that way.
- Anything pTycl is semi-intentionally designed to be a syntax error in Python.
- This was done to help prevent wrongness slipping-through unnoticed.
- For example, "<?praga elide off?>" will be ignored because pTycl doesn't
- understand it, so it will pass through to Python, where it will be an error.
- Example:
- <?pragma elide off?>
- // This is a pTycl program
- # This line will be in the output
- // This line won't
- class Thing {
- // For starters, we don't have to indent inside the class.
- // "self" will be added automatically to a "mefod".
- // Braces are used to enclose the method body.
- // "$" is used for self.
- mef __init__ () {
- $num = 4
- }
- // A mefod that takes a parameter and returns something.
- mef add_num1 (num) {
- ^ $num + num
- }
- // Same but with regular "def" and "$" (self)
- def add_num2 ($, num) {
- ^ self.num + num
- }
- // A mefod with a lambda
- mef lamda_add_num (num) {
- ^ | n | num + n |
- }
- // Indenting
- mef indenting (some, unused, args) {
- if some == unused or args is None {
- ^ do_some_thing(args)
- } elif args is not None and args[0] == 4 {
- ^ $me_do_something(some)
- } elif args is None {
- notice here
- that even
- with the messed-up indenting {
- if the braces are {
- balanced
- for example {
- here
- }
- then
- things
- will
- work
- out
- } else {
- things could get messy
- }
- }
- } else {
- try {
- unused += args
- } except Exception as ex {
- print(ex)
- } else {
- for i in range(unused) {
- print($add_num1(i))
- }
- }
- }
- }
- } # class Thing (this is just a comment after the close brace. This whole line will be gone.)
- # EOF
- // EOF
- #---------------------------------
- $ ptycl < README
- # This line will be in the output
- class Thing:
- def __init__ (self):
- self.num = 4
- def add_num1 (self, num):
- return self.num + num
- def add_num2 (self, num):
- return self.num + num
- def lamda_add_num (self, num):
- return lambda n:num + n
- def indenting (self, some, unused, args):
- if some == unused or args is None:
- return do_some_thing(args)
- elif args is not None and args[0] == 4:
- return self.me_do_something(some)
- elif args is None:
- notice here
- that even
- with the messed-up indenting:
- if the braces are:
- balanced
- for example:
- here
- then
- things
- will
- work
- out
- else:
- things could get messy
- else:
- try:
- unused += args
- except Exception as ex:
- print(ex)
- else:
- for i in range(unused):
- print(self.add_num1(i))