Add custom decoders to eliminate boilerplate
This change overhauls the approach to custom decoding. Instead of decoding
the parts of the struct that are "trivial" in one pass, and then patching up
the structure afterwards with custom decoding, the decoder itself understands
the notion of custom decoder functions.
First, the general purpose logic has moved out of pkg/pack/encoding and into
a new package, pkg/util/mapper. Most functions are now members of a new top-
level type, Mapper, which may be initialized with custom decoders. This
is a map from target type to a function that can decode objects into it.
Second, the AST-specific decoding logic is rewritten to use it. All AST nodes
are now supported, including definitions, statements, and expressions. The
overall approach here is to simply define a custom decoder for any interface
type that will occur in a node field position. The mapper, upon encountering
such a type, will consult the custom decoder map; if a decoder is found, it
will be used, otherwise an error results. This decoder then needs to switch
on the type discriminated kind field that is present in the metadata, creating
a concrete struct of the right type, and then converting it to the desired
interface type. Note that, subtly, interface types used only for "marker"
purposes don't require any custom decoding, because they do not appear in
field positions and therefore won't be encountered during the decoding process.
2017-01-16 18:41:26 +01:00
|
|
|
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
|
|
|
|
package encoding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
Begin overhauling semantic phases
This change further merges the new AST and MuPack/MuIL formats and
abstractions into the core of the compiler. A good amount of the old
code is gone now; I decided against ripping it all out in one fell
swoop so that I can methodically check that we are preserving all
relevant decisions and/or functionality we had in the old model.
The changes are too numerous to outline in this commit message,
however, here are the noteworthy ones:
* Split up the notion of symbols and tokens, resulting in:
- pkg/symbols for true compiler symbols (bound nodes)
- pkg/tokens for name-based tokens, identifiers, constants
* Several packages move underneath pkg/compiler:
- pkg/ast becomes pkg/compiler/ast
- pkg/errors becomes pkg/compiler/errors
- pkg/symbols becomes pkg/compiler/symbols
* pkg/ast/... becomes pkg/compiler/legacy/ast/...
* pkg/pack/ast becomes pkg/compiler/ast.
* pkg/options goes away, merged back into pkg/compiler.
* All binding functionality moves underneath a dedicated
package, pkg/compiler/binder. The legacy.go file contains
cruft that will eventually go away, while the other files
represent a halfway point between new and old, but are
expected to stay roughly in the current shape.
* All parsing functionality is moved underneath a new
pkg/compiler/metadata namespace, and we adopt new terminology
"metadata reading" since real parsing happens in the MetaMu
compilers. Hence, Parser has become metadata.Reader.
* In general phases of the compiler no longer share access to
the actual compiler.Compiler object. Instead, shared state is
moved to the core.Context object underneath pkg/compiler/core.
* Dependency resolution during binding has been rewritten to
the new model, including stashing bound package symbols in the
context object, and detecting import cycles.
* Compiler construction does not take a workspace object. Instead,
creation of a workspace is entirely hidden inside of the compiler's
constructor logic.
* There are three Compile* functions on the Compiler interface, to
support different styles of invoking compilation: Compile() auto-
detects a Mu package, based on the workspace; CompilePath(string)
loads the target as a Mu package and compiles it, regardless of
the workspace settings; and, CompilePackage(*pack.Package) will
compile a pre-loaded package AST, again regardless of workspace.
* Delete the _fe, _sema, and parsetree phases. They are no longer
relevant and the functionality is largely subsumed by the above.
...and so very much more. I'm surprised I ever got this to compile again!
2017-01-18 21:18:37 +01:00
|
|
|
"github.com/marapongo/mu/pkg/compiler/ast"
|
Add custom decoders to eliminate boilerplate
This change overhauls the approach to custom decoding. Instead of decoding
the parts of the struct that are "trivial" in one pass, and then patching up
the structure afterwards with custom decoding, the decoder itself understands
the notion of custom decoder functions.
First, the general purpose logic has moved out of pkg/pack/encoding and into
a new package, pkg/util/mapper. Most functions are now members of a new top-
level type, Mapper, which may be initialized with custom decoders. This
is a map from target type to a function that can decode objects into it.
Second, the AST-specific decoding logic is rewritten to use it. All AST nodes
are now supported, including definitions, statements, and expressions. The
overall approach here is to simply define a custom decoder for any interface
type that will occur in a node field position. The mapper, upon encountering
such a type, will consult the custom decoder map; if a decoder is found, it
will be used, otherwise an error results. This decoder then needs to switch
on the type discriminated kind field that is present in the metadata, creating
a concrete struct of the right type, and then converting it to the desired
interface type. Note that, subtly, interface types used only for "marker"
purposes don't require any custom decoding, because they do not appear in
field positions and therefore won't be encountered during the decoding process.
2017-01-16 18:41:26 +01:00
|
|
|
"github.com/marapongo/mu/pkg/util/contract"
|
|
|
|
"github.com/marapongo/mu/pkg/util/mapper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func decodeExpression(m mapper.Mapper, tree mapper.Object) (ast.Expression, error) {
|
|
|
|
k, err := mapper.FieldString(tree, reflect.TypeOf((*ast.Expression)(nil)).Elem(), "kind", true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if k != nil {
|
|
|
|
kind := ast.NodeKind(*k)
|
|
|
|
switch kind {
|
|
|
|
// Literals
|
|
|
|
case ast.NullLiteralKind:
|
|
|
|
return decodeNullLiteral(m, tree)
|
|
|
|
case ast.BoolLiteralKind:
|
|
|
|
return decodeBoolLiteral(m, tree)
|
|
|
|
case ast.NumberLiteralKind:
|
|
|
|
return decodeNumberLiteral(m, tree)
|
|
|
|
case ast.StringLiteralKind:
|
|
|
|
return decodeStringLiteral(m, tree)
|
|
|
|
case ast.ArrayLiteralKind:
|
|
|
|
return decodeArrayLiteral(m, tree)
|
|
|
|
case ast.ObjectLiteralKind:
|
2017-01-16 21:27:30 +01:00
|
|
|
return decodeObjectLiteral(m, tree)
|
Add custom decoders to eliminate boilerplate
This change overhauls the approach to custom decoding. Instead of decoding
the parts of the struct that are "trivial" in one pass, and then patching up
the structure afterwards with custom decoding, the decoder itself understands
the notion of custom decoder functions.
First, the general purpose logic has moved out of pkg/pack/encoding and into
a new package, pkg/util/mapper. Most functions are now members of a new top-
level type, Mapper, which may be initialized with custom decoders. This
is a map from target type to a function that can decode objects into it.
Second, the AST-specific decoding logic is rewritten to use it. All AST nodes
are now supported, including definitions, statements, and expressions. The
overall approach here is to simply define a custom decoder for any interface
type that will occur in a node field position. The mapper, upon encountering
such a type, will consult the custom decoder map; if a decoder is found, it
will be used, otherwise an error results. This decoder then needs to switch
on the type discriminated kind field that is present in the metadata, creating
a concrete struct of the right type, and then converting it to the desired
interface type. Note that, subtly, interface types used only for "marker"
purposes don't require any custom decoding, because they do not appear in
field positions and therefore won't be encountered during the decoding process.
2017-01-16 18:41:26 +01:00
|
|
|
|
|
|
|
// Loads
|
|
|
|
case ast.LoadLocationExpressionKind:
|
|
|
|
return decodeLoadLocationExpression(m, tree)
|
|
|
|
case ast.LoadDynamicExpressionKind:
|
|
|
|
return decodeLoadDynamicExpression(m, tree)
|
|
|
|
|
|
|
|
// Functions
|
|
|
|
case ast.NewExpressionKind:
|
|
|
|
return decodeNewExpression(m, tree)
|
|
|
|
case ast.InvokeFunctionExpressionKind:
|
|
|
|
return decodeInvokeFunctionExpression(m, tree)
|
|
|
|
case ast.LambdaExpressionKind:
|
|
|
|
return decodeLambdaExpression(m, tree)
|
|
|
|
|
|
|
|
// Operators
|
|
|
|
case ast.UnaryOperatorExpressionKind:
|
|
|
|
return decodeUnaryOperatorExpression(m, tree)
|
|
|
|
case ast.BinaryOperatorExpressionKind:
|
|
|
|
return decodeBinaryOperatorExpression(m, tree)
|
|
|
|
|
|
|
|
// Type testing
|
|
|
|
case ast.CastExpressionKind:
|
|
|
|
return decodeCastExpression(m, tree)
|
|
|
|
case ast.IsInstExpressionKind:
|
|
|
|
return decodeIsInstExpression(m, tree)
|
|
|
|
case ast.TypeOfExpressionKind:
|
|
|
|
return decodeTypeOfExpression(m, tree)
|
|
|
|
|
|
|
|
// Miscellaneous
|
|
|
|
case ast.ConditionalExpressionKind:
|
|
|
|
return decodeConditionalExpression(m, tree)
|
|
|
|
case ast.SequenceExpressionKind:
|
|
|
|
return decodeSequenceExpression(m, tree)
|
|
|
|
|
|
|
|
default:
|
Begin merging MuPackage/MuIL into the compiler
This is the first change of many to merge the MuPack/MuIL formats
into the heart of the "compiler".
In fact, the entire meaning of the compiler has changed, from
something that took metadata and produced CloudFormation, into
something that takes MuPack/MuIL as input, and produces a MuGL
graph as output. Although this process is distinctly different,
there are several aspects we can reuse, like workspace management,
dependency resolution, and some amount of name binding and symbol
resolution, just as a few examples.
An overview of the compilation process is available as a comment
inside of the compiler.Compile function, although it is currently
unimplemented.
The relationship between Workspace and Compiler has been semi-
inverted, such that all Compiler instances require a Workspace
object. This is more natural anyway and moves some of the detection
logic "outside" of the Compiler. Similarly, Options has moved to
a top-level package, so that Workspace and Compiler may share
access to it without causing package import cycles.
Finally, all that templating crap is gone. This alone is cause
for mass celebration!
2017-01-18 02:04:15 +01:00
|
|
|
contract.Failf("Unrecognized Expression kind: %v\n%v\n", kind, tree)
|
Add custom decoders to eliminate boilerplate
This change overhauls the approach to custom decoding. Instead of decoding
the parts of the struct that are "trivial" in one pass, and then patching up
the structure afterwards with custom decoding, the decoder itself understands
the notion of custom decoder functions.
First, the general purpose logic has moved out of pkg/pack/encoding and into
a new package, pkg/util/mapper. Most functions are now members of a new top-
level type, Mapper, which may be initialized with custom decoders. This
is a map from target type to a function that can decode objects into it.
Second, the AST-specific decoding logic is rewritten to use it. All AST nodes
are now supported, including definitions, statements, and expressions. The
overall approach here is to simply define a custom decoder for any interface
type that will occur in a node field position. The mapper, upon encountering
such a type, will consult the custom decoder map; if a decoder is found, it
will be used, otherwise an error results. This decoder then needs to switch
on the type discriminated kind field that is present in the metadata, creating
a concrete struct of the right type, and then converting it to the desired
interface type. Note that, subtly, interface types used only for "marker"
purposes don't require any custom decoding, because they do not appear in
field positions and therefore won't be encountered during the decoding process.
2017-01-16 18:41:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeNullLiteral(m mapper.Mapper, tree mapper.Object) (*ast.NullLiteral, error) {
|
|
|
|
var lit ast.NullLiteral
|
|
|
|
if err := m.Decode(tree, &lit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeBoolLiteral(m mapper.Mapper, tree mapper.Object) (*ast.BoolLiteral, error) {
|
|
|
|
var lit ast.BoolLiteral
|
|
|
|
if err := m.Decode(tree, &lit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeNumberLiteral(m mapper.Mapper, tree mapper.Object) (*ast.NumberLiteral, error) {
|
|
|
|
var lit ast.NumberLiteral
|
|
|
|
if err := m.Decode(tree, &lit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeStringLiteral(m mapper.Mapper, tree mapper.Object) (*ast.StringLiteral, error) {
|
|
|
|
var lit ast.StringLiteral
|
|
|
|
if err := m.Decode(tree, &lit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeArrayLiteral(m mapper.Mapper, tree mapper.Object) (*ast.ArrayLiteral, error) {
|
|
|
|
var lit ast.ArrayLiteral
|
|
|
|
if err := m.Decode(tree, &lit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeObjectLiteral(m mapper.Mapper, tree mapper.Object) (*ast.ObjectLiteral, error) {
|
|
|
|
var lit ast.ObjectLiteral
|
|
|
|
if err := m.Decode(tree, &lit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeLoadLocationExpression(m mapper.Mapper, tree mapper.Object) (*ast.LoadLocationExpression, error) {
|
|
|
|
var expr ast.LoadLocationExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeLoadDynamicExpression(m mapper.Mapper, tree mapper.Object) (*ast.LoadDynamicExpression, error) {
|
|
|
|
var expr ast.LoadDynamicExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeNewExpression(m mapper.Mapper, tree mapper.Object) (*ast.NewExpression, error) {
|
|
|
|
var expr ast.NewExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeInvokeFunctionExpression(m mapper.Mapper, tree mapper.Object) (*ast.InvokeFunctionExpression, error) {
|
|
|
|
var expr ast.InvokeFunctionExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeLambdaExpression(m mapper.Mapper, tree mapper.Object) (*ast.LambdaExpression, error) {
|
|
|
|
var expr ast.LambdaExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeUnaryOperatorExpression(m mapper.Mapper, tree mapper.Object) (*ast.UnaryOperatorExpression, error) {
|
|
|
|
var expr ast.UnaryOperatorExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeBinaryOperatorExpression(m mapper.Mapper, tree mapper.Object) (*ast.BinaryOperatorExpression, error) {
|
|
|
|
var expr ast.BinaryOperatorExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeCastExpression(m mapper.Mapper, tree mapper.Object) (*ast.CastExpression, error) {
|
|
|
|
var expr ast.CastExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeIsInstExpression(m mapper.Mapper, tree mapper.Object) (*ast.IsInstExpression, error) {
|
|
|
|
var expr ast.IsInstExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeTypeOfExpression(m mapper.Mapper, tree mapper.Object) (*ast.TypeOfExpression, error) {
|
|
|
|
var expr ast.TypeOfExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeConditionalExpression(m mapper.Mapper, tree mapper.Object) (*ast.ConditionalExpression, error) {
|
|
|
|
var expr ast.ConditionalExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeSequenceExpression(m mapper.Mapper, tree mapper.Object) (*ast.SequenceExpression, error) {
|
|
|
|
var expr ast.SequenceExpression
|
|
|
|
if err := m.Decode(tree, &expr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expr, nil
|
|
|
|
}
|