Compiler for Bash [closed] - bash

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am taking a Compiler Design course in my undergraduate studies. As a part of the learning process, I'd have to develop the compiler for a language.
Can a compiler be written for Bash?
Would it be more difficult than designing a compiler for a regular programming language, like C/C++ and thus outright inconceivable, at least for a newbie?

Can a compiler be written for Bash?
Yes. (Existence proof - shc.)
If yes, how?
That's the hard part.
POSIX shell languages are very different to typical programming languages because of the effects of things like backticks, variable substitution, quoting, and so on.
You could ignore this and implement a "bash like" language, either leaving out the difficult features, or treating them in a way that doesn't conform to POSIX behavior.
Then ... there is the problem of how to generate something that is executable. Again, that is possible (see above), but if your aim is to be faster than a regular shell then you need to do things like emulating the behavior of common Linux commands in the compiled code. That is a huge task.
I'm not saying this is a bad project, but you will need to do a lot of work, including:
finding, reading and (fully) understanding the POSIX shell specs
researching how to implement a parser that deals with POSIX idiosyncracies
figuring out which linux commands need to be implemented directly, and
figuring out how to deal with the ones that you don't; e.g. all the complexity of pipelines.

Related

What's the fastest way to run a shell command in ruby? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've seen many times that the overhead of Kernel#` and system(...) themselves can be a bottleneck in scripts. There are sometimes ways of reducing the overall # of shell invocations, e.g. with xargs, but sometimes I do need to invoke the shell many times in a row. What's the fastest way to do this? (I don't need any special environment variable setup or anything like that, nor even always the result code or stdout)
Remember there's two ways to run an external process:
With system("script with arguments") where that must pass through the shell first.
With system("script", "with", "arguments") where the command is run directly, bypassing the shell. This can be slightly faster if spinning up a shell is expensive, which it can be depending on your shell's configuration.
Note that both of these involve a fork/exec cycle that cannot be avoided.
If you need to do a lot of processing you might want to look into how xargs can work for you, especially when you can feed in a list of files/arguments via Open3. If you can somehow separate the output, which could be simple or hard, you can get a lot of mileage out of one shell process.

How do Hardware Description Languages differ from General Purpose languages at the low level? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Question:
How do Hardware languages (HDLs) differ from general purpose languages such as Python, Java, etc. In particular, what is the primary trade-off that causes general purpose languages to be sub-optimal for FPGA's when compared to VHDL and Verilog?
Context:
I'm a programmer but definitely work at a high level of abstraction such as JavaScript, tinkering with API's, etc. My low-level knowledge is very limited but I am playing around with an FPGA and have some novice questions that I cannot solve with Google or Wikis.
Considering I am a novice, please do not vote harshly against this post. Just state your suggestions for the question and I will happily revise! :)
Example:
For example, why isn't everyone just coding FPGAs and ASICs with Python or C# instead of Verilog or VHDL? I understand that there are some Python libraries, but I have read that they are limited in their viable use-cases. I would greatly appreciate someone shining some light on why HDLs are necessary and beneficial and why general purpose languages are not optimal in comparison for these scenarios.
Thanks in advance!
This is a broad opinionated question, but I think there is a short answer. In some sense, they are all programming languages, i.e text descriptions that gets compiled into a set of machine instructions to be executed on a host machine(software).
But an HDL is also a text description that gets compiled into a set of machine instructions to build another machine (hardware).
Technically, any programming language could be used to describe hardware (SystemC in C++ as an example), Verilog and VHDL were specifically developed to model and simulate hardware most efficiently.

When to use declarative programming over imperative programming [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
As far as I know the main difference between declarative and imperative programming is the fact that in declarative programming you rather specify what the problem is, while in imperative programming you state exactly how to solve a problem.
However it is not entirely clear for me when to use one over another. Imagine you are imposed to solve a certain problem, according to which properties you decide to tackle this down declaratively (i.e using prolog) or imperatively (i.e using Java)? For what kind of problems would you prefer to use one over the other?
Imperative programming is closer to what the actual machine performs. This is a quite low level form of programming, and the more complex your application grows, the harder it will be for you to grasp all details at such a low level. On the plus side, being close to the machine, you can write quite performant code if you are good at that.
Declarative programming is more abstract and higher level: With comparatively little code, you can express quite sophisticated relationships in a way that can be more easily seen to be correct.
To see an important difference, compare for example pure Prolog with Java: Suppose I take away one of the rules in a Prolog program. I know a priori that this can make the program at most more specific: Some things that held previously may now no longer hold.
On the other hand, suppose I take away a statement in a Java program: Nothing much can be said about the effect in general. The program may even no longer compile.
So, changes in an imperative program can have very unforeseen effects, and are extremely hard to reason about, because there are few guarantees and invariants, and many things are implicit in some global state of the program. This makes imperative programming very error-prone.

Style of writing conditional operators [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is considered more appropriate style of writing conditional operators?
if(1){
puts("Hello")
}
or
if(1) puts("Hello")
Similar aspects of coding style are welcome too.
That's all depends on your preference, that's why we rarely see people code in the same style.. Moreover, it depends on which programming language you're using.. IMHO, the important thing in coding is code readability and comments, so when your BOSS asks other people to help or develop your code. He /she will spend the least amount of their time to understand your code..
If you ask specifically from your example above, I would prefer the first one.. Because in my OPINION, imagining the WHOLE code, that one will give better readability. HOWEVER, some people may argue that it will spend some of your time typing those brackets over and over..
As per the PSR standards any structure must always enclose the code in parentheses.
The body of each structure MUST be enclosed by braces. This standardizes how the structures look, and reduces the likelihood of introducing errors as new lines get added to the body.
from the official website
Please have a look under control structures section http://www.php-fig.org/psr/psr-2/

First programming language to have an interactive shell? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Out of sheer curiosity and the pursuit of trivia, I couldn't find an answer on Google quickly.
Dear fellow programmers, what is the first programming language to provide an interactive shell?
I can't prove other systems weren't earlier but the LISP REPL construct is one common name given to this style of interpreter.
The LISP I Programmers Manual from 1960 (PDF) includes a mention on page 2 that is apropos:
Enlargements of the basic system are available for various purposes. The compiler version of the LISP system can be used to compile S-expressions into machine code. Values of compiled functions are computed about 60 times faster than the S-expressions for the functions could be interpreted and evaluated. The LISP-compiler system uses about half of the 32,000 memory.

Resources