GithubHelp home page GithubHelp logo

privateger / greenlight Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 8.0 107 KB

A small scripting language combining syntax of multiple languages I like with a decent standard API. Under active development.

License: GNU General Public License v3.0

JavaScript 100.00%
hacktoberfest

greenlight's Introduction

GreenLight

A small scripting language combining syntax of multiple languages I like with a decent standard API. Under active development. This language compiles back into Node.js code.

Contributing

Want to help? Thanks! There are multiple areas you can help with.

Standard Library

This is currently needed the most. Open lib/stdlib.js and create the function that you think should be part of greenlight. Make sure it is prefixed with greenlight_internal_ in order to not interfere with user-created functions.

Then simply enter lib/implementations/inbuilt_function.js and add your function to functionMap by creating a new key with the name the function should have with the value of the internal name.

Finally, test if your function works, then submit your PR. Thanks! :)

Transpiler

Take a look at lib/lexer.js and lib/transpiler.js. The current transpilation solution is slow and not suitable for the long run. Maybe you can help with it!

Documentation

Compile & run

To run a program:

node greenlight.js e <filename>

To compile a program:

node greenlight.js c <filename> > compiled.js

Input/Output

print(""); // Print a passed string to STDOUT
logError(""); // Prints the passed string as red text
input("Name? "); // Print the passed string and read user input until a newline is encountered, the input is returned

Function declarations & calls

fun say_hello(name) {
  print("Hello, " + name);
}

call say_hello("Reader");

The function keyword in this language is "fun". Function calls in Greenlight need to be prefixed with "call", otherwise undefined behavior is likely to occur.

Variables

var hello = "world"; // Mutable variable
const constant = "light"; // Constant variable

Objects

var object = {
  "key": "hello",
  "array": [
    "1",
    "2"
  ],
  "nestedObject": {
    "nested": "hi!"
  }
};

Objects in Greenlight work the same as they do in JavaScript. An object is a keyed map which can contain all data types, including other objects or arrays.

Object & Array access

let array = [498, 49, 299, 18]; // Declare a new array
array[1] = 133; // Modify the second element of the array
print(array[1]); // Print the second element of the array

let object = { // Declare a new object...
  "key": "hello world" // with the value "hello world" at key "key"
};
print(object>>key); // Print the value located at the key "key"

Standard library

sqrt(16); // => 4 Get the square root of a number
log(1); // => 0 Get the natural logarithm of a number
pow(2, 10); // => 1024 Raise a number to an arbitrary power
exp(1); // => 2.718281828459045 Get the exponential of a number
max(2, 4, 1, 3); // => 4 Get the maximum element of an array
min(2, 4, 1, 3); // => 1 Get the minimum element of an array
random(); // Generates a random number between 0 and 1
sin(0); // => 0 Gives the sine of number
cos(0); // => 1 Gives the cosine of number
tan(0); // => 0 Gives the tangent of number
abs(-16); // => 16 Get the absolute value of a number
isNumber(78); // => true Get a boolean value depending if passed value is a number
randomNumberBetween(min, max); // Get a random integer between two numbers (inclusive)
exit(0); // Terminate the program with the passed exit code. 0 = normal, anything else = error
httpGet("https://example.com/"); // Send a HTTP GET request to the provided URI, returns string with response text
run("ls"); // Run a system command and receive output after the command has exited. Returns STDERR if an error occurs.
sha256("data"); // Hash the provided data with SHA256 and return a hex string
sha3("data"); // Hash the provided data with SHA3-256 and return a hex string
md5("data"); // Hash the provided data with MD5 and return a hex string. Warning: MD5 is outdated and insecure.
getObjectKeys(object); // Returns an array of the keys of a js object
getObjectValues(object); // Returns an array of the values of a js object
objToArray(object); // Returns an array of an object's key:value pairs
objToJsonString(object); // Converts an object to a JSON string
arrMin(array); // Returns the min value in an array
arrMax(array); // Returns the max value in an array
arrAvg(array); // Returns the average of all values in an array
arrSum(array); // Returns the sum of all values in an array
arrDiff(array1, array2); // Return the elements from Array 1 that are not in the Array 2
arrDiffSymmetric(array1, array2); // Return the elements from Array 1 that are not in the Array 2 and vice-versa
arrIntersect(array1, array2); // Return the elements that both arrays share in common
trueTypeOf(input); // More accurately check the type of an object
bin(input); // Convert an integer to binary. Negative integers are funky. Explanation here: https://stackoverflow.com/questions/16155592/negative-numbers-to-binary-string-in-javascript

greenlight's People

Contributors

davilhlapak avatar hiperbolt avatar klee214 avatar privateger avatar spicyyboi avatar tangilj avatar thegeekylad avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

greenlight's Issues

Enhance documentation with code examples

Enhance the current documentation in the README.md with some short code examples to explain the functions available. The current short comments are insufficient.

Add syntax checker

Implement a syntax checker that works. The current one is very situational and barely does anything at all.

Take a look at lib/syntax_checker.js for an idea of how to implement syntax checking functions.
You may introduce breaking changes into the project, but I would prefer it if you could keep them as smooth as possible.

This issue will likely require modification of the transpiler. It is located in lib/transpiler.js.

New lexer engine

The current lexer is bad. Very bad.
Implement a better system to accurately parse GreenLight code and create syntax objects for the transpiler.

Breaking changes are allowed, but discouraged. This will likely require rewriting of a large amount of code.

Re-order the token file

The current token file is messy, to say the last. It needs to be properly ordered and cleaned up.

Take a look at lib/tokens.js and re-order the token object.

Order the entries according to the priority they should have. For example, in-built functions should always have priority over user-defined functions, so their token definition should be placed above the definition of user functions.

Add documentation for new functions

Add documentation of the new standard library functions introduced in #6 into the README.
If you would like to take this issue, simply comment and I will assign it to you.

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.