Code Monkey home page Code Monkey logo

mini-c-interpreter's Introduction

Mini-C-Interpreter

An Interpreter for a Mini C language that is a subset of the C language.

Description:

For this project, I wrote a type-checker and interpreter for a toy imperative language named MINI-C, a C-Like language with block scoping. The type-checker and interpreter were written in Java and incorporate a pre-provided parser and code base. I completed both static functions TypeChecker.isValid( Program p ) & Interpreter.meaning( Program p ) for this project.

The TypeChecker.isValid( Program p ) function accepts a program and returns true if the program is valid and false otherise. The intent of this function is to ensure that the program is strongly and statically typed. Note that the input program must be syntactically correct in order to obtain a Program object on which to operate.

The Interpreter.meaning( Program p ) function accepts a single valid program object and returns the resulting state. The meaning of a program is given by the state that it produces.

How to Run:

Navigate to the directory containing the clite package in the command line.
To compile the meaing function, type: javac ./clite/interpreter/Interpreter.java.
Once compiled, to test it type: java ./clite/interpreter/Interpreter.java test1.c. The test file name can be changed.

Mini-C Language:

Mini-C is an imperative language that supports only the boolean and int data types along with basic arithmetic, logic, and relational operators. There are no function calls, arrays, strings, or complex data types.

Semantics

This section informally defines the semantics of each program element. Since this specification is not formal, it should convey a reasonable specification of the expected meaning of each program element.

PROGRAM
Execution of a program means execute the body in the context of the state imposed by the DECLARATIONS. The meaning of the program is the state that results from executing the body.

BLOCK
Executing a block means execute each statement in the body of the block in the context of the state imposed by combining the enclosing state with modifications imposed by the DECLARATIONS. Each STATEMENT is executed in the order it occurs and the meaning of the BLOCK is the state produced by the final statement in the BLOCK. Note that blocks introduce a new variable scope such that variables from external scopes can be hidden (i.e. new variables with the same name can be declared in nested blocks).

DECLARATIONS
Executing a DECLARATIONS means execute each DECLARATION in the order that they occur. The meaning of the DECLARATIONS is the state that results after execution of the last DECLARATION.

DECLARATION
The meaning of a DECLARATION is the state that results from adding the declared variable to the state and assigning it a value that denotes the notion of not initialized. Note, that the scope of a single declaration extends only to the BODY of the associated BLOCK or PROGRAM.

ASSIGN
Binds the value the expression to the named variable. The type of the variable and the type of the expression must be identical; otherwise there is an error.

IF
The meaning is the meaning of the first statement if the conditional expression evaluates to true. Otherwise, the meaning is the meaning of the second statement if it is present. Otherwise, the meaning is the state in which the IF is executed.

WHILE
Execution of a while first evaluates the expression and then executes the associated statement if the expression is true after which this process repeats.

EXPRESSION
Evaluation of an expression produces the value of the expression.

BINARY
A binary expression represents either a logical or arithmetic operation. Each operator is defined as that of the corresponding Java operator.

UNARY
There are two unary expressions. Each operator is defined as that of the corresponding Java operator.

Mini-C Abstract Syntax

The abstract syntax of ART-C is given below. The abstract syntax defines the objects that you must use when writing your type checker and interpreter. These grammatical classes correspond to actual classes in the provided code base.

Program = Declarations decPart; Block body;
Declarations = Declaration*;
Declaration = Type t, Variable v;
Statment = Skip | Block | Assignment | Conditional | Loop
Skip = empty;
Block = Declarations* declarations; Statement* statements;
Assignment = Variable target; Expression source;
Conditional = Expression test; Statement thenbranch; Statement elsebranch;
Loop = Expression test; Statement body;
Expression = Variable | Value | Binary | Unary;
Binary = Operator op; Expression term2; Expression term2;
Unary = Operator op; Expression term;

mini-c-interpreter's People

Contributors

dakota-kallas 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.