├── .gitignore ├── LICENSE ├── README.md ├── requirements.txt ├── test ├── sample1.flex ├── sample2.flex ├── sample3.flex ├── sample4.flex └── sample5.flex └── transpiler ├── __init__.py ├── data ├── intents │ ├── assignment.md │ ├── conditional.md │ ├── declarative.md │ ├── input.md │ ├── loop.md │ └── print.md └── model_config.yml ├── flex_transpiler.py └── languages ├── __init__.py ├── c_plus_plus.py ├── java.py └── python.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | 103 | # Code editors and IDEs 104 | .vscode 105 | 106 | # Trained model files 107 | model/ 108 | 109 | # Generated target source files 110 | test/*.cpp 111 | test/*.java 112 | test/*.py 113 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 The Flex Language 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Flex Transpiler 2 | 3 | Implementation of Flex's transpiler using the NLU model trained using [Rasa NLU](https://github.com/RasaHQ/rasa_nlu). 4 | 5 | ## Setup 6 | 7 | 1. Clone the repo and **`cd`** into it: 8 | ```bash 9 | git clone https://github.com/Flex-lang/transpiler-wit.git 10 | cd transpiler-wit 11 | ``` 12 | 13 | 1. Create a Python 3 virtual environment (named `venv`): 14 | ```bash 15 | virtualenv -p python3 venv 16 | ``` 17 | 18 | 1. Activate your virtual environment: 19 | ```bash 20 | . venv/bin/activate 21 | ``` 22 | 23 | 1. Install the required libraries: 24 | ```bash 25 | pip install rasa_core rasa_nlu[spacy] 26 | ``` 27 | 28 | 1. Download spAcy model: 29 | ```bash 30 | python -m spacy download en_core_web_md 31 | ``` 32 | 33 | ## Train NLU model 34 | 35 | From the `transpiler/data` directory, run: 36 | 37 | ```bash 38 | python -m rasa_nlu.train --config model_config.yml --data intents --path model 39 | ``` 40 | 41 | ## Run 42 | 43 | ``` 44 | Usage: 45 | flex_transpiler -l [-o ] 46 | flex_transpiler -h 47 | 48 | Arguments: 49 | Path to the input Flex source file. 50 | 51 | Options: 52 | -l , --target-language The target language to transpile to. 53 | -o , --output Path to the generated output file. 54 | -h, --help Print this help text. 55 | 56 | Target languages available: 57 | c++ 58 | java 59 | python 60 | ``` 61 | 62 | From the `transpiler` directory, run `flex_transpiler.py` with proper arguments. For example, if your Flex source code is written in `~/input_source.flex` and you want to transpile it to Python into `~/output_source.py`, you need to run the following command: 63 | ```bash 64 | ./flex_transpiler.py ~/input_source.flex -l python -o ~/output_source.py 65 | ``` 66 | 67 | ## Results 68 | 69 | ##### Input Flex Code 70 | ```bash 71 | Main() 72 | arr is an array of integers 73 | x is integer 74 | input 10 elements in arr 75 | input x 76 | flag: boolean 77 | flag is false 78 | for every element in arr 79 | if element == x 80 | display "FOUND!" 81 | flag is true 82 | if flag == false 83 | display "NOT FOUND!" 84 | ``` 85 | 86 | ##### Transpiled Python Code 87 | ```bash 88 | if __name__ == '__main__': 89 | input(x) 90 | value_list = [1, 5, 8, 6, 0, 4, 5] 91 | print(x) 92 | for element in value_list: 93 | element = x * element 94 | print(element) 95 | ``` 96 | 97 | ##### Transpiled CPP Code 98 | ```bash 99 | #include 100 | #include 101 | #include 102 | #include 103 | #include 104 | #include 105 | #include 106 | #include 107 | 108 | typedef int integer; 109 | typedef float real; 110 | typedef char character; 111 | typedef bool boolean; 112 | 113 | int main() { 114 | std::vector arr; 115 | integer x; 116 | for (size_t i = 0; i < 10; i++) { 117 | integer temp; 118 | std::cin >> temp; 119 | arr.push_back(temp); 120 | } 121 | std::cin >> x; 122 | boolean flag; 123 | flag = false; 124 | for (const auto& element : arr) { 125 | If (element == x) { 126 | std::cout << "found!"; 127 | flag = true; 128 | } 129 | if (flag == false) { 130 | std::cout << "not found!"; 131 | } 132 | 133 | return 0; 134 | } 135 | ``` 136 | 137 | ##### Transpiled Java Code 138 | ```bash 139 | import java.awt.*; 140 | import java.awt.geom.*; 141 | import java.io.*; 142 | import java.util.*; 143 | 144 | public class test { 145 | public static void main() { 146 | ArrayList arr = new ArrayList(); 147 | integer x = new integer(); 148 | String arr = System.console().readLine(); 149 | String x = System.console().readLine(); 150 | boolean flag = new boolean(); 151 | flag = false; 152 | for (Object element : arr) { 153 | if(element == x) { 154 | System.out.println("found!"); 155 | flag = true; 156 | } 157 | If (flag == false) { 158 | System.out.println("not found!"); 159 | } 160 | } 161 | } 162 | ``` 163 | 164 | ## License 165 | 166 | This project is licensed under the terms of the [MIT license](LICENSE). 167 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | absl-py==0.2.1 2 | alabaster==0.7.10 3 | APScheduler==3.5.1 4 | astor==0.6.2 5 | astroid==1.6.4 6 | attrs==18.1.0 7 | Automat==0.6.0 8 | Babel==2.5.3 9 | bleach==1.5.0 10 | boto3==1.7.24 11 | botocore==1.10.24 12 | certifi==2018.4.16 13 | chardet==3.0.4 14 | click==6.7 15 | cloudpickle==0.5.3 16 | coloredlogs==10.0 17 | colorhash==1.0.2 18 | ConfigArgParse==0.13.0 19 | constantly==15.1.0 20 | cycler==0.10.0 21 | cymem==1.31.2 22 | cytoolz==0.8.2 23 | decorator==4.3.0 24 | dill==0.2.7.1 25 | docopt==0.6.2 26 | docutils==0.14 27 | en-core-web-md==2.0.0 28 | entrypoints==0.2.3 29 | fakeredis==0.10.3 30 | fbmessenger==5.1.0 31 | Flask==1.0.2 32 | Flask-Cors==3.0.4 33 | future==0.16.0 34 | gast==0.2.0 35 | gevent==1.3.1 36 | graphviz==0.8.3 37 | greenlet==0.4.13 38 | grpcio==1.12.0 39 | h5py==2.7.1 40 | html5lib==0.9999999 41 | humanfriendly==4.12.1 42 | hyperlink==18.0.0 43 | idna==2.6 44 | imagesize==1.0.0 45 | incremental==17.5.0 46 | ipython-genutils==0.2.0 47 | isort==4.3.4 48 | itsdangerous==0.24 49 | Jinja2==2.10 50 | jmespath==0.9.3 51 | jsonpickle==0.9.6 52 | jsonschema==2.6.0 53 | jupyter-core==4.4.0 54 | Keras==2.1.6 55 | kiwisolver==1.0.1 56 | klein==17.10.0 57 | lazy-object-proxy==1.3.1 58 | Markdown==2.6.11 59 | MarkupSafe==1.0 60 | matplotlib==2.2.2 61 | mattermostwrapper==2.1 62 | mccabe==0.6.1 63 | mistune==0.8.3 64 | msgpack-numpy==0.4.1 65 | msgpack-python==0.5.6 66 | murmurhash==0.28.0 67 | nbconvert==5.3.1 68 | nbformat==4.4.0 69 | nbsphinx==0.3.3 70 | networkx==2.1 71 | numpy==1.14.3 72 | packaging==17.1 73 | pandoc==1.0.2 74 | pandocfilters==1.4.2 75 | pathlib==1.0.1 76 | plac==0.9.6 77 | ply==3.11 78 | preshed==1.0.0 79 | protobuf==3.5.2.post1 80 | Pygments==2.2.0 81 | PyJWT==1.6.4 82 | pykwalify==1.6.0 83 | pylint==1.9.1 84 | pyparsing==2.2.0 85 | PySocks==1.6.8 86 | python-crfsuite==0.9.5 87 | python-dateutil==2.7.3 88 | python-telegram-bot==10.1.0 89 | pytz==2018.4 90 | PyYAML==3.12 91 | rasa-core==0.9.0 92 | rasa-nlu==0.12.3 93 | redis==2.10.6 94 | regex==2017.4.5 95 | requests==2.18.4 96 | ruamel.yaml==0.15.37 97 | s3transfer==0.1.13 98 | scikit-learn==0.19.1 99 | scipy==1.1.0 100 | simplejson==3.15.0 101 | six==1.11.0 102 | sklearn-crfsuite==0.3.6 103 | slackclient==1.2.1 104 | snowballstemmer==1.2.1 105 | spacy==2.0.11 106 | Sphinx==1.7.4 107 | sphinxcontrib-websupport==1.0.1 108 | tabulate==0.8.2 109 | tensorboard==1.8.0 110 | tensorflow==1.8.0 111 | termcolor==1.1.0 112 | testpath==0.3.1 113 | thinc==6.10.2 114 | toolz==0.9.0 115 | tqdm==4.23.3 116 | traitlets==4.3.2 117 | twilio==6.14.0 118 | Twisted==18.4.0 119 | typing==3.6.4 120 | tzlocal==1.5.1 121 | ujson==1.35 122 | urllib3==1.22 123 | websocket-client==0.47.0 124 | Werkzeug==0.15.3 125 | wrapt==1.10.11 126 | zope.interface==4.5.0 127 | -------------------------------------------------------------------------------- /test/sample1.flex: -------------------------------------------------------------------------------- 1 | Main() 2 | x is an integer 3 | input x 4 | values is an array of integers 5 | values = 1, 5, 8, 6, 0, 4, 5 6 | print x 7 | for every value in values 8 | value = x * value 9 | print value 10 | -------------------------------------------------------------------------------- /test/sample2.flex: -------------------------------------------------------------------------------- 1 | Main() 2 | x is and integer 3 | x = 5 4 | arr is an array of integers 5 | if x == 5 6 | display "all is well!" 7 | otherwise 8 | output "error!" 9 | input x 10 | print x 11 | for every element in arr 12 | print element 13 | x = x + element 14 | print x 15 | -------------------------------------------------------------------------------- /test/sample3.flex: -------------------------------------------------------------------------------- 1 | Main() 2 | arr is an array of integers 3 | arr = 15, 20, 19, 5, 3, 4, 6, 8, 20 4 | x: integer 5 | input x 6 | flag: boolean 7 | flag is false 8 | for every element in arr 9 | if element == x 10 | flag is true 11 | display "found!" 12 | if flag == false 13 | display "not found!" 14 | -------------------------------------------------------------------------------- /test/sample4.flex: -------------------------------------------------------------------------------- 1 | # program to swap two numbers 2 | Main() 3 | x,y are real 4 | get value of x from user 5 | prompt user to enter y 6 | real temp 7 | temp = x 8 | x = y 9 | y = temp 10 | display "Swapped Values:\n" 11 | output "x = " 12 | print x 13 | display "\n" 14 | output "y = " 15 | print y 16 | -------------------------------------------------------------------------------- /test/sample5.flex: -------------------------------------------------------------------------------- 1 | Main() { 2 | operator is char 3 | firstNumber,secondNumber are double 4 | print "Enter an operator (+, -, *, /): " 5 | get operator 6 | display "Enter two operands: " 7 | input firstNumber,secondNumber 8 | switch operator 9 | case '+' -> 10 | display "Addition = " 11 | output firstNumber+secondNumber 12 | case '-' -> 13 | display "Subtraction = " 14 | output firstNumber-secondNumber 15 | case '*' -> 16 | display "Multiplication = " 17 | output firstNumber*secondNumber 18 | case '/' -> 19 | display "Division = " 20 | output firstNumber/secondNumber 21 | // operator is doesn't match any case constant (+, -, *, /) 22 | default: 23 | print "Error! operator is not correct" 24 | 25 | -------------------------------------------------------------------------------- /transpiler/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flex-lang/transpiler/18e23183ddd6f532889eaa0f53433733cd67614e/transpiler/__init__.py -------------------------------------------------------------------------------- /transpiler/data/intents/assignment.md: -------------------------------------------------------------------------------- 1 | ## intent:assign_variable 2 | * [fdsfdafdf](name) is [12](expression) 3 | * [regfsfd](name) = [12](expression) 4 | * [sg4_dzv](name) is [12.05](expression) 5 | * [ggxdfxfg](name) = [12.05](expression) 6 | * [sdzxxg](name) = [dwuandjks](expression) 7 | * [kdwokd9](name) = [jis9dws](expression) 8 | * [gsd_bxx](name) is [b](expression) 9 | * [gfdfgd](name) is [true](expression) 10 | * [gsdffdxg](name) = [true](expression) 11 | * [zdceds](name) is a [false](expression) 12 | * [ijgyshg](name) = [false](expression) 13 | * [rgh_fghd](name) is a [true](expression) 14 | * [egrhfdf](name) is [0](expression) 15 | * [grgdrfd](name) = [0](expression) 16 | * [hdfsr](name) = [0.0001](expression) 17 | * [ewgvxx](name) is [9.8087](expression) 18 | * [gtdhzsdfg](name) = ["hello"](expression) 19 | * [dfrghhr](name) is ["hello world"](expression) 20 | * [gtf_xdfe](name) = ["any string"](expression) 21 | * [rdfas4](name) = [192 / 168 * 20 - t](expression) 22 | * [gjs43sdf](name) = [some_var * some_other_var](expression) 23 | * [element](name) = [x * element](expression) 24 | * [fse3f](name) = [2 * pi * theta](expression) 25 | * [z](name) = [x * y](expression) 26 | * [mc3psem](name) is [54 * 7 + x - 1](expression) 27 | * [c9spif](name) is [doi3mk * dmo3imd * dmi3mdkas](expression) 28 | * [nv3posf](name) is [2 * pi * theta / 360](expression) 29 | * [fmsio3mfk](name) is [x * y](expression) 30 | 31 | ## intent:assign_array 32 | * [fn943fu](name) = [7, 4, 2, 8, 3, 6](array_values) 33 | * [fpo_3jfk](name) = [[7, 4, 2, 8, 3, 6](array_values)] 34 | * [du9w3oujf](name) = [ [4, 2, 1, 3](array_values)] 35 | * [xnu9_wene](name) = [ [8, 17, 24, 9, 7, 16, 213, 47](array_values) ] 36 | * [cui4wfcd](name) = [ [0, 1, 1](array_values) ] 37 | * [nguengj](name) = {[5, 3, 8, 4, 20, 9](array_values)} 38 | * [dewjnr4fns](name) = { [6, 3, 239, 42](array_values)} 39 | * [fm4iowm4](name) = {[61, 9, 5, 4, 3, 8, 13, 2](array_values) } 40 | * [iowm_4fks](name) = { [3, 0, 0](array_values) } 41 | * [fn943fu](name) = ["7", "mfsdkjf", "fks sefk", "htdg", "3", "fmsk3"](array_values) 42 | * [fn943fu](name) = [["7", "mfsdkjf", "fks sefk", "htdg", "3", "fmsk3"](array_values)] 43 | * [du9w3ouj](name) = [ ["4", "2", "djasn", "fnjsd"](array_values)] 44 | * [xnu9wene](name) = [ ["8", "17", "24", "9", "7", "16", "213", "47"](array_values) ] 45 | * [cui4wfcd](name) = [ ["whjska", "nfsd", "1"](array_values) ] 46 | * [nguengj](name) = {["5", "3", "8", "fsdjf", "fmksdf", "9"](array_values)} 47 | * [dewjnr4fns](name) = { ["fnjs", "3", "239", "42"](array_values)} 48 | * [fm4iowm4](name) = {["61", "9", "fnsjd", "4", " fskenf", "8", "fsiefnl3", "2"](array_values) } 49 | * [iowm_4fks](name) = { ["3", "0", "0"](array_values) } 50 | * [vsevsd](name) = ['7', 'mfsdkjf', 'fks sefk', 'htdg', '3', 'fmsk3'](array_values) 51 | * [fnuw_8osdn](name) = [['7', 'mfsdkjf', 'fks sefk', 'htdg', '3', 'fmsk3'](array_values)] 52 | * [brbsezf](name) = [ ['4', '2', 'djasn', 'fnjsd'](array_values)] 53 | * [nrzfdzg](name) = [ ['8', '17', '24', '9', '7', '16', '213', '47'](array_values) ] 54 | * [zhx5yg](name) = [ ['whjska', 'nfsd', '1'](array_values) ] 55 | * [ngty5hg](name) = {['5', '3', '8', 'fsdjf', 'fmksdf', '9'](array_values)} 56 | * [gzr45hgxgf](name) = { ['fnjs', '3', '239', '42'](array_values)} 57 | * [hdxfr4](name) = {['61', '9', 'fnsjd', '4', ' fskenf', '8', 'fsiefnl3', '2'](array_values) } 58 | * [hhh54hj](name) = { ['3', '0', '0'](array_values) } 59 | * [jcesnk3jf](name) = [x, y, z, p, q, r](array_values) 60 | * [some_list](name) = [a, l, r, i, g, h, t](array_values) 61 | -------------------------------------------------------------------------------- /transpiler/data/intents/conditional.md: -------------------------------------------------------------------------------- 1 | ## intent:begin_if 2 | * if [a>1](expression) 3 | * if [e > 90](expression) 4 | * if [k >= 1](expression) 5 | * if [a>1.86](expression) 6 | * if [e > 78.124](expression) 7 | * if [k >= 1.001](expression) 8 | * if [a>b](expression) 9 | * if [e > f](expression) 10 | * if [k >= A](expression) 11 | * if [a<10](expression) 12 | * if [e < 90](expression) 13 | * if [k <= 1](expression) 14 | * if [a<5.86](expression) 15 | * if [e < 23.124](expression) 16 | * if [k <= 1.1](expression) 17 | * if [a1](expression) 61 | * else if [e > 90](expression) 62 | * else if [k >= 1](expression) 63 | * else if [a>1.86](expression) 64 | * else if [e > 78.124](expression) 65 | * else if [k >= 1.001](expression) 66 | * else if [a>b](expression) 67 | * else if [e > f](expression) 68 | * else if [k >= A](expression) 69 | * else if [a<10](expression) 70 | * else if [e < 90](expression) 71 | * else if [k <= 1](expression) 72 | * else if [a<5.86](expression) 73 | * else if [e < 23.124](expression) 74 | * else if [k <= 1.1](expression) 75 | * else if [a1](expression) 109 | * elif [e > 90](expression) 110 | * elif [k >= 1](expression) 111 | * elif [a>1.86](expression) 112 | * elif [e > 78.124](expression) 113 | * elif [k >= 1.001](expression) 114 | * elif [a>b](expression) 115 | * elif [e > f](expression) 116 | * elif [k >= A](expression) 117 | * elif [a<10](expression) 118 | * elif [e < 90](expression) 119 | * elif [k <= 1](expression) 120 | * elif [a<5.86](expression) 121 | * elif [e < 23.124](expression) 122 | * elif [k <= 1.1](expression) 123 | * elif [a1](expression) 157 | * otherwise if [e > 90](expression) 158 | * otherwise if [k >= 1](expression) 159 | * otherwise if [a>1.86](expression) 160 | * otherwise if [e > 78.124](expression) 161 | * otherwise if [k >= 1.001](expression) 162 | * otherwise if [a>b](expression) 163 | * otherwise if [e > f](expression) 164 | * otherwise if [k >= A](expression) 165 | * otherwise if [a<10](expression) 166 | * otherwise if [e < 90](expression) 167 | * otherwise if [k <= 1](expression) 168 | * otherwise if [a<5.86](expression) 169 | * otherwise if [e < 23.124](expression) 170 | * otherwise if [k <= 1.1](expression) 171 | * otherwise if [a1](expression) 207 | * unless [e > 90](expression) 208 | * unless [k >= 1](expression) 209 | * unless [a>1.86](expression) 210 | * unless [e > 78.124](expression) 211 | * unless [k >= 1.001](expression) 212 | * unless [a>b](expression) 213 | * unless [e > f](expression) 214 | * unless [k >= A](expression) 215 | * unless [a<10](expression) 216 | * unless [e < 90](expression) 217 | * unless [k <= 1](expression) 218 | * unless [a<5.86](expression) 219 | * unless [e < 23.124](expression) 220 | * unless [k <= 1.1](expression) 221 | * unless [a1](expression) 56 | * while [e > 90](expression) 57 | * while [k >= 1](expression) 58 | * while [a>1.86](expression) 59 | * while [e > 78.124](expression) 60 | * while [k >= 1.001](expression) 61 | * while [a>b](expression) 62 | * while [e > f](expression) 63 | * while [k >= A](expression) 64 | * while [a<10](expression) 65 | * while [e < 90](expression) 66 | * while [k <= 1](expression) 67 | * while [a<5.86](expression) 68 | * while [e < 23.124](expression) 69 | * while [k <= 1.1](expression) 70 | * while [a1](expression) 125 | * unless [e > 90](expression) 126 | * unless [k >= 1](expression) 127 | * unless [a>1.86](expression) 128 | * unless [e > 78.124](expression) 129 | * unless [k >= 1.001](expression) 130 | * unless [a>b](expression) 131 | * unless [e > f](expression) 132 | * unless [k >= A](expression) 133 | * unless [a<10](expression) 134 | * unless [e < 90](expression) 135 | * unless [k <= 1](expression) 136 | * unless [a<5.86](expression) 137 | * unless [e < 23.124](expression) 138 | * unless [k <= 1.1](expression) 139 | * unless [a1](expression) 194 | * until [e > 90](expression) 195 | * until [k >= 1](expression) 196 | * until [a>1.86](expression) 197 | * until [e > 78.124](expression) 198 | * until [k >= 1.001](expression) 199 | * until [a>b](expression) 200 | * until [e > f](expression) 201 | * until [k >= A](expression) 202 | * until [a<10](expression) 203 | * until [e < 90](expression) 204 | * until [k <= 1](expression) 205 | * until [a<5.86](expression) 206 | * until [e < 23.124](expression) 207 | * until [k <= 1.1](expression) 208 | * until [a -l [-o ] 8 | flex_transpiler -h 9 | 10 | Arguments: 11 | Path to the input Flex source file. 12 | 13 | Options: 14 | -l , --target-language The target language to transpile to. 15 | -o , --output Path to the generated output file. 16 | -h, --help Print this help text. 17 | 18 | Target languages available: 19 | c++ 20 | java 21 | python 22 | ''' 23 | 24 | from os import listdir 25 | import re 26 | 27 | from docopt import docopt 28 | from rasa_nlu.model import Interpreter 29 | 30 | MAIN_REGEX = re.compile(r'Main\(\)\s*') 31 | MODEL_DIR = './data/model/default/' 32 | 33 | 34 | def generate_code(interpreted_data, code_dict): 35 | print() 36 | print(interpreted_data) 37 | print() 38 | intent = interpreted_data['intent']['name'] 39 | 40 | kwargs = {} 41 | for entity in code_dict[intent]['entities']: 42 | for e in interpreted_data['entities']: 43 | if e['entity'] == entity: 44 | kwargs[entity] = e['value'] 45 | 46 | return code_dict[intent]['code'].format_map(kwargs) + '\n' 47 | 48 | 49 | def counts_tabs(line): 50 | num_tabs = 0 51 | for character in line: 52 | if character == '\t': 53 | num_tabs += 1 54 | else: 55 | break 56 | return num_tabs 57 | 58 | 59 | if __name__ == '__main__': 60 | args = docopt(__doc__) 61 | source_file_path = args[''] 62 | target_language = args['--target-language'] 63 | output_file_path = args['--output'] 64 | 65 | # import code dictionary for target language 66 | if target_language == 'python': 67 | from languages.python import code_dict 68 | elif target_language == 'c++': 69 | from languages.c_plus_plus import code_dict 70 | elif target_language == 'java': 71 | from languages.java import code_dict 72 | 73 | # load NLU model 74 | interpreter = Interpreter.load(MODEL_DIR + listdir(MODEL_DIR)[0]) 75 | 76 | # parse each line from the input file and store its transpiled code in the 77 | # `code` list 78 | code = [code_dict['default_code']] 79 | current_indent_level = 0 80 | prev_indent_level = 0 81 | with open(source_file_path, 'r') as source: 82 | source_lines = source.readlines() 83 | for line in source_lines: 84 | print(line, end='') 85 | current_indent_level = counts_tabs(line) 86 | if current_indent_level < prev_indent_level: 87 | code.append('\t' * current_indent_level + code_dict['end_block']) 88 | if not line.isspace(): # line is not "blank" 89 | if MAIN_REGEX.match(line): 90 | code.append(code_dict['begin_main']) 91 | else: 92 | interpreted_data = interpreter.parse(line.strip()) 93 | code.append('\t' * current_indent_level 94 | + generate_code(interpreted_data, code_dict)) 95 | prev_indent_level = current_indent_level 96 | while current_indent_level > 0: 97 | code.append('\t' * (current_indent_level - 1) + code_dict['end_block']) 98 | current_indent_level -= 1 99 | 100 | # write lines in `code` list to output file 101 | with open(output_file_path, 'w') as output: 102 | output.writelines(code) 103 | # special case: closing brace for the Java class 104 | if target_language == 'java': 105 | output.write('}') 106 | -------------------------------------------------------------------------------- /transpiler/languages/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flex-lang/transpiler/18e23183ddd6f532889eaa0f53433733cd67614e/transpiler/languages/__init__.py -------------------------------------------------------------------------------- /transpiler/languages/c_plus_plus.py: -------------------------------------------------------------------------------- 1 | code_dict = { 2 | 'end_block': '}\n', 3 | 4 | 'default_code': '#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\ntypedef int integer;\ntypedef float real;\ntypedef string str;\ntypedef char character;\ntypedef bool boolean;\n\n', 5 | 6 | 'begin_main': 'int main() {\n', 7 | 8 | # Output 9 | 'print': { 10 | 'entities': ['to_print'], 11 | 'code': 'std::cout << {to_print};' 12 | }, 13 | 14 | 15 | 'print_elements': { 16 | 'entities': ['to_print'], 17 | 'code': ''' 18 | for (const auto& element : {to_print}) 19 | std::cout << element << ", "; 20 | std::cout << "\b\b \n"; 21 | ''' 22 | }, 23 | 24 | # Input 25 | 'input': { 26 | 'entities': ['var_name'], 27 | 'code': 'std::cin >> {var_name};' 28 | }, 29 | # TODO: multiple input 30 | 31 | 32 | # Conditional 33 | # if 34 | 'begin_if': { 35 | 'entities': ['expression'], 36 | 'code': 'if({expression}) {{' 37 | }, 38 | # else 39 | 'begin_else': { 40 | 'entities': [], 41 | 'code': 'else {{' 42 | }, 43 | # else if 44 | 'begin_else_if': { 45 | 'entities': ['expression'], 46 | 'code': 'else if ({expression}) {{' 47 | }, 48 | # switch 49 | 'begin_switch': { 50 | 'entities': ['switch_var'], 51 | 'code': 'switch ({switch_var}) {{' 52 | }, 53 | # case 54 | 'begin_case': { 55 | 'entities': ['case_value'], 56 | 'code': 'case {case_value} :' 57 | }, 58 | # unless 59 | 'begin_unless': { 60 | 'entities': ['expression'], 61 | 'code': 'if (!({expression})) {{' 62 | }, 63 | 64 | 65 | # Loops 66 | # for each 67 | 'begin_for_each': { 68 | 'entities': ['loop_over', 'loop_as'], 69 | 'code': 'for (const auto& {loop_as} : {loop_over}) {{' 70 | }, 71 | # while 72 | 'begin_while': { 73 | 'entities': ['expression'], 74 | 'code': 'while ({expression}) {{' 75 | }, 76 | 77 | # Declare variables 78 | 'declare_var': { 79 | 'entities': ['name', 'type'], 80 | 'code': '{type} {name};' 81 | }, 82 | # TODO 83 | # 'declare_multi_var': { 84 | # 'entities': ['name', 'type'], 85 | # 'code': '{type} ', 86 | # 'exec': ''' 87 | # for arg in response['entities']['name']: 88 | # code += arg['value'] 89 | # code += ';' 90 | # ''', 91 | # }, 92 | 'declare_array': { 93 | 'entities': ['name', 'type'], 94 | 'code': 'std::vector<{type}> {name};' 95 | }, 96 | 97 | # Assignment / initialisation 98 | 'assign_variable': { 99 | 'entities': ['name', 'expression'], 100 | 'code': '{name} = {expression};' 101 | }, 102 | 'assign_array': { 103 | 'entities': ['name', 'array_values'], 104 | 'code': '{name} = {{ {array_values} }};' 105 | }, 106 | } 107 | -------------------------------------------------------------------------------- /transpiler/languages/java.py: -------------------------------------------------------------------------------- 1 | code_dict = { 2 | 'end_block': '}\n', 3 | 4 | 'default_code': 'import java.awt.*;\nimport java.awt.geom.*;\nimport java.io.*;\nimport java.util.*;\n\n', 5 | 6 | 'begin_main': 'public class Test {\npublic static void main() {\n', 7 | 8 | # Output 9 | 'print': { 10 | 'entities': ['to_print'], 11 | 'code': 'System.out.println({to_print});' 12 | }, 13 | 14 | 15 | 'print_elements': { 16 | 'entities': ['to_print'], 17 | 'code': ''' 18 | for (Object element : {to_print}) 19 | System.out.print(element + ", "); 20 | System.out.println("\b\b \n"); 21 | ''' 22 | }, 23 | 24 | # Input 25 | # TODO handling different data types 26 | 'input': { 27 | 'entities': ['var_name'], 28 | 'code': '{var_name} = System.console().readLine();' 29 | }, 30 | # TODO: multiple input 31 | 32 | 33 | #Conditional 34 | # if 35 | 'begin_if': { 36 | 'entities': ['expression'], 37 | 'code': 'if ({expression}) {{' 38 | }, 39 | # else 40 | 'begin_else': { 41 | 'entities': [], 42 | 'code': 'else {{' 43 | }, 44 | # else if 45 | 'begin_else_if': { 46 | 'entities': ['expression'], 47 | 'code': 'else if ({expression}) {{' 48 | }, 49 | # switch 50 | 'begin_switch': { 51 | 'entities': ['switch_var'], 52 | 'code': 'switch ({switch_var}) {{' 53 | }, 54 | # case 55 | 'begin_case': { 56 | 'entities': ['case_value'], 57 | 'code': 'case {case_value} :' 58 | }, 59 | # unless 60 | 'begin_unless': { 61 | 'entities': ['expression'], 62 | 'code': 'if (!({expression})) {{' 63 | }, 64 | 65 | 66 | # Loops 67 | # for each 68 | 'begin_for_each': { 69 | 'entities': ['loop_over', 'loop_as'], 70 | 'code': 'for (Object {loop_as} : {loop_over}) {{' 71 | }, 72 | # while 73 | 'begin_while': { 74 | 'entities': ['expression'], 75 | 'code': 'while ({expression}) {{' 76 | }, 77 | 78 | # Declare variables 79 | 'declare_var': { 80 | 'entities': ['name', 'type'], 81 | 'code': '{type} {name} = new {type}();' 82 | }, 83 | # TODO 84 | # 'declare_multi_var': { 85 | # 'entities': ['name', 'type'], 86 | # 'code': '{type} ', 87 | # 'exec': ''' 88 | # for arg in response['entities']['name']: 89 | # code += arg['value'] 90 | # code += ';' 91 | # ''', 92 | # }, 93 | 'declare_array': { 94 | 'entities': ['name', 'type'], 95 | 'code': 'ArrayList<{type}> {name} = new ArrayList<{type}>();' 96 | }, 97 | 98 | # Assignment / initialisation 99 | 'assign_variable': { 100 | 'entities': ['name', 'expression'], 101 | 'code': '{name} = {expression};' 102 | }, 103 | 'assign_array': { 104 | 'entities': ['name', 'array_values'], 105 | 'code': '{name} = {{ {array_values} }};' 106 | }, 107 | } 108 | -------------------------------------------------------------------------------- /transpiler/languages/python.py: -------------------------------------------------------------------------------- 1 | code_dict = { 2 | 'end_block': '\n', 3 | 4 | 'default_code': '', 5 | 6 | 'begin_main': '''if __name__ == '__main__':\n''', 7 | 8 | # Output 9 | 'print': { 10 | 'entities': ['to_print'], 11 | 'code': 'print({to_print})' 12 | }, 13 | 'print_elements': { 14 | 'entities': ['to_print'], 15 | 'code': ''' 16 | for element in {to_print}: 17 | print(element, end=', ') 18 | print('\b\b ') 19 | ''' 20 | }, 21 | 22 | # Input 23 | 'input': { 24 | 'entities': ['var_name'], 25 | 'code': 'input({var_name})' 26 | }, 27 | # TODO: multiple input 28 | 29 | 30 | #Conditional 31 | # if 32 | 'begin_if': { 33 | 'entities': ['expression'], 34 | 'code': 'if {expression}:' 35 | }, 36 | # else 37 | 'begin_else': { 38 | 'entities': [], 39 | 'code': 'else:' 40 | }, 41 | # else if 42 | 'begin_else_if': { 43 | 'entities': ['expression'], 44 | 'code': 'elif {expression}:' 45 | }, 46 | # switch 47 | 'begin_switch': { 48 | 'entities': ['switch_var'], 49 | 'code': '' 50 | }, 51 | # case 52 | 'begin_case': { 53 | 'entities': ['case_value'], 54 | 'code': '' 55 | }, 56 | # unless 57 | 'begin_unless': { 58 | 'entities': ['expression'], 59 | 'code': 'if not {expression}:' 60 | }, 61 | 62 | 63 | # Loops 64 | # for each 65 | 'begin_for_each': { 66 | 'entities': ['loop_over', 'loop_as'], 67 | 'code': 'for {loop_as} in {loop_over}:' 68 | }, 69 | # while 70 | 'begin_while': { 71 | 'entities': ['expression'], 72 | 'code': 'while {expression}:' 73 | }, 74 | 75 | # Declare variables 76 | 'declare_var': { 77 | 'entities': ['name', 'type'], 78 | 'code': '' 79 | }, 80 | 81 | 'declare_array': { 82 | 'entities': ['name', 'type'], 83 | 'code': '{name} = []' 84 | }, 85 | 86 | # Assignment / initialisation 87 | 'assign_variable': { 88 | 'entities': ['name', 'expression'], 89 | 'code': '{name} = {expression}' 90 | }, 91 | 'assign_array': { 92 | 'entities': ['name', 'array_values'], 93 | 'code': '{name} = [ {array_values} ]' 94 | }, 95 | } 96 | --------------------------------------------------------------------------------