Skip to content

Commit

Permalink
wdte: funcmod functions (#204)
Browse files Browse the repository at this point in the history
* res/grammar: Change funcmods.

* scanner: Remove funcmod keywords.

* wdte: Implenent new funcmod system.

* std: Reimplement `memo` and `rev` as funcmod functions.

* std: Attempt to reimplement method.

* scanner: Fix FuncMods test.

* multiple: Fix a bunch of linter warnings.

* multiple: Remove the `nolint`s.
  • Loading branch information
DeedleFake committed Jul 16, 2020
1 parent 1d75089 commit dd5726a
Show file tree
Hide file tree
Showing 23 changed files with 449 additions and 399 deletions.
277 changes: 135 additions & 142 deletions ast/internal/pgen/table.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions ast/node.go
Expand Up @@ -23,7 +23,7 @@ type Term struct {
p Node
}

func (t Term) Parent() Node { // nolint
func (t Term) Parent() Node {
return t.p
}

Expand All @@ -32,7 +32,7 @@ func (t Term) Tok() scanner.Token {
return t.tok
}

func (t Term) Children() []Node { // nolint
func (t Term) Children() []Node {
return nil
}

Expand All @@ -49,7 +49,7 @@ func (nt NTerm) Name() string {
return string(nt.nt)
}

func (nt NTerm) Parent() Node { // nolint
func (nt NTerm) Parent() Node {
return nt.p
}

Expand All @@ -63,7 +63,7 @@ func (nt *NTerm) AddChild(n Node) {
nt.c = append(nt.c, n)
}

func (nt NTerm) Children() []Node { // nolint
func (nt NTerm) Children() []Node {
return nt.c
}

Expand All @@ -72,10 +72,10 @@ type Epsilon struct {
p Node
}

func (e Epsilon) Parent() Node { // nolint
func (e Epsilon) Parent() Node {
return e.p
}

func (e Epsilon) Children() []Node { // nolint
func (e Epsilon) Children() []Node {
return nil
}
2 changes: 1 addition & 1 deletion cmd/pgen/grammar.go
Expand Up @@ -175,7 +175,7 @@ func (g Grammar) followWithout(nt NTerm, ignore []NTerm) TokenSet {
continue
}

for i := i + 1; i < len(rule); i++ { // nolint
for i := i + 1; i < len(rule); i++ {
ts.AddAll(g.First(rule[i]), rule)
if !g.Nullable(rule[i]) {
break
Expand Down
2 changes: 1 addition & 1 deletion cmd/pgen/pgen.go
Expand Up @@ -37,7 +37,7 @@ func main() {

out := &formatter{w: os.Stdout}
if *output != "" {
file, err := os.Create(*output) // nolint
file, err := os.Create(*output)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating %q: %v", *output, err)
os.Exit(1)
Expand Down
4 changes: 0 additions & 4 deletions cmd/wdte/stdin.go
Expand Up @@ -18,10 +18,6 @@ func printRet(ret wdte.Func) {
case error, fmt.Stringer:
fmt.Printf(": %v\n", ret)
return

case wdte.GoFunc:
fmt.Println(": complex value (GoFunc)")
return
}

switch k := reflect.Indirect(reflect.ValueOf(ret)).Kind(); k {
Expand Down
2 changes: 1 addition & 1 deletion readme_test.go
Expand Up @@ -40,7 +40,7 @@ func Sum(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return sum
}

func ExampleREADME() {
func Example() {
m, err := wdte.Parse(strings.NewReader(src), wdte.ImportFunc(im), nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing script: %v\n", err)
Expand Down
2 changes: 1 addition & 1 deletion repl/examples_test.go
Expand Up @@ -14,7 +14,7 @@ func ExamplePartial() {
stack, partial = repl.Partial(strings.NewReader("import 'io'"), stack, nil)
fmt.Println(partial)

stack, partial = repl.Partial(strings.NewReader(";"), stack, nil)
_, partial = repl.Partial(strings.NewReader(";"), stack, nil)
fmt.Println(partial)
// Output: true
// true
Expand Down
5 changes: 1 addition & 4 deletions res/grammar.ebnf
@@ -1,9 +1,6 @@
<script> -> <cexprs> Ω
<funcmods> -> <funcmod> <funcmods>
<funcmods> -> ( <expr> ; ) <funcmods>
| ε
<funcmod> -> memo
| rev
| method
<argdecls> -> <argdecl> <argdecls>
| ε
<argdecl> -> id
Expand Down
8 changes: 6 additions & 2 deletions scanner/scanner_test.go
Expand Up @@ -104,9 +104,13 @@ o => print "double\n" 'single\\';`,
},
{
name: "FuncMods",
in: `memo test => ();`,
in: `let (memo) test => ();`,
out: []scanner.Token{
{Type: scanner.Keyword, Val: "memo"},
{Type: scanner.Keyword, Val: "let"},
{Type: scanner.Keyword, Val: "("},
{Type: scanner.ID, Val: "memo"},
{Type: scanner.Keyword, Val: ";"},
{Type: scanner.Keyword, Val: ")"},
{Type: scanner.ID, Val: "test"},
{Type: scanner.Keyword, Val: "=>"},
{Type: scanner.Keyword, Val: "("},
Expand Down
2 changes: 1 addition & 1 deletion scanner/token.go
Expand Up @@ -15,7 +15,7 @@ type Token struct {
type TokenType uint

const (
Invalid TokenType = iota // nolint
Invalid TokenType = iota
Number
String
ID
Expand Down
3 changes: 0 additions & 3 deletions scanner/util.go
Expand Up @@ -26,9 +26,6 @@ var (
}

keywords = []string{
"memo",
"rev",
"method",
"let",
"import",
}
Expand Down
8 changes: 4 additions & 4 deletions std/arrays/arrays.go
Expand Up @@ -137,11 +137,11 @@ func Stream(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return &streamer{a: args[0].(wdte.Array)}
}

func (a *streamer) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
func (a *streamer) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return a
}

func (a *streamer) Next(frame wdte.Frame) (wdte.Func, bool) { // nolint
func (a *streamer) Next(frame wdte.Frame) (wdte.Func, bool) {
if a.i >= len(a.a) {
return nil, false
}
Expand All @@ -151,11 +151,11 @@ func (a *streamer) Next(frame wdte.Frame) (wdte.Func, bool) { // nolint
return r, true
}

func (a *streamer) String() string { // nolint
func (a *streamer) String() string {
return "<stream>"
}

func (a *streamer) Reflect(name string) bool { // nolint
func (a *streamer) Reflect(name string) bool {
return name == "Stream"
}

Expand Down
6 changes: 3 additions & 3 deletions std/io/file/file.go
Expand Up @@ -27,15 +27,15 @@ type File struct {
mode int
}

func (f File) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
func (f File) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return f
}

func (f File) String() string { // nolint
func (f File) String() string {
return fmt.Sprintf("<file %q>", f.Name())
}

func (f File) Reflect(name string) bool { // nolint
func (f File) Reflect(name string) bool {
return name == "File" ||
(((f.mode == 0) || (f.mode&modeReader != 0)) && (name == "Reader")) ||
(((f.mode == 0) || (f.mode&modeWriter != 0)) && (name == "Writer"))
Expand Down
30 changes: 15 additions & 15 deletions std/io/io.go
Expand Up @@ -37,7 +37,7 @@ func (stdin) String() string {
return "<reader(stdin)>"
}

func (stdin) Reflect(name string) bool { // nolint
func (stdin) Reflect(name string) bool {
return name == "Reader"
}

Expand All @@ -55,7 +55,7 @@ func (stdout) String() string {
return "<writer(stdout)>"
}

func (stdout) Reflect(name string) bool { // nolint
func (stdout) Reflect(name string) bool {
return name == "Writer"
}

Expand All @@ -73,7 +73,7 @@ func (stderr) String() string {
return "<writer(stderr)>"
}

func (stderr) Reflect(name string) bool { // nolint
func (stderr) Reflect(name string) bool {
return name == "Writer"
}

Expand All @@ -92,19 +92,19 @@ type Reader struct {
io.Reader
}

func (r Reader) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
func (r Reader) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return r
}

func (r Reader) String() string { // nolint
func (r Reader) String() string {
if inner, ok := r.Reader.(fmt.Stringer); ok {
return inner.String()
}

return "<reader>"
}

func (r Reader) Reflect(name string) bool { // nolint
func (r Reader) Reflect(name string) bool {
return name == "Reader"
}

Expand All @@ -123,19 +123,19 @@ type Writer struct {
io.Writer
}

func (w Writer) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
func (w Writer) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return w
}

func (w Writer) String() string { // nolint
func (w Writer) String() string {
if inner, ok := w.Writer.(fmt.Stringer); ok {
return inner.String()
}

return "<writer>"
}

func (w Writer) Reflect(name string) bool { // nolint
func (w Writer) Reflect(name string) bool {
return name == "Writer"
}

Expand Down Expand Up @@ -316,11 +316,11 @@ type scanner struct {
s *bufio.Scanner
}

func (s scanner) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
func (s scanner) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return s
}

func (s scanner) Next(frame wdte.Frame) (wdte.Func, bool) { // nolint
func (s scanner) Next(frame wdte.Frame) (wdte.Func, bool) {
ok := s.s.Scan()
if !ok {
err := s.s.Err()
Expand All @@ -333,7 +333,7 @@ func (s scanner) Next(frame wdte.Frame) (wdte.Func, bool) { // nolint
return wdte.String(s.s.Text()), true
}

func (s scanner) Reflect(name string) bool { // nolint
func (s scanner) Reflect(name string) bool {
return name == "Stream"
}

Expand Down Expand Up @@ -431,11 +431,11 @@ type runeStream struct {
r io.RuneReader
}

func (r runeStream) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
func (r runeStream) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return r
}

func (r runeStream) Next(frame wdte.Frame) (wdte.Func, bool) { // nolint
func (r runeStream) Next(frame wdte.Frame) (wdte.Func, bool) {
c, _, err := r.r.ReadRune()
if err != nil {
if err == io.EOF {
Expand All @@ -446,7 +446,7 @@ func (r runeStream) Next(frame wdte.Frame) (wdte.Func, bool) { // nolint
return wdte.Number(c), true
}

func (r runeStream) Reflect(name string) bool { // nolint
func (r runeStream) Reflect(name string) bool {
return name == "Stream"
}

Expand Down

0 comments on commit dd5726a

Please sign in to comment.