Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 539bcd3

Browse files
committed
chore: created custom writer for playground eval
1 parent eab558b commit 539bcd3

11 files changed

Lines changed: 74 additions & 30 deletions

File tree

cmd/aurora/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ var runCmd = &cobra.Command{
133133
}
134134
ev := evaluator.New(evaluator.NewEvaluatorOptions{
135135
EnableLogging: slices.Contains(loggers, "evaluator"),
136+
EchoWriter: os.Stdout,
137+
PrintWriter: os.Stdout,
136138
})
137139
if player {
138140
ev.SetPlayer(evaluator.NewPlayer(os.Stdin))

cmd/playground/main.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,17 @@ func init() {
5252

5353
temps, err := evaluator.New(evaluator.NewEvaluatorOptions{
5454
EnableLogging: false,
55+
EchoWriter: ToPlaygroundWriter("echo"),
56+
PrintWriter: ToPlaygroundWriter("print"),
5557
}).Evaluate(insts)
5658
if err != nil {
5759
fmt.Println(err)
5860
return nil
5961
}
60-
output := document.Call("getElementById", "output")
61-
fmt.Println(output)
6262
for _, temp := range temps {
6363
u8 := js.Global().Get("Uint8Array").New(len(temp))
6464
js.CopyBytesToJS(u8, temp)
6565
js.Global().Call("evalResultHandler", u8)
66-
67-
/*
68-
code := document.Call("createElement", "code")
69-
code.Set("innerHTML", fmt.Sprintf("= %v", temp))
70-
li := document.Call("createElement", "li")
71-
li.Call("appendChild", code)
72-
output.Call("appendChild", li)
73-
*/
7466
}
7567
return nil
7668
})

cmd/playground/writer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build js && wasm
2+
3+
package main
4+
5+
import (
6+
"io"
7+
"syscall/js"
8+
)
9+
10+
type pw struct {
11+
builtin string
12+
}
13+
14+
func (w *pw) Write(bs []byte) (int, error) {
15+
u8 := js.Global().Get("Uint8Array").New(len(bs))
16+
js.CopyBytesToJS(u8, bs)
17+
js.Global().Call("evalResultHandler", u8, w.builtin)
18+
return len(bs), nil
19+
}
20+
21+
func ToPlaygroundWriter(builtin string) io.Writer {
22+
return &pw{builtin}
23+
}

evaluator/builtin/functions.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package builtin
22

33
import (
44
"fmt"
5+
"io"
56
)
67

