An open API service indexing awesome lists of open source software.

https://github.com/andreainfufsm/testing-liascript-coderunner


https://github.com/andreainfufsm/testing-liascript-coderunner

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

          

# Testing LiaScript's CodeRunner

> This is a playground to test some use cases with [LiaScript](https://github.com/LiaScript/LiaScript) and [CodeRunner](https://github.com/liascript/CodeRunner)

It all started with `git clone https://github.com/liascript/CodeRunner`

CodeRunner is a *"code-running server, based on Python, that can compile and execute code and communicate via websockets"*. It is not to be confused with these other CodeRunners: https://github.com/trampgeek/moodle-qtype_coderunner, https://github.com/codeclassroom/CodeRunner.

CodeRunner's clients are LiaScript documents, so we have a single document and multiple ways to view it:

- LiaScript server on GitHub:
https://liascript.github.io/course/?https://github.com/andreainfufsm/testing-liascript-coderunner

- GitHub-rendered Markdown:
https://github.com/andreainfufsm/testing-liascript-coderunner

- Raw Markdown on GitHub:
https://raw.githubusercontent.com/AndreaInfUFSM/testing-liascript-coderunner/master/README.md

- Local LiaScript development server: `liascript-devserver --input README.md --port 3001 --live`

## Where does the code run?

The code runs on a WebSocket server specified in the header of this document. The server can be deployed anywhere:

```
//window.CodeRunner.init("wss://coderunner.informatik.tu-freiberg.de/")
window.CodeRunner.init("wss://ancient-hollows-41316.herokuapp.com/")
//window.CodeRunner.init("ws://127.0.0.1:8000/")
//window.CodeRunner.init("wss://testing-coderunner.andreaschwertne.repl.co/")
```

## Running C code with GCC

Using `@LIA.eval` macro:

```
@LIA.eval(`["main.c"]`, `gcc -Wall main.c -o a.out`, `./a.out`)
```

Click the tiny button below the code to see the magic happen:

```c
#include

int main (void){
printf ("It works!\n");
return 0;
}
```
@LIA.eval(`["main.c"]`, `gcc -Wall main.c -o a.out`, `./a.out`)

## Running Haskell code with GHC

Now we'll be exploring two approaches to run Haskell example codes: using CodeRunner or embedding a Repl.it project.

Choose one from the menu or just click next to continue.

### CodeRunner via LiaScript Macros

About the macros:

- Implementation described in: https://github.com/LiaScript/CodeRunner/blob/master/README.md#implementation

- We've pasted the code into the header of our MarkDown document. Check the raw view of the document:
https://raw.githubusercontent.com/AndreaInfUFSM/testing-liascript-coderunner/master/README.md

> The `@LIA.eval` macro gives us more flexibility, but `@LIA.haskell` is shorter and should be enough for most use cases.

Using `@LIA.eval` macro: we just have to copy this line right below the Haskell code

```
@LIA.eval(`["main.hs"]`, `ghc main.hs -o main`, `./main`)
```

``` haskell
main = do
print "Hello"
```
@LIA.eval(`["main.hs"]`, `ghc main.hs -o main`, `./main`)

Using the `@LIA.haskell` macro: same result as above, but a shorter line to copy below the Haskell code

```
@LIA.haskell()
```

``` haskell
main = do
print "Hello"
```
@LIA.haskell()

### Problem with backticks (solved)

Haskell infix operator uses backticks around a function: https://wiki.haskell.org/Infix_operator

For example, consider the function `mod`.

In prefix notation:

```
mod 9 2
```

In infix notation:

```
9 `mod` 2
```

That seems to be a problem for LiaScript to run such Haskell code.

This works:

``` haskell
main = do
putStrLn $ show $ mod 9 2
```
@LIA.haskell()

But this didn't work with a CodeRunner script using `@input(` :

``` haskell
main = do
putStrLn $ show $ 9 `mod` 2
```

**Update:** This is now solved with a new macro `@'input(` replacing `@input(` in the CodeRunner script located in the header of this document.

``` haskell
main = do
putStrLn $ show $ 9 `mod` 2
```
@LIA.eval(`["main.hs"]`, `ghc main.hs -o main`, `./main`)

### Embedded Repl.it

Repl.it online integrated development environment is another alternative to run code inside a LiaScript document. It can be embedded in a document using `iframe` or LiaScript's oEmbed service (see below).

> It is a powerful alternative because Repl.it supports any language/library using Nix. However, it takes longer to run/show and even its simple public interface may be confusing to some students. Also, the browser may prevent the embed to show and force the user to open the site in another window.

This Repl.it project runs a simple `main` function and keeps GHCi running so we can try Haskell functions in the interactive environment: https://replit.com/@AndreaSchwertne/2022haskell-console

Embedding using `iframe`:

```

```

Embedding using `oEmbed`:

```
??[replit](https://replit.com/@AndreaSchwertne/2022haskell-console)
```

??[replit](https://replit.com/@AndreaSchwertne/2022haskell-console)

## Deploying CodeRunner on Repl.it

Repl.it allows users to easily deploy HTTP servers: https://docs.replit.com/hosting/deploying-http-servers

WebSockets are also supported, so we could try to deploy CodeRunner on Repl.it.

After some tweaking with Nix packages and Repl.it ports, here goes a proof of concept!

https://replit.com/@AndreaSchwertne/testing-coderunner

Notes:

- This project install Nix Python packages required by CodeRunner and starts the server on port 80 (otherwise it would not work)

- Click `Show files` then click the three dots and `Show hidden files` to see Nix and Repl.it configuration files

- In the LiaScript document, change the hostname: `window.CodeRunner.init("wss://testing-coderunner.andreaschwertne.repl.co/")`

- Only C and Python included in this proof of concept, but we could add more packages (compilers, libraries) in the `replit.nix` (although this can slow the repl to start).

## More to test...

__More languages__

The original CodeRunner template has a lot more examples on many popular programming languages: https://github.com/liascript/CodeRunner

__Deploying on Repl.it__

- CodeRunner server.py uses [python-websocket-server](https://github.com/Pithikos/python-websocket-server), which is not found as a Nix package, so we had to install it on user space. There is a WebSocket server library for Python called [simple-websocket-server](https://github.com/dpallot/simple-websocket-server) which is distributed as a Nix package, so it would be worth trying to replace the websocket library to avoid reinstallation every time the repl restarts.