项目作者: mesopelagique

项目描述 :
Convert Javascript to 4D code
高级语言: 4D
项目地址: git://github.com/mesopelagique/Mesopotamia.git
创建时间: 2020-04-18T13:06:13Z
项目社区:https://github.com/mesopelagique/Mesopotamia

开源协议:MIT License

下载


Mesopotamia

language language-top code-size release license discord
Sponsor

Become a Patron!


Buy me a coffee

Convert Javascript to 4D code.

WebServer

This component provide a web page to paste your javascript to convert.

preview

You could open this component and open the default URL http://localhost:8119 (as defined by component settings.4DSettings)

Example

javascript

  1. function nestTokens (tokens) {
  2. var nestedTokens = [6];
  3. var collector = nestedTokens;
  4. var sections = [];
  5. var token, section;
  6. for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
  7. token = tokens[i];
  8. switch (token[0]) {
  9. case '#':
  10. case '^':
  11. collector.push(token);
  12. sections.push(token);
  13. collector = token[4] = [];
  14. break;
  15. case '/':
  16. section = sections.pop();
  17. section[5] = token[2];
  18. collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens;
  19. break;
  20. default:
  21. collector.push(token);
  22. }
  23. }
  24. }

4D

  1. Function nestTokens
  2. $tokens:=$1
  3. $nestedTokens:=New collection(6)
  4. $collector:=$nestedTokens
  5. $sections:=New collection()
  6. For ($i:=0$numTokens:=$tokens.length;$i<$numTokens;1)
  7. $token:=$tokens[$i]
  8. Case of
  9. : ($token[0]="#")
  10. // TODO merge with next case
  11. : ($token[0]="^")
  12. $collector.push($token)
  13. $sections.push($token)
  14. $collector:=$token[4]:=New collection()
  15. // break
  16. : ($token[0]="/")
  17. $section:=$sections.pop()
  18. $section[5]:=$token[2]
  19. $collector:=Choose($sections.length>0;$sections[sections.length-1][4];$nestedTokens) // TODO heck that alternate path could be executed even if false, if not do a If Else
  20. // break
  21. Else
  22. $collector.push($token)
  23. End case
  24. End for
  25. $0:=$nestedTokens

How it works?

The result of Javascript Babel parser (previously Babylon) compiler is decoded into 4D classes.

class diagram

Then 4D code is generated from the created node tree.

Acknowledgment

TODO

  • Support more JS code.
  • Factorize code

mesopelagique