Skip to content

Commit

Permalink
Use env logger to print parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenreup committed Nov 17, 2024
1 parent 33c2a99 commit 8a5b87f
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.22
go-version: 1.23

- name: Set up gotestfmt
uses: gotesttools/gotestfmt-action@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ">=1.22.2"
go-version: ">=1.23.0"

- run: go mod tidy
# - name: Run tests
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ build:

build-wasm:
set TEMP=U:\projects\skybox\duwa\chewa\temp
@GOOS=js GOARCH=wasm tinygo build -o ../chewa-site/public/duwa.wasm -opt 1 ./src/cmd/wasm/duwa.go
@GOOS=js GOARCH=wasm tinygo build -o ../duwa-site/public/duwa.wasm -opt 1 ./src/cmd/wasm/duwa.go

build-docker:
@docker-compose up -d
@docker-compose exec tinygo-dev tinygo build -o ./bin/duwa.wasm -target=wasm ./src/cmd/wasm/duwa.go
@docker-compose exec tinygo-dev cp /tinygo/targets/wasm_exec.js /app/bin/
@cp ./bin/duwa.wasm ../chewa-site/public/duwa.wasm
@cp ./bin/wasm_exec.js ../chewa-site/src/components/editor/wasm_exec.js
@cp ./bin/duwa.wasm ../duwa-site/public/duwa.wasm
@cp ./bin/wasm_exec.js ../duwa-site/public/wasm_exec.js
@docker-compose stop

build-all: build build-wasm
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/sevenreup/duwa

go 1.22.0
go 1.23.0

require github.com/shopspring/decimal v1.3.1
8 changes: 8 additions & 0 deletions src/cmd/wasm/duwa.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ func emitConsoleEvent(r slog.Record) {
"level": slogLevelToConsoleLevel(r.Level),
})

logType := "runtime"
for attr := range r.Attrs {
if attr.Key == "type" {
logType = attr.Value.String()
}
}
eventInit.Get("detail").Set("type", logType)

event := js.Global().Get("CustomEvent").New("goConsoleEvent", eventInit)

// Dispatch the event
Expand Down
2 changes: 1 addition & 1 deletion src/duwa/duwa.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *Duwa) run(data []byte) object.Object {
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
utils.PrintParserErrors(os.Stdout, p.Errors())
utils.PrintParserErrors(c.Environment.Logger, p.Errors())
}
return evaluator.Eval(program, c.Environment)
}
Expand Down
6 changes: 4 additions & 2 deletions src/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"log/slog"

"github.com/sevenreup/duwa/src/evaluator"
"github.com/sevenreup/duwa/src/object"
Expand All @@ -19,8 +20,9 @@ func Start(in io.Reader, out io.Writer) {
object.RegisterEvaluator(evaluator.Eval)
scanner := bufio.NewScanner(in)
env := object.Default()
log := slog.Default()
for {
fmt.Printf(PROMPT)
fmt.Print(PROMPT)
scanned := scanner.Scan()
if !scanned {
return
Expand All @@ -30,7 +32,7 @@ func Start(in io.Reader, out io.Writer) {
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
utils.PrintParserErrors(out, p.Errors())
utils.PrintParserErrors(log, p.Errors())
continue
}
evaluated := evaluator.Eval(program, env)
Expand Down
17 changes: 11 additions & 6 deletions src/utils/logging.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package utils

import "io"
import (
"log/slog"
"strings"
)

const ERROR_HEDEAR = "Errorr!!"

func PrintParserErrors(out io.Writer, errors []string) {
io.WriteString(out, ERROR_HEDEAR)
io.WriteString(out, "Woops! We ran into some monkey business here!\n")
io.WriteString(out, " parser errors:\n")
func PrintParserErrors(logger *slog.Logger, errors []string) {
var builder strings.Builder
builder.WriteString(ERROR_HEDEAR)
builder.WriteString("Parser errors:\n")
for _, msg := range errors {
io.WriteString(out, "\t"+msg+"\n")
builder.WriteString("\t" + msg + "\n")
}

logger.Error(builder.String(), "type", "parser")
}

0 comments on commit 8a5b87f

Please sign in to comment.