Skip to content

Commit c6f497c

Browse files
committed
Fix implementation of RND function in BASIC interpreter
1 parent 5a355ca commit c6f497c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

samples/basic.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cmath>
1111
#include <cstdlib>
1212
#include <cstring>
13+
#include <ctime>
1314
#include <deque>
1415
#include <fstream>
1516
#include <iomanip>
@@ -57,7 +58,7 @@ class basic_interpreter
5758
| id_%Var > "(" > r1_%Expr > ")" <[this]{ return &at(lists_[*id_], *r1_); }
5859
| id_%Var <[this]{ return &vars_[*id_]; };
5960

60-
rule Value = !("[A-Z][A-Z][A-Z]"_irx > "(")
61+
rule Value = !"[A-Z][A-Z][A-Z]"_irx
6162
> ( ref_%Ref <[this]{ return **ref_; }
6263
| Real | "(" > Expr > ")" )
6364
| fn_%Fn > "(" > r1_%Expr > ")" <[this]{ return call(*fn_, *r1_); }
@@ -70,7 +71,7 @@ class basic_interpreter
7071
| "LOG"_isx > "(" > r1_%Expr > ")" <[this]{ return std::log(*r1_); }
7172
| "SQR"_isx > "(" > r1_%Expr > ")" <[this]{ return std::sqrt(*r1_); }
7273
| "INT"_isx > "(" > r1_%Expr > ")" <[this]{ return std::trunc(*r1_); }
73-
| "RND"_isx > "(" > r1_%Expr > ")" <[] { return std::rand() / static_cast<double>(RAND_MAX); };
74+
| "RND"_isx > ~ ( "(" > ~Expr > ")" ) <[] { return std::rand() / static_cast<double>(RAND_MAX); };
7475

7576
rule Factor = r1_%Value > ~(u8"[↑^]"_rx > r2_%Value <[this]{ *r1_ = std::pow(*r1_, *r2_); }
7677
) <[this]{ return *r1_; };
@@ -408,6 +409,7 @@ class basic_interpreter
408409
int main(int argc, char** argv)
409410
{
410411
try {
412+
std::srand(std::time(nullptr));
411413
basic_interpreter interpreter;
412414
while (--argc > 1)
413415
interpreter.load(*++argv);

0 commit comments

Comments
 (0)