View Single Post
Old 09-05-2020, 10:37 AM   #4
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
In progress.

The template language will support if ... then ... else ... fi as an expression returning a value. This means that the following two fragments are equivalent.
Code:
program:
	if field('#mybool') then
		f = 'series'
	else
		f = 'authors'
	fi;
	field(f)
Code:
program:
	a = field(if field('#mybool') then 'series' else 'authors' fi);
Nested ifs will be supported. The else clause is optional.

In addition, I added 2 infix comparison operators, == and !=, so the following fragment will work, assigning the result of the 'if' to the variable 'q'. And yes, I know that the answer will be 'bar aa'.
Code:
program:
	q =
		if 'a' == 'b' then
			'foo'
		else
			'bar'
		fi;
	strcat(q, ' aa')
The operators compare strings. If you want to compare numbers you will still need to use the cmp() function. You will also need to use strcmp() if you want < or >.

I am not going to add infix booleans (and/or) because that would require adding precedence and parenthesized expressions. The functions are easy enough to use.

One problem is that the parser is rather stupid so error messages might not be helpful. For example, forgetting a 'fi' on an embedded if might indicate the error at the end of the outermost if. Fixing this would require building a real parser, which at the moment I don't want to do.
chaley is offline   Reply With Quote