started thinking about macros

This commit is contained in:
Brian Picciano 2014-10-20 22:51:05 -04:00
parent 4bd9c94f82
commit f7bc7be1f7
3 changed files with 58 additions and 0 deletions

10
macros/builtins.go Normal file
View File

@ -0,0 +1,10 @@
package macros
import (
"github.com/mediocregopher/ginger/macros/pkgctx"
"github.com/mediocregopher/ginger/types"
)
func Package(p *pkgctx.PkgCtx, el types.Elem) string {
return ""
}

30
macros/macros.go Normal file
View File

@ -0,0 +1,30 @@
package macros
import (
"fmt"
"os"
"time"
"github.com/mediocregopher/ginger/macros/pkgctx"
"github.com/mediocregopher/ginger/types"
)
// A Macro takes in a ginger structure and returns the go code which corresponds
// to it. The structure will contain everything in the calling list after the
// macro name (for example, (. jkjkNo error is returned, Bail can be called to stop compilation mid-way
// instead.
type Macro func(*pkgctx.PkgCtx, types.Elem) string
// Bail stops compilation. The given element should be the reason compilation
// has stopped
func Bail(el types.Elem, reason string) {
fmt.Fprintln(os.Stderr, reason)
time.Sleep(100 * time.Second)
os.Exit(1)
}
// Bailf is like Bail, but takes in formatting
func Bailf(el types.Elem, format string, args ...interface{}) {
reason := fmt.Sprintf(format, args...)
Bail(el, reason)
}

18
macros/pkgctx/pkgctx.go Normal file
View File

@ -0,0 +1,18 @@
package pkgctx
// PkgCtx is given to all macros and represents the package that they are being
// evaluated within.
type PkgCtx struct {
// Packages describes the external packages imported by this one. Each key
// is the absolute package path, the value is the alias for it (or empty
// string for no alias)
Packages map[string]string
// CallDict is a map used by Eval for making actual calls dynamically. The
// key is the string representation of the call to be used (for example,
// "fmt.Println") and must agree with the aliases being used in Packages.
// The value need not be set during actual compilation, but it is useful to
// use it during testing
CallDict map[string]interface{}
}