From 41bb9eb9d3881bfb48bb3077a0d83b802a5bb1ab Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 28 Nov 2019 17:41:03 +0000 Subject: move hlang to another repo --- cmd/h/Dockerfile | 19 --- cmd/h/compile.go | 105 -------------- cmd/h/http.go | 423 ------------------------------------------------------- cmd/h/main.go | 133 ----------------- cmd/h/run.go | 73 ---------- cmd/h/version.go | 13 -- 6 files changed, 766 deletions(-) delete mode 100644 cmd/h/Dockerfile delete mode 100644 cmd/h/compile.go delete mode 100644 cmd/h/http.go delete mode 100644 cmd/h/main.go delete mode 100644 cmd/h/run.go delete mode 100644 cmd/h/version.go (limited to 'cmd') diff --git a/cmd/h/Dockerfile b/cmd/h/Dockerfile deleted file mode 100644 index 5d3ea5a..0000000 --- a/cmd/h/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -ARG X_VERSION -FROM xena/xperimental:$X_VERSION AS x - -FROM xena/alpine AS build -WORKDIR /wabt -RUN apk --no-cache add build-base cmake git python \ - && git clone --recursive https://github.com/WebAssembly/wabt /wabt \ - && mkdir build \ - && cd build \ - && cmake .. \ - && make && make install -RUN ldd $(which wat2wasm) - -FROM xena/alpine -COPY --from=build /usr/local/bin/wat2wasm /usr/local/bin/wat2wasm -COPY --from=x /usr/local/bin/h /usr/local/bin/h -ENV PORT 5000 -RUN apk --no-cache add libstdc++ -CMD /usr/local/bin/h diff --git a/cmd/h/compile.go b/cmd/h/compile.go deleted file mode 100644 index a2077da..0000000 --- a/cmd/h/compile.go +++ /dev/null @@ -1,105 +0,0 @@ -package main - -import ( - "bytes" - "context" - "io/ioutil" - "os" - "os/exec" - "path/filepath" - "strings" - "text/template" - "time" - - "github.com/eaburns/peggy/peg" - "within.website/x/h" -) - -var ( - wat2wasmLoc string - wasmTemplateObj *template.Template -) - -func init() { - loc, err := exec.LookPath("wat2wasm") - if err != nil { - panic(err) - } - - wat2wasmLoc = loc - wasmTemplateObj = template.Must(template.New("h.wast").Parse(wasmTemplate)) -} - -// CompiledProgram is a fully parsed and compiled h program. -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) { - tree, err := h.Parse(source) - if err != nil { - return nil, err - } - - var sb strings.Builder - err = peg.PrettyWrite(&sb, tree) - - result := CompiledProgram{ - Source: source, - AST: sb.String(), - } - - dir, err := ioutil.TempDir("", "h") - if err != nil { - return nil, err - } - defer os.RemoveAll(dir) - - fout, err := os.Create(filepath.Join(dir, "h.wast")) - if err != nil { - return nil, err - } - - buf := bytes.NewBuffer(nil) - - err = wasmTemplateObj.Execute(buf, []byte(tree.Text)) - if err != nil { - return nil, err - } - - result.WebAssemblyText = buf.String() - _, err = fout.WriteString(result.WebAssemblyText) - if err != nil { - return nil, err - } - - fname := fout.Name() - - err = fout.Close() - if err != nil { - return nil, err - } - - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - - cmd := exec.CommandContext(ctx, wat2wasmLoc, fname, "-o", filepath.Join(dir, "h.wasm")) - cmd.Dir = dir - - err = cmd.Run() - if err != nil { - return nil, err - } - - data, err := ioutil.ReadFile(filepath.Join(dir, "h.wasm")) - if err != nil { - return nil, err - } - - result.Binary = data - - return &result, nil -} diff --git a/cmd/h/http.go b/cmd/h/http.go deleted file mode 100644 index b95ba18..0000000 --- a/cmd/h/http.go +++ /dev/null @@ -1,423 +0,0 @@ -package main - -import ( - "encoding/json" - "flag" - "fmt" - "io/ioutil" - "net/http" - - "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) - - return http.ListenAndServe(":"+*port, ex.HTTPLog(cors.Default().Handler(http.DefaultServeMux))) -} - -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.

- - - -
- -

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:

- - - -
- -

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.

- - - -
- -

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:

- - -

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.

- -

How do I install and use h?

- -

With any computer running Go 1.11 or higher:

- -
go get -u -v within.website/x/cmd/h
- - Usage is simple: - -
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:

- - - -

Who wrote h?

- -

Within

- -
- - -
- -` - -const playgroundTemplate = ` - - The h Programming Language - Playground - - - - -
- - -

Playground

- -

Unfortunately, Javascript is required to use this page, sorry.

