lang: Add function values and lambdas
This adds a giant missing piece of the language: proper function values! It is lovely to now understand why early programming language designers didn't implement these, but a joy to now reap the benefits of them. In adding these, many other changes had to be made to get them to "fit" correctly. This improved the code and fixed a number of bugs. Unfortunately this touched many areas of the code, and since I was learning how to do all of this for the first time, I've squashed most of my work into a single commit. Some more information: * This adds over 70 new tests to verify the new functionality. * Functions, global variables, and classes can all be implemented natively in mcl and built into core packages. * A new compiler step called "Ordering" was added. It is called by the SetScope step, and determines statement ordering and shadowing precedence formally. It helped remove at least one bug and provided the additional analysis required to properly capture variables when implementing function generators and closures. * The type unification code was improved to handle the new cases. * Light copying of Node's allowed our function graphs to be more optimal and share common vertices and edges. For example, if two different closures capture a variable $x, they'll both use the same copy when running the function, since the compiler can prove if they're identical. * Some areas still need improvements, but this is ready for mainstream testing and use!
This commit is contained in:
@@ -41,8 +41,14 @@ func init() {
|
||||
funcs.Register("template", func() interfaces.Func { return &TemplateFunc{} })
|
||||
}
|
||||
|
||||
// TemplateName is the name of our template as required by the template library.
|
||||
const TemplateName = "template"
|
||||
const (
|
||||
// TemplateName is the name of our template as required by the template
|
||||
// library.
|
||||
TemplateName = "template"
|
||||
|
||||
argNameTemplate = "template"
|
||||
argNameVars = "vars"
|
||||
)
|
||||
|
||||
// TemplateFunc is a static polymorphic function that compiles a template and
|
||||
// returns the output as a string. It bases its output on the values passed in
|
||||
@@ -62,6 +68,15 @@ type TemplateFunc struct {
|
||||
closeChan chan struct{}
|
||||
}
|
||||
|
||||
// ArgGen returns the Nth arg name for this function.
|
||||
func (obj *TemplateFunc) ArgGen(index int) (string, error) {
|
||||
seq := []string{argNameTemplate, argNameVars}
|
||||
if l := len(seq); index >= l {
|
||||
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
|
||||
}
|
||||
return seq[index], nil
|
||||
}
|
||||
|
||||
// Polymorphisms returns the possible type signatures for this template. In this
|
||||
// case, since the second argument can be an infinite number of values, it
|
||||
// instead returns either the final precise type (if it can be gleamed from the
|
||||
@@ -72,7 +87,8 @@ type TemplateFunc struct {
|
||||
// XXX: is there a better API than returning a buried `variant` type?
|
||||
func (obj *TemplateFunc) Polymorphisms(partialType *types.Type, partialValues []types.Value) ([]*types.Type, error) {
|
||||
// TODO: return `variant` as second arg for now -- maybe there's a better way?
|
||||
variant := []*types.Type{types.NewType("func(a str, b variant) str")}
|
||||
str := fmt.Sprintf("func(%s str, %s variant) str", argNameTemplate, argNameVars)
|
||||
variant := []*types.Type{types.NewType(str)}
|
||||
|
||||
if partialType == nil {
|
||||
return variant, nil
|
||||
@@ -150,10 +166,15 @@ func (obj *TemplateFunc) Validate() error {
|
||||
|
||||
// Info returns some static info about itself.
|
||||
func (obj *TemplateFunc) Info() *interfaces.Info {
|
||||
var sig *types.Type
|
||||
if obj.Type != nil { // don't panic if called speculatively
|
||||
str := fmt.Sprintf("func(%s str, %s %s) str", argNameTemplate, argNameVars, obj.Type.String())
|
||||
sig = types.NewType(str)
|
||||
}
|
||||
return &interfaces.Info{
|
||||
Pure: true,
|
||||
Memo: false,
|
||||
Sig: types.NewType(fmt.Sprintf("func(template str, vars %s) str", obj.Type.String())),
|
||||
Sig: sig,
|
||||
Err: obj.Validate(),
|
||||
}
|
||||
}
|
||||
@@ -293,8 +314,8 @@ func (obj *TemplateFunc) Stream() error {
|
||||
}
|
||||
obj.last = input // store for next
|
||||
|
||||
tmpl := input.Struct()["template"].Str()
|
||||
vars := input.Struct()["vars"]
|
||||
tmpl := input.Struct()[argNameTemplate].Str()
|
||||
vars := input.Struct()[argNameVars]
|
||||
|
||||
result, err := obj.run(tmpl, vars)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user