Skip to content

Commit

Permalink
Code refactor and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWeirdDev committed May 9, 2020
1 parent 00629cf commit 10494c8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ dub --compiler=ldc

# Usage

### Use in command line

```
Usage: calcool [OPTION] [ARGUMENT]
-h : Print this help message
-i : Set input file (each expression in a separate line)
-c : Calculate the given expression
```

### Use as a library

```d
auto p = new Prser();
string result = p.evaluateFromString("sin(45*2) - 22 * -exp(3)");
```

# License

GPL-3.0+
13 changes: 10 additions & 3 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ void run(ref Parser p) {
stderr.writeln(u.msg);
} catch (EolException e) {
continue;
} catch (EofException e) {
break;
} catch (Exception e) {
stderr.writefln("Error: %s", e.msg);
break;
}
}
Expand All @@ -63,15 +66,19 @@ int run(ref Parser p, string input) {
p.evaluateFromString(input).writeln();
} catch (ParseException p) {
stderr.writefln("Parser error: %s", p.msg);
return 1;
} catch (LexerException l) {
stderr.writefln("Lexer error: %s", l.msg);
return 2;
} catch (UnsupportedTokenException u) {
stderr.writeln(u.msg);
return 3;
} catch (EolException e) {
return 0;
} catch (EofException e) {
return 0;
} catch (Exception e) {
return 1;
return 4;
}
return 1;

return 0;
}
6 changes: 6 additions & 0 deletions source/calcool/exceptions.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class EolException : Exception {
}
}

class EofException : Exception {
this(string msg) {
super(msg);
}
}

class LexerException : Exception {
this(string msg) {
super(msg);
Expand Down
2 changes: 1 addition & 1 deletion source/calcool/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public:
if (input.length == 0) {
input = lexer.nextLine();
if (input.length == 0) {
throw new Exception("END OF INPUT");
throw new EofException("END OF INPUT");
}
}
const f = input.front();
Expand Down

0 comments on commit 10494c8

Please sign in to comment.