Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 09-03-2020, 02:05 PM   #1
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,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
(The else part is optional.) The test would be an expression in the current template language that returns either an empty value or not. One can imagine using field(...), first_non_empty(...), and/or(), and strcmp, but all the functions will be available. Only one of the "then" or "else" parts will be evaluated. I wouldn't try hard to make it work in template program mode.

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
chaley is offline   Reply With Quote
Old 09-03-2020, 11:32 PM   #2
davidfor
Grand Sorcerer
davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.
 
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
And wonder why they never get "nonseries" as part of the result.

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.
davidfor is offline   Reply With Quote
Old 09-04-2020, 11:10 PM   #3
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
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.)
ownedbycats is offline   Reply With Quote
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,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);
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
Old 09-05-2020, 01:06 PM   #5
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by chaley View Post
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 >.
While improving performance I found I could add all the relational operators without a performance penalty. I also could add numeric relational ops, using slightly different operators. What will be there:
  • String (case insensitive): ==, !=, <, <=, >, >=
    String comparisons of numbers will use lexical ordering. For example, 11 < 2 is True.
  • Numeric: ==#, !=#, <#, <=#, >#, >=#
    Numeric comparison returns False if either left or right is non-numeric. Specifically, the empty string is not a number. For example, '' <# 1 is False

Last edited by chaley; 09-05-2020 at 04:28 PM. Reason: Improve comparison examples
chaley is offline   Reply With Quote
Old 09-06-2020, 06:13 AM   #6
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Known incompatibilities in the "improved" program mode language:
  • The words if, then, else, and fi are now reserved. If you have variables or custom functions with these names then the template won't compile.
  • In rare cases the old parser accepted expressions of the form
    Code:
    'a' 'b'
    These are now always errors.

Last edited by chaley; 09-06-2020 at 09:18 AM. Reason: Clarity
chaley is offline   Reply With Quote
Old 09-06-2020, 06:57 AM   #7
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,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.
chaley is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
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


All times are GMT -4. The time now is 12:50 PM.


MobileRead.com is a privately owned, operated and funded community.