summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/libraries/luamath/pgflibraryluamath.code.tex
blob: b06695a87fa036233284e44e25c4732981778b44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
% Copyright 2011 by Christophe Jorssen and Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.
%
% $Id: pgflibraryluamath.code.tex,v 1.11 2012/03/06 16:06:09 cjorssen Exp $
%
% !!! Warning: this library does not work with fpu!!!

% *******************************************************************
% Some luatex stuff. Should be put elsewhere (e.g. in
% pgfutil-luatex-engine).

% We assume luatex version > 0.39:
%   - \directlua <general text> will work
%   - \directlua is the only luatex primitive that we can assume
%     accessible without being prefixed by the format via
%     tex.enableprimitives.
% Ideas taken from the ifluatex package (Heiko Oberdiek)
\let\pgfutil@ifluatex\iffalse
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname directlua\endcsname\relax
\else
  \expandafter\let\csname pgfutil@ifluatex\expandafter\endcsname
    \csname iftrue\endcsname
\fi

\pgfutil@ifluatex
  \let\pgfutil@directlua\directlua
  \pgfutil@directlua{%
    tex.enableprimitives('pgfutil@',{'luaescapestring'})}
\else
  \def\pgfutil@directlua#1{}
  \def\pgfutil@luaescapestring#1{}
\fi

% End of luatex stuff
% *******************************************************************

% Loading part: based on fpu library

\newif\ifpgfluamathcomputationactive
\newif\ifpgfluamathparseractive

\def\pgfluamath@makecomputationactive{%
  \ifpgfluamathcomputationactive
  \else
    \pgfluamath@checkuninstallcmd%
    \pgfluamath@install%
    \pgfluamathcomputationactivetrue
  \fi}

\def\pgfluamath@makecomputationinactive{%
  \ifpgfluamathcomputationactive
    \pgfluamath@uninstall%
    \pgfluamathcomputationactivefalse
  \fi}

\def\pgfluamath@makeparseractive{%
  \ifpgfluamathparseractive
  \else
    \let\pgfluamath@pgfmathparse\pgfmathparse
    \let\pgfmathparse\pgfluamathparse
    \pgfluamathparseractivetrue
  \fi}

\def\pgfluamath@makeparserinactive{%
  \ifpgfluamathparseractive
    \let\pgfmathparse\pgfluamath@pgfmathparse
    \pgfluamathparseractivefalse
  \fi}    

\pgfqkeys{/pgf}{%
  % Enable lua-side computation of \pgfmathresult
  % (will be deactivated after the current TeX group)
  luamath/.is choice,
  luamath/only computation/.code={%
    \pgfutil@ifluatex
      \pgfluamath@makecomputationactive
      \pgfluamath@makeparserinactive
    \else
      \pgfmath@error{Sorry, you need the luaTeX engine to use the
        luamath library}%
    \fi},
  luamath/parser and computation/.code={%
    \pgfutil@ifluatex
      \pgfluamath@makecomputationactive
      \pgfluamath@makeparseractive
    \else
      \pgfmath@error{Sorry, you need the luaTeX engine to use the
        luamath library}%
    \fi},
  luamath/off/.code={%
    \pgfluamath@makecomputationinactive
    \pgfluamath@makeparserinactive},
  luamath/.default=only computation}

