Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Add Playground to website #100

Merged
merged 12 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ module github.com/flipez/rocket-lang
go 1.18

require (
github.com/abiosoft/ishell/v2 v2.0.2
github.com/chzyer/readline v1.5.1
github.com/spf13/pflag v1.0.5
)

require (
github.com/abiosoft/ishell v2.0.0+incompatible // indirect
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
)
require golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
41 changes: 9 additions & 32 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
github.com/abiosoft/ishell v2.0.0+incompatible h1:zpwIuEHc37EzrsIYah3cpevrIc8Oma7oZPxr03tlmmw=
github.com/abiosoft/ishell v2.0.0+incompatible/go.mod h1:HQR9AqF2R3P4XXpMpI0NAzgHf/aS6+zVXRj14cVk9qg=
github.com/abiosoft/ishell/v2 v2.0.2 h1:5qVfGiQISaYM8TkbBl7RFO6MddABoXpATrsFbVI+SNo=
github.com/abiosoft/ishell/v2 v2.0.2/go.mod h1:E4oTCXfo6QjoCart0QYa5m9w4S+deXs/P/9jA77A9Bs=
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db h1:CjPUSXOiYptLbTdr1RceuZgSFDQ7U15ITERUGrUORx8=
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BMXYYRWTLOJKlh+lOBt6nUQgXAfB7oVIQt5cNreqSLI=
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:rZfgFAXFS/z/lEd6LJmf9HVZ1LkgYiHx5pHhV5DR16M=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
77 changes: 57 additions & 20 deletions repl/repl.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,76 @@
//go:build !wasm

package repl

import (
//"bufio"
"fmt"
"io"
"log"
"os"
"strings"

"github.com/abiosoft/ishell/v2"
"github.com/chzyer/readline"

"github.com/flipez/rocket-lang/ast"
"github.com/flipez/rocket-lang/evaluator"
"github.com/flipez/rocket-lang/lexer"
"github.com/flipez/rocket-lang/object"
"github.com/flipez/rocket-lang/parser"
)

const PROMPT = ">> "

var buildVersion = "v0.10.0"
var buildDate = "2021-12-27T21:13:44Z"

func Start(in io.Reader, out io.Writer) {
shell := ishell.New()
shell.SetHomeHistoryPath(".rocket_history")
shell.SetOut(out)
shell.SetPrompt("🚀 > ")
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

rl, err := readline.NewEx(&readline.Config{
Prompt: "🚀 \033[31m»\033[0m ",
HistoryFile: homeDir + "/.rocket_history",
InterruptPrompt: "^C",
DisableAutoSaveHistory: true,
})

if err != nil {
panic(err)
}

defer rl.Close()

env := object.NewEnvironment()
imports := make(map[string]struct{})
var cmds []string

fmt.Println(SplashScreen())

shell.Println(SplashScreen())
shell.NotFound(func(ctx *ishell.Context) {
for {
//source, err := line.Prompt("🚀 > ")
line, err := rl.Readline()

l := lexer.New(strings.Join(ctx.RawArgs, " "))
if err != nil {
break
}

line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}

cmds = append(cmds, line)

//if !strings.HasSuffix(line, ";") {
// rl.SetPrompt("🚀 \033[31m»»»\033[0m ")
// continue
//}

cmd := strings.Join(cmds, " ")
rl.SetPrompt("🚀 \033[31m»\033[0m ")
rl.SaveHistory(cmd)

l := lexer.New(cmd)
p := parser.New(l, imports)

object.AddEvaluator(evaluator.Eval)
Expand All @@ -40,17 +79,15 @@ func Start(in io.Reader, out io.Writer) {

program, imports = p.ParseProgram()
if len(p.Errors()) > 0 {
printParserErrors(ctx, p.Errors())
printParserErrors(p.Errors())
return
}

evaluated := evaluator.Eval(program, env)
if evaluated != nil {
ctx.Println("=> " + evaluated.Inspect())
fmt.Println(" " + evaluated.Inspect())
}
})

shell.Run()
}
}

