calvin-lang
v0.0.2
Published
A little scripting language inspired by Julia and Lisp.
Downloads
2
Maintainers
Readme
(: calvin)
Write on lisp. Compile to pure javascript without runtime dependencies. Enjoy compiler that can guess your thoughts and generate intentionally missed parts of code.
Knowledge-oriented programming language
calvin is yet another attempt to create better programming language for modern world. More about calvin background, why it was chosen Lisp-syntax and why it's compiled to Javascript, you can find on coect.net/calvin/. Project Coect aims to replace outdated Email and XMPP with modern universal communication protocol.
calvin for Coect plays the same role as Emacs Lisp for Emacs. calvin is written in calvin and can recompile itself. Look at the interactive calvin-documentation where you can try calvin without leaving your browser.
Knowledge-oriented programming as opposed to object-oriented or functional one gives main priority to semantic models of the program instead of building blocks of the program (objects or functions). Each semantic model (in the form that compiler can understand) is called logos.
You can find more information about semantic code transformations, examples of symbolic and entitative calvin to JavaScipt transformations in the calvin_semantic_code_transformations.pdf. Please look also at the high-level calvin language overview calvin_lisp.pdf.
Seamless integration with Javascript
calvin is compiled to Javascript code without runtime dependencies and don't use
own datastructures (like for example ClojureScript does). calvin uses native
Javascript's arrays as lists and so can perform in any Javascript environment
without unnecessary overhead. JSON documents are valid calvin documents and so
can be included with usual include
. calvin tries to generate beautiful
javascript code that passes JSHint without warnings. The generated code is
compatible with EcmaScript5. For
legacy browsers like IE8 it should be used
es5-shim or other
polyfill.
calvin is implemented in calvin and you can extend language easily by adding new
macros. For example, to add support of yield
keyword introduced in
Javascript 1.7
just create a macro like:
(defmacro yield (x)
`(js "yield %" ~x))
Feature highlights
calvin supports destructuring across all forms that accept name-value bindings:
(def actors ["Neo" "Trinity" "Morpheus"]
[neo trinity morpheus] actors)
(let digits [1 2 3]
[one two three] digits
(set [one two three] digits))
Forms can be chained with well-known ->
macro. For chaining methods calls often
used in javascript libraries like jQuery or
D3 there is also special syntax based on method hooks. For
example let's look at following code-sample from
D3 homepage:
d3.selectAll("circle").transition()
.duration(750)
.delay(function(d, i) { return i * 10; })
.attr("r", function(d) { return Math.sqrt(d * scale); });
and rewrite it in calvin using hooks and short anonymous functions:
(d3 .selectAll "circle" .transition
.duration 750
.delay #(* %2 10)
.attr "r" #(Math.sqrt (* %1 scale)))
To define a macro you can use standard Lisp syntax-quote, unquote and unquote-splicing:
(defmacro when (x & code)
`(if ~x (do ~@code)))
Functions can have optional parameters and parameters with default values. Each parameter can be passed positionally or as keyword. In addition function can accept variable number of parameters that are accessible as list. Shortly syntax for keyword-only function parameters will be finalised.
;; 'a' is required, 'b' is required and associated with an entity 'Thing'
;; 'c' is optional, 'd' is optional and has default value 2, 'more' holds rest positional parameters
(defn demo-fn (a b:Thing c:? d:2 & more)
(log a b c d more))
;; an example of the function call
(demo-fn "Just A" "noumenon" c:42)
You can embed variables inside interpolated
strings that are started with #
.
Inside such strings $symbol
is replaced with symbol's value and $=var
replaced with pair name=value. For escaping $
itself use $$
.
(= #"Hello, $nickname!" (str "Hello, " nickname "!"))
calvin is under active development. In the nearest plans is to finish javascript source maps support and explicit semantic code transformations. Then add namespaces and integrate support of Browserify. You can see full list of planned changes and offer your own.
How to install and try calvin
Firstly, please install Node.js.
If you want to install latest development version of calvin:
$ git clone https://github.com/dogada/calvin.git
$ cd ./calvin
$ npm install
$ npm link .
If you don't plan to rebuild calvin and just want to try it:
$ npm install -g calvin
You have just installed calvin and become qualified for a calvin-developer T-shirt. It's time to dive into calvin:
$ calvin -x test/index.calv # run calvin test suite
$ calvin --lint test/index.calv # check unit tests for errors
$ calvin --lint --lint-log-level=1 test/index.calv # show only Lint errors, hide warnings and hints
$ calvin # start TEPL (Translate Eval Print Loop)
$ calvin src/cli.calv # compile single file to stdout
$ calvin test/def.calv test/hook.calv --output ./ # compile 2 files to the working directory
$ calvin -e "(+ 2 2)" # advanced calculator
$ calvin --help # print help to stdout
$ make # rebuild calvin compiler and run test suite
If you use Emacs you will enjoy calvin-support for flymake mode. The simplest method is to use my fork of flymake, however you can also extend standard flymake with calvin support (use this changeset as a hint). With flymake you will receive calvin feedback in real-time mode during edithing the code.
How to get involved or learn more
Add any bugs or feature requests to the issues page. Follow @meta_js or d0gada on Twitter to receive latest calvin news. Like calvin page on Facebook. Join our mailing list. Please visit www.coect.net to find more docs and examples.
calvin allows compiler to generate source code. Will a computer create programs instead of a human?
The compiler will execute the instructions exactly as before, but in addition to grammar instructions, it will also execute semantic instructions defined specifically for your program.
Imagine that you're explaining how does your program work to someone who knows nothing about programming — it's an old grammar compiler. Now imagine that you're explaining the same thing to someone with a degree in computer science — it's a new semantic compiler. But you will have to explain it in both cases.