Simple Guide to try CirruScript

JiyinYiyong
Cirru Project
Published in
4 min readAug 22, 2015

I’ve spent quite some time playing with my own compile-to-js language called CirruScript. I feel quite fun about it. You may see here I have already many small repos written in CirruScript:

https://api.github.com/search/repositories?q=language:Cirru

Here’s how you can try CirruScript:

I published a CirruScript interpreter on npm which compiles and evaluates CirruScript just like CoffeeScript:

npm i -g cirru-scriptcirru-script # to start a REPL
cirru-script demo.cirru # to interpret a file
  • gulp-cirru-script compiler

Also, like CoffeeScript you may choose to compile it to JavaScript before running it. I didn’t offer a command line tool but it works fine in Gulp:

gulp.task 'script', ->
script = require 'gulp-cirru-script'
gulp.src('src/')
.pipe script(es6: false)
.pipe gulp.dest('lib/')

There is also a chance to compile CirruScript into a subset of ES6 code, which is actually how CirruScript is internally implemented.

  • Webpack cirru-script-loader

The Webpack loader is the same as CoffeeScript, just install the loader and add configs to webpack.config.coffee file:

{test: /\.cirru$/, loader: 'cirru-script'}

Here are a few tips you may need to start coding in CirruScript. The procedue of compiling CirruScript is divided into several steps:

Cirru -> Cirru Tree -> ES6 AST -> JavaScript

The parser is Cirru Parser and the compiler backend is Babel, which is good at compiling ES6 into ES5. And Cirru code is just aliases for the ES6 AST. You may easily get knowledges about that by reading souce code:

The first thing you need to know is in writing Cirru code, you are creating a tree of the program, rather than writing code in many syntax rules. You may try at here:

Cirru do set several rules, Cirru uses only double quotes to quote strings or other things, but quote mark is in many cases optional. The only reason you have to add quote marks is you have to escape characters like spaces, newlines, backslashes, etc.

console.log ":a a" ":a" :a

I have to admit the colon mark may confuse many people. “:a” represents a string of “a”, not a keyword in Clojure. The syntax is special, but you will always need it in CirruScript. Just don’t be too confused:

console.log ":a a" ":a" :a

Dollar mark is introduced to reduce parentheses and make indentations more natural. And it will work with indentations too:

console.log (Math.round 1.2)
console.log (Math.round (parseFloat :1.2))
console.log $ Math.round 1.2
console.log $ Math.round $ parseFloat :1.2
console.log $ Math.round
parseFloat :1.2

A comma is to unwrap an expression. please just learn from my demo below:

console.log (Math.round 1.2) 3
console.log
Math.round 1.2
, 3

A through guide on operations can be found on script.cirru.org , here’e link:

Objects and arrays are written in Polish Notation, which looks a bit strange comparing what JSON looks like:

{} (:a 1) (:b 2)
:c 3
:d $ {}
:e ":a string"
:f $ [] ":an array item"

It compiles to JavaScript like this:

{
a: 1,
b: 2,
c: 3,
d: {
e: “a string”,
f: [“an array item”]
}
}

You may also notice that “[]” and “{}” are actually aliases of “array” and “object”, which was frequently used in older versions of CirruScript.

You may reach me on Gitter about any question in CirruScript:

CirruScript is currently stable but not mature. I will suggest some benefits of CirruScript to you:

  • CirruScript proposed a concise and extensive way to manage indentations in a programming language, which makes is a interesting DSL.
  • It’s compiler backend(Babel) is widely accepted and you can trust it.
  • The implementation of CirruScript is simple. You may even fork it and create your own CirruScript dialect by simply generating ES6 AST from arrays from Cirru code. The core of CirruScipt is a AST generator called Scirpus:
  • The Style of CirruScript learnt a lot from Scheme and Haskell, also CoffeeScript. You may already see from how it looks. It’s cooler than JavaScript right?
  • GitHub is highlighting Cirru. Sublime Text is highlighting Cirru. So are some other tools, you may check them on cirru.org the project home page.

CirruScript is not mature for some reasons:

  • Cirru does not run static analysis on the AST, it’s not a robust programming language basis.
  • Lack of SourceMap support, in many cases it’s harder to debug than it does in CoffeeScript. Someone may fork to add SourceMaps support, the work is not difficult. I just feel more like debugging in valina JavaScript.
  • Writing JavaScript in Polish Notation and weird string syntax is… weird. ClojureScript might be a good example creating a powerful language with sophisticated syntax. Cirru is not making a new language but only add to ES6 code a new style of syntax.
  • Cirru is supported in many editors and highlighting tools. However, not all of them. I would always struggle with that.
  • CirruScript currently represents my personal taste about a beautify style of a programming language, not a community’s.

In a word, I still recommend you give it a try. It’s a bit strange but meanwhile reasonable. And I’m looking forward to feedback.

--

--