wdibi / Pivot

A new spin on programming

Home Page:https://wdibi.github.io/Pivot/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

  • Statically typed with some auto type inference
  • Strongly typed
  • Scripting
  • Impure functional language

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

Contents

Types

Primitive Types

  • Strings: str
  • Characters: char
  • Booleans: bool
  • Numbers: num
  • Auto: _

Data types

  • Lists: [type]
  • Dictionaries: {type:type}

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

  • Single line: //
  • Multi-line: /* */

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

  • contains(keyId) contains key
  • del(keyId) delete pair by key
  • keys list of keys
  • values list of values
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

  • head first element
  • tail last element
  • len number of elements
  • find(elemId) index of element
  • push(elem) add to the end of a list
  • pop() remove from the end of a list
  • unshift(elem) add to the beginning of a list
  • shift() remove from the beginning of a list
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

  • Type mismatch in declaration
  • Variable already declared
  • Variable assignment type mismatch
  • Variable not yet declared
  • Non-existing function call
  • Incorrect number of function parameters
  • Mismatched function return type
  • Types are not compatible
  • Function missing return statement
  • Arithmetic with undefined variable
  • Invalid types used with addition
  • Invalid types used with multiplication
  • Invalid types used with subtraction
  • Invalid types used with division
  • Incorrect use of unary operator
  • Inconsistent list types
  • Invalid variable type
  • Break outside of loops or task
  • Deterministic condition
  • Invalid dict types
  • Unreachable statement
  • Inconsistent dict expression types

Optimizations

Strength Reduction

  • Negation of BoolLiteral and NumericLiteral
  • Arithmetic expressions
  • Relational expressions
  • OR operations
  • AND operations

IfShort

  • Returning either the expression or alternate if the condition can be evaluated

While

  • No op optimization

📄 License

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

About

A new spin on programming

https://wdibi.github.io/Pivot/

License:MIT License


Languages

Language:JavaScript 90.3%Language:HTML 5.0%Language:CSS 4.7%