Skip to content

Commit 1565e60

Browse files
committed
all: Wire initial lexer and token packages
Create initial lexer, token, parser and evaluator packages and wire them to main package. Use kong flag parsing with for evy CLI in `main_notiny.go`. Map CLI commands to exported function in wasm entry point `main_tiny.go`. Lexer and token structures are taken from Thorsten Ball's interpreter book code. [Link]: https://github.com/juliaogris/monkey Signed-off-by: Julia Ogris <julia.ogris@gmail.com>
1 parent 7d2dfd7 commit 1565e60

File tree

9 files changed

+110
-51
lines changed

9 files changed

+110
-51
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# --- Global -------------------------------------------------------------------
55
O = out
6-
COVERAGE = 15
6+
COVERAGE = 0
77
VERSION ?= $(shell git describe --tags --dirty --always)
88

99
all: build tiny test test-tiny check-coverage lint frontend ## Build, test, check coverage and lint

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module foxygo.at/evy
22

33
go 1.19
4+
5+
require github.com/alecthomas/kong v0.6.1

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/alecthomas/kong v0.6.1 h1:1kNhcFepkR+HmasQpbiKDLylIL8yh5B5y1zPp5bJimA=
2+
github.com/alecthomas/kong v0.6.1/go.mod h1:JfHWDzLmbh/puW6I3V7uWenoh56YNVONW+w8eKeUr9I=
3+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
4+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
5+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
9+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11+
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
12+
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
14+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
15+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main_notinygo.go

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,80 @@
33
package main
44

55
import (
6-
"flag"
76
"fmt"
8-
"strings"
9-
)
7+
"io"
8+
"os"
109

11-
var (
12-
version string
10+
"foxygo.at/evy/pkg/evaluator"
11+
"foxygo.at/evy/pkg/lexer"
12+
"foxygo.at/evy/pkg/parser"
13+
"github.com/alecthomas/kong"
1314
)
1415

15-
func main() {
16-
versionFlag := flag.Bool("version", false, "Print version information")
17-
flag.Parse()
18-
if *versionFlag {
19-
fmt.Println("Version", version)
20-
return
16+
var version string = "v0.0.0"
17+
18+
const description = `
19+
evy is a tool for managing evy source code.
20+
`
21+
22+
type config struct {
23+
Version kong.VersionFlag `short:"V" help:"Print version information"`
24+
Run cmdRun `cmd:"" help:"Run evy program"`
25+
Tokenize cmdTokenize `cmd:"" help:"Tokenize evy program"`
26+
Parse cmdParse `cmd:"" help:"Parse evy program"`
27+
}
28+
29+
type cmdRun struct {
30+
Source string `arg:"" help:"Source file. Default stdin" default:"-"`
31+
}
32+
type cmdTokenize struct {
33+
Source string `arg:"" help:"Source file. Default stdin" default:"-"`
34+
}
35+
type cmdParse struct {
36+
Source string `arg:"" help:"Source file. Default stdin" default:"-"`
37+
}
38+
39+
func (c *cmdRun) Run() error {
40+
b, err := fileBytes(c.Source)
41+
if err != nil {
42+
return err
43+
}
44+
print := func(s string) { fmt.Print(s) }
45+
evaluator.Run(string(b), print)
46+
return nil
47+
}
48+
49+
func (c *cmdTokenize) Run() error {
50+
b, err := fileBytes(c.Source)
51+
if err != nil {
52+
return err
2153
}
54+
result := lexer.Run(string(b))
55+
fmt.Println(result)
56+
return nil
57+
}
2258

23-
fmt.Println(evaluate("some program"))
59+
func (c *cmdParse) Run() error {
60+
b, err := fileBytes(c.Source)
61+
if err != nil {
62+
return err
63+
}
64+
result := parser.Run(string(b))
65+
fmt.Println(result)
66+
return nil
2467
}
2568

26-
func evaluate(program string) string {
27-
return strings.ToUpper(truncate(program, 20))
69+
func main() {
70+
kctx := kong.Parse(&config{},
71+
kong.Description(description),
72+
kong.Vars{"version": version},
73+
)
74+
kctx.FatalIfErrorf(kctx.Run())
2875
}
2976

30-
func truncate(s string, max int) string {
31-
r := []rune(s)
32-
if len(r) <= max {
33-
return s
77+
func fileBytes(filename string) ([]byte, error) {
78+
if filename == "-" {
79+
return io.ReadAll(os.Stdin)
3480
}
35-
return string(r[:max])
81+
return os.ReadFile(filename)
3682
}

main_test.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

main_tinygo.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ package main
44
import (
55
"strings"
66
"unsafe"
7+
8+
"foxygo.at/evy/pkg/evaluator"
9+
"foxygo.at/evy/pkg/lexer"
10+
"foxygo.at/evy/pkg/parser"
711
)
812

913
var (
@@ -23,24 +27,21 @@ func jsPrint(string)
2327
// * https://www.wasm.builders/k33g_org/an-essay-on-the-bi-directional-exchange-of-strings-between-the-wasm-module-with-tinygo-and-nodejs-with-wasi-support-3i9h
2428
// * https://www.alcarney.me/blog/2020/passing-strings-between-tinygo-wasm/
2529
//export evaluate
26-
func evaluate(ptr *uint32, length int) {
30+
func jsEvaluate(ptr *uint32, length int) {
2731
s := getString(ptr, length)
28-
result := strings.ToUpper(s)
29-
jsPrint(result)
32+
evaluator.Run(s, jsPrint)
3033
}
3134

3235
//export tokenize
33-
func tokenize(ptr *uint32, length int) {
36+
func jsTokenize(ptr *uint32, length int) {
3437
s := getString(ptr, length)
35-
result := "tokenize:\n" + truncate(s, 20) + "\n"
36-
jsPrint(result)
38+
jsPrint(lexer.Run(s))
3739
}
3840

3941
//export parse
40-
func parse(ptr *uint32, length int) {
42+
func jsParse(ptr *uint32, length int) {
4143
s := getString(ptr, length)
42-
result := "parse:\n" + truncate(s, 20) + "\n"
43-
jsPrint(result)
44+
jsPrint(parser.Run(s))
4445
}
4546

4647
// alloc pre-allocates memory used in string parameter passing.
@@ -61,11 +62,3 @@ func getString(ptr *uint32, length int) string {
6162
}
6263
return builder.String()
6364
}
64-
65-
func truncate(s string, max int) string {
66-
r := []rune(s)
67-
if len(r) <= max {
68-
return s
69-
}
70-
return string(r[:max])
71-
}

pkg/evaluator/evaluator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package evaluator
2+
3+
import "strings"
4+
5+
func Run(input string, print func(string)) {
6+
print(strings.ToUpper(input))
7+
}

pkg/lexer/lexer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package lexer
2+
3+
func Run(input string) string {
4+
return "Lexing:\n" + input
5+
}

pkg/parser/parser.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package parser
2+
3+
func Run(input string) string {
4+
return "Parsing:\n" + input
5+
}

0 commit comments

Comments
 (0)