Jump to content

Arithmetic evaluation: Difference between revisions

no edit summary
No edit summary
Line 14:
 
The implementation provides an advanced error handling and skipping blanks and Ada comments (these are taken from the library).
<lang ada>
with Ada.Unchecked_Deallocation;
with Parsers.String_Source; use Parsers.String_Source;
Line 106:
);
end Parsers.Simple;
</adalang>
Here is the implementation of the package.
<lang ada>
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Exceptions; use Ada.Exceptions;
Line 277:
 
end Parsers.Simple;
</adalang>
The next is a little test. It reads a line from the keyboard and then evaluates it. The program stops when the input is empty:
<lang ada>
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Text_IO; use Ada.Text_IO;
Line 320:
end loop;
end Test_Simple_Parser;
</adalang>
Sample exchange. When the expression is evaluated its range in the source string is indicated. Upon errors, the location of is shown as well:
<pre>
Line 481:
 
{{libheader|Boost.Spirit}}1.8.4
<lang cpp> #include <boost/spirit.hpp>
#include <boost/spirit/tree/ast.hpp>
#include <string>
Line 597:
}
};
</cpplang>
 
=={{header|D}}==
Following the previous number-operator dual stacks approach, an AST is built while previous version is evaluating the expression value. After the AST tree is constructed, a visitor pattern is used to display the AST structure and calculate the value.
<lang d>//module evaluate ;
import std.stdio, std.string, std.ctype, std.conv ;
 
Line 786:
"1 + 2*(3 - 2*(3 - 2)*((2 - 4)*5 - 22/(7 + 2*(3 - 1)) - 1)) + 1" ; // should be 60
calcVis((new AST).parse(expression)) ;
}</dlang>
 
=={{header|Haskell}}==
Line 1,059:
 
=={{header|Perl}}==
<lang perl>sub ev
# Evaluates an arithmetic expression like "(1+3)*7" and returns
# its value.
Line 1,119:
my ($op, @operands) = @$ast;
$_ = ev_ast($_) foreach @operands;
return $ops{$op}->(@operands);}}</perllang>
 
=={{header|Pop11}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.