7-
func PrintFunction(bs []byte) {
8-
_, _ = fmt.Printf("%v\n", bs)
8+
func PrintFunction(w io.Writer, bs []byte) {
9+
_, _ = w.Write(bs)
910
}
1011

1112
// EchoFunction converts bytes to text and prints it
1213
// If bytes represent a reel (array of tapes), it prints each tape in sequence
1314
// If bytes represent a tape (8 bytes), it prints the tape as a character
1415
// If bytes represent a number, it encodes it as text (ASCII character)
15-
func EchoFunction(bs []byte) {
16+
func EchoFunction(w io.Writer, bs []byte) {
1617
// Check if this is a reel (array of tapes)
1718
// A reel is a concatenation of multiple 8-byte tapes
1819
// If the length is a multiple of 8 and greater than 8, it's likely a reel
@@ -32,9 +33,9 @@ func EchoFunction(bs []byte) {
3233
}
3334
}
3435
if result != "" {
35-
fmt.Println(result)
36+
_, _ = w.Write([]byte(result))
3637
} else {
37-
fmt.Println()
38+
_, _ = w.Write([]byte("\n"))
3839
}
3940
return
4041
}
@@ -53,10 +54,10 @@ func EchoFunction(bs []byte) {
5354
char := rune(significant[len(significant)-1])
5455
if char >= 32 && char <= 126 {
5556
// Printable ASCII character
56-
fmt.Println(string(char))
57+
_, _ = w.Write([]byte(string(char)))
5758
} else {
5859
// Non-printable, print as-is
59-
fmt.Println(string(significant))
60+
_, _ = w.Write([]byte(string(significant)))
6061
}
6162
}
6263

evaluator/evaluator.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package evaluator
33
import (
44
"encoding/binary"
55
"fmt"
6+
"io"
67
"math"
78

89
"github.com/guiferpa/aurora/byteutil"
@@ -21,6 +22,8 @@ type Evaluator struct {
2122
result [][]byte
2223
counter *uint64
2324
assertErrors []string // Buffer to collect assert errors
25+
echoWriter io.Writer
26+
printWriter io.Writer
2427
}
2528

2629
func isTemp(bs []byte) bool {
@@ -251,13 +254,13 @@ func (e *Evaluator) exec(label []byte, op byte, left, right []byte) error {
251254
}
252255

253256
if op == emitter.OpPrint {
254-
builtin.PrintFunction(left)
257+
builtin.PrintFunction(e.printWriter, left)
255258
e.cursor++
256259
return nil
257260
}
258261

259262
if op == emitter.OpEcho {
260-
builtin.EchoFunction(left)
263+
builtin.EchoFunction(e.echoWriter, left)
261264
e.cursor++
262265
return nil
263266
}
@@ -498,6 +501,8 @@ func (e *Evaluator) SetPlayer(player *Player) {
498501
type NewEvaluatorOptions struct {
499502
EnableLogging bool
500503
Player *Player
504+
EchoWriter io.Writer
505+
PrintWriter io.Writer
501506
}
502507

503508
func New(options NewEvaluatorOptions) *Evaluator {
@@ -510,5 +515,7 @@ func New(options NewEvaluatorOptions) *Evaluator {
510515
result: make([][]byte, 0),
511516
counter: new(uint64),
512517
assertErrors: make([]string, 0),
518+
echoWriter: options.EchoWriter,
519+
printWriter: options.PrintWriter,
513520
}
514521
}

playground/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ assets:
1717

1818
build:
1919
@rm -rf ./dist/
20-
@GOOS=js GOARCH=wasm go build -o ./dist/main.wasm ../cmd/playground/main.go
20+
@GOOS=js GOARCH=wasm go build -o ./dist/main.wasm ../cmd/playground/*.go
2121

2222
# --------------------------------------------------------------------
2323

playground/dist/index.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@
149149
<div id="info">
150150
<span id="version"></span>
151151
</div>
152-
<ul id="output">
153-
<li><code>= [fasdfasfsadfasdf]</code></li>
154-
<li><code>= [fasdfasfsadfasdf]</code></li>
155-
</ul>
152+
<ul id="output"></ul>
156153
<div id="controls">
157154
<button id="runner" disabled>Run</button>
158155
</div>

playground/dist/main.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function print(result) {
2-
console.log("PRINT", result);
2+
return `(print) ${toHex(result)}`;
33
}
44

55
function echo(result) {
6-
console.log("ECHO", result);
6+
return `(echo) ${toText(result)}`;
77
}
88

99
const builtins = {
@@ -34,8 +34,17 @@ async function init() {
3434
}
3535
}
3636

37+
function toText(result) {
38+
const decoder = new TextDecoder('utf-8');
39+
return decoder.decode(result);
40+
}
41+
42+
function toHex(result) {
43+
return Array.from(result).map(b => b.toString(16).padStart(2, '0')).join(' ');
44+
}
45+
3746
function resultToText(result) {
38-
const body = Array.from(result).map(b => b.toString(16).padStart(2, '0')).join(' ');
47+
const body = toHex(result);
3948
const len = result.length;
4049
return `= (${len / 8}) ${body}`;
4150
}
@@ -50,6 +59,7 @@ function renderOutput(text) {
5059
}
5160

5261
window.evalResultHandler = (result, builtin) => {
62+
console.log(result, builtin);
5363
const fn = builtins[builtin];
5464
const text = (!fn) ? resultToText(result) : fn(result);
5565
renderOutput(text);

playground/dist/main.wasm

1.16 KB
Binary file not shown.

playground/src/main.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function print(result) {
2-
console.log("PRINT", result);
2+
return `(print) ${toHex(result)}`;
33
}
44

55
function echo(result) {
6-
console.log("ECHO", result);
6+
return `(echo) ${toText(result)}`;
77
}
88

99
const builtins = {
@@ -34,8 +34,17 @@ async function init() {
3434
}
3535
}
3636

37+
function toText(result) {
38+
const decoder = new TextDecoder('utf-8');
39+
return decoder.decode(result);
40+
}
41+
42+
function toHex(result) {
43+
return Array.from(result).map(b => b.toString(16).padStart(2, '0')).join(' ');
44+
}
45+
3746
function resultToText(result) {
38-
const body = Array.from(result).map(b => b.toString(16).padStart(2, '0')).join(' ');
47+
const body = toHex(result);
3948
const len = result.length;
4049
return `= (${len / 8}) ${body}`;
4150
}
@@ -50,6 +59,7 @@ function renderOutput(text) {
5059
}
5160

5261
window.evalResultHandler = (result, builtin) => {
62+
console.log(result, builtin);
5363
const fn = builtins[builtin];
5464
const text = (!fn) ? resultToText(result) : fn(result);
5565
renderOutput(text);

0 commit comments

Comments
 (0)