aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2019-06-26 14:07:01 +0000
committerChristine Dodrill <me@christine.website>2019-06-26 14:07:01 +0000
commit29d37991ba40df0ad8c31035fbbca64806d87622 (patch)
tree697b008065ccde9a766ffda3f25a7007f87d0e7b /cmd
parentd84e24a979831ba2e7d392dee740cb80c2ebe607 (diff)
downloadx-29d37991ba40df0ad8c31035fbbca64806d87622.tar.xz
x-29d37991ba40df0ad8c31035fbbca64806d87622.zip
cmd/h: more feature completeness
Diffstat (limited to 'cmd')
-rw-r--r--cmd/h/compile.go7
-rw-r--r--cmd/h/http.go137
-rw-r--r--cmd/h/main.go17
-rw-r--r--cmd/h/version.go13
4 files changed, 159 insertions, 15 deletions
diff --git a/cmd/h/compile.go b/cmd/h/compile.go
index 7b4d6e3..a2077da 100644
--- a/cmd/h/compile.go
+++ b/cmd/h/compile.go
@@ -7,9 +7,11 @@ import (
"os"
"os/exec"
"path/filepath"
+ "strings"
"text/template"
"time"
+ "github.com/eaburns/peggy/peg"
"within.website/x/h"
)
@@ -33,6 +35,7 @@ type CompiledProgram struct {
Source string `json:"src"`
WebAssemblyText string `json:"wat"`
Binary []byte `json:"bin"`
+ AST string `json:"ast"`
}
func compile(source string) (*CompiledProgram, error) {
@@ -41,8 +44,12 @@ func compile(source string) (*CompiledProgram, error) {
return nil, err
}
+ var sb strings.Builder
+ err = peg.PrettyWrite(&sb, tree)
+
result := CompiledProgram{
Source: source,
+ AST: sb.String(),
}
dir, err := ioutil.TempDir("", "h")
diff --git a/cmd/h/http.go b/cmd/h/http.go
index 0b7bbd0..410fab2 100644
--- a/cmd/h/http.go
+++ b/cmd/h/http.go
@@ -15,6 +15,8 @@ var (
func doHTTP() error {
http.Handle("/", doTemplate(indexTemplate))
http.Handle("/docs", doTemplate(docsTemplate))
+ http.Handle("/faq", doTemplate(faqTemplate))
+ http.Handle("/play", doTemplate(playgroundTemplate))
http.HandleFunc("/api/playground", runPlayground)
return http.ListenAndServe(":"+*port, nil)
@@ -47,6 +49,7 @@ func runPlayground(w http.ResponseWriter, r *http.Request) {
return
}
+ w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(struct {
Program *CompiledProgram `json:"prog"`
Results *ExecResult `json:"res"`
@@ -86,15 +89,15 @@ const indexTemplate = `<html>
<h2>Example Program</h2>
- <code>
- h
- </code>
+ <code><pre>
+h
+ </pre></code>
<p>Outputs:</p>
- <code>
- h
- </code>
+ <code><pre>
+h
+ </pre></code>
<hr />
@@ -189,7 +192,7 @@ const docsTemplate = `<html>
<h1>Documentation</h1>
- <p><big id="comingsoon">Coming out soon...</big></p>
+ <p><big id="comingsoon">Coming soon...</big></p>
<script>
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
@@ -203,5 +206,121 @@ const docsTemplate = `<html>
</script>
</main>
</body>
-</html>
-`
+</html>`
+
+const faqTemplate = `<html>
+ <head>
+ <title>The h Programming Language - FAQ</title>
+ <link rel="stylesheet" href="https://within.website/static/gruvbox.css">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ </head>
+ <body>
+ <main>
+ <nav>
+ <a href="/">The h Programming Language</a> -
+ <a href="/docs">Docs</a> -
+ <a href="/play">Playground</a> -
+ <a href="/faq">FAQ</a>
+ </nav>
+
+ <h1>Frequently Asked Questions</h1>
+
+ <h2>What are the instructions of h?</h2>
+
+ <p>h supports the following instructions:</p>
+ <ul>
+ <li><code>h</code></li>
+ <li><code>'</code></li>
+ </ul>
+
+ <p>All valid h instructions must be separated by a space (<code>\0x20</code> or the spacebar on your computer). No other forms of whitespace are permitted. Any other characters will render your program <a href="http://jbovlaste.lojban.org/dict/gentoldra">gentoldra</a>.</p>
+
+ <h2>How do I install and use h?</h2>
+
+ <p>With any computer running <a href="https://golang.org">Go</a> 1.11 or higher:</p>
+
+ <code><pre>
+go get -u -v within.website/x/cmd/h
+ </pre></code>
+
+ Usage is simple:
+
+ <code><pre>
+Usage of h:
+ -config string
+ configuration file, if set (see flagconfyg(4))
+ -koan
+ if true, print the h koan and then exit
+ -license
+ show software licenses?
+ -manpage
+ generate a manpage template?
+ -max-playground-bytes int
+ how many bytes of data should users be allowed to
+ post to the playground? (default 75)
+ -o string
+ if specified, write the webassembly binary created
+ by -p here
+ -o-wat string
+ if specified, write the uncompiled webassembly
+ created by -p here
+ -p string
+ h program to compile/run
+ -port string
+ HTTP port to listen on
+ -v if true, print the version of h and then exit
+ </pre></code>
+
+ <h2>What version is h?</h2>
+
+ <p>Version 1.0, this will hopefully be the only release.</p>
+
+ <h2>What is the h koan?</h2>
+
+ <p>And Jesus said unto the theologians, "Who do you say that I am?"</p>
+
+ <p>They replied: "You are the eschatological manifestation of the ground of our being, the kerygma of which we find the ultimate meaning in our interpersonal relationships."</p>
+
+ <p>And Jesus said "...What?"</p>
+
+ <p>Some time passed and one of them spoke "h".</p>
+
+ <p>Jesus was enlightened.</p>
+
+ <h2>Why?</h2>
+
+ <p>That's a good question. The following blogposts may help you understand this more:</p>
+
+ <h2>Who wrote h?</h2>
+
+ <p><a href="https://christine.website">Within</a></p>
+
+ <ul>
+ <li><a href="https://christine.website/blog/the-origin-of-h-2015-12-14">The Origin of h</a></li>
+ <li><a href="https://christine.website/blog/formal-grammar-of-h-2019-05-19">A Formal Grammar of h</a></li>
+ </ul>
+ </main>
+ </body>
+</html>`
+
+const playgroundTemplate = `<html>
+ <head>
+ <title>The h Programming Language - Playground</title>
+ <link rel="stylesheet" href="https://within.website/static/gruvbox.css">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ </head>
+ <body>
+ <main>
+ <nav>
+ <a href="/">The h Programming Language</a> -
+ <a href="/docs">Docs</a> -
+ <a href="/play">Playground</a> -
+ <a href="/faq">FAQ</a>
+ </nav>
+
+ <h1>Playground</h1>
+
+ <p><small>Unfortunately, Javascript is required to use this page, sorry.</small></p>
+ </main>
+ </body>
+</html>`
diff --git a/cmd/h/main.go b/cmd/h/main.go
index cdfbf99..96f601b 100644
--- a/cmd/h/main.go
+++ b/cmd/h/main.go
@@ -11,14 +11,15 @@ import (
)
var (
- program = flag.String("p", "", "h program to compile/run")
- outFname = flag.String("o", "", "if specified, write the webassembly binary created by -p here")
- watFname = flag.String("o-wat", "", "if specified, write the uncompiled webassembly created by -p here")
- port = flag.String("port", "", "HTTP port to listen on")
- writeTao = flag.Bool("koan", false, "if true, print the h koan and then exit")
+ program = flag.String("p", "", "h program to compile/run")
+ outFname = flag.String("o", "", "if specified, write the webassembly binary created by -p here")
+ watFname = flag.String("o-wat", "", "if specified, write the uncompiled webassembly created by -p here")
+ port = flag.String("port", "", "HTTP port to listen on")
+ writeTao = flag.Bool("koan", false, "if true, print the h koan and then exit")
+ writeVersion = flag.Bool("v", false, "if true, print the version of h and then exit")
)
-const koan = `And Jesus said unto the theologians, "Who do you say that I am?".
+const koan = `And Jesus said unto the theologians, "Who do you say that I am?"
They replied: "You are the eschatological manifestation of the ground of our
being, the kerygma of which we find the ultimate meaning in our interpersonal
@@ -79,6 +80,10 @@ func oneOff() error {
func main() {
internal.HandleStartup()
+ if *writeVersion {
+ dumpVersion()
+ }
+
if *writeTao {
tao()
}
diff --git a/cmd/h/version.go b/cmd/h/version.go
new file mode 100644
index 0000000..f321497
--- /dev/null
+++ b/cmd/h/version.go
@@ -0,0 +1,13 @@
+package main
+
+import (
+ "fmt"
+ "os"
+)
+
+const version = "1.0.0"
+
+func dumpVersion() {
+ fmt.Println("h programming language version", version)
+ os.Exit(0)
+}