Contents

pedroesli/plato

Plato is an interpreter written in Swift and inspired by R. It is a high-level, [imperative](https://en.wikipedia.org/wiki/Imperative_programming), [structured programming](https://en.wikipedia.org/wiki/Structured_programming) and [gradually typed](https://en.wikipedia.org/wiki/G

This is

A multiline comment ## print(61 < 3)


# Data Types

- boolean
- int
- float
- double
- string

boolean

true false

int

42 1_000_00 # is equivalent to 10000

float

15.123456 1_00.00_2 # is equivalent to 100.002

double

0.236819230486571

string

"Hello, World!" "Plato" + " is" + " awesome!"


# Collection Types

- Array

Array

x = [1, 2, 3, 4] x[0] = 5

Append another array

x = x + [5, 6]

Arrays can hold any type

y = [1, 1.5, true, "hello"]


# Variables

Variables can be declared by using the `=` sign and are implicit of `any` type

a = 20 b = 25.5 c = a + b


Variables can be assigned a specific type. Once assigned, the variable must always hold that type, and assigning an incompatible value will cause an error.

Types are specified using the `:` symbol.

a: any = 25 b: bool = true c: int = 1 d: float = 1.5 e: double = 1.1235632 f: number = 25 # accepts float, int and bool g: string = "This is a string" h: array = [1, 2, 3]


# Operators

**Arithmetic Operators**

| Operator | Name | | --- | --- | | + | Addition | | - | Subtraction | | | Multiplication | | / | Division | | * | Exponent | | % | Modulus |

**Comparison Operators**

| Operator | Name | | --- | --- | | == | Equal | | != | Not equal | | > | Greater than | | < | Less than | | >= | Greater than or equal to | | <= | Less than or equal to |

**Logical Operators**

| Operator | Description | | --- | --- | | and | Logical AND operator | | or | Logical OR operator | | not | Logical NOT |

Assignment Operators

| Operator | Description | | --- | --- | | = | Assign a value to a variable | | *= | Multiplication assignment | | /= | Division assignment | | %= | Modulus assignment | | += | Addition assignment | | -= | Subtraction assignment |

Control Flow

If

The if statement executes a set of statements if the condition is TRUE. Otherwise, the else statement is executed.

if 2==3 { 
	print("2==3 is true")
} else if 2<3 { 
	print("2<3 is true")
} else {
	print("no condition is true")
}

While

i = 0
while i < 10 { 
	print(i)
	i += 1
}

For

numbers = [1, 2, 3, 4, 5, 6, 7]

for number in numbers { 
	print(number)
}

for index from 0 to 10 by 1 { 
	print(index)
}

Type Functions

Type functions are special methods that facilitates the type casting of values or do some especial operations like for array.

bool("true")                     # returns true
int("hello")                     # returns 0
float(true)                      # returns 1.0
string(42)                       # returns "42"
array(1, 2, 3, 4)                # returns [1, 2, 3, 4]
array(repeating: 2.5, count: 4)  # returns [2.5, 2.5, 2.5, 2.5]

Functions

func helloWorld() {
    "Hello, World!"
}

func sum(a, b) {
    return a + b
}

Functions in Plato support polymorphism where the type of the parameter makes the identity of each function.

func mul(a: int, b: int) {
    return a * b
}
  
func mul(a: float, b: float) {
    return a * b
}

The order in which you declared the function does matter in Plato.

func sum(a, b) {
    return a + b
}

func sum(a: int, b: int) {
    return a + b
}

# The first function will always be called because the parameters are 
# inplicitly of any type 

The correct approach will require the reorder of the functions, like the following:

func sum(a: int, b: int) {
    return a + b
}

func sum(a, b) {
    return a + b
}

You can also use the at keyword before a parameter identifier to enforce its use when calling the function.

func sum(at a, at b) {
    return a + b
}

result = sum(a: 10, b: 10)

Native Functions

Here are all of the built in functions.

Foundation

  • print
print("Hello, World!", 1, 2, 3)
print(1, 2, 3, 4, separator: " ... ", terminator: "\n\n")
  • random

Returns a random number within the specified range.

random(1, 10)
random(0.1, 0.9)

Math

  • Exponential function: exp (base-e exponential of x)
  • Logarithmic functions: log , log2, log10
  • Trigonometric functions: cossintan
  • Inverse trigonometric functions: acosasinatan
  • Hyperbolic functions: coshsinhtanh
  • Inverse hyperbolic functions: acoshasinhatanh
  • Power and root functions: powsqrt
  • Gamma functions: gammalogGamma

Example:

pow(2, 4) # 16
sqrt(4)   # 2
cos(90)   # -0.44807363

Package Metadata

Repository: pedroesli/plato

Default branch: main

README: README.md