diff --git a/macros/builtins.go b/macros/builtins.go new file mode 100644 index 0000000..8d76f2d --- /dev/null +++ b/macros/builtins.go @@ -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 "" +} diff --git a/macros/macros.go b/macros/macros.go new file mode 100644 index 0000000..5f953f5 --- /dev/null +++ b/macros/macros.go @@ -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) +} diff --git a/macros/pkgctx/pkgctx.go b/macros/pkgctx/pkgctx.go new file mode 100644 index 0000000..5f1bcde --- /dev/null +++ b/macros/pkgctx/pkgctx.go @@ -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{} +}