Engine¶
The Engine orchestrates parsing, interpretation, and execution in the Go implementation.
Overview¶
The Engine is the main entry point for Grammar School in Go. It manages the parser, method handlers, and execution flow.
Creating an Engine¶
dsl := &MyDSL{}
parser := &MyParser{} // You need to implement Parser interface
engine, err := gs.NewEngine("", dsl, parser)
if err != nil {
log.Fatal(err)
}
Executing DSL Code¶
The Execute method parses and executes DSL code by calling methods directly:
Complete Example¶
package main
import (
"context"
"fmt"
"grammar-school/go/gs"
)
type MyDSL struct{}
func (d *MyDSL) Greet(args gs.Args) error {
name := args["name"].Str
fmt.Printf("Hello, %s!\n", name)
return nil
}
func main() {
dsl := &MyDSL{}
parser := &MyParser{} // Implement gs.Parser interface
engine, _ := gs.NewEngine("", dsl, parser)
engine.Execute(context.Background(), `greet(name="World")`)
// Output: Hello, World!
}
Method Discovery¶
The Engine uses reflection to automatically discover method handlers. A method is considered a handler if it matches this signature:
The Engine will automatically register all methods with this signature. Methods execute directly when called - no Action return needed.
Custom Grammar¶
You can provide a custom grammar string: