Giter Club home page Giter Club logo

stupid's Introduction

stupid Build Status

A simple scripting language with a Java runtime and interoperability. Works with Android.

Usage

Imports are omitted for brevity.

Compiling and evaluating an expression (essentially a glorified calculator):

// create a new parser
ExpressionFactory parser = new ExpressionFactory();
// parse a string into an expression tree
Value expr = parser.parseExpression("3 + 5");
// evaluate the expression and print the result
System.out.println(expr.value(new BaseContext())); // prints "8"

Lets spice things up and use a context with some variables instead of the plain BaseContext.

ExpressionFactory parser = new ExpressionFactory();
Value expr = parser.parseExpression("pi*r*r)");
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("pi", Math.PI);
vars.put("r", 2.0f);
// prints "Area of a circle with r=2: 12.566370614359172"
System.out.println("Area of a circle with r=2: " + expr.value(new VarContext(vars)));

Lets also bind it to the System.out object so we can call the println method from the script:

ExpressionFactory parser = new ExpressionFactory();
Value expr = parser.parseExpression("println('Area of a circle with r=2: ' + pi*r*r)");
// a stack context to keep both our vars and binding to System.out
StackContext ctx = new StackContext();
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("pi", Math.PI);
vars.put("r", 2.0f);
ctx.pushExecContext(new VarContext(vars));
// push a reflection context that binds to system.out onto the stack
ctx.pushExecContext(new ReflectionContext(System.out));
// prints "Area of a circle with r=2: 12.566370614359172"
expr.value(ctx);

The Language

Mostly really similar to Java. The grammar lives in src/main/antlr4/Stupid.g4. A short introduction is available in a blogpost.

License

MIT License, see the LICENSE file.

stupid's People

Contributors

madisp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

stupid's Issues

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.