![]() |
#1 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Template if -- then -- else
I am considering implementing classic if-then-else clauses in general program mode of the template language. They would look something like this, borrowed from Algol 68.
Code:
if func_or_var then series of statements; [else series of statements;] fi This isn't trivial to implement and might break something. For example, having variables named "if" will probably break things. The question is "Will people use it?" Thanks Last edited by chaley; 09-06-2020 at 09:16 AM. Reason: Grammar |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 24,905
Karma: 47303824
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
"If you build it, they will come."
I hate the current conditionals. There is something about "test" that means I just don't read it properly. Of course, I'm expecting one of the first questions about using "if..then" to have code that looks like: Code:
program: if test(field('series'), field('series'), 'nonseries') then dir_name= field('series'); else dir_name= 'nonseries' fi Of course it wouldn't be me. I'd never write something like that in other languages and then spend an embarrassing amount of time trying to work out what was wrong. No, not me, never. |
![]() |
![]() |
![]() |
#3 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 10,974
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
I suspect that this might be a little easier to figure out than the current conditionals. (I just can't seem to wrap my brain around them.)
|
![]() |
![]() |
![]() |
#4 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
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); 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') 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. |
![]() |
![]() |
![]() |
#5 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Last edited by chaley; 09-05-2020 at 04:28 PM. Reason: Improve comparison examples |
|
![]() |
![]() |
![]() |
#6 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Known incompatibilities in the "improved" program mode language:
Last edited by chaley; 09-06-2020 at 09:18 AM. Reason: Clarity |
![]() |
![]() |
![]() |
#7 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
The improvements are now in calibre's source code and will be in the next calibre 5 beta.
Please let me know if something that used to work is broken, or if the new features do unexpected things. |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help with a template | dunhill | Library Management | 10 | 06-04-2020 06:34 AM |
Using built-in template functions in a custom template function | ilovejedd | Library Management | 4 | 01-28-2018 12:20 PM |
Help with template... or something else | Glottis | Library Management | 5 | 10-07-2015 12:55 PM |
Help with template | KALKITO | Library Management | 1 | 07-02-2012 02:54 PM |