wdibi / Pivot

A new spin on programming
https://wdibi.github.io/Pivot/
MIT License
3 stars 0 forks source link

Pivot

A new spin on programming

React Native is released under the MIT license. Test status. ESLint

This is Pivot, a scripting language designed to make programming a more enjoyable experience. Pivot is developed with the user in mind and has speech-like logic syntax structure. The core team aims to build a feature rich language that can serve both expert and novice programmers alike.

Inspired by programming languages like JavaScript, F#, and Python, Pivot features syntax and semantics designed with an effort to be simple while still preserving the complex, logical features that make these languages so great.

Pivot is created by @Will DiBiagio, @Jigar Swaminarayan, @Manny Barreto and @Nicolas Raymundo.

Contents

Types

Primitive Types

Data types

Operators

Binary Operators

Operation Type Compatibility
Add + Strings, Numbers, Lists
Subtract - Numbers
Multiply * Numbers
Power ** Numbers
Divide / Numbers
Modulus % Numbers
Strict Equality == Strings, Numbers, Lists
Less than < Strings, Numbers
Greater than > Strings, Numbers
Less than or equal <= Strings, Numbers
Greater than or equal >= Strings, Numbers
Logical AND and, && Booleans
Logical OR or, \|\| Booleans

Unary Operators

Operation Type Compatibility
Negative - Numbers
Negation !, not Booleans

Comments

Pivot Examples

Variable Declarations

str name <- "Jigar";
_ age <- 21;
bool below6ft <- true;
[str] animals <- ["dog", "cat", "pig"];
all num a, b, c <- 1, 2, 3;
{str:num} ages <- {"john" : 5, "tim" : 6};

Tasks

Task Statement

num pow4 -> num default ** 4;

Call Chain

Call chain with a built-in task or user defined task

print (-33) >> abs >> pow4;

Functions

Function Definition

add5(num x) -> num
    return x + 5;
end

Function Call

add5(10);

Control Flow

If Then

num x <- 3.1415;
if x > 3 then print "larger than 3"; end

If Then Else

num x <- 3.1415;
if x > 3 then 
  print "larger than 3";
else
  print "less than 3";
end

Short If

num x <- 32102123;
str msg <- "hello" when x % 3 == 2 otherwise "bye";

Iteration

For Loop

for num x <- 0; x <= 10; x <- x + 1 do
    print x;
end

While Loop

num x <- 25;
while x do
    print x;
    x <- x - 1;
end

Repeat Loop

num x <- 30;
repeat
    print x;
    x <- x - 5;
when x == -30 end

Dictionaries

Dictionary Declaration

{str:num} ages <- {"john" : 5, "tim" : 6};

Dictionary Accessing

ages:"john" // 5

Dictionary Built-ins

Yet to be implemented

ages::keys                // ["john", "tim"]
ages::values              // [5, 6]
ages::contains("michael") // false
ages::del("john")         // {"tim" : 6}

Lists

[str] friends <- [ "john", "tim", "steve" ];

List Indexing

friends:1
friends:1...3

List Built-ins

friends::head()    // "john"
friends::tail()    // "steve"
friends::len()     // 3
friends::find(tim) // 1

List Concatenation

[str] friends <- ["john", "tim"];
friends <- friends + ["alex", "sasha"];
print friends;                           // ["john", "tim", "alex", "sasha"]

JavaScript Comparison

Find Minimum Element

Pivot
findMin([num] arr, num low, num high) -> num
    if high < low then return arr:0; end

    if high == low then return arr:low; end

    num mid <- (low + high)/2;

    if mid < high and arr:mid+1 < arr:mid then
      return arr:mid+1;
    end

    if mid > low and arr:mid < arr:mid-1 then
      return arr:mid;
    end

    if arr:high > arr:mid then return findMin(arr, low, mid - 1); end

    return findMin(arr, mid + 1, high);
end
JavaScript
function findMin(arr, low, high) {
  if (high < low) {
    return arr[0] 
  }

  if (high == low) {
    return arr[low] 
  } 

  let mid = (low + high)/2

  if (mid < high && (arr[mid+1] < arr[mid])) {
    return arr[mid+1]
  } 

  if (mid > low && (arr[mid] < arr[mid - 1])) {
    return arr[mid]
  }

  if (arr[high] > arr[mid]) {
    return findMin(arr, low, mid-1) 
  }
  return findMin(arr, mid+1, high) 
}

Fibonacci

Pivot
fibonacci(num x) -> num
    all num a, b, temp <- 1, 0, 0;

    repeat
        temp <- a;
        a <- a + b;
        b <- temp;
        x <- x - 1;
    when num == 0 end
    return b;
end
JavaScript
function fibonacci(x) {
    let a = 1, b = 0, temp = 0;
    do {
        temp = a;
        a = a + b;
        b = temp;
        x--;
    }
    while (!(x === 0));
    return b;
};

Even or Odd

Pivot
evenOdd(num x) -> bool
    return x % 2 == 0;
end
JavaScript
function evenOdd(x) {
  return x % 2 == 0;
}

Greatest Common Divisor

Pivot
gcd(num a, num b) -> num
    return a when !b otherwise gcd(b, a % b);
end
JavaScript
function gcd(a, b) {
  return !b ? a : gcd(b, a % b);
}

First Factorial

Pivot
firstFactorial(num x) -> num
    if x == 0 or x == 1 then return 1; end
    return x * firstFactorial(x - 1);
end
JavaScript
function firstFactorial(x) {
  if (x == 0 || x == 1) {
    return 1;
  }
  return x * firstFactorial(x - 1);
}

Semantic Errors

Optimizations

Strength Reduction

IfShort

While

📄 License

Pivot is MIT licensed, as found in the LICENSE file.