Code Monkey home page Code Monkey logo

bc-interpreter's Introduction

bc-interpreter

Grammar and interpretter for bc

How to run

antlr4 Calculator.g4

javac *.java

java CalculatorRun input/<input file> (if no file name is provided, it will read input from the standard input stream)

The start rule is prog

Features

  • Scoping
    • Functions, loops, if-statements, and code blocks ({}) each have their own scopes
  • Comments: both block /* ... */ and line // comments
  • Arithmetic expressions: addition (+), subtraction(-), multiplication(*), division(/), exponent(^), modulo(%)
  • Arithmetic expressions with integers and doubles
  • Arithmetic expressions with variables
  • Boolean Expressions (!, &&, ||)
  • Precedence (handles unary minus)
  • Special Expression: read and sqrt
  • print: prints expression followed by print i.g print 1, 2, 3 output 123
  • Math library functions: s, c, l, e
  • Relational Expressions (i.g >, >=, <, <=, ==)
  • Function declaration
  • Function call
  • If-statements
  • For loops
  • While loops

Note: read() only accepts integers or doubles

Scoping

alt text CalcGlobalScope object is created at start of parsing and its parent is set to null. For every new block statement, for loop, if statement, and while loop, a new FuncLocalScope object is created and the currentscope is temporarily set to this new scope.

The scope is restored to previous scope upon exiting current scope. For function delcaration, scoping works similarly. In addition to symbol table that maps variable names to its values, a CalcFunctionScope object stores its function parameters and statement context so that they can be executed during function calls.

In a function call visitor method, the current scope will be checked to find a function scope that matches the function name. If found, current scope is set to this function call, and using the arguments, the parameter variable names saved previously are mapped to the argument values. Code is executed and any new variables and scopes are added to the current function scope.

Upon exiting function, scope is restore to previous scope.

Only global variables and are visible in the functions along with their own local scopes.

bc-interpreter

bc-interpreter's People

Contributors

ncarbon 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.