DEV Community

IDRSolutions
IDRSolutions

Posted on

FormCalc vs Javascript [PDF / XFA in HTML5]

This is one of our XFAarticles which investigates the important factors to be considered during the conversion from FormCalc script to JavaScript (since none of the current browsers supports formcalc script, therefore the conversion to JavaScript is a prerequisite in HTML5).

Prior to diving into the conversion, we should separate out the keywords, operators and separators used in the FormCalc script.

This separation would be helpful to allow for replacing the tags with related JavaScript-supported tags.

Keywords

[“and”, “else”, “elseif”, “endif”, “eq”, “ge”, “gt”, “if”, “infinity”,”le”, “lt”, “nan”, “ne”, “not”, “null”, “or”, “then”, “this”, “break”, “continue”,”do”, “downto”, “end”, “endfor”, “endfunc”, “endwhile”, “exit”, “for”, “foreach”,”func”, “in”, “return”, “step”, “throw”, “upto”, “var”, “while”];

Operators

[“+”, “{“, “}”, “-“, “%”, “*”, “/”, “;”, “{“, “}”];

Separators

[“(“, “)”, “[“, “]”, “,”, “.”, “..”, “.#”, “.*”];

Some examples of replacement

Operational

Opertation Replacement FormCalc vs Javascript

Logical

**If Expression** ::= ‘if’ ‘(‘ SimpleExpression ‘)’ ‘then’
ExpressionList ( ‘elseif’ ‘(‘ SimpleExpression ‘)’ ‘then’
ExpressionList )* ( ‘else’ ExpressionList )? ‘endif’

eg:

if ( totalMarks < minimum ) then
// fail the student
endif

**for Expression** ::=’for’ Assignment ‘upto’ Accessor (‘step’ SimpleExpression)?
‘do’ ExpressionList ‘endfor’ |
‘for’ Assignment ‘downto’ Accessor (‘step’ SimpleExpression)?
‘do’ ExpressionList ‘endfor’

eg:

var y = 1;
for var x = 1 upto power do
y = y * base
endfor

upto keyword denotes increment and downto denotes decrements in for loop. upto keyword performs increment one by one unless it is specified in step keyword

for x=2 upto 100 step 2 do
count = count + 1;
endfor

**ForeachExpression** ::=
‘foreach’ Identifier ‘in’ ‘(‘ ArgumentList ‘)’
‘do’ ExpressionList ‘endfor’

eg;

var total = 0.0
foreach expense in ( travel_exp[*], living_exp[*],parking_exp[*] ) do
total = total + expense
endfor

WhileExpression ::=
‘while’ ‘(‘ SimpleExpression ‘)’ ‘do’ ExpressionList ‘endwhile’

eg:

var count = 0;
while ( count gt 10 ) do
count = count +1;
endwhile

If you read the code examples above carefully, you might have noticed that ecmascript357 type node access mechanism is used in several places of the formcalc script.

example amount[1] …. amount[*]

If you have encountered such kind of scenarios, then you need to convert the formcalc script to the resolveNode method like scripting in JavaScript; please read this article to learn more details about the resolveNode method in JavaScript.

For further information on XFA, please read the XFA 3.3 specification for more details.

Top comments (0)