Which tool privides the easiest way to create a textual external DSL for simple code translation? - dsl

In my application I have to type in some code for a specific domain and I would like to create a DSL for that. The DSL just should have some really basic commands.
DSL code example:
srccode{
code: "if("
func: insertInputData(1)
code: "){\n "
func: insertOutputData(0)
code: "\n}\n"
cond: checkForOutputConnection(1):
code: "else{\n "
func: insertOutputData(1)
code: "\n}\n"
}
This code should get translated into source code of a general purpose language (f.ex. Python) like this:
def getSrcCode():
s = ""
s += "if("
s += insertInputData(1)
s += "){\n "
s += insertOutputData(0)
s += "\n}\n"
if(outputConnected(1)):
s += "else{\n "
s += insertOutputData(1)
s += "\n}\n"
return s
So its actually just about a simple translation.
Xtext seems to get quite complex no later than when trying to integrate it in other (non-java) applications and well it seems to stich to Java.
JetBrains MPS is surely is something completely awesome but I don't want a projectional editor, just pure text
(this might be wrong, I didn't use any of them so far)
What workbench or tool would you recommend me to use to achieve the possibility to translate the code into other languages easily like as shown (I prefer something, that is fairly limited somehow but easy to learn and use while fitting my low needs).

It's true that you can't get rid of the projectional editor in MPS, but with a good design of the DSL, you can limit the end-user and control their flow.
If you decide to give a chance to the projectional editor, this is the plugin that you will need http://dslfoundry.com/first-prototype-of-plaintextflow-released/
Cheers!

Related

tool to convert pre-cordinated SNOMED to post-coordinated

I have typed into the international Snomed browser tool `Superficial injury of head" to which I get the following -
http://browser.ihtsdotools.org/?perspective=full&conceptId1=283025007&edition=en-edition&release=v20170131&server=http://browser.ihtsdotools.org/api/snomed&langRefset=900000000000509007
or rather the important details:
Pre-coordinated:
283025007 |Superficial injury of head (disorder)|
*Post-coordinated:
82271004 |Injury of head (disorder)| +
283024006 |Superficial injury of head and neck (disorder)| :
{ 363698007 |Finding site (attribute)| = 69536005 |Head structure (body structure)|,
116676008 |Associated morphology (attribute)| = 3380003 |Superficial injury (morphologic abnormality)| }
I would find it hard to believe that the creators of SNOMED did not have a tool to take pre-coordinated exporessions and output the post-coordinated expressions.
Any SNOMED familiars happen to know of an automated way to achieve this is a tool doesn't already exist?
Thanks
I am not aware of a free online tool to do this. But what is happening is normal form generation; in your example it is giving the definition of a pre-coordinated expression but it could be taking a post-coordinated expression and generating a different normalised expression.
Without the capability of normalising an expression it is impossible to use some more advanced features of SNOMED CT, for example inheritance testing between post-coordinated expressions requires that expressions are converted into a normal form.
Having said I'm not aware of a free tool to do this, there are explicit instructions on how to do this - if you're feeling brave: https://confluence.ihtsdotools.org/display/DOCTSG/12.3.3+Building+Long+and+Short+Normal+Forms

Is it possible / easy to include some mruby in a nim application?

I'm currently trying to learn Nim (it's going slowly - can't devote much time to it). On the other hand, in the interests of getting some working code, I'd like to prototype out sections of a Nim app I'm working on in ruby.
Since mruby allows embedding a ruby subset in a C app, and since nim allows compiling arbitrary C code into functions, it feels like this should be relatively straightforward. Has anybody done this?
I'm particularly looking for ways of using Nim's funky macro features to break out into inline ruby code. I'm going to try myself, but I figure someone is bound to have tried it and /or come up with more elegant solutions than I can in my current state of learning :)
https://github.com/micklat/NimBorg
This is a project with a somewhat similar goal. It targets python and lua at the moment, but using the same techniques to interface with Ruby shouldn't be too hard.
There are several features in Nim that help in interfacing with a foreign language in a fluent way:
1) Calling Ruby from Nim using Nim's dot operators
These are a bit like method_missing in Ruby.
You can define a type like RubyValue in Nim, which will have dot operators that will translate any expression like foo.bar or foo.bar(baz) to the appropriate Ruby method call. The arguments can be passed to a generic function like toRubyValue that can be overloaded for various Nim and C types to automatically convert them to the right Ruby type.
2) Calling Nim from Ruby
In most scripting languages, there is a way to register a foreign type, often described in a particular data structure that has to be populated once per exported type. You can use a bit of generic programming and Nim's .global. vars to automatically create and cache the required data structure for each type that was passed to Ruby through the dot operators. There will be a generic proc like getRubyTypeDesc(T: typedesc) that may rely on typeinfo, typetraits or some overloaded procs supplied by user, defining what has to be exported for the type.
Now, if you really want to rely on mruby (because you have experience with it for example), you can look into using the .emit. pragma to directly output pieces of mruby code. You can then ask the Nim compiler to generate only source code, which you will compile in a second step or you can just change the compiler executable, which Nim will call when compiling the project (this is explained in the same section linked above).
Here's what I've discovered so far.
Fetching the return value from an mruby execution is not as easy as I thought. That said, after much trial and error, this is the simplest way I've found to get some mruby code to execute:
const mrb_cc_flags = "-v -I/mruby_1.2.0_path/include/ -L/mruby_1.2.0_path/build/host/lib/"
const mrb_linker_flags = "-v"
const mrb_obj = "/mruby_1.2.0_path/build/host/lib/libmruby.a"
{. passC: mrb_cc_flags, passL: mrb_linker_flags, link: mrb_obj .}
{.emit: """
#include <mruby.h>
#include <mruby/string.h>
""".}
proc ruby_raw(str:cstring):cstring =
{.emit: """
mrb_state *mrb = mrb_open();
if (!mrb) { printf("ERROR: couldn't init mruby\n"); exit(0); }
mrb_load_string(mrb, `str`);
`result` = mrb_str_to_cstr(mrb, mrb_funcall(mrb, mrb_top_self(mrb), "test_func", 0));
mrb_close(mrb);
""".}
proc ruby*(str:string):string =
echo ruby_raw("def test_func\n" & str & "\nend")
"done"
let resp = ruby """
puts 'this was a puts from within ruby'
"this is the response"
"""
echo(resp)
I'm pretty sure that you should be able to omit some of the compiler flags at the start of the file in a well configured environment, e.g. by setting LD_LIBRARY_PATH correctly (not least because that would make the code more portable)
Some of the issues I've encountered so far:
I'm forced to use mrb_funcall because, for some reason, clang seems to think that the mrb_load_string function returns an int, despite all the c code I can find and the documentation and several people online saying otherwise:
error: initializing 'mrb_value' (aka 'struct mrb_value') with an expression of incompatible type 'int'
mrb_value mrb_out = mrb_load_string(mrb, str);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~
The mruby/string.h header is needed for mrb_str_to_cstr, otherwise you get a segfault. RSTRING_PTR seems to work fine also (which at least gives a sensible error without string.h), but if you write it as a one-liner as above, it will execute the function twice.
I'm going to keep going, write some slightly more idiomatic nim, but this has done what I needed for now.

Capybara: Should I get rid of extracted constants or keep them?

I was wondering about some best practices regarding extraction of selectors to constants. As a general rule, it is usually recommended to extract magic numbers and string literals to constants so they can be reused, but I am not sure if this is really a good approach when dealing with selectors in Capybara.
At the moment, I have a file called "selectors.rb" which contains the selectors that I use. Here is part of it:
SELECTORS = {
checkout: {
checkbox_agreement: 'input#agreement-1',
input_billing_city: 'input#billing\:city',
input_billing_company: 'input#billing\:company',
input_billing_country: 'input#billing\:country_id',
input_billing_firstname: 'input#billing\:firstname',
input_billing_lastname: 'input#billing\:lastname',
input_billing_postcode: 'input#billing\:postcode',
input_billing_region: 'input#billing\:region_id',
input_billing_street1: 'input#billing\:street1',
....
}
In theory, I put my selectors in this file, and then I could do something like this:
find(SELECTORS[:checkout][:input_billing_city]).click
There are several problems with this:
If I want to know the selector that is used, I have to look it up
If I change the name in selectors.rb, I could forget to change it somewhere else in the file which will result in find(nil).click
With the example above, I can't use this selector with fill_in(SELECTORS[:checkout][:input_billing_city]), because it requires an ID, name or label
There are probably a few more problems with that, so I am considering to get rid of the constants. Has anyone been in a similar spot? What is a good way to deal with this situation?
Someone mentioned the SitePrism gem to me: https://github.com/natritmeyer/site_prism
A Page Object Model DSL for Capybara
SitePrism gives you a simple, clean and semantic DSL for describing
your site using the Page Object Model pattern, for use with Capybara
in automated acceptance testing.
It is very helpful in that regard and I have adjusted my code accordingly.

How to add custom IntelliJ Language Injection to scala string-interpolation?

using Scala in IntelliJ, you can already do
var c = s"some ${compound * expression}"
and have proper syntax highlighting for the ${compound * expression}. Scala allows you to define custom string interpolaters, to do other things
var c = javascript"var x = [1, 2, 3]"
Does anyone know how to fiddle with the custom-language-injection functionality to nicely highlight these custom string interpolators? I've messed around with the stuff under the File->Settings->Language Injections but it seems really confusing, and I can't find any existing injections that do the magic string interpolation syntax. Presumably that one is hard coded (since it also has the nice code navigation features) but i'm hopeful there'll be some way of getting it to recognize the nice something"..." syntax and highlighting it nicely for me.
You could do it by hand for each string using the "light bulb", but if you want to do it automatically for each string interpolator, I think you will have to write your own plugin (which may take some time if it is your first plugin).
You may have better answers/help in JetBrains forums directly.

Embedding a domain specific language in an OCaml toplevel -- to Camlp4 or not?

I have some code that includes a menhir-based parser for a domain specific language (a logic). For the sake of my sanity while debugging, it would be great to be able to type instances of this language (formulas) directly in the toplevel like so:
# f = << P(x,y) & x!=y >>
Is campl4/5 my only option? If yes, I find the documentation rather intimidating. Is there an example/tutorial that is close-enough to my use case and that I could conceivably adapt? (For instance, syntax extensions that introduce new keywords do not seem relevant). Thanks!
If you're willing to call a function to do the parsing, you can use ocamlmktop to include your parser into the top level. Then you can install printers for your types using #install_printer. The sessions might look like this then:
# let x = parse ()
<< type your expression here >>
# x : type = <<formatted version>>
I have used specialed printers, and they definitely help a lot with complicated types. I've never gotten around to using ocamlmktop. I always just load in my code with #load and #use.
This is a lot easier than mastering camlp4/5 (IMHO). But maybe it's a bit too crude.
Yes, you can use camlp4 and it will work reasonably well (including in the toplevel), but no, it's not well-documented, and you will have to cope with that.
For an example that is close to your use-case, see the Lambda calculus quotation example of the Camlp4 wiki.
For the toplevel, it will work easily. You can dynamically load "camlp4o.cmo" then your syntactic extension in the toplevel, or use findlib which handles that: from the toplevel, #use "topfind";;, then #camlp4o;;, then #require "myfoo.syntax";; where myfoo.syntax is the name of the findlib package you've created to deploy your extension.

Resources