Skip to content

tristan/clojure-infix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

# clojure-infix

A simple numerical infix parser for clojure, mimicing the behaviour of clojure's read-string and load-string for simple numerical expressions writen in standard infix notation.

supports most numerical opperations
i.e. + - * /, "power of" operator as either ** or ^, and brackets.
any function calls (e.g. sqrt(x)) are prefixed with Math/
allows the use of symbols, and read-infix-string returns any found in the meta of the returned code

## DISCLAIMER

I have updated the code so it runs on clojure 1.6 using leiningen to start the repl, but as I've not worked on this or even clojure since v1.1, I cannot garantee the code makes any sense in the clojure 1.6 world, nor provide any support for it.

## Usage

The functions `read-infix-string` and `load-infix-string` provide the same interface and functionality as the core clojure functions `read-string` and `load-string` respectively.

## Example Usage 

$ lein repl
user=> (require 'clojure-infix)                 
nil
user=> (clojure-infix/load-infix-string "1 + 1 * 2")
3
user=> (clojure-infix/read-infix-string "(1 + 1) * 2")
(* (+ 1 1) 2)
user=> (clojure-infix/read-infix-string "a + a")
(+ a a)
user=> ((fn [a] (eval (clojure-infix/read-infix-string "a + a"))) 5)
10
user=> (meta (clojure-infix/read-infix-string "a + a"))
{:variables [a]}
user=> (clojure-infix/read-infix-string "sqrt(2) ** 2")  
(Math/pow (Math/sqrt 2) 2)

## TODO

* bitwise operations
* allow functions to accept more than 1 input
* keyword support?
* TESTS!!!

## License

FIXME: write

About

A simple numerical infix parser for clojure

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages