Is there a programming language supports parsing customized DSL? - javascript

As a programmer, I have to write DSL from time to time. Mostly, they are written in a "host" programming language, in a form of strings or function calling chain. For example, when writing database-relative code, I have to concatenate SQL for querying. If I decide to use a ORM framework like hibernate, usually I will chain functions to define my query conditions, which can be regarded as another form of DSL.
In many cases, it is acceptable to write DSL inside a host language, but if the DSL become complicated, it will guadually loss its readability. Since no matter how, in-host DSL has to comply with the host language's syntax, some additional things have to be added to the DSL, like braces/brackets/parentheses, which doesn't convey any meanging but produces noises.
Is there any solution about this problem? I think the answer is yes - but partially. We can observe that many languages support "templated string", and JSX, a enhanced JavaScript, supports HTML template embedding. But all these solutions are only for curtain particular DSL. As far as I known, there isn't a language provides a general mechanism on dealing this problem. But there are so many programming languages in the world, and new programming languages are introduced oftenly. This makes me wondering, Are there some less-known languages
allow people to write DSL freely, without considering the host language's syntax? For example, a language with a DSL-parsing-system, which is able to analyze a DSL as long as you install a parser of that DSL.

Related

Is Javascript any better than before as mentioned in the O'Reilly Javascript Patterns book for closures and anonymous functions?

This is a quote from the O'Reilly Javascript Patterns book:
JavaScript is also an unusual language. It doesn’t have classes, and
functions are first class objects used for many tasks. Initially the
language was considered deficient by many developers, but in more
recent years these sentiments have changed. Interestingly, languages
such as Java and PHP started adding features such as closures and
anonymous functions, which JavaScript developers have been enjoying
and taking for granted for a while.
and that's it. I really don't understand how Javascript was considered "deficient" before, and now is not, because other languages like Java or PHP has added closures and anonymous functions? Aren't they just generic computing concepts? Aren't they available in other languages like Ruby? So I don't really know how Javascript is now not "deficient" because Java and PHP added closures and anonymous functions as their features? Why is that?
I think what it's referring to is that in the past many developers considered JavaScript as a 'toy' language and only used it to do quick Web UI tasks like validation etc. without bothering to understand how the language really worked.
In recent years the 'hidden' features of JavaScript such as closures, prototypal inheritance etc. have come to the fore and people are now taking JavaScript much more seriously as a 'real' language.
So JavaScript was never really "deficient" but people may have thought that it was due to their misconceptions about the language.
I personally think it's a poor editing job.
The paragraph should have read (bold addition is mine and is just a suggestion on how
to read it):
JavaScript is also an unusual language. It doesn’t have classes, and
functions are first class objects used for many tasks. Initially the
language was considered deficient by many developers, but in more
recent years these sentiments have changed due to a better and more uniform browser support, extensive work done by various ECMA editions and evolution of various JavaScript frameworks. All, or much, of that change is a direct result of ever-expanding movement of software products to the Web and growing demand for a lightweight language for mobile applications (this one can be somewhat argued).
Interestingly, languages such as Java and PHP started adding features
such as closures and anonymous functions, which JavaScript developers
have been enjoying and taking for granted for a while.
For years, JavaScript has been known to only be a browser language, for (simple) features that required more dynamic and/or flexibility than offered by HTML and CSS.
For several years now, JavaScript evolved as a language and as platform, offering powerful libraries, which also removed certain cross-browser incompatibilities. With rise of these libraries, community actually only started to learn how to use some powerful concepts of JavaScript, such as closures and prototypal inheritance.
They have not been widely used nor known even to JavaScript developers before, since JavaScript is in its basics very simple language, and most developers didn't even have to learn it as you'd learn Python, C or Java - all they had to look at were tutorials and many of them didn't include these language features.
Duo to the increase in popularity, JavaScript (officially ECMAScript) has received several 'editions', where features have been added to the language itself. Latest edition (still under development) will introduce some radical novelties (considered are classes, module system, iterators, algebraic types among others), so JS will become even better, more structured and more easily maintainable language.
JavaScript is raising in popularity on server side also (node.js), and proves to be easy, fast and enough powerful language to do (almost) any kind of work. Node widely 'abuses' probably its best feature - asynchronous nature of JavaScript. That is not new, many languages have async libraries, but as Ryan Dahl said in one of his earliest speeches on node, JS is perfect for it. And mainly because of the closures and other concepts built into it.
So final answer would be the author of the book you're quoting has poorly expressed his thoughts or is simply wrong. It was a toy language and didn't mean to be used for such variety of use cases as it is today. But it evolved, just as Java or PHP did (don't forget PHP didn't have OOP support that seems so obvious now until v5). Other languages are just picking up goodies that JavaScript proved to be more efficient for certain use cases. Overall, my thoughts are that JavaScript hasn't been used properly from the start, and that's the case it had been called 'deficient'. Now developers got chance to explore its magnificence.

Are there any prototype-based languages with a whole development cycle?

Are there any real-world prototype-based programming languages with a whole development cycle?
"A whole development cycle" like Ruby and Python: web frameworks, scripting/interacting with the system, tools for debugging, profiling, etc.
Thank you
A brief note on PBPLs: (let's call these languages PBPL : prototype-based programming language)
There are some PBPLs out there. Some are being widely used like JavaScript (which Node.js may bring it into the field - or may not!). One other language is ActionScript which is also a PBPL but tightly bound to Flash VM (is it correct to say so?).
From less known ones I can speak of Lua which has a strong reputation in game development (mostly spread by WOW) but never took off as a full language. Lua has a table concept which can provide you some sort of prototype based programming facility.
There is also JScript (Windows scripting tool) which is already pointless by the newcomer PowerShell (I have used JScript to manipulate IIS but I never understood what is JScript!).
Others can be named like io (indeed very very neat, you will fall in love with it; absolutely impossible to use) and REBOL (What is this all about? A proprietary scripting tool? You must be kidding!) and newLISP (Which is actually a full language, but no one ever heard about it).
For sure there are much more to list here but either I do not remember or I did not understood them as a real world thing, like Self).
I would argue that JavaScript is a real world language. The main difference is that it tends to be embedded into lots of different environments rather than being a stand alone development environment.
Aside from the obvious use in a browser, JavaScript can be used on the server side using CouchDB (which is becoming very popular as a database both for websites and the desktop) or Node.js - there are lots of others as well. It can also be used to create desktop applications via seed or gjs.
As for debugging tools, these are included in most web browsers and there are extensions such as firebug.
The approach is different in JavaScript - instead of having a core language and using libraries to access different programming environments, it is embedded directly into these environments.
It doesn't look like it. Checking out the Wikipedia list, I can't say any of them are particularly popular for systems-type or standalone programming. And I have a few theories why:
Inertia - people are more familiar with procedural/procedure-class based languages. In the same way procedural languages are more popular than functional, I think we find it easier to mentally keep track of the objects we've specified beforehand
Less error-prone - When I use a class, I can't use things not specified in that class. It's a bit less flexible, but when working on a large project I'm very, very grateful to be able to look at the spec for an object (even if I've written that object) and know how the object will behave. That is, I know the compiler will probably throw an error if I try to do something stupid like use the wrong variable name.
You mention "scripting/interacting with the system". As far as that goes, classes aren't even particularly popular. One-off scripts may use a few functions, or even just straight procedural code. I know if I write a simple little Python script, I'll have a few simple functions and I'll use builtin objects. It's OO, but I'm not writing anything.
No/not many suitable languages. This is I think the biggest one. Lua's great for embedded scripting, but I don't think it can replace Python/Perl scripts in my toolkit.
Prejudice - the sordid past of Javascript has not been good for prototype-based languages, I don't think. Javascript's actually a pretty nice language, once you see code written by somebody that understands it, but there's still a lot of derision - it's seen like a kiddie language. If you say "I'm a Javascript programmer", it's hard to be taken seriously.
EDIT It's either a sign or a symptom, but most PT languages are embedded or application-specific scripting languages (Lua in games or for UIs, Javascript in webpages or for the UI like in Firefox, etc). I'm not sure why this is, but they are either more suited to application customizing or otherwise-embeddedness, or that's what the common implementations are designed for. Python can be easily embedded into a program, and it sometimes is, but it's more common standalone.
This may be more philosophical than you're looking for, and I'm by no means a prototype-based expert, but I've done a fair bit of Javascript and fiddled with Lua. I stand by my answer, though, based on at least personal experience. YMMV.
I see no reason why a PT language as useful as Python couldn't be written, but it doesn't seem like anyone's done it.
I did not found one and the only candidate (JavaScript) is still spending very first steps (like Node.js).
Still one other valid candidate would be JavaScript! There is a .NET based implementation named IronJS which is implemented in F# and is going pretty well. This way one can have whole .NET development cycle at his toolbox. It is based on DLR and can be used in both .NET and Mono!

What “type” of language is JavaScript?

Programming languages are often classified by their structure. To date there are many types, including (but not limited to): Procedural, Structured, Object-Oriented, Concurrent, Functional, Event-Driven, Compiled, and Interpreted (Source). Some programming languages are a mixture of these constructs, while others may fall under only one of the headings listed above.
My question is: What type of language can JavaScript be classified as?
The Javascript Wikipedia entry tells you.
JavaScript (/ˈdʒɑːvəˌskrɪpt/), often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions.
Paragraph three goes on to say:
As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative programming styles (emphasis added). It has application programming interfaces (APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM).
Regarding your selection of Procedural, Structured, Object-Oriented, Concurrent, Functional, Event-Driven, Compiled, and Interpreted as "types" of a computer language, Javascript is quite powerful; it is at least (or can be) Procedural, Structured, Object-Oriented, Functional, Event-Driven and Interpreted.
However, as your source says, many languages do not fall into one category only, but can be used in different ways. Even for example Java could be used in a rather strict procedural way.
And although Javascript supports functional programming, it is not the lingua franca of functional programmers - but they could use it, if they wanted to.
Javascript is so versatile, because it is prototype-based and because you can pass functions as parameters to other functions, which really allows for some crazy programming techniques (read: HACKS) combined with dynamic creation of Javascript it can be really powerful and confusing as well :)
You might want to look up http://en.wikipedia.org/wiki/Prototype-based_programming for more explanations.
Mozilla Developer Network
...
JavaScript is a multi-paradigm, dynamic language with types and operators, standard built-in objects, and methods. Its syntax is based on the Java and C languages — many structures from those languages apply to JavaScript as well. JavaScript supports object-oriented programming with object prototypes, instead of classes (see more about prototypical inheritance and ES2015 classes). JavaScript also supports functional programming — functions are objects, giving functions the capacity to hold executable code and be passed around like any other object.
...
JavaScript is an implementation of the ECMAScript language standard and is primarily used in the form of client-side JavaScript, implemented as part of a web browser in order to provide enhanced user interfaces and dynamic websites. This enables programmatic access to computational objects within a host environment.

What scripting languages are similar to ECMA?

I am looking for an ECMAScript alternative to work as a scripting language for custom application logic. There are things I like about ECMA, especially the syntax for the newer spec(like AS3).
Unfortunately, the AS3 engine is not open source to be able to integrate in a project. I would like a scripting language that was designed for object oriented use.
Specifically, is there a language that has:
Statically typed variables(optional)
Classes, including public/private members
Inheritance, including Interfaces
Packages(optional)
Clean syntax
Must be able to interface as an internal scripting language for an application(like Javascript for a browser), can not be an external system call.
Things I would rather do without
The messy ECMA prototype object
What languages that you know about fit this profile? I've had difficulty finding a quality scripting language that was designed for good object oriented design.
In Java the best ECMAScript (Javascript) option is to embed Rhino. I don't find the prototype-based inheritance a deal killer, as with a bit of discipline you can make it behave almost like class-based inheritance.
If you want something that interoperates very closely with Java, go with Groovy. If you need JVM compatibility, also look into Jython (python on the JVM), Jruby (Ruby on the JVM) and Scala (a functional language on the JVM). If you don't care about Java and JVM compatibility, look at Ruby, Python, and Erlang. Clojure is a dialect of Lisp on the JVM.
Going further afield, TCL (Tool Command Language) lets you embed an interpreter in C/C++ code, there are many embeddable Lisp and Scheme interpreters, etc.
If you want a scripting language that works like ECMAScript, why not use ECMAScript? There are many Open Source implementations, just take a look at the list on Wikipedia.
I'd recommend either Python or Ruby. Neither are like ECMA, but I learned them after JavaScript, and they were a snap to pick up. Plus, they are more powerful languages, making it a better alternative to using a JavaScript engine inside of your application (Rhino for Java).
Python
Forces clean syntax (almost like English while is not False:)
Multiple inheritance (no interfaces)
Interpreter can be extended using C/C++ (possibly used for your adapters, if needed)
Ruby
Syntax is supposed to be close to English (unless conditional, until loop)
Everything is an object
Only supports single inheritance, but uses Mixins to add functionality
Both
Classes
Can be embedded in another application
Private members
Packages
Lua - everything you want and more in ~100KB
See this page for comparison betwen Lua and other mentioned languages.
Haxe on Neko looks like the exact thing you want. I don't know how embeddable nekovm is, but it is opensource so you can fiddle with it. http://haxe.org
The Ruby interpreter can also be embedded within C programs, and may be considered by some to be more object-oriented than Python.
We use ECMAscript as an extension language for the software product I work on and it works quite well. Being a standard (and popular) language, it's nice to be able to point our users to the copious off the shelf documentation for the language. We built our own ECMAscript compiler that translates into bytecode for the platform we are running on, but if I had it to do over again, I'd seriously consider embedding Google's V8 ECMAscript engine (in fact, I'd even consider building our entire app on it).
I'd go with something fairly mainstream to simplify things. Having read your requirements, I'd recommend Python. It doesn't really have interfaces in the Java/C# way, but it doesn't really need them, either. Other than that, it should be a good fit.
QtScript is ECMAScript. You don't mention what the main part of your application is written in, but I suppose it's not Qt, or you'd already know about QtScript.

Scripting and Programming [duplicate]

This question already has answers here:
When is a language considered a scripting language? [closed]
(40 answers)
Closed 8 years ago.
Where does Scripting ends and Programming begins? ActionScript 3 and JavaScript/JScript are so different...
The distinction was meaningful once, but the line is getting increasingly blurred to the point where I dont think it is useful today.
historically, scripting languages are interpreted, programming langauages are compiled. But with the advent of VM's and JIT, the line is increasingly blurred.
scripting is when an existing application or tool is manipulated programatically (e.g. office macros), rather than building an app from scratch. Again the line is getting blurred, because libraries and frameworks means that everybody is building on existing tools. Also, API's allow you to use traditional compiled languages to manipulate applications, eg. in Office you can now automate using .net, which I supppose means you can write Excel macros in managed C++.
languages tend to outgrow their niche. Perl was concieved as a scripting language for text manipulation, but has since grown into a full-fledged programming language.
scripting languages have traditionally been higher-level with features like garbage collection and implicit typing which allowed the developer to focus on the task rather than the metal. Real programming languages like C forced you to be more concerned about memory management and machine architecture. Again, the line is being blurred by newer languages. For example, C# has high-level features like garbage-collection and dynamic types, and low-level features like pointers.
For these reasons scripting has been seen as more accessible, where programming has been seen as more demanding and hard-core. Therefore the term scripting can be used disparagingly, like in "JavaScript is just a scripting languague, not a real programming language" or "he is just a scripter, not a real programmer".
Is there a difference? Youre just writing software in different languages for different run-time environments.
It reminds me of an old definition of the difference between prose and poetry which went something like:
There once was a lady from Glass
Who went into water up to her knees.
That's prose. If she had gone deeper, it would have been poetry.
It strikes me that the difference between scripting and programming is a matter of degree, not what language you are using. I've seen people write some pretty sophisticated programs using the bash shell, and others write trivial things using C. Which is the programmer and which is the scripter?
When you're able to write something that's more than a quick hack or something knocked together to get the immediate job done, when you've written something with an eye towards craftsmanship and maintainability, then you've written a program.
The German Wikipedia says:
Aus architektonischer Sicht werden
Skriptsprachen verwendet, um aus
bestehenden Komponenten ein
Anwendungsprogramm zu bauen. Die
Komponenten selbst werden in einer
Programmiersprache entwickelt.
Translation:
From an architectural point of view, script languages are used to
build an application program from
existing components. The components
themselves are developed using
programming languages.
Script languages use components that are already there to make programs. These components are written in programming languages. :)
Think about shell scripting... There you use little programs "mostly" written in C.
Programming is programming.
People often differentiate between scripting languages and compiled languages, but the distinction isn't that useful IMO.
I.e. can compile many scripting languages and interpret many compiled languages.
This question is similar to these past questions:
Programming vs scripting — what’s your definition?
What’s the difference between a “script” and an “application”?
A script, in common usage, is a sequence of actions ("enter stage left"), like a recipe.
For example, a testing script is a simple sequence of actions to perform. It has zero cyclometric complexity.
Programs are more general, and can do anything. They should be modular and designed for reuse.
As software expands, scripts become more complex, but at heart they are imperative recipes, designed for a single purpose.
In one of the wargaming simulations I've worked on, one of the big selling points was that it wasn't scripted, but each entity in the simulation was an autonomous actor which responded to events.
Whether or not the runtime is interpreted, compiled, jited or whatever doesn't matter. Awk scripts were compiled. Lisp programs were often interpreted. Often the unit test scripts for a program are written in the same language as the program.
If I had to point to a language difference, it would be the presence of a module system. Apart from C, all 'proper' programming languages have a module system. JavaScript is getting a module system as part of the ServerJS working group, and is obviously mature enough for applications programming, so is now neither anything to do with Java nor exclusively a scripting language.
The difference should really be made between scripted (interpreted) and compiled code. Programming, I think, is just the general term of giving instructions to a computer or machine.
Programming Languages: Languages that are compiled before they are deployed.
Scripting Languages: Languages that are code when they are deployed and are interpreted on the fly when they are requested.
In the end it's all the same sh*t, they are all programming languages
Two definitions that I find particularly useless are:
Programming depends on the code's being compiled before deployment
Scripting means that the code is "only" used to connect components
I was initially tempted to draw the line around the intention with which the code was written. I would have called one-off with limited lifetime and scope a script and everything else a program. But then, I have written quite a few such programs in C ... and I wouldn't call anything in C as scripted.
My current working definition is this: Scripted code tend to be limited in scope, small, and (hopefully) simple to understand and replace. Everything else is programmed. :-)
Remember that most modern scripting languages have frameworks or backends with some built-in compiling to pre-build and cache the scripts. (see PHP with some optimizers like Zend or eAccelerator, template engines...)
Modern scripting languages like Actionscript3, PHP, Javascript allowing most of modern design pattern (like MVC) and some OOP paradigms - and some advanced designs like "closures". So you have all for "programming".
Rich Internet Applications (RIA) needs a lot of programming skills, the same with RIA running as a desktop application (Flash/Flex with Adobe Air). This has nothing todo anymore with some easy scripting embedded in a website. This are complex applications needing a lot programming skills.
Because most of the scripting languages uses some "backend" to get/retrieve data from databases, this applications are often not easy to develop because of much side effects. People must have knowledge with server stuff to setup their development environment, other scripting languages like PHP for the backends and so so on. Implement some same thing with C++ and a good framework like Qt, wxWidgets are sometimes easier and needs not much more programming skills.
Scripting and programming are for me the same. Yes, you could say "writing a onclick handler for a button on a website is scripting... writing a videoplayer or game with Flex/Flash and Actionscript3 is programming"?
There a differences between a scripting language and a compiled language (host access, sandbox, performance...). But its all programming.

Categories

Resources