Skip to content

PapajScript

Paul Lipkowski edited this page Dec 9, 2023 · 61 revisions

PapajScript (PS) is an interpreted script language. It has been developed by Paul Lipkowski while working on Papaj (previously named RPN Calculator), which became later a interpreter of PS. The PS's code aims to be compact and easily appendable.

History

The first overall piece of code, which was more than just a RPN expression, appeared on November 2017 – however, prior to September 3, 2018 this language had no name – on that day the name of PapajScript was given. The name change was made after claryfing the main look of the language. The version 0.4.1 (Zain) is the first release of RPN Calculator that uses the name of PapajScript as the name of the language it interpretes. 2 people gave an inspiration to name this language that way – Popeye the Sailor (pronounced like Papaj in Polish) and John Paul II (Papaj is a kind of Polish internet slang about particularly that Pope). The former person is characterized as a powerful man and the latter one made a huge powerful impact on politics of Poland and all Eastern Europe in 1980s.

drawing drawing

Structure

Its structure is based mostly on Reverse Polish Notation (with a handful of exceptions), which uses a stack of entities when computing values. Therefore all the operations are being done on the stack. The PS's semi-stack, which is a part of PapajScript Environment, is an extended version of a classic stack, as we can get an indirect access to the entities not being on the top of the semi-stack and we can programme it like this semi-stack can simulate the behavior of a queue. The entities are put on the stack and may be used from the semi-stack, however we can also store them in the named variables.
Everything comes around the semi-stack and the entities. The entities may be numbers, text strings, logical expressions, functions or exceptions.

PS's interpreter is RPN Calculator, a computational program written in FreePascal.

Packages of functions

Future

The future enhancements of the language include an introduction of objects, dataframes and better file management. The even further perspective would be a slight improvement of syntax, creation of a IDE and also an interpreter for different environments (e.g. JVM). The PapajStudio, i.e. IDE for PapajScript, is planned to be released as well.

Links

Examples of code of Gen3 standard (versions 0.5.2/Leviathan)

  1. Hello world
"Hello world!" println
  1. Factorial (different methods)
// using recursive function
function (n) {
    if ( n 0 = ) {
        1
    } else { 
        n n -- factorial * 
    }
} >factorial

scan toNumber factorial
// using stack-aggregating function "seq"
scan toNumber 1 1 seq
all product
// using Math package
@uses(Math)
scan toNumber factorial
  1. Fibonacci sequence (different methods)
// using non-linear recursion
function (n) {
    if ( n 0 = ) {
        0
    } elif ( n 1 = ) {
        1
    } else {
        n -- fib
        n -- -- fib
        +
    }
} -> fib

scan toNumber fib
// using times-loop
function (n) {
    if ( n 0 = ) { 
        0
    } elif ( n 1 = ) {
        1
    } else {
        // put 2 initial values we will add up
        0 1
        n -- times {
            // duplicate the values and create a sum
            2 copy +
            // remove the LHS value: reverse 3 elements, remove the last one and order them back
            3 rev rem swap
            // so now we have a RHS value and the added one as the new values to sum
            // and so on n-2 more times
        } 
        swap rem
    }
} >fib

scan toNumber fib
// using classical for-loop
function (n) {
    if ( n 0 = ) { 
        0
    } elif ( n 1 = ) {
        1
    } else {
        // put 2 initial values we will add up
        0 1
        for ( 2 -> i ; i n <= ; i ++ -> i ) {
            // duplicate the values and create a sum
            2 copy +
            // remove the LHS value: reverse 3 elements, remove the last one and order them back
            3 rev rem swap
            // so now we have a RHS value and the added one as the new values to sum
            // and so on up to i <= n
        } 
        swap rem
    }
} -> fib

scan toNumber fib
// using Math package
@use(Math)
scan toNumber fibonacci
  1. Checking if there is a person more stupid than Forrest Gump in a group of N people using normal distribution
@silent
@use(Math)
@use(String)
@use(Array)

"Enter the population size: " print
scan toNumber -> n

[ $n 100 15 genNorm ] reduceMin -> m1


"Sample's Lowest IQ: " $m1 toString concat println
"Forrest Gump's IQ:  70" println
  1. Sorting an array
// sorting an array with 10 random integers from a range [0..99]
@use(Array)
[ 10 times { 100 random } ] sortNumbers println
// sorting a user-defined array
// without double exclamation at push, making it return the new array that is later assigned to a variable
@use(Array)
@use(String)
"Set array length: " print
scan toNumber -> n
[] -> T
1 -> i
n times {
	"Push the " i toString ". element: " 2 times concat print
	T scan toNumber push -> T
	i ++ -> i
}
T sortNumbers println
// sorting a user-defined array
// with double exclamation at push, making it act like a procedure
@use(Array)
@use(String)
"Set array length: " print
scan toNumber -> n
[] -> T
1 -> i
n times {
	"Push the " i toString ". element: " 2 times concat print
	T scan toNumber push!!
	i ++ -> i
}
T sortNumbers println
  1. Dirichlet Convolution
// computing Dirichlet's convolution of 1/n and sin(n) in a point defined by user
// using basic sum through array elements
@use(Math)

function { -1 ^ } -> f
function { sin } -> g

function (f g k) {
	0 -> sum
	for ( $i : [ $k genNaturalDivisors ] ) {
		$i f $k $i div g * $sum + -> $sum
	}
	$sum
} -> DirichletConvolve

scan toNumber -> $n
$f $g $n DirichletConvolve
// computing Dirichlet's convolution of 1/n and sin(n) in a point defined by user
@use(Math)
@use(Array)

function { -1 ^ } -> f
function { sin } -> g

function (f g k) {
	[$k genNaturalDivisors] 
		fun (i) {
                      $i f $k $i div g * 
		} map
		reduceSum
} -> DirichletConvolve

scan toNumber -> $n
$f $g $n DirichletConvolve
Clone this wiki locally