title: Pseudocode interpreter?
body: <p>Like lots of you guys on SO, I often write in several languages. And when it comes to planning stuff, (or even answering some SO questions), I actually think and write in some unspecified hybrid language. Although I used to be taught to do this using flow diagrams or UML-like diagrams, in retrospect, I find <strong>"my"</strong> pseudocode language has components of <code>C</code>, <code>Python</code>, <code>Java</code>, <code>bash</code>, <code>Matlab</code>, <code>perl</code>, <code>Basic</code>. I seem to unconsciously select the idiom best suited to expressing the concept/algorithm. </p>
  
  <p>Common idioms might include Java-like braces for scope, pythonic list comprehensions or indentation, C++like inheritance, C#-style lambdas, matlab-like slices and matrix operations.</p>
  
  <p>I noticed that it's actually quite easy for people to recognise exactly what I'm triying to do, and quite easy for people to intelligently translate into other languages. Of course, that step involves considering the corner cases, and the moments where each language behaves idiosyncratically.</p>
  
  <p>But in reality, most of these languages share a subset of keywords and library functions which generally behave identically - maths functions, type names, <code>while</code>/<code>for</code>/<code>if</code> etc. Clearly I'd have to exclude many 'odd' languages like lisp, APL derivatives, but...</p>
  
  <p>So my questions are, </p>
  
  <ol>
  <li><p>Does code already exist that <strong>recognises the programming language</strong> of a text file? (Surely this must be a less complicated task than eclipse's syntax trees or than google translate's language guessing feature, right?) In fact, does the SO syntax highlighter do anything like this?</p></li>
  <li><p>Is it <strong>theoretically possible</strong> to create a single interpreter or compiler that recognises what language idiom you're using at any moment and (maybe "intelligently") executes or translates to a runnable form. And flags the corner cases where my syntax is ambiguous with regards to behaviour. Immediate difficulties I see include: knowing when to switch between indentation-dependent and brace-dependent modes, recognising funny operators (like <code>*pointer</code> vs <code>*kwargs</code>) and knowing when to use list vs array-like representations.</p></li>
  <li><p>Is there any language or interpreter in existence, that can manage this kind of flexible interpreting? </p></li>
  <li><p>Have I missed an obvious obstacle to this being possible?</p></li>
  </ol>
  
  <h2>edit</h2>
  
  <p>Thanks all for your answers and ideas. I am planning to write a constraint-based heuristic translator that could, <em>potentially</em>, "solve" code for the intended meaning and translate into real python code. It will notice keywords from many common languages, and will use syntactic clues to disambiguate the human's intentions - like spacing, brackets, optional helper words like <code>let</code> or <code>then</code>, context of how variables are previously used etc, plus knowledge of common conventions (like capital names, i for iteration, and some simplistic limited understanding of naming of variables/methods e.g containing the word <code>get</code>, <code>asynchronous</code>, <code>count</code>, <code>last</code>, <code>previous</code>, <code>my</code> etc). In real pseudocode, variable naming is as informative as the operations themselves! </p>
  
  <p>Using these clues it will create assumptions as to the implementation of each operation (like 0/1 based indexing, when should exceptions be caught or ignored, what variables ought to be const/global/local, where to start and end execution, and what bits should be in separate threads, notice when numerical units match / need converting). Each assumption will have a given certainty - and the program will <strong>list the assumptions</strong> on each statement, as it coaxes what you write into something executable!</p>
  
  <p>For each assumption, you can 'clarify' your code if you don't like the initial interpretation. The libraries issue is very interesting. My translator, like some IDE's,  will read all definitions available from all modules, use some statistics about which classes/methods are used most frequently and in what contexts, and just guess! (adding a note to the program to say why it guessed as such...) I guess it should attempt to execute everything, and warn you about what it doesn't like. It should <strong>allow anything</strong>, but let you know what the several alternative interpretations are, if you're being ambiguous.</p>
  
  <p>It will certainly be some time before it can manage such unusual examples like @Albin Sunnanbo's <code>ImportantCustomer</code> example. But I'll let you know how I get on!</p>