JS like simple language LEXER, PARSER and INTERPRETER
Project description
A single file, dependency free language written in python. The goal of this project is to show that programming languages are not a black box and that you could learn the basic concpets required to implement your own language. While this language is not a real programming language however a lot of the internal concepts displayed in this language is taken from other real programming languages.
how to Run
python -m pjscript <file.js>
#or you could use the lexer, parser and interpreter in your python file
from pjcript import *
lexer = Lexer("var x = 1;")
tree = Parser(lexer)
interpreter = Interpreter(tree)
interpreter.interpret()
Data types:
- Int (positive & negitive)
- String
- Array
- Object
- Null
Basic statemnets
/* this is a comment */
var age = 2;
var name = "jack";
var newage = age;
name = "jack and me";
log(1, "it's working fuck yea", name);
var x = add(age, add(2,3));
log(x);
Functions and Scope
var x = "hello world";
function printme(st1, st2, st3){
log(st1);
log(st2);
log(st3);
};
function testfunc(){
var x = "new thing";
printme(x, 1, 3);
};
testfunc();
log(x);
Expressions
var x = 0 + add(2, 3);
var x = x + add(2, 3);
var x = (x * 2) / 2;
log(x); /* should log 10*/
function Mul(){
return add(2, add(2 * 3, 2));
};
var result = Mul();
log(result);
/* post-increment and decrement */
var x = 2;
log("x: ", x);
var y = x++ * 2;
log("y: ", y); /* output y: 4 */
log("x: ", x); /* output y: 3 */
/* pre-increment and decrement */
var x = 2;
log("x: ", x);
var y = ++x * 2;
log("y: ", y);/* output y: 6 */
log("x: ", x);/* output y: 3 */
/* Comparison Operators */
var x = 2;
var y = x >= 2;
log("y: ", y); /* True */
log("x: ", x); /* 2 */
For Loops
for(var i = 0; i < 5; i++){
log(i);
};
/* or */
var i = 0;
for(i = 2; i < 5; i++){
log(i);
};
/* infinite loop */
var i = 0;
for(; ; i++){
log(i);
};
IF ELSE
var x = 0;
if(x == 3){
log("if");
} else if(x == 2){
log("else if");
} else if (x == 1){
log("else if second");
}else{
log("else");
};
var y = 2;
if (x == 2){
log("if only");
};
for(var i = 0; i < 5; i++){
if(i == 3){
log(i);
};
};
Array
// numbers
var numbers = [1, 2, 3];
log(numbers);
log(numbers[2]);
// Strings
var person = ["jack", "jay", "nick"];
for(var i = 0; i < 3; i++){
log(person[i]);
};
Object
// Object
var person = {
name: "jack",
age: 23
};
log(person.name);
log(person.age);
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pjscript-0.0.4.tar.gz
(13.1 kB
view details)
Built Distribution
pjscript-0.0.4-py3-none-any.whl
(13.1 kB
view details)
File details
Details for the file pjscript-0.0.4.tar.gz
.
File metadata
- Download URL: pjscript-0.0.4.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ee381cf1311eb6ee1cd98dc8ca596352904306a1ee92a8e9eacc89c6c4349bf |
|
MD5 | 32e1b6f8232c2b6e202593427195be78 |
|
BLAKE2b-256 | 39c64562ed1459f6c4c8c891ee9cc6ec3abad7ccf7a55fdeb05f07e270a37370 |
File details
Details for the file pjscript-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: pjscript-0.0.4-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d830053f58ee41201ee17a67a3cda787c62b4e4ae8f6d0ba7b7862b6795e28b |
|
MD5 | 801f47fcfd44f8f0478e6e60b701b841 |
|
BLAKE2b-256 | a55c422d7a54e8885da474614e6b198a8c2f4804ac4e18e536790fabb0758103 |