Permalink
Browse files

Add experimental reason compiler. Rename generators directory to comp…

…iler
  • Loading branch information...
dabbott committed Dec 5, 2017
1 parent a48a161 commit f6d84f4f2307ff34f1050c0becd54c55f385755f
Showing with 2,914 additions and 1 deletion.
  1. +1 −1 README.md
  2. +8 −0 compiler/core/.gitignore
  3. +18 −0 compiler/core/README.md
  4. +15 −0 compiler/core/bsconfig.json
  5. +20 −0 compiler/core/package.json
  6. +196 −0 compiler/core/src/ast.bs.js
  7. +93 −0 compiler/core/src/ast.re
  8. +46 −0 compiler/core/src/component.bs.js
  9. +22 −0 compiler/core/src/component.re
  10. +256 −0 compiler/core/src/decode.bs.js
  11. +97 −0 compiler/core/src/decode.re
  12. +29 −0 compiler/core/src/demo.bs.js
  13. +18 −0 compiler/core/src/demo.re
  14. +520 −0 compiler/core/src/layer.bs.js
  15. +199 −0 compiler/core/src/layer.re
  16. +289 −0 compiler/core/src/logic.bs.js
  17. +127 −0 compiler/core/src/logic.re
  18. +293 −0 compiler/core/src/render.bs.js
  19. +106 −0 compiler/core/src/render.re
  20. +113 −0 compiler/core/src/stringMap.bs.js
  21. +24 −0 compiler/core/src/stringMap.re
  22. +122 −0 compiler/core/src/stringSet.bs.js
  23. +19 −0 compiler/core/src/stringSet.re
  24. +137 −0 compiler/core/src/tree.bs.js
  25. +76 −0 compiler/core/src/tree.re
  26. +2 −0 compiler/core/src/types.bs.js
  27. +30 −0 compiler/core/src/types.re
  28. +38 −0 compiler/core/tasks.json
  29. 0 {generators → compiler}/react/.eslintrc.js
  30. 0 {generators → compiler}/react/.gitignore
  31. 0 {generators → compiler}/react/.importjs.js
  32. 0 {generators → compiler}/react/convert.js
  33. 0 {generators → compiler}/react/converters/convertColors.js
  34. 0 {generators → compiler}/react/converters/convertFonts.js
  35. 0 {generators → compiler}/react/index.js
  36. 0 {generators → compiler}/react/package.json
  37. 0 {generators → compiler}/react/transform.js
  38. 0 {generators → compiler}/react/transforms/boolean-binary-expressions.js
  39. 0 {generators → compiler}/react/transforms/consolidate-variable-declarations.js
  40. 0 {generators → compiler}/react/transforms/extract-assignment-in-if.js
  41. 0 {generators → compiler}/react/transforms/helpers/countAssignments.js
  42. 0 {generators → compiler}/react/transforms/helpers/getProps.js
  43. 0 {generators → compiler}/react/transforms/inline-assets.js
  44. 0 {generators → compiler}/react/transforms/inline-to-stylesheet.js
  45. 0 {generators → compiler}/react/transforms/proptype-assumptions.js
  46. 0 {generators → compiler}/react/transforms/remove-empty-blocks.js
  47. 0 {generators → compiler}/react/transforms/remove-name-annotation.js
  48. 0 {generators → compiler}/react/transforms/remove-unused-variables.js
  49. 0 {generators → compiler}/react/transforms/replace-dead-ternary-expressions.js
  50. 0 {generators → compiler}/react/transforms/replace-ternary-expressions.js
  51. 0 {generators → compiler}/react/transforms/required-props-always-defined.js
  52. 0 {generators → compiler}/react/utils/component.js
  53. 0 {generators → compiler}/react/utils/j.js
  54. 0 {generators → compiler}/react/utils/layer.js
View
@@ -118,7 +118,7 @@ Then choose a directory path:
> Very very experimental. Likely won't generate usable code in its current state
Run the script in `generators/react` on a Lona workspace, e.g:
Run the script in `compiler/react` on a Lona workspace, e.g:
`node index.js [my_workspace_path] output`
View
@@ -0,0 +1,8 @@
.DS_Store
.merlin
.bsb.lock
npm-debug.log
/lib/bs/
/node_modules/
.ninja_log
View
@@ -0,0 +1,18 @@
# Basic Reason Template
Hello! This project allows you to quickly get started with Reason and BuckleScript. If you wanted a more sophisticated version, try the `react` template (`bsb -theme react -init .`).
# Build
```
npm run build
```
# Build + Watch
```
npm run watch
```
# Editor
If you use `vscode`, Press `Windows + Shift + B` it will build automatically
@@ -0,0 +1,15 @@
// This is the configuration file used by BuckleScript's build system bsb. Its documentation lives here: http://bucklescript.github.io/bucklescript/docson/#build-schema.json
// BuckleScript comes with its own parser for bsconfig.json, which is normal JSON, with the extra support of comments and trailing commas.
{
"name": "lona-compiler-core",
"version": "0.1.0",
"sources": ["src"],
"package-specs": {
"module": "commonjs",
"in-source": true
},
"suffix": ".bs.js",
"bs-dependencies": ["bs-node", "bs-json", "bs-batteries"],
"namespace": true,
"refmt": 3
}
View
@@ -0,0 +1,20 @@
{
"name": "lona-compiler-core",
"version": "0.1.0",
"scripts": {
"build": "bsb -make-world",
"start": "bsb -make-world -w",
"clean": "bsb -clean-world"
},
"keywords": ["BuckleScript"],
"author": "",
"license": "MIT",
"devDependencies": {
"bs-platform": "^2.1.0"
},
"dependencies": {
"bs-batteries": "0.0.14",
"bs-json": "^0.2.4",
"bs-node": "github:buckletypes/bs-node"
}
}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,93 @@
module JavaScript = {
type binaryOperator =
| Eq
| Neq
| Gt
| Gte
| Lt
| Lte
| Plus
| Noop;
type node =
| Return(node)
| Literal(Types.lonaValue)
| Identifier(list(string))
| Class(string, option(string), list(node))
| Method(string, list(string), list(node))
| CallExpression(node, list(node))
| JSXAttribute(string, node)
| JSXElement(string, list(node), list(node))
| VariableDeclaration(node)
| AssignmentExpression(node, node)
| BooleanExpression(node, binaryOperator, node)
| ConditionalStatement(node, list(node))
| ArrayLiteral(list(node))
| ObjectLiteral(list(node))
| ObjectProperty(node, node)
| Block(list(node))
| Program(list(node))
| Unknown;
/* Children are mapped first */
let rec map = (f, node) =>
switch node {
| Return(value) => f(Return(value |> map(f)))
| Literal(_) => f(node)
| Identifier(_) => f(node)
| Class(a, b, body) => f(Class(a, b, body |> List.map(map(f))))
| Method(a, b, body) => f(Method(a, b, body |> List.map(map(f))))
| CallExpression(value, body) => f(CallExpression(value |> map(f), body |> List.map(map(f))))
| JSXAttribute(a, value) => f(JSXAttribute(a, value |> map(f)))
| JSXElement(a, attributes, body) =>
f(JSXElement(a, attributes |> List.map(map(f)), body |> List.map(map(f))))
| VariableDeclaration(value) => f(VariableDeclaration(value |> map(f)))
| AssignmentExpression(value1, value2) =>
f(AssignmentExpression(value1 |> map(f), value2 |> map(f)))
| BooleanExpression(value1, a, value2) =>
f(BooleanExpression(value1 |> map(f), a, value2 |> map(f)))
| ConditionalStatement(condition, body) =>
f(ConditionalStatement(condition |> map(f), body |> List.map(map(f))))
| ArrayLiteral(body) => f(ArrayLiteral(body |> List.map(map(f))))
| ObjectLiteral(body) => f(ObjectLiteral(body |> List.map(map(f))))
| ObjectProperty(value1, value2) => f(ObjectProperty(value1 |> map(f), value2 |> map(f)))
| Block(body) => f(Block(body |> List.map(map(f))))
| Program(body) => f(Program(body |> List.map(map(f))))
| Unknown => f(node)
};
/* Takes an expression like `a === true` and converts it to `a` */
let optimizeTruthyBooleanExpression = (node) => {
let booleanValue = (sub) =>
switch sub {
| Literal(Types.Value(_, value)) => value |> Json.Decode.optional(Json.Decode.bool)
| _ => (None: option(bool))
};
switch node {
| BooleanExpression(a, cmp, b) =>
let boolA = booleanValue(a);
let boolB = booleanValue(b);
switch (boolA, cmp, boolB) {
| (_, Eq, Some(true)) => a
| (Some(true), Eq, _) => b
| _ => node
}
| _ => node
}
};
/* Renamed "layer.View.backgroundColor" to something JS-safe and nice looking */
let renameIdentifiers = (node) =>
switch node {
| Identifier([head, ...tail]) =>
switch head {
| "parameters" => Identifier(["this", "props", ...tail])
| "layers" =>
switch tail {
| [second, ...tail] =>
Identifier([tail |> List.fold_left((a, b) => a ++ "$" ++ b, second)])
| _ => node
}
| _ => node
}
| _ => node
};
let optimize = (node) => node |> map(optimizeTruthyBooleanExpression);
let prepareForRender = (node) => node |> map(renameIdentifiers);
};
Oops, something went wrong.

0 comments on commit f6d84f4

Please sign in to comment.