MCPcopy
hub / github.com/jamiebuilds/the-super-tiny-compiler / transformer

Function transformer

the-super-tiny-compiler.js:845–932  ·  view source on GitHub ↗
(ast)

Source from the content-addressed store, hash-verified

843
844// So we have our transformer function which will accept the lisp ast.
845function transformer(ast) {
846
847 // We'll create a `newAst` which like our previous AST will have a program
848 // node.
849 let newAst = {
850 type: 'Program',
851 body: [],
852 };
853
854 // Next I'm going to cheat a little and create a bit of a hack. We're going to
855 // use a property named `context` on our parent nodes that we're going to push
856 // nodes to their parent's `context`. Normally you would have a better
857 // abstraction than this, but for our purposes this keeps things simple.
858 //
859 // Just take note that the context is a reference *from* the old ast *to* the
860 // new ast.
861 ast._context = newAst.body;
862
863 // We'll start by calling the traverser function with our ast and a visitor.
864 traverser(ast, {
865
866 // The first visitor method accepts any `NumberLiteral`
867 NumberLiteral: {
868 // We'll visit them on enter.
869 enter(node, parent) {
870 // We'll create a new node also named `NumberLiteral` that we will push to
871 // the parent context.
872 parent._context.push({
873 type: 'NumberLiteral',
874 value: node.value,
875 });
876 },
877 },
878
879 // Next we have `StringLiteral`
880 StringLiteral: {
881 enter(node, parent) {
882 parent._context.push({
883 type: 'StringLiteral',
884 value: node.value,
885 });
886 },
887 },
888
889 // Next up, `CallExpression`.
890 CallExpression: {
891 enter(node, parent) {
892
893 // We start creating a new node `CallExpression` with a nested
894 // `Identifier`.
895 let expression = {
896 type: 'CallExpression',
897 callee: {
898 type: 'Identifier',
899 name: node.name,
900 },
901 arguments: [],
902 };

Callers 2

compilerFunction · 0.85
test.jsFile · 0.85

Calls 1

traverserFunction · 0.85

Tested by

no test coverage detected