package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"github.com/rs/cors"
"within.website/ln/ex"
)
var (
maxBytes = flag.Int64("max-playground-bytes", 75, "how many bytes of data should users be allowed to post to the playground?")
)
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)
srv := &http.Server{
Addr: ":" + *port,
Handler: ex.HTTPLog(cors.Default().Handler(http.DefaultServeMux)),
}
if *sockpath != "" {
os.RemoveAll(*sockpath)
l, err := net.Listen("unix", *sockpath)
if err != nil {
return fmt.Errorf("can't listen on %s: %v", *sockpath, err)
}
defer l.Close()
return srv.Serve(l)
} else {
return srv.ListenAndServe()
}
}
func httpError(w http.ResponseWriter, err error, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(struct {
Error string `json:"err"`
}{
Error: err.Error(),
})
}
func runPlayground(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.NotFound(w, r)
return
}
rc := http.MaxBytesReader(w, r.Body, *maxBytes)
defer rc.Close()
data, err := ioutil.ReadAll(rc)
if err != nil {
httpError(w, err, http.StatusBadGateway)
return
}
comp, err := compile(string(data))
if err != nil {
httpError(w, fmt.Errorf("compliation error: %v", err), http.StatusBadRequest)
return
}
er, err := run(comp.Binary)
if err != nil {
httpError(w, fmt.Errorf("runtime error: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(struct {
Program *CompiledProgram `json:"prog"`
Results *ExecResult `json:"res"`
}{
Program: comp,
Results: er,
})
}
func doTemplate(body string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintln(w, body)
})
}
const indexTemplate = `
The h Programming Language
The h Programming Language
A simple, fast, open-source, complete and safe language for developing modern software for the web
Example Program
h
Outputs:
h
Fast Compilation
h compiles hundreds of characters of source per second. I didn't really test how fast it is, but when I was testing it the speed was fast enough that I didn't care to profile it.
Safety
h is completely memory safe with no garbage collector or heap allocations. It does not allow memory leaks to happen, nor do any programs in h have the possibility to allocate memory.
No null
Completely deterministic behavior
No mutable state
No persistence
All functions are pure functions
No sandboxing required
Zero* Dependencies
h generates WebAssembly, so every binary produced by the compiler is completely dependency free save a single system call: h.h. This allows for modern, future-proof code that will work on all platforms.
Simple
h has a simple grammar that gzips to 117 bytes. Creating a runtime environment for h is so trivial just about anyone can do it.
Platform Support
h supports the following platforms:
Google Chrome
Electron
Chromium Embedded Framework
Microsoft Edge
Olin
International Out of the Box
h supports multiple written and spoken languages with true contextual awareness. It not only supports the Latin h as input, it also accepts the Lojbanic' as well. This allows for full 100% internationalization into Lojban should your project needs require it.
Testimonials
Not convinced? Take the word of people we probably didn't pay for their opinion.
I don't see the point of this.
This solves all my problems. All of them. Just not in the way I expected it to.
Yes.
Perfect.
h is the backbone of my startup.
Open-Source
The h compiler and default runtime are open-source free software sent out into the Public Domain. You can use h for any purpose at all with no limitations or restrictions.
`
const docsTemplate = `
The h Programming Language - Docs
Documentation
Coming soon...
`
const faqTemplate = `
The h Programming Language - FAQ
Frequently Asked Questions
What are the instructions of h?
h supports the following instructions:
h
'
All valid h instructions must be separated by a space (\0x20 or the spacebar on your computer). No other forms of whitespace are permitted. Any other characters will render your program gentoldra.
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
What version is h?
Version 1.0, this will hopefully be the only release.
What is the h 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 relationships."
And Jesus said "...What?"
Some time passed and one of them spoke "h".
Jesus was enlightened.
Why?
That's a good question. The following blogposts may help you understand this more: