Coder Social home page Coder Social logo

test_interp's Introduction

This is a simple interpreter of a basic programming language.

It consists of a lexer, parser and back-end evaluator.
The interpreter.py file reads a source file and feeds it to the lexer.
The lexer takes this string as input and produces a list of tokens.
The parser is then fed this list, which it converts to a tree of classes.
This tree is then evaluated via small-step semantics with the evaluator.

To run the program, type:

python interpreter.py <file_name>

If no file name is given, it will attempt to run a file called 'test' in the same directory.

It can comprehend:
  -integers, booleans and strings
  -expressions
  -variables
  -assignments
  -sequences of statements
  -if statements
  -while loops
  -function definitions and calls
    -closure supported and functions can be passed as variables
    -automatic currying, partial applications
    -function scope
  -special print(val) and input(val) functions
  -pairs, with car(p), cdr(p), p=setcar(p,newcar) and p=setcdr(p,newcdr) functions for pair 'p'
  -comments (//comment\n)
  -Multi-file programs (import <filename>) and libraries
  -lists, with elem() and setelem()

The EBNF semantics can be seen in the parser.py file.

Planned features:
  -Lambda expressions
  -Scope for if and while statements (?)
  -Greater type functionality
    -Strong typing
    -Type declarations for functions (?)
    -Structures, unions, enumerations
    -Classes and objects
  -Lazy evaluation
  -Streams

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


The language is weakly typed, so variables can have any value at any time.


x = 5;                // Integer
x = "Hello";          // String
x = false;            // Boolean
x = pair[1, "hey!"];  // Pair (can take any data types as vals)
x = [1, 2, 3];        // List


Several operations and comparisons are supported: +, -, *, /, %, >, <, and ==.
Functions are also considered a data type, so are assigend to variables.


f = function(x,y){  // Function (indentation not necessary, can be on one line)
  return x+y;
};
y = f(5,6);  // y = 11
g = f;       // g can now be called as a function
t = g(1,2);  // t = 3


Closure is also supported.


f = function(x){
  return function(a){ return x+a; };
};
g = f(2);  // g = function(a){return 2+a;};
x = g(4);  // x = 6


All functions are automatically curried, so partial application is supported.
The predefined function curry() used to be used for this, but is now deprecated.


add = function(x,y){    // Example multi argument function
  return x+y;
};
addfive = add(5);       // addfive = function(y) { return 5+y; }
print(addfive(6));      // Prints '11'


Pairs and lists have several special functions.


p = pair[1, 2];         // Create pair
x = car(p);             // x = 1
y = cdr(p);             // y = 2
a = setcar(p, false);   // a = [false, 2]  (does NOT alter p)
b = setcdr(p, [2,3]);   // b = [1, [2, 3]] (does NOT alter p)

l = [1,2,3,4];          // Create list
x = elem(l, 0);         // x = 0th element of l
m = setelem(l, 1, "j"); // m = [1, "j", 3, 4] (setelem(list, index, new_value))


For input and output, print() and input() can be used.


print(5);                       // Print out value 5
x = function(x){return x+1};
print(x);                       // Print out function
y = input("Type something: ")   // print out "Type something: ", y = whatever is typed


NB: Predefined functions cannot be partially applied.
To partially apply one, use this workaround:


partialsetcar = function(x, y) {  //Outer function will be curried, so as if setcar() was curried
  return setcar(x,y);
}
g = partialsetcar(pair[1,2]);   // x = Pair[1,2]
print(g(5));                    // prints (5, 2)


For control flow, if statements and while loops are available.


x = 5;

if(x>0) then {
  print("x is big");
} else {
  print("x is nothing");
}

while(x > 0) {
  x = x-1;
  print("x is " + x);  // NB: strings and other data types can be concatenated.
}


Programs can be split between multiple files; simply use the 'import' keyword.
All this does is run the specified file, and add any values defined by the execution to the environment.

import "hello";       //Import the file "hello"; hello contains "x=4;" in this example.
print(x);             //print(4);


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

test_interp's People

Contributors

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