- -

Program

- - - - -

- -

Output

- -
- -

WebAssembly Text Format

- -
- -

Gas used:

-

Execution time (nanoseconds):

- -

AST

- -
- - - -
- - -
- -` diff --git a/cmd/h/main.go b/cmd/h/main.go deleted file mode 100644 index 63229ac..0000000 --- a/cmd/h/main.go +++ /dev/null @@ -1,133 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io/ioutil" - "log" - "os" - - "within.website/x/internal" -) - -var _ = func() error { log.SetFlags(log.LstdFlags | log.Llongfile); return nil }() - -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") - 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?" - -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.` - -func tao() { - fmt.Println(koan) - os.Exit(0) -} - -func oneOff() error { - log.Println("compiling...") - comp, err := compile(*program) - if err != nil { - return err - } - - log.Println("running...") - er, err := run(comp.Binary) - if err != nil { - return err - } - - log.Println("success!") - - log.Printf("gas used:\t%d", er.GasUsed) - log.Printf("exec time:\t%s", er.ExecTime) - log.Println("output:") - fmt.Print(er.Output) - - if *outFname != "" { - err := ioutil.WriteFile(*outFname, comp.Binary, 0666) - if err != nil { - return err - } - - log.Printf("wrote %d bytes to %s", len(comp.Binary), *outFname) - } - - if *watFname != "" { - err := ioutil.WriteFile(*watFname, []byte(comp.WebAssemblyText), 0666) - if err != nil { - return err - } - - log.Printf("write %d bytes of source to %s", len(comp.WebAssemblyText), *watFname) - } - - return nil -} - -func main() { - internal.HandleStartup() - - if *writeVersion { - dumpVersion() - } - - if *writeTao { - tao() - } - - if *program != "" { - err := oneOff() - if err != nil { - panic(err) - } - - return - } - - if *port != "" { - err := doHTTP() - if err != nil { - panic(err) - } - - return - } -} - -const wasmTemplate = `(module - (import "h" "h" (func $h (param i32))) - (func $h_main - (local i32 i32 i32) - (local.set 0 (i32.const 10)) - (local.set 1 (i32.const 104)) - (local.set 2 (i32.const 39)) - {{ range . -}} - {{ if eq . 32 -}} - (call $h (get_local 0)) - {{ end -}} - {{ if eq . 104 -}} - (call $h (get_local 1)) - {{ end -}} - {{ if eq . 39 -}} - (call $h (get_local 2)) - {{ end -}} - {{ end -}} - (call $h (get_local 0)) - ) - (export "h" (func $h_main)) -)` diff --git a/cmd/h/run.go b/cmd/h/run.go deleted file mode 100644 index fb55bb7..0000000 --- a/cmd/h/run.go +++ /dev/null @@ -1,73 +0,0 @@ -package main - -import ( - "errors" - "time" - - "github.com/perlin-network/life/compiler" - "github.com/perlin-network/life/exec" -) - -type Process struct { - Output []byte -} - -// ResolveGlobal does nothing, currently. -func (p *Process) ResolveGlobal(module, field string) int64 { return 0 } - -// ResolveFunc resolves h's ABI and importable function. -func (p *Process) ResolveFunc(module, field string) exec.FunctionImport { - switch module { - case "h": - switch field { - case "h": - return func(vm *exec.VirtualMachine) int64 { - frame := vm.GetCurrentFrame() - data := frame.Locals[0] - p.Output = append(p.Output, byte(data)) - - return 0 - } - - default: - panic("impossible state") - } - - default: - panic("impossible state") - } -} - -type ExecResult struct { - Output string `json:"out"` - GasUsed uint64 `json:"gas"` - ExecTime time.Duration `json:"exec_duration"` -} - -func run(bin []byte) (*ExecResult, error) { - p := &Process{} - - var cfg exec.VMConfig - gp := &compiler.SimpleGasPolicy{GasPerInstruction: 1} - vm, err := exec.NewVirtualMachine(bin, cfg, p, gp) - if err != nil { - return nil, err - } - - mainFunc, ok := vm.GetFunctionExport("h") - if !ok { - return nil, errors.New("impossible state: no h function exposed") - } - - t0 := time.Now() - _, err = vm.Run(mainFunc) - if err != nil { - return nil, err - } - - return &ExecResult{ - Output: string(p.Output), - GasUsed: vm.Gas, - ExecTime: time.Since(t0), - }, nil -} diff --git a/cmd/h/version.go b/cmd/h/version.go deleted file mode 100644 index f321497..0000000 --- a/cmd/h/version.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "fmt" - "os" -) - -const version = "1.0.0" - -func dumpVersion() { - fmt.Println("h programming language version", version) - os.Exit(0) -} -- cgit v1.2.3