const ROCKET = `
Expand All @@ -70,10 +107,10 @@ func SplashVersion() string {
return fmt.Sprintf("rocket-lang version %s (%s)\n", buildVersion, buildDate)
}

func printParserErrors(ctx *ishell.Context, errors []string) {
ctx.Println("🔥 Great, you broke it!")
ctx.Println(" parser errors:")
func printParserErrors(errors []string) {
fmt.Println("🔥 Great, you broke it!")
fmt.Println(" parser errors:")
for _, msg := range errors {
ctx.Printf("\t %s\n", msg)
fmt.Printf("\t %s\n", msg)
}
}
17 changes: 17 additions & 0 deletions repl/repl_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build wasm

package repl

import (
"fmt"
"io"
)

var buildVersion = "v0.10.0"
var buildDate = "2021-12-27T21:13:44Z"

func Start(in io.Reader, out io.Writer) {}

func SplashVersion() string {
return fmt.Sprintf("rocket-lang version %s (%s)\n", buildVersion, buildDate)
}
26 changes: 4 additions & 22 deletions utilities/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,13 @@ package utilities

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
)

var SearchPaths []string

func init() {
cwd, err := os.Getwd()

if err != nil {
log.Fatalf("error getting cwd: %s", err)
}

if e := os.Getenv("ROCKETLANGPATH"); e != "" {
tokens := strings.Split(e, ":")

for _, token := range tokens {
if err := AddPath(token); err != nil {
log.Fatalf("error adding token: %s", err)
}
}
} else {
SearchPaths = append(SearchPaths, cwd)
}
}
var once sync.Once

func AddPath(path string) error {
path = os.ExpandEnv(filepath.Clean(path))
Expand All @@ -50,6 +30,8 @@ func Exists(path string) bool {
}

func FindModule(name string) string {
once.Do(initSearchPaths)

basename := fmt.Sprintf("%s.rl", name)

for _, p := range SearchPaths {
Expand Down
29 changes: 29 additions & 0 deletions utilities/utilities_nowasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !wasm

package utilities

import (
"log"
"os"
"strings"
)

func initSearchPaths() {
cwd, err := os.Getwd()

if err != nil {
log.Printf("error getting cwd: %s", err)
}

if e := os.Getenv("ROCKETLANGPATH"); e != "" {
tokens := strings.Split(e, ":")

for _, token := range tokens {
if err := AddPath(token); err != nil {
log.Fatalf("error adding token: %s", err)
}
}
} else {
SearchPaths = append(SearchPaths, cwd)
}
}
5 changes: 5 additions & 0 deletions utilities/utilities_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build wasm

package utilities

func initSearchPaths() {}
112 changes: 112 additions & 0 deletions wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>🚀🇱🅰🆖 Playground</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
</head>
<body>
<div class="container py-4">
<header class="pb-3 mb-4 border-bottom">
<a href="/" class="d-flex align-items-center text-dark text-decoration-none">
<img src="https://rocket-lang.org/img/logo.png" height="56px" alt="RocketLang">
<h1>Playground</h1>
</a>
</header>

<div class="row align-items-md-stretch">
<div class="col-md-6">
<div class="h-100 p-5 bg-light border rounded-3">
<h2>Input</h2>
<hr />
<p>
<textarea class="form-control" rows="10" id="input" disabled placeholder="Loading WebAssembly binary..."></textarea>
</p>
<pi class="float-end">
<button id="shareButton" class="btn btn-outline-primary" type="button">Share</button>
<button id="runButton" class="btn btn-outline-success" type="button">Run</button>
</p>
</div>
</div>
<div class="col-md-6">
<div class="h-100 p-5 text-bg-dark rounded-3">
<h2>Output</h2>
<hr />
<pre id="output"></pre>
</div>
</div>
</div>

<footer class="pt-3 mt-4 text-muted border-top">
🚀🇱🅰🆖 Playground.
See <a href="https://rocket-lang.org">rocket-lang.org</a> for more information | &nbsp;
<a href="https://github.com/flipez/rocket-lang">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 16" width="45" height="16"><path fill-rule="evenodd" d="M18.53 12.03h-.02c.009 0 .015.01.024.011h.006l-.01-.01zm.004.011c-.093.001-.327.05-.574.05-.78 0-1.05-.36-1.05-.83V8.13h1.59c.09 0 .16-.08.16-.19v-1.7c0-.09-.08-.17-.16-.17h-1.59V3.96c0-.08-.05-.13-.14-.13h-2.16c-.09 0-.14.05-.14.13v2.17s-1.09.27-1.16.28c-.08.02-.13.09-.13.17v1.36c0 .11.08.19.17.19h1.11v3.28c0 2.44 1.7 2.69 2.86 2.69.53 0 1.17-.17 1.27-.22.06-.02.09-.09.09-.16v-1.5a.177.177 0 00-.146-.18zM42.23 9.84c0-1.81-.73-2.05-1.5-1.97-.6.04-1.08.34-1.08.34v3.52s.49.34 1.22.36c1.03.03 1.36-.34 1.36-2.25zm2.43-.16c0 3.43-1.11 4.41-3.05 4.41-1.64 0-2.52-.83-2.52-.83s-.04.46-.09.52c-.03.06-.08.08-.14.08h-1.48c-.1 0-.19-.08-.19-.17l.02-11.11c0-.09.08-.17.17-.17h2.13c.09 0 .17.08.17.17v3.77s.82-.53 2.02-.53l-.01-.02c1.2 0 2.97.45 2.97 3.88zm-8.72-3.61h-2.1c-.11 0-.17.08-.17.19v5.44s-.55.39-1.3.39-.97-.34-.97-1.09V6.25c0-.09-.08-.17-.17-.17h-2.14c-.09 0-.17.08-.17.17v5.11c0 2.2 1.23 2.75 2.92 2.75 1.39 0 2.52-.77 2.52-.77s.05.39.08.45c.02.05.09.09.16.09h1.34c.11 0 .17-.08.17-.17l.02-7.47c0-.09-.08-.17-.19-.17zm-23.7-.01h-2.13c-.09 0-.17.09-.17.2v7.34c0 .2.13.27.3.27h1.92c.2 0 .25-.09.25-.27V6.23c0-.09-.08-.17-.17-.17zm-1.05-3.38c-.77 0-1.38.61-1.38 1.38 0 .77.61 1.38 1.38 1.38.75 0 1.36-.61 1.36-1.38 0-.77-.61-1.38-1.36-1.38zm16.49-.25h-2.11c-.09 0-.17.08-.17.17v4.09h-3.31V2.6c0-.09-.08-.17-.17-.17h-2.13c-.09 0-.17.08-.17.17v11.11c0 .09.09.17.17.17h2.13c.09 0 .17-.08.17-.17V8.96h3.31l-.02 4.75c0 .09.08.17.17.17h2.13c.09 0 .17-.08.17-.17V2.6c0-.09-.08-.17-.17-.17zM8.81 7.35v5.74c0 .04-.01.11-.06.13 0 0-1.25.89-3.31.89-2.49 0-5.44-.78-5.44-5.92S2.58 1.99 5.1 2c2.18 0 3.06.49 3.2.58.04.05.06.09.06.14L7.94 4.5c0 .09-.09.2-.2.17-.36-.11-.9-.33-2.17-.33-1.47 0-3.05.42-3.05 3.73s1.5 3.7 2.58 3.7c.92 0 1.25-.11 1.25-.11v-2.3H4.88c-.11 0-.19-.08-.19-.17V7.35c0-.09.08-.17.19-.17h3.74c.11 0 .19.08.19.17z"></path></svg>
</a>.
</footer>

</div>

<script src="wasm_exec.js"></script>
<script src="term.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
<script>
const urlParams = new URLSearchParams(window.location.search);
let shareData = urlParams.get('share')
if (shareData) {
window.input.value = atob(shareData)
}


this.shareButton.addEventListener('click',function(e) {
urlParams.set('share', btoa(window.input.value));
window.location.search = urlParams;
});


window.input.addEventListener('keydown', function(e) {
if (e.key == 'Tab') {
e.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;

// set textarea value to: text before caret + tab + text after caret
this.value = this.value.substring(0, start) +
"\t" + this.value.substring(end);

// put caret at right position again
this.selectionStart =
this.selectionEnd = start + 1;
}
});


(function() {
if (!('WebAssembly' in window)) {
document.body.innerText = 'Sorry, you need a browser with WebAssembly support';
return;
}

set_output(window.output);

fetch('main.wasm').then(response => response.arrayBuffer()).then(function(bin) {
this.runButton.addEventListener('click',function(e) {
window.output.innerText = ""

const go = new Go();
go.argv = ['rocket-lang', '-e', window.input.value];
go.exit = (code) => {
if (code > 0)
output.innerText += 'Exit ' + code + '\n';
};
WebAssembly.instantiate(bin, go.importObject).then((result) => {
go.run(result.instance);
});
})
window.input.placeholder = 'Ready!';
window.input.disabled = false;
});
})();
</script>
</body>
</html>
Loading