Skip to content

Latest commit

History

33 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

JavaScript Interpreter

An interpreter for a (modified) subset of JavaScript written in Python.

Examples of Supported Features

/******** Basic Types ******************************/// Numbersconsole.log('numbers',123,3.14);// Stringsconsole.log("double quotes",'single quotes','escape\n\t\asequences');// null (no undefined for simplicity)vara;console.log(a,null,a==null);// Object literalsvarorigin={x:0,y:0};varp={'x': 1,'y':1};// Bracket notationvardx=origin['x']-p['x'];// Dot notationvardy=origin.y-p.y;/******** Functions ******************************/// Closures and recursionvarfib=(function(){varmemo={};returnfunction(n){if(n<2){returnn;}elseif(ninmemo){returnmemo[n];}else{// No tail recursion optimization or named functions yetvarf=fib(n-1)+fib(n-2);memo[n]=f;returnf;}};})();// Higher order functionsvarmap=function(f,start,end,step){vari=start;// While loops (for and do-while loops omitted for simplicity)while(i<end){f(i);// +=, -=, *=, /=, %= operators supportedi+=step;}};map(function(x){console.log('fib',x,'=',fib(x));},0,10,1);/******** Operators ******************************/// Arithmetic1+2*3/4-(9%2);// Comparison: >, >=, <, <=, ==, !=// Note: uses '==' and '!=' instead of '===' and '!==' for simplicity// Assignment: =, +=, -=, *=, /=, %=// Unimplemented: postfix and prefix -- and ++, bitwise operators, typeof, **, instanceof, ternary, comma// Short-circuiting logical operatorsfalse&&console.log("this does not execute");true||console.log("this does not execute");/******** Exceptions ******************************/vare="hi";try{console.log(e);throw{error:":("};console.log("should not run");}catch(e){console.log("catch",e);}console.log(e);/******** Scope ******************************/// JavaScript's scope rules are weird and annoying. I ignored them and tried to// stick to conventional scope rules. The main differences are:// 1) Block scope// Example:(function(){vara=0;varb=0;console.log(a,b);// 0, 0if(true){varb=1;varc=1;console.log(a,b,c);// 0, 1, 1a=1;console.log(a,b,c);// 1, 1, 1}console.log(a,b);// 1, 0// JavaScript: c is availible in the whole function// My block scope implementation: c is only in scope inside the if block// c is undefined here})();// 2) No hoisting// In JavaScript variables can be defined before being declared// My implementation does not allow this/*// Example:(function() { x = "hi"; console.log(x); // hi var x;})();*/// 3) No global hoisting/*(function(){	// JavaScript: creates global variable	// My implementation: causes error	x = 1;})();*//******** Standard libraries ******************************/// Full math standard library (up to ES5, with parts of ES6)console.log(Math.asin(dy/dx)*180/Math.PI,'degrees');// Partial implementation of consoleconsole.log('hello');console.assert(true&&(1>2||2>1));

About

An interpreter for a (modified) subset of JavaScript written in Python

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages