-
Notifications
You must be signed in to change notification settings - Fork 0
jackromo/test_interp
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
About
An interpreter for a programming language written in Python.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published