mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-09-09 03:24:59 -06:00
changed svn layout
git-svn-id: svn://ultimatepp.org/upp/trunk@281 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
commit
263ff5f895
2665 changed files with 642923 additions and 0 deletions
85
reference/CParser/main.cpp
Normal file
85
reference/CParser/main.cpp
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#include <Core/Core.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
VectorMap<String, double> var;
|
||||
|
||||
double Exp(CParser& p);
|
||||
|
||||
double Term(CParser& p)
|
||||
{
|
||||
if(p.Id("abs")) {
|
||||
p.PassChar('(');
|
||||
double x = Exp(p);
|
||||
p.PassChar(')');
|
||||
return fabs(x);
|
||||
}
|
||||
if(p.IsId())
|
||||
return var.Get(p.ReadId(), 0);
|
||||
if(p.Char('(')) {
|
||||
double x = Exp(p);
|
||||
p.PassChar(')');
|
||||
return x;
|
||||
}
|
||||
return p.ReadDouble();
|
||||
}
|
||||
|
||||
double Mul(CParser& p)
|
||||
{
|
||||
double x = Term(p);
|
||||
for(;;)
|
||||
if(p.Char('*'))
|
||||
x = x * Term(p);
|
||||
else
|
||||
if(p.Char('/')) {
|
||||
double y = Term(p);
|
||||
if(y == 0)
|
||||
p.ThrowError("Divide by zero");
|
||||
x = x / y;
|
||||
}
|
||||
else
|
||||
return x;
|
||||
}
|
||||
|
||||
double Exp(CParser& p)
|
||||
{
|
||||
double x = Mul(p);
|
||||
for(;;)
|
||||
if(p.Char('+'))
|
||||
x = x + Mul(p);
|
||||
else
|
||||
if(p.Char('-'))
|
||||
x = x - Mul(p);
|
||||
else
|
||||
return x;
|
||||
}
|
||||
|
||||
CONSOLE_APP_MAIN
|
||||
{
|
||||
for(;;) {
|
||||
Cout() << "Expresion: ";
|
||||
String l = ReadStdIn();
|
||||
if(l.IsEmpty())
|
||||
break;
|
||||
CParser p(l);
|
||||
try {
|
||||
String id;
|
||||
if(p.IsId()) {
|
||||
CParser::Pos pos = p.GetPos();
|
||||
id = p.ReadId();
|
||||
if(p.Char('=')) {
|
||||
double x = Exp(p);
|
||||
Cout() << id << " <- " << x << '\n';
|
||||
var.GetAdd(id) = x;
|
||||
goto done;
|
||||
}
|
||||
p.SetPos(pos);
|
||||
}
|
||||
Cout() << Exp(p) << '\n';
|
||||
done:;
|
||||
}
|
||||
catch(CParser::Error e) {
|
||||
Cout() << "ERROR: " << e << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue