MPS 3.4 Tutorials
Total Page:16
File Type:pdf, Size:1020Kb
MPS 3.4 Tutorials Formatted by Space2Latex from the MPS wiki 2016-09-30 i Credits The MPS user guide has evolved thanks to numerous big and small contributions of many contributors. Although most of the work has beed done by the individual members of the MPS core team, external MPS users have done their share, as well. On this page we list the prominent external contributors, who have helped evolve the user guide: • Markus Voelter • Marco Lombardo Contents Front cover i Credits i Contents ii I Start 1 1 Fast Track to MPS 2 1.1 Ten steps to mastery..............................................2 1.2 Step 1 - Install MPS...............................................2 1.3 Step 2 - MPS from the programmer’s perspective...............................2 1.4 Step 3 - MPS from the language designer’s perspective............................3 1.5 Step 4 - Creating your first language.....................................4 1.6 Step 5 - Why MPS...............................................5 1.7 Step 6 - Projectional editing tips and tricks..................................5 1.8 Step 7 - Shapes tutorial.............................................5 1.9 Step 8 - Calculator tutorial...........................................6 1.9.1 The Calculator tutorial in JavaScript..................................6 1.10 Step 9 - Other sample projects.........................................7 1.11 Step 10 - Generator tutorial...........................................8 1.12 Reviewing the progress.............................................8 1.13 Moving up the ladder..............................................9 1.14 Documentation sources.............................................9 1.14.1 Books.................................................. 10 1.14.2 Cookbooks................................................ 10 1.14.3 User guides............................................... 10 1.14.4 Screencasts............................................... 10 1.14.5 Guided Trainings............................................ 10 1.14.6 General documentation......................................... 10 1.14.7 Glossary................................................. 11 II Introduction 12 2 Shapes - an introductory MPS tutorial 13 2.1 Prerequisities.................................................. 13 2.2 Goal....................................................... 13 2.3 Create a new project.............................................. 14 2.4 Languages and programs under the hood.................................... 16 2.5 Graphical shape................................................. 17 2.6 Circle....................................................... 19 2.7 Square...................................................... 22 2.8 Canvas...................................................... 22 2.9 An early test ride................................................ 24 2.10 Editors...................................................... 26 2.10.1 Shape.................................................. 27 2.10.2 Circle.................................................. 27 2.10.3 Square.................................................. 28 2.10.4 Canvas.................................................. 29 2.11 Second run.................................................... 30 ii CONTENTS iii 2.12 Coloring..................................................... 30 2.12.1 Concept for colors............................................ 31 2.12.2 Pre-defined colors............................................ 31 2.12.3 First touch on dependencies...................................... 33 2.12.4 Concept for color reference....................................... 35 2.12.5 Updating Shape............................................. 37 2.13 Embedding the editor component........................................ 38 2.14 A third run.................................................... 38 2.15 Generator.................................................... 39 2.15.1 Parametrizing the template....................................... 49 2.15.2 Drawing shapes............................................. 51 2.15.3 Generating circles............................................ 53 2.15.4 Parametrization............................................. 59 2.15.5 Reducing Squares............................................ 64 2.15.6 A more robust generation for Squares................................. 65 2.16 Generating code................................................. 67 2.17 Running the code................................................ 69 2.18 What to do next................................................. 72 III Generator 73 3 Generator Demos 74 3.1 Generator Tutorial................................................ 74 3.2 How to Use This Documentation........................................ 74 3.3 Setting-Up Demo Project............................................ 74 3.3.1 New Project............................................... 74 3.3.2 New Demo Solution........................................... 76 3.3.3 New Test Model............................................. 77 3.4 Test Generation................................................. 78 3.5 What’s Inside.................................................. 78 3.5.1 Generator User Guide Demo1...................................... 78 3.5.2 Generator User Guide Demo2...................................... 78 3.5.3 Generator User Guide Demo3...................................... 78 3.5.4 Generator User Guide Demo4...................................... 78 3.5.5 Generator User Guide Demo5...................................... 78 3.5.6 Generator User Guide Demo6...................................... 79 3.5.7 Generator User Guide Demo7...................................... 79 4 Generator User Guide Demo1 80 4.1 Generator User Guide Demo 1......................................... 80 4.2 New Language.................................................. 80 4.3 New Generator.................................................. 81 4.4 Root Mapping Rule............................................... 81 4.5 Property-macro................................................. 82 4.6 The main() method............................................... 83 4.7 SWITCH-macro................................................. 84 4.8 Getting Ready for the First Test........................................ 86 4.9 Generating Test Model............................................. 86 5 Generator User Guide Demo2 88 5.1 Generator User Guide Demo 2......................................... 88 5.2 New Language.................................................. 88 5.3 Conditional Root Rule.............................................. 89 5.4 LOOP-macro.................................................. 91 5.5 Running First Test................................................ 91 5.6 Abandon Root Rule............................................... 92 CONTENTS iv 6 Generator User Guide Demo3 93 6.1 Generator User Guide Demo 3......................................... 93 6.2 New Language.................................................. 93 6.3 addContent(container)............................................. 93 6.4 Generating components............................................. 94 6.5 Inserting components.............................................. 96 6.6 New Test Model................................................. 99 6.7 Adding an $IF$ macro.............................................. 100 6.8 Reusable template................................................ 101 6.9 MAP_SRC-macro................................................ 102 6.10 INCLUDE-macro................................................. 103 6.11 Testing what we have.............................................. 104 6.12 Reference-macro (Resolving by Name)..................................... 104 6.13 The final test.................................................. 106 7 Generator User Guide Demo4 107 7.1 Generator User Guide Demo 4......................................... 107 7.2 New Language.................................................. 107 7.3 Template for Panel............................................... 107 7.4 First Test.................................................... 108 7.5 Restricting the LOOP macro to root elements................................. 109 7.6 COPY_SRC-macro............................................... 111 7.7 Reduction Rule................................................. 112 7.8 Final Test.................................................... 114 8 Generator User Guide Demo5 116 8.1 Generator User Guide Demo 5......................................... 116 8.2 New Language.................................................. 116 8.3 New Test Model................................................. 116 8.4 Pre-processing Script.............................................. 117 8.5 First Test.................................................... 118 8.6 Utility Classes in Generator........................................... 118 8.7 Post-processing Script.............................................. 119 8.8 Second Test................................................... 120 9 Generator User Guide Demo6 121 9.1 Generator User Guide Demo 6......................................... 121 9.2 The core idea.................................................. 121 9.3 New Language.................................................. 121 9.4 New Test Model................................................. 122 9.5 Generator.................................................... 123 9.6 Reduction Rules................................................