Giter Site home page Giter Site logo

bash's Introduction

Bash interpreter

A proof of concept to see how feasible a bash interpreter for the JVM would/could be.

Current status

Very early prototype built on top of the libbash ANTLR grammar, aims to implement basic stuff. The grammar provides an AST.

Test drive

Interactive repl can trivially be started:

mvn test -Prepl
...
> a=4
> echo $a
4
> echo $((a + 4))
8
> for((i=1;i<=20;i++)); do echo $i;done | grep 1
1
10
11
12
13
14
15
16
17
18
19
> echo $i
21
>

Anything that does not work provide the abstract syntax tree portion not implemented:

> ls | more
java.lang.UnsupportedOperationException: Tree PIPE not yet implemented:
tree: PIPE
  tree: COMMAND
    tree: STRING
      tree: NAME
  tree: COMMAND
    tree: STRING
      tree: NAME

	at org.crashub.bash.Script._LIST(Script.java:417)
	at org.crashub.bash.Script.execute(Script.java:64)
	at org.crashub.bash.Repl.main(Repl.java:40)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
	at java.lang.Thread.run(Thread.java:744)

Implementing missing features

We use test driven development for implementing the interpreter.

Add a test that make the execution fail

public void testARITHMETIC_EXPRESSION_LESS_THAN() throws Exception {
  assertEquals("0", eval("$((2<2))\n"));
  assertEquals("1", eval("$((2<3))\n"));
  assertEquals("0", eval("$((3<2))\n"));
}

Execute the tests

java.lang.UnsupportedOperationException: Tree LESS_THAN not yet implemented:
tree: LESS_THAN
  tree: DIGIT
  tree: DIGIT
        at org.crashub.bash.Script.assertTree(Script.java:90)
        at org.crashub.bash.Script._ARITHMETIC_EXPRESSION(Script.java:234)
        at org.crashub.bash.Script._STRING(Script.java:143)
        at org.crashub.bash.Script._VARIABLE_DEFINITIONS(Script.java:125)
        at org.crashub.bash.Script._COMMAND(Script.java:386)
        at org.crashub.bash.Script._LIST(Script.java:403)
        at org.crashub.bash.Script.execute(Script.java:64)
        at org.crashub.bash.TestScript.eval(TestScript.java:24)
        at org.crashub.bash.TestScript.eval(TestScript.java:19)
        at org.crashub.bash.TestScript.testARITHMETIC_EXPRESSION_LESS_THAN(TestScript.java:88)

Implement the missing code

case java_libbashParser.LESS_THAN:
  return l < r ? 1 : 0;

Wanna help ?

Sure there are plenty of things to do, get in touch with me

bash's People

Contributors

psiniemi avatar vietj avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.