Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ariya/calculator.clj
Learning Clojure by writing a lexer, parser, evaluator
https://github.com/ariya/calculator.clj
clojure clojurescript evaluator graalvm graalvm-native-image interpreter lexer parser scanner tokenizer
Last synced: 15 days ago
JSON representation
Learning Clojure by writing a lexer, parser, evaluator
- Host: GitHub
- URL: https://github.com/ariya/calculator.clj
- Owner: ariya
- License: epl-2.0
- Created: 2022-05-07T04:26:36.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-01T19:49:41.000Z (over 1 year ago)
- Last Synced: 2024-10-19T23:19:59.036Z (17 days ago)
- Topics: clojure, clojurescript, evaluator, graalvm, graalvm-native-image, interpreter, lexer, parser, scanner, tokenizer
- Language: Clojure
- Homepage:
- Size: 47.9 KB
- Stars: 8
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# calculator
This is a simple math expression evaluator written in [Clojure](https://clojure.org).
With [Java](https://adoptium.net) and [Leiningen](https://leiningen.org):
```
$ lein run "2 + (5 * 6 + 10)"
42.0
```With [Babashka](https://babashka.org/) or [nbb](https://github.com/babashka/nbb):
```
$ bb --classpath src -m calculator.core "2 + (5 * 6 + 10)"
42.0
$ npx nbb --classpath src --main calculator.core "2 + (5 * 6 + 10)"
42.0
```With [JavaScript](https://www.ecma-international.org/publications-and-standards/standards/ecma-262/) and [Node.js](https://nodejs.org):
```
$ npm install
$ npm run build
$ npm run calc "2 + (5 * 6 + 10)"
42
```Supported features:
* Integer and floating-point literals
* Additions, subtractions, multiplications, divisions
* Parenthesized sub-expressionsTODO:
* Floating-point literals with exponents (e.g. `6.022E23`)
* Modulus/remainder
* Trigonometric functions
* More functionsCreate a native executable with [native-image](https://www.graalvm.org/22.0/reference-manual/native-image) from [GraalVM](https://www.graalvm.org/):
```bash
$ lein uberjar
Compiling calculator.core
Compiling calculator.evaluator
Compiling calculator.lexer
Compiling calculator.parser
$ native-image --no-fallback -jar target/uberjar/calculator-0.1.0-SNAPSHOT-standalone.jar calc
42.0
GraalVM Native Image: Generating 'calc' (executable)...
[1/7] Initializing...
Version info: 'GraalVM 22.1.0 Java 11 CE'
C compiler: gcc (linux, x86_64, 9.4.0)
Garbage collector: Serial GC
[2/7] Performing analysis...
[3/7] Building universe...
[4/7] Parsing methods...
[5/7] Inlining methods...
[6/7] Compiling methods...
[7/7] Creating image...
Finished generating 'calc' in 12.8s.
$ ./calc "2 + (5 * 6 + 10)"
42.0
```