\def\pgfluamath@uninstall@appendcmd#1{%
  \expandafter\gdef\expandafter\pgfluamath@uninstall\expandafter{%
    \pgfluamath@uninstall #1}}

% If the uninstall command is already assembled, it will skip the
% uninstall assemblation.
\def\pgfluamath@checkuninstallcmd{%
  \pgfutil@ifundefined{pgfluamath@uninstall}{%
    \pgfutil@directlua{%
      pgfluamathfunctions = require("pgfluamath.functions")
      pgfluamathparser = require("pgfluamath.parser")}
    \global\let\pgfluamath@uninstall=\pgfutil@empty
  }{%
    % We already HAVE an uninstall command (prepared globally).
    % So: don't waste time assembling one!
    \def\pgfluamath@uninstall@appendcmd##1{}%
    \def\pgfluamath@prepareuninstallcmd##1{}%
  }%
}

% This assembles an uninstall command globally ON FIRST USAGE.
% See \pgfmathfloat@plots@checkuninstallcmd
\def\pgfluamath@prepareuninstallcmd#1{%
  \expandafter\global\expandafter\let
    \csname pgfluamath@backup@\string#1\endcsname=#1%
  \expandafter\gdef\expandafter\pgfluamath@uninstall\expandafter{%
    \pgfluamath@uninstall
    \expandafter\let\expandafter#1\csname pgfluamath@backup@\string#1\endcsname}%
}

\def\pgfluamath@install@function#1=#2{%
  \pgfluamath@prepareuninstallcmd{#1}%
  \let#1=#2%
}

\def\pgfluamath@install{%
  \pgfluamath@install@function\pgfmathadd@=\pgfluamathadd@%
  \pgfluamath@install@function\pgfmathsubtract@=\pgfluamathsubtract@%
  \pgfluamath@install@function\pgfmathneg@=\pgfluamathneg@%
  \pgfluamath@install@function\pgfmathmultiply@=\pgfluamathmultiply@%
  \pgfluamath@install@function\pgfmathdivide@=\pgfluamathdivide@%
  % \pgfluamath@install@function\pgfmathdiv@=\pgfluamathdiv@%
  \pgfluamath@install@function\pgfmathfactorial@=\pgfluamathfactorial@%
  \pgfluamath@install@function\pgfmathsqrt@=\pgfluamathsqrt@%
  % \pgfluamath@install@function\pgfmathpow@=\pgfluamathpow@%
  \pgfluamath@install@function\pgfmathe@=\pgfluamathe@%
  \pgfluamath@install@function\pgfmathexp@=\pgfluamathexp@%
  \pgfluamath@install@function\pgfmathln@=\pgfluamathln@%
  \pgfluamath@install@function\pgfmathlogten@=\pgfluamathlogten@%
  % \pgfluamath@install@function\pgfmathlogtwo@=\pgfluamathlogtwo@%
  \pgfluamath@install@function\pgfmathabs@=\pgfluamathabs@%
  \pgfluamath@install@function\pgfmathmod@=\pgfluamathmod@%
  \pgfluamath@install@function\pgfmathMod@=\pgfluamathMod@%
  \pgfluamath@install@function\pgfmathround@=\pgfluamathround@%
  \pgfluamath@install@function\pgfmathfloor@=\pgfluamathfloor@%
  \pgfluamath@install@function\pgfmathceil@=\pgfluamathceil@%
  % \pgfluamath@install@function\pgfmathint@=\pgfluamathint@%
  % \pgfluamath@install@function\pgfmathfrac@=\pgfluamathfrac@%
  % \pgfluamath@install@function\pgfmathreal@=\pgfluamathreal@%
  \pgfluamath@install@function\pgfmathgcd@=\pgfluamathgcd@%
  \pgfluamath@install@function\pgfmathisprime@=\pgfluamathisprime@%
  \pgfluamath@install@function\pgfmathpi@=\pgfluamathpi@%
  \pgfluamath@install@function\pgfmathrad@=\pgfluamathrad@%
  \pgfluamath@install@function\pgfmathdeg@=\pgfluamathdeg@%
  \pgfluamath@install@function\pgfmathsin@=\pgfluamathsin@%
  \pgfluamath@install@function\pgfmathcos@=\pgfluamathcos@%
  \pgfluamath@install@function\pgfmathtan@=\pgfluamathtan@%
  \pgfluamath@install@function\pgfmathsec@=\pgfluamathsec@%
  \pgfluamath@install@function\pgfmathcosec@=\pgfluamathcosec@%
  \pgfluamath@install@function\pgfmathcot@=\pgfluamathcot@%
  \pgfluamath@install@function\pgfmathasin@=\pgfluamathasin@%
  \pgfluamath@install@function\pgfmathacos@=\pgfluamathacos@%
  \pgfluamath@install@function\pgfmathatan@=\pgfluamathatan@%
  \pgfluamath@install@function\pgfmathatantwo@=\pgfluamathatantwo@%
  \pgfluamath@install@function\pgfmathmax@=\pgfluamathmax@%
  \pgfluamath@install@function\pgfmathmin@=\pgfluamathmin@%
  % \pgfluamath@install@function\pgfmath@pi=\pgfluamathpi@%
  % \pgfluamath@install@function\pgfmathpi=\pgfluamathpi@%
  % \pgfluamath@install@function\pgfmathe@=\pgfluamathe@%
  % \pgfluamath@install@function\pgfmathe=\pgfluamathe@%
  % \pgfluamath@install@function\pgfmathlessthan@=\pgfluamathlessthan@%
  % \pgfluamath@install@function\pgfmathless@=\pgfluamathlessthan@%
  % \pgfluamath@install@function\pgfmathgreaterthan@=\pgfluamathgreaterthan@%
  % \pgfluamath@install@function\pgfmathgreater@=\pgfluamathgreaterthan@%
  % \pgfluamath@install@function\pgfmathpow@=\pgfluamathpow@
  \pgfluamath@install@function\pgfmathrand@=\pgfluamathrand@
  \pgfluamath@install@function\pgfmathrand=\pgfluamathrand@
  \pgfluamath@install@function\pgfmathrnd@=\pgfluamathrnd@
  \pgfluamath@install@function\pgfmathrnd=\pgfluamathrnd@
  % \pgfluamath@install@function\pgfmathtrue@=\pgfluamathtrue@
  % \pgfluamath@install@function\pgfmathfalse@=\pgfluamathfalse@
  % \pgfluamath@install@function\pgfmathnot@=\pgfluamathnot@
  % \pgfluamath@install@function\pgfmathhex@=\pgfluamathhex@
  % \pgfluamath@install@function\pgfmathHex@=\pgfluamathHex@
  % \pgfluamath@install@function\pgfmathoct@=\pgfluamathoct@
  % \pgfluamath@install@function\pgfmathbin@=\pgfluamathbin@
  % \pgfluamath@install@function\pgfmathand@=\pgfluamathand@
  % \pgfluamath@install@function\pgfmathor@=\pgfluamathor@
  % \pgfluamath@install@function\pgfmathfactorial@=\pgfluamathfactorial@
  % \pgfluamath@install@function\pgfmathveclen@=\pgfluamathveclen@
  % \pgfluamath@install@function\pgfmathcosh@=\pgfluamathcosh@
  % \pgfluamath@install@function\pgfmathsinh@=\pgfluamathsinh@
  % \pgfluamath@install@function\pgfmathtanh@=\pgfluamathtanh@
  % \pgfluamath@install@function@unimplemented{ceil}%
  % \pgfluamath@install@function@unimplemented{frac}%
  % \pgfluamath@install@function@unimplemented{log2}%
  % \pgfluamath@install@function@unimplemented{log10}%
  % \pgfluamath@install@function@unimplemented{equalto}%
  % \pgfluamath@install@function@unimplemented{random}%
  % \pgfluamath@install@function@unimplemented{setseed}%
  % \pgfluamath@install@function@unimplemented{Mod}%
  % \pgfluamath@install@function@unimplemented{real}%
  % \pgfluamath@install@function@unimplemented{notequal}%
  \pgfluamath@install@function\pgfmathreciprocal=\pgfluamathreciprocal%
  \pgfluamath@install@function\pgfpointnormalised=\pgfluamathpointnormalised
}

\def\pgfluamathgetresult#1{%
  \edef\pgfmathresult{\pgfutil@directlua{tex.print(-1,#1)}}}

\def\pgfluamathpi@{%
  \pgfluamathgetresult{pgfluamathfunctions.pi()}}
\def\pgfluamathe@{%
  \pgfluamathgetresult{pgfluamathfunctions.e()}}
\def\pgfluamathadd@#1#2{%
  \pgfluamathgetresult{pgfluamathfunctions.add(#1,#2)}}
\def\pgfluamathsubtract@#1#2{%
  \pgfluamathgetresult{pgfluamathfunctions.substract(#1,#2)}}
\def\pgfluamathneg@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.neg(#1)}}
\def\pgfluamathmultiply@#1#2{%
  \pgfluamathgetresult{pgfluamathfunctions.multiply(#1,#2)}}
\def\pgfluamathdivide@#1#2{%
  \pgfluamathgetresult{pgfluamathfunctions.divide(#1,#2)}}
\def\pgfluamathabs@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.abs(#1)}}
\def\pgfluamathround@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.round(#1)}}
\def\pgfluamathfloor@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.floor(#1)}}
\def\pgfluamathceil@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.ceil(#1)}}
\def\pgfluamathgcd@#1#2{%
  \pgfluamathgetresult{pgfluamathfunctions.gcd(#1,#2)}}
\def\pgfluamathisprime@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.isprime(#1)}}
\def\pgfluamathmax@#1{%
  \pgfluamathgetresult{%
    math.max(pgfluamathfunctions.split_braces_to_explist("#1"))}}
\def\pgfluamathmin@#1{%
  \pgfluamathgetresult{%
    math.min(pgfluamathfunctions.split_braces_to_explist("#1"))}}
\def\pgfluamathsin@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.Sin(#1)}}
\def\pgfluamathcos@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.Cos(#1)}}
\def\pgfluamathtan@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.Tan(#1)}}
\def\pgfluamathmod@#1#2{%
  \pgfluamathgetresult{pgfluamathfunctions.mod(#1,#2)}}
\def\pgfluamathMod@#1#2{%
  \pgfluamathgetresult{pgfluamathfunctions.Mod(#1,#2)}}
\def\pgfluamathrad@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.rad(#1)}}
\def\pgfluamathdeg@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.deg(#1)}}
\def\pgfluamathatan@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.aTan(#1)}}
\def\pgfluamathatantwo@#1#2{%
  \pgfluamathgetresult{pgfluamathfunctions.aTan2(#1,#2)}}
\def\pgfluamathasin@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.aSin(#1)}}
\def\pgfluamathacos@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.aCos(#1)}}
\def\pgfluamathcot@#1{%
  \pgfluamathgetresult{1/pgfluamathfunctions.Tan(#1)}}
\def\pgfluamathsec@#1{%
  \pgfluamathgetresult{1/pgfluamathfunctions.Cos(#1)}}
\def\pgfluamathcosec@#1{%
  \pgfluamathgetresult{1/pgfluamathfunctions.Sin(#1)}}
\def\pgfluamathexp@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.exp(#1)}}
\def\pgfluamathln@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.log(#1)}}
\def\pgfluamathlogten@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.log10(#1)}}
\def\pgfluamathsqrt@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.sqrt(#1)}}
\def\pgfluamathrnd@{%
  \pgfluamathgetresult{pgfluamathfunctions.rnd()}}
\def\pgfluamathrand@{%
  \pgfluamathgetresult{pgfluamathfunctions.rand(-1,1)}}
\def\pgfluamathfactorial@#1{%
  \pgfluamathgetresult{pgfluamathfunctions.factorial(#1)}}
\def\pgfluamathreciprocal#1{%
  \pgfluamathgetresult{1/#1}}
% \pgfluamath@install@function\pgfmath@pi=\pgfluamathpi@%
% \pgfluamath@install@function\pgfmathpi=\pgfluamathpi@%
% \pgfluamath@install@function\pgfmathe@=\pgfluamathe@%
% \pgfluamath@install@function\pgfmathe=\pgfluamathe@%
% \pgfluamath@install@function\pgfmathlessthan@=\pgfluamathlessthan@%
% \pgfluamath@install@function\pgfmathless@=\pgfluamathlessthan@%
% \pgfluamath@install@function\pgfmathgreaterthan@=\pgfluamathgreaterthan@%
% \pgfluamath@install@function\pgfmathgreater@=\pgfluamathgreaterthan@%
% \pgfluamath@install@function\pgfmathpow@=\pgfluamathpow@
% \pgfluamath@install@function\pgfmathrand@=\pgfluamathrand@
% \pgfluamath@install@function\pgfmathrand=\pgfluamathrand@
% \pgfluamath@install@function\pgfmathrnd@=\pgfluamathrnd@
% \pgfluamath@install@function\pgfmathrnd=\pgfluamathrnd@
% \pgfluamath@install@function\pgfmathtrue@=\pgfluamathtrue@
% \pgfluamath@install@function\pgfmathfalse@=\pgfluamathfalse@
% \pgfluamath@install@function\pgfmathnot@=\pgfluamathnot@
% \pgfluamath@install@function\pgfmathhex@=\pgfluamathhex@
% \pgfluamath@install@function\pgfmathHex@=\pgfluamathHex@
% \pgfluamath@install@function\pgfmathoct@=\pgfluamathoct@
% \pgfluamath@install@function\pgfmathbin@=\pgfluamathbin@
% \pgfluamath@install@function\pgfmathand@=\pgfluamathand@
% \pgfluamath@install@function\pgfmathor@=\pgfluamathor@
% \pgfluamath@install@function\pgfmathfactorial@=\pgfluamathfactorial@
% \pgfluamath@install@function\pgfmathveclen@=\pgfluamathveclen@
% \pgfluamath@install@function\pgfmathcosh@=\pgfluamathcosh@
% \pgfluamath@install@function\pgfmathsinh@=\pgfluamathsinh@
% \pgfluamath@install@function\pgfmathtanh@=\pgfluamathtanh@

\def\pgfluamathpointnormalised#1{%
  \pgf@process{#1}%
  \pgfutil@directlua{%
    pgfluamathfunctions.pointnormalised(%
      \pgf@sys@tonumber{\pgf@x},\pgf@sys@tonumber{\pgf@y})}%
  \ignorespaces}

% Parser

\newif\ifpgfluamathunitsdeclared

\def\pgfluamathparse#1{%
  \pgfluamathunitsdeclaredfalse
  % The following "two passes" (TeX -> lua -> TeX -> lua -> TeX) is
  % required for chadef'ed named boxes. The parser 
  % adds \number in front of the box name (e.g. \mybox ->
  % \number\mybox). Then this is expanded to the chardef'ed number via
  % \edef expansion.
  \edef\pgfluamath@temp{%
    \pgfutil@directlua{%
    % Double quotes " are needed here. On the lua side, single quotes
    % ' are used. Take care to don't mix them.
      pgfluamathparser.parse("\pgfutil@luaescapestring{#1}")
      tex.sprint(parsed_expression)}}%
  % We can now feed back the evaluator.
  \edef\pgfluamathresult{%
    \pgfutil@directlua{%
      pgfluamathparser.eval('\pgfluamath@temp')
      if pgfluamathparser.result == nil then
        tex.print('nil')
      else
        tex.print(pgfluamathparser.result)
      end}}%
  \csname pgfluamathunitsdeclared\pgfutil@directlua{%
    if pgfluamathparser.units_declared == true then
      tex.print('true')
    else
      tex.print('false')
    end}\endcsname
  \ifpgfluamathparseractive
    \let\pgfmathresult\pgfluamathresult
    \let\ifpgfmathunitsdeclared=\ifpgfluamathunitsdeclared
  \fi}

\endinput
% NEW: CJ (06 March 2012) Work in progress
\documentclass{minimal}

\usepackage{pgfmath}

\makeatletter
\let\pgfutil@directlua\directlua

\directlua{dofile('pgfmathparser.lua')}

\def\pgfluamathdeclarefunction#1#2#3{%
  \pgfutil@directlua{%
    pgfluamath.declare_new_function('#1',#2,#3)}}

\pgfluamathdeclarefunction{add}{2}{%
  function (a,b) return a + b end}

\pgfluamathdeclarefunction{substract}{2}{%
  function (a,b) return a - (b) end}

\pgfluamathdeclarefunction{neg}{1}{%
  function (a) return -(a) end}

\pgfluamathdeclarefunction{multiply}{2}{%
  function (a,b) return a * b end}

\pgfluamathdeclarefunction{divide}{2}{%
  function (a,b) return a / b end}

\pgfluamathdeclarefunction{pow}{2}{%
  function (a,b) return math.pow(a,b) end}

\pgfluamathdeclarefunction{deg}{1}{%
  function (a) return math.deg(a) end}

\pgfluamathdeclarefunction{ifthenelse}{3}{%
  function (a,b,c) if a == 1 then return b else return c end end}

\pgfluamathdeclarefunction{equal}{2}{%
  function (a,b) if a == b then return 1 else return 0 end end}

\pgfluamathdeclarefunction{greater}{2}{%
  function (a,b) if a > b then return 1 else return 0 end end}

\pgfluamathdeclarefunction{less}{2}{%
  function (a,b) if a < b then return 1 else return 0 end end}

\pgfluamathdeclarefunction{notequal}{2}{%
  function (a,b) if a < b or a > b then return 1 else return 0 end end}

\pgfluamathdeclarefunction{notless}{2}{%
  function (a,b) if a >= b then return 1 else return 0 end end}

\pgfluamathdeclarefunction{notgreater}{2}{%
  function (a,b) if a <= b then return 1 else return 0 end end}

\pgfluamathdeclarefunction{andPGF}{2}{%
  function (a,b) if (a < 0 or a > 0) and (b < 0 or b > 0) then return
  1 else return 0 end end}

\pgfluamathdeclarefunction{orPGF}{2}{%
  function (a,b) if (a < 0 or a > 0) or (b < 0 or b > 0) then return 1
  else return 0 end end}

\pgfluamathdeclarefunction{modulo}{2}{%
  % This is the definition of the lua % modulo operator
  % The % operator cannot be used here (catcode issue)
  function (a,b) return a - math.floor(a/b)*b end}

\pgfluamathdeclarefunction{int}{1}{%
  function (a) return a - pgfluamath.defined_functions.modulo.code(a,1) end}

\pgfluamathdeclarefunction{frac}{1}{%
  function (a) return pgfluamath.defined_functions.modulo.code(a,1) end}

\pgfluamathdeclarefunction{factorial}{1}{%
  function (a)
    a = math.abs(pgfluamath.defined_functions.int.code(a))
    if a == 1 then
      return 1
    else
      return a * pgfluamath.defined_functions.factorial.code(a-1)
    end
  end}

\pgfluamathdeclarefunction{sqrt}{1}{%
  function (a) return math.sqrt(a) end}



\def\pgfluamathparseandresult#1{%
  \pgfutil@directlua{%
    local s = pgfluamath.transform_math_expr('#1',pgfluamath.defined_functions_pattern)
    texio.write_nl('pgfluamath: parsed expression "' .. s .. '"')
    loadstring('tex.sprint(-1,' .. s .. ')')()}}

\makeatother
\begin{document}

$\pgfluamathparseandresult{1+2} = 3$

$\pgfluamathparseandresult{2*3} = 6$

$\pgfluamathparseandresult{1--1} = 2$

$\pgfluamathparseandresult{6/2} = 3$

$\pgfluamathparseandresult{2^2} = 4$

$\pgfluamathparseandresult{1>2} = 0$

$\pgfluamathparseandresult{2>1} = 1$

$\pgfluamathparseandresult{1!=1} = 0$

$\pgfluamathparseandresult{1?2:3} = 2$

$\pgfluamathparseandresult{1.0||0} = 1$

$\pgfluamathparseandresult{1&&0} = 0$

$\pgfluamathparseandresult{sqrt(2)} = \pgfmathparse{sqrt(2)}\pgfmathresult$

%\pgfluamathparseandresult{{1,{2,3-7}[1],4}[1]^2}

\end{document}
% Local Variables:
% TeX-engine: luatex
% End: