## Building from Source

1. Clone the repository
2. Configure/fetch dependencies
    * [sbt](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Linux.html)
    * [BNFC](http://bnfc.digitalgrammars.com/) from github:
   ```sudo apt install haskell-platform
      git clone https://github.com/BNFC/bnfc.git
      cd bnfc/source
      cabal install```
    * jflex from apt
    * [cup 0.11b-2014 or later](https://versioncontrolseidl.in.tum.de/parsergenerators/cup), easiest to get from git
3. cd into the rholang/ folder
4. Run `sbt bnfc:generate` to generate the parser
5. Run `sbt compile` to compile classes
6. Run `sbt assembly` to build a stand-alone .jar file

## Command-line usage

    $ ./rho2rbl examples/token.rho
    compiled examples/token.rho to examples/token.rbl

which is short for:

    $ java -jar target/scala-2.11/rholang-assembly-0.1-SNAPSHOT.jar examples/token.rho 
    compiled examples/token.rho to examples/token.rbl

## SBT Console

After generating the parser:

1. Run `sbt console` to launch the sbt console
2. In the sbt console import the compiler with `import coop.rchain.rho2rose._`
3. And then compile any Rholang ".rho" file with `Rholang2RosetteCompiler.main(Array("<path_to_Rholang_file>.rho"))`

Certain (rare) situations may require sbt clean or sbt bnfc:clean. (Mainly if you have changed bnfc)

## Structure of rholang/ directory
```
.
├── examples                            # Contains all example contracts
│   ├── old                             # Contains all old example contracts
│   ├── hello_world_again.rho
│   ├── log_time.rho
│   └── token.rho
├── lib                                 # Any jars we use in the compiler
│   ├── java-cup-11b.jar
│   ├── java-cup-11b-runtime.jar
│   └── JLex.jar
├── project                             
│   ├── target
│   ├── BNFC.scala
│   └── build.properties
├── src                                 # All source files
│   └── main
│       ├── bnfc                        # Folder containing current BNFC spec
│       ├── bnfc_old
│       ├── java
│       │   ├── JLex
│       │   └── rholang                 # BNFC generated AST
│       │       └── parsing
│       │           ├── delimc
│       │           │   └── Absyn
│       │           ├── lambda
│       │           │   └── Absyn
│       │           ├── rholang1
│       │           │   └── Absyn
│       │           └── rholang2
│       │               └── Absyn
│       ├── k
│       └── scala
│           ├── lib                     # Helper classes for the compiler
│           │   └── term
│           └── rholang
│               └── rosette             # Scala files for the compiler
├── target                              # SBT compile output
│   ├── resolution-cache
│   ├── scala-2.11
│   └── streams
├── build.sbt                           # SBT build file
├── LICENSE
└── README.md
```

## Adding a new construct to Rholang

1. Add the syntax of the new construct in rholang/src/main/bnfc/rholang.fc
2. Run "sbt bnfc:generate" to get the new construct into the parser
3. Add a new visit method in src/main/scala/rholang/rosette/Roselang.scala and specify what the new construct should translate to in RBL. For example, if we are adding the Map type to the compiler and it has type QMap in the AST and we want it to translate to a RblTable in Rosette, we would write as follows:

```
  override def visit( p : QMap, arg : A) : R = {
    combine(
      arg,
      L(G( s"""(new RblTable)"""), Top())
    )
  }
```

4. Run "sbt compile" and "sbt assembly" to have that translation included into the compiler

## Debugging

I have found it useful to step through using the Intellij Debugger. I have also tried debugging using jdb but the interface wasn't as nice.

### Intellij Debugger Steps 

1. Open the rchain/rholang project in Intellij
2. Goto run -> debug -> edit configurations...
3. Click on the "+" on the top left and create a new Scala Console configuration
4. Add a debugging breakpoint on any line in the source code
5. Run the compiler through the scala console as instructed above