aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <[email protected]>2013-08-12 09:51:08 +0800
committerTeddy <[email protected]>2013-08-12 09:51:08 +0800
commitc5364249b2600f25155f4c7ac206b3d6ca0e5b06 (patch)
treed00c85150a826c9d131790fa877d49a53cf80be4
parent88b91db506969f3ed7752bfe9e1e916f5e098942 (diff)
rectified a bug in `modulo`
-rw-r--r--Makefile2
-rw-r--r--main.cpp46
-rw-r--r--types.cpp2
3 files changed, 45 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 45795d8..9b08a29 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ main: main.o parser.o builtin.o model.o eval.o exc.o consts.o types.o
g++ -o main $^ -pg -lgmp
.cpp.o:
- g++ $< -c -g -pg -DGMP_SUPPORT -Wall -O2
+ g++ $< -c -g -pg -DGMP_SUPPORT -Wall
clean:
rm -f *.o
diff --git a/main.cpp b/main.cpp
index 221941d..28bd27d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,22 +5,62 @@
#include "exc.h"
#include <cstdio>
-int main() {
+int main(int argc, char **argv) {
//freopen("in.scm", "r", stdin);
Tokenizor *tk = new Tokenizor();
ASTGenerator *ast = new ASTGenerator();
Evaluator *eval = new Evaluator();
+ bool interactive = false;
+ bool preload = false;
+ char *fname;
+
int rcnt = 0;
+ for (int i = 1; i < argc; i++)
+ if (strcmp(argv[i], "-i") == 0)
+ interactive = true;
+ else if (strcmp(argv[i], "-l") == 0 && i < argc - 1)
+ {
+ preload = true;
+ fname = argv[i + 1];
+ }
+
+ if (preload)
+ {
+ FILE *f = fopen(fname, "r");
+ if (!f)
+ {
+ printf("Can not open file: %s\n", fname);
+ return 0;
+ }
+ tk->set_stream(f);
+ while (1)
+ {
+ try
+ {
+ Pair *tree = ast->absorb(tk);
+ if (!tree) break;
+ eval->run_expr(tree)->ext_repr().c_str();
+ }
+ catch (GeneralError &e)
+ {
+ fprintf(stderr, "An error occured: %s\n", e.get_msg().c_str());
+ }
+ }
+ interactive = true;
+ }
+ tk->set_stream(stdin);
while (1)
{
+ if (interactive)
fprintf(stderr, "Sonsi> ");
try
{
Pair *tree = ast->absorb(tk);
if (!tree) break;
- fprintf(stderr, "Ret> $%d = %s\n", rcnt++,
- eval->run_expr(tree)->ext_repr().c_str());
+ string output = eval->run_expr(tree)->ext_repr();
+ if (interactive)
+ fprintf(stderr, "Ret> $%d = %s\n", rcnt++, output.c_str());
}
catch (GeneralError &e)
{
diff --git a/types.cpp b/types.cpp
index 8611cc6..de7ef66 100644
--- a/types.cpp
+++ b/types.cpp
@@ -892,7 +892,7 @@ NumObj *IntNumObj::mod(NumObj *_r) {
const mpz_class &rval = static_cast<IntNumObj*>(_r)->val;
if (rval == 0) throw NormalError(RUN_ERR_NUMERIC_OVERFLOW);
mpz_class ret = val % rval;
- if (sgn(ret) != sgn(rval))
+ if (ret != 0 && sgn(ret) != sgn(rval))
ret = ret + rval;
return new IntNumObj(ret);
}