-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efdb60d
commit 70613b8
Showing
8 changed files
with
1,080 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"net/http" | ||
) | ||
|
||
var addr = ":9090" | ||
|
||
func main() { | ||
fmt.Printf("listening on %v\n", addr) | ||
err := http.ListenAndServe(addr, http.FileServer(http.Dir("web"))) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "error: unable to start server: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
//go:build js | ||
|
||
package main | ||
|
||
import ( | ||
"syscall/js" | ||
|
||
"github.com/blackchip-org/zc/v6" | ||
"github.com/blackchip-org/zc/v6/app" | ||
"github.com/blackchip-org/zc/v6/app/repl" | ||
) | ||
|
||
var ( | ||
c *app.Calc | ||
r *repl.Repl | ||
) | ||
|
||
func zcCommonPrefix() js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
if len(args) != 1 { | ||
panic("zcCommonPrefix: invalid number of arguments") | ||
} | ||
jsValues := args[0] | ||
var outValues []string | ||
for i := 0; i < jsValues.Length(); i++ { | ||
outValues = append(outValues, jsValues.Index(i).String()) | ||
} | ||
common := repl.CommonPrefix(outValues) | ||
return common | ||
}) | ||
} | ||
|
||
func zcEval() js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
in := args[0].String() | ||
r.Eval(in) | ||
var err string | ||
var stack []any | ||
|
||
if r.Error() != nil { | ||
err = r.Error().Error() | ||
} | ||
|
||
items := c.Items() | ||
for _, item := range items { | ||
stack = append(stack, map[string]any{ | ||
"value": valueOf(item), | ||
"label": item.Label, | ||
"unit": item.Unit, | ||
}) | ||
} | ||
return map[string]any{ | ||
"stack": stack, | ||
"notice": r.Notice(), | ||
"error": err, | ||
} | ||
}) | ||
} | ||
|
||
func zcStackLen() js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
return c.Len() | ||
}) | ||
} | ||
|
||
func zcStack() js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
var stack []any | ||
for _, item := range c.Items() { | ||
stack = append(stack, map[string]any{ | ||
"value": valueOf(item), | ||
"label": item.Label, | ||
"unit": item.Unit, | ||
}) | ||
} | ||
return stack | ||
}) | ||
} | ||
|
||
func zcOpNames() js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
var names []any | ||
for _, n := range c.Catalog.OpNames() { | ||
names = append(names, n) | ||
} | ||
return names | ||
}) | ||
} | ||
|
||
func zcSetStack() js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
if len(args) != 1 { | ||
panic("zcSetStack: invalid number of arguments") | ||
} | ||
jsStack := args[0] | ||
c.SetItems([]zc.Item{}) | ||
for i := 0; i < jsStack.Length(); i++ { | ||
item := jsStack.Index(i) | ||
val := item.Get("value") | ||
zc.String.Push(c, val.String()) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func zcQuoteEnd() js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
return r.EndQuote | ||
}) | ||
} | ||
|
||
func zcWordCompleter() js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
if len(args) != 2 { | ||
panic("zcWordCompleter: invalid number of arguments") | ||
} | ||
line := args[0].String() | ||
pos := args[1].Int() | ||
prefix, candidates, suffix := r.WordCompleter(line, pos) | ||
var jsCandidates []any | ||
for _, c := range candidates { | ||
jsCandidates = append(jsCandidates, c) | ||
} | ||
return map[string]any{ | ||
"prefix": prefix, | ||
"candidates": jsCandidates, | ||
"suffix": suffix, | ||
} | ||
}) | ||
} | ||
|
||
func valueOf(item zc.Item) string { | ||
return item.Val() | ||
} | ||
|
||
func main() { | ||
c = app.NewCalc() | ||
r = repl.New(c) | ||
|
||
js.Global().Set("zcCommonPrefix", zcCommonPrefix()) | ||
js.Global().Set("zcEval", zcEval()) | ||
js.Global().Set("zcStack", zcStack()) | ||
js.Global().Set("zcStackLen", zcStackLen()) | ||
js.Global().Set("zcOpNames", zcOpNames()) | ||
js.Global().Set("zcSetStack", zcSetStack()) | ||
js.Global().Set("zcQuoteEnd", zcQuoteEnd()) | ||
js.Global().Set("zcWordCompleter", zcWordCompleter()) | ||
|
||
<-make(chan struct{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//go:build !js | ||
|
||
package main | ||
|
||
func main() {} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<link rel="stylesheet" type="text/css" href="zc.css"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<script src="wasm_exec.js"></script> | ||
<script> | ||
const go = new Go(); | ||
WebAssembly.instantiateStreaming(fetch("zc.wasm"), go.importObject).then((result) => { | ||
go.run(result.instance); | ||
init(); | ||
}); | ||
</script> | ||
<meta name="google" content="notranslate"> | ||
</head> | ||
<body> | ||
<div id="main"> | ||
<div id="output-panel"> | ||
<div id="output"></div> | ||
</div> | ||
<div id="popup"></div> | ||
<div id="footer-panel"> | ||
<div id="footer"> | ||
<label id="prompt" for="input">zc ></label> | ||
<textarea id="input" type="search" rows="1" autocorrect="off" autocapitalize="off" autocomplete="new-password"></textarea> | ||
<button id="auto">?</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
<script src="zc.js"></script> | ||
</html> |
Oops, something went wrong.