Commit graph

448 commits

Author SHA1 Message Date
joeduffy bbb60799f8 Add a Require family of functions to pkg/util/contract 2017-01-17 15:58:11 -08:00
joeduffy 0260aae0d4 Add the start of a pkg/graph package
This change introduces a pkg/graph package, which is, for now, very
barebones.  This is where the MuGL data types and functions will go.
2017-01-17 15:57:24 -08:00
joeduffy 33f67f16ae Remove the index entrypoint
For now, an index entrypoint in the MuPackage metadata isn't
required (and in fact, gets rejected as an unrecognized field).
2017-01-17 15:07:11 -08:00
joeduffy bc376f8f8d Move pkg/pack/symbols to pkg/symbols 2017-01-17 15:06:53 -08:00
joeduffy 7ea5331f7f Merge pkg/pack/encoding into pkg/encoding 2017-01-17 14:58:45 -08:00
joeduffy 3a5217d722 Fix truncated output...again
The prior workaround to avoid truncated pending stdout writes, it
turns out, doesn't actually work.  (Piping output leads to more buffering
and asynchrony, and turned up additional problems.)  Digging through some
GitHub issues led me to these "best practices":

    https://nodejs.org/api/process.html#process_process_exit_code

    The reason this is problematic is because writes to process.stdout in
    Node.js are sometimes non-blocking and may occur over multiple ticks of
    the Node.js event loop. Calling process.exit(), however, forces the
    process to exit before those additional writes to stdout can be performed.

    Rather than calling process.exit() directly, the code should set the
    process.exitCode and allow the process to exit naturally by avoiding
    scheduling any additional work for the event loop.

This change adopts this part of the best practices, by simply setting
exitCode upon normal termination and letting the event loop quiesce.

Note that I am still not obeying the other part of the guidance:

    If it is necessary to terminate the Node.js process due to an error
    condition, throwing an uncaught error and allowing the process to
    terminate accordingly is safer than calling process.exit().

Somewhat confusingly, writes to process.stderr do not suffer from these
same problems, because writes to stderr are synchronous.  We prefer to
tear down the process gracefully, without an unhandled exception, and
we are okay losing some stdout writes as a result, given that all error-
related ones will have gone to stderr.
2017-01-17 14:54:15 -08:00
joeduffy 901c1cc6cf Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41.  I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).

After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 14:40:55 -08:00
joeduffy 853a8f5197 Add support for YAML project files to MuJS
This change now recognizes Mu.yaml files, in addition to Mu.json,
from the MuJS compiler.  Not the most important thing in the world,
however all of our project files are in YAML and it's less work to
implement this than to convert them all to JSON ...
2017-01-17 12:09:57 -08:00
joeduffy 037f117303 Start a MuJS design document
This is admittedly very light at the moment, however, I had it on my
enlistment and want to checkpoint *something*, however minimal it is.
2017-01-17 11:42:49 -08:00
joeduffy 3db75444fc Clarify some language around package/module naming 2017-01-17 11:41:12 -08:00
joeduffy 15b043c0c1 Add a missing range check 2017-01-17 10:00:14 -08:00
joeduffy c576e7cae4 Print the imports in mu describe 2017-01-17 09:55:58 -08:00
joeduffy 6769107c66 Track module imports
This change tracks the set of imported modules in the ast.Module
structure.  Although we can in principle gather up all imports simply
by looking through the fully qualified names, that's slightly hokey;
and furthermore, to properly initialize all modules, we need to know
in which order to do it (in case there are dependencies).  I briefly
considered leaving it up to MetaMu compilers to inject the module
initialization calls explicitly -- for infinite flexibility and perhaps
greater compatibility with the source languages -- however, I'd much
prefer that all Mu code use a consistent module initialization story.
Therefore, MetaMus declare the module imports, in order, and we will
evaluate the initializers accordingly.
2017-01-17 09:50:32 -08:00
joeduffy dbc17656f9 Emit more types
This change emits more types.  In particular:

* Previously, only primitive types got emitted, yielding "any" for any
  custom types.  Now we emit custom types, including fully qualified
  module names for type references resolving to imported modules.

* Prior to this change, we erroneously used the type node on the function
  declaration itself as an approximation for return type.  To get the
  true return type, we need to dig through a few nodes, including the
  Declaration and Signature.  This change now properly emits return types.

This doesn't close out marapongo/mu#46, however we are getting close.
2017-01-17 09:34:38 -08:00
joeduffy cca8619351 Fix output truncation issue 2017-01-16 15:18:57 -08:00
joeduffy a7b4d482a4 Mark ast.Export nodes as "public" in MuJS
By default, exports in MuJS should be available outside of the package.
This change adds the public modifier to them.
2017-01-16 15:07:14 -08:00
joeduffy 2849a3e64b Print descriptions as header comments 2017-01-16 14:45:32 -08:00
joeduffy 2adb334e6a Fix a bogus decoding case
The ObjectLiteral case should obviously call the decodeObjectLiteral
function, and not decodeArrayLiteral.
2017-01-16 12:27:30 -08:00
joeduffy 57d5538ec1 Fix IfStatement json metadata
The node name is "condition", not "expression".
2017-01-16 12:27:04 -08:00
joeduffy f226392051 Fix a bug with reexport-all
The prior code wasn't extracting the member part of the token name
when reexporting symbols, meaning the member was fully qualified.  For
example, when reexporting a class "C" from another module "other",
the resulting name was "other:C".  This is correct for the export node's
token -- so it can be resolved correctly to its source -- however,
clearly the identifier for the export should not contain the module
part.  This change strips it in the appropriate place.
2017-01-16 12:15:02 -08:00
joeduffy ae8284e86e Wrap variable initializers in an ExpressionStatement
The compiler was emitting incorrect variable initialization code,
resulting in a "=" BinaryOperatorExpression in a Statement position.
This fix wraps it in an ExpressionStatement so it results in valid IL.
2017-01-16 12:07:50 -08:00
joeduffy a2d847f1ef Use stable map enumeration
This change uses stable map enumeration so that output doesn't
change randomly based on hashing.
2017-01-16 12:02:33 -08:00
joeduffy b4a6c94a1b Print modifiers for class methods 2017-01-16 11:51:20 -08:00
joeduffy 2ee3671c36 Progress on the mu describe command
This change makes considerable progress on the `mu describe` command;
the only thing remaining to be implemented now is full IL printing.  It
now prints the full package/module structure.

For example, to print the set of exports from our scenarios/point test:

    $ mujs tools/mujs/tests/output/scenarios/point/ | mu describe - -e
    package "scenarios/point" {
	    dependencies []
	    module "index" {
		    class "Point" [public] {
			    method "add": (other: any): any
			    property "x" [public, readonly]: number
			    property "y" [public, readonly]: number
			    method ".ctor": (x: number, y: number): any
		    }
	    }
    }

This is just a pretty-printed, but is coming in handy with debugging.
2017-01-16 11:47:21 -08:00
joeduffy 3b0184cec3 Add a missing JSON annotation
The NewExpression AST node type was missing a JSON annotation on
its Type field, leading to decoding errors.

Now, with this, the full suite of MuJS test cases can be unmarshaled
into fully populated MuPack and MuIL structures.
2017-01-16 10:04:25 -08:00
joeduffy 908522aa0f Fix a problem with identifiers as expressions
MuIL doesn't support identifiers as expressions; instead, an explicit
LoadLocationExpression is required.  There was a problem in the current
MuJS translation process -- masked by TypeScript silently duck typing
from Identifier to Expression -- but our Mu decoding library discovered
it.  This fixes the problem by wrapping Identifiers used in Expression
positions in a proper LoadLocationExpression node.
2017-01-16 10:01:11 -08:00
joeduffy 96c7f4fa52 Mark some AST members as optional 2017-01-16 10:00:40 -08:00
joeduffy 1948380eb2 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 09:41:26 -08:00
joeduffy 9939024ccd Begin decoding statements 2017-01-16 07:47:41 -08:00
joeduffy 3ef55ac6b3 Add AST type assertions and kind constants 2017-01-16 07:47:12 -08:00
joeduffy 99f60bf5c8 Clean up some decoding logic
This change splits up the decoding logic into multiple files, to
mirror the AST package structure that the functions correspond to.

Additionally, there is now less "loose" reflection and dynamic lookup
code scattered throughout; it is now consolidated into the decoder,
with a set of "generic" functions like `fieldObject`, `asString`, etc.
2017-01-16 06:46:56 -08:00
joeduffy fee6d94e1f Implement class member decoding
This change implements custom class member decoding.  As with module methods,
the function body AST nodes remain nil, as custom AST decoding isn't yet done.
2017-01-16 06:10:24 -08:00
joeduffy 14c040bc7f Implement custom decoding of ModuleMembers
This change begins to implement some of the AST custom decoding, beneath
the Package's Module map.  In particular, we now unmarshal "one level"
beyond this, populating each Module's ModuleMember map.  This includes
Classes, Exports, ModuleProperties, and ModuleMethods.  The Class AST's
Members have been marked "custom", in addition to Block's Statements,
because they required kind-directed decoding.  But Exports and
ModuleProperties can be decoded entirely using the tag-directed decoding
scheme.  Up next, custom decoding of ClassMembers.  At that point, all
definition-level decoding will be done, leaving MuIL's ASTs.
2017-01-15 14:57:42 -08:00
joeduffy 5f33292496 Move assertion/failure functions
This change just moves the assertion/failure functions from the pkg/util
package to pkg/util/contract, so things read a bit nicer (i.e.,
`contract.Assert(x)` versus `util.Assert(x)`).
2017-01-15 14:26:48 -08:00
joeduffy f0b9f157de Add tests for decoding more complex nested structures
This adds deeper nesting into our tests, plus a few more examples of
pointer-based data structures inside of those deeply nested structures.
2017-01-15 13:50:00 -08:00
joeduffy 1ad99bcea1 Fix a few AST marshaling things
This fixes a few things so that MuPackages now unmarshal:

* Mark Module.Members as requiring "custom" decoding.  This is required
  because that's the first point in the tree that leverages polymorphism.
  Everything "above" this unmarshals just fine (e.g., package+modules).

* As such, stop marking Package.Modules as "custom".

* Add the Kind field to the Node.  Although we won't use this for type
  discrimination in the same way, since Go gives us RTTI on the structs,
  it is required for unmarshaling (to avoid "unrecognized fields" errors)
  and it's probably handy to have around for logging, messages, etc.

* Mark Position.Line and Column with "json" annotations so that they
  unmarshal correctly.
2017-01-14 11:22:54 -08:00
joeduffy f90679a717 Also print module names from describe 2017-01-14 11:22:26 -08:00
joeduffy bd32867e81 Adjust pointers
This change adjusts pointers correctly when unmarshaling into target
pointer types.  This handles arrays and maps of pointer elements, in
addition to consolidating existing logic for marshaling into a
destination top-level pointer as well.
2017-01-14 11:21:27 -08:00
joeduffy d1188ed8c8 Add recursive struct unmarshaling 2017-01-14 10:06:55 -08:00
joeduffy c0c75d0f08 Split decoder logic out from MuPack-specific decode functions 2017-01-14 09:50:25 -08:00
joeduffy ab2d0ae6cb Implement tag-directed decoding
This change eliminates boilerplate decoding logic in all the different
data structures, and instead uses a new tag-directed decoding scheme.
This works a lot like the JSON deserializers, in that it recognizes the
`json:"name"` tags, except that we permit annotation of fields that
require custom deserialization, as `json:"name,custom"`.  The existing
`json:"name,omitempty"` tag is recognized for optional fields.
2017-01-14 09:42:05 -08:00
joeduffy 120f139812 Decode dependencies metadata 2017-01-14 07:58:21 -08:00
joeduffy d334ea322b Add custom decoding for MuPack metadata
This adds basic custom decoding for the MuPack metadata section of
the incoming JSON/YAML.  Because of the type discriminated union nature
of the incoming payload, we cannot rely on the simple built-in JSON/YAML
unmarshaling behavior.  Note that for the metadata section -- what is
in this checkin -- we could have, but the IL AST nodes are problematic.
(To know what kind of structure to creat requires inspecting the "kind"
field of the IL.)  We will use a reflection-driven walk of the target
structure plus a weakly typed deserialized map[string]interface{}, as
is fairly customary in Go for scenarios like this (though good libaries
seem to be lacking in this area...).
2017-01-14 07:40:13 -08:00
joeduffy cc16e85266 Add the scaffolding for a new mu describe command
This command will simply pretty-print the contents of a MuPackage.
My plan is to use it for my own development and debugging purposes,
however, I suspect it will be generally useful (since MuIL can be
quite verbose).  Again, just scaffolding, but I'll flesh it out
incrementally as more of the goo in here starts working...
2017-01-13 15:00:20 -08:00
joeduffy e7dbfa59c3 Create MuPack and MuIL packages in our Go toolset
This change carries over all of the metadata shapes in the MuPack
and MuIL file formats to our Go toolset.  This includes creating a
proper discriminated AST type tree along with correct annotations
so that the metadata will serialize and deserialize correctly.
2017-01-13 14:32:10 -08:00
joeduffy 517f0c9197 Add an | undefined to make this clearer
(I wish TypeScript required this.)
2017-01-13 12:17:44 -08:00
joeduffy b6e65ddcea Mimic TypeScript's logic for export modules
This depends on an internal API, and so has been tagged with
marapongo/mu#52.
2017-01-13 12:07:45 -08:00
joeduffy 179796d26f Use the same "heuristic" as TypeScript for public modules 2017-01-13 11:15:15 -08:00
joeduffy 2db55b5df2 Do a bit of house-cleaning 2017-01-13 10:42:50 -08:00
joeduffy 3c8990f720 Add basic type mapping
This change adds basic type mapping using bound node information for the
primitive types (string, number, bool, void, any).
2017-01-13 08:23:22 -08:00