examples - Raesangur/PascalScript_Reference GitHub Wiki
This page presents a few simple algorithms in PascalScript as a reference.
include std.io.println;
fn main():
{
println("Hello World");
}
include std.io;
include std.string;
fn main():
{
// Capture user input until a newline is entered
const string name = io.readln();
// Append the inputted string to "Hello " and prints the resulting string.
io.println("Hello " + name);
}
include std.io;
include std.string
fn main():
{
// Some basic file names
const string srcFilename = "source.txt";
const string destFilename = "destination.txt";
// Open the source file in read mode and create a destination file.
// If the destination file already exist, overwrite it.
io.file srcFile = io.file.open_read(srcFilename);
io.file destFile = io.file.open_write(destFilename);
// Read all content from the file and store it in a string
const string fileContent = srcFile.read_all();
// Write all content in the newly created file
destFile.write(fileContent);
// Both file close automatically in the destructor.
// They can alternatively be closed with the `io.file.close` method.
}
include std.io;
fn main():
{
// For the first 50 digits of FizzBuzz
loop(int i in [1 ... 50]):
{
// If the index is both a multiple of 3 and a multiple of 5
if(i % 3 == 0 and i % 5 == 0):
io.println("FizzBuzz");
// If the index is a multiple of 3
else if(i % 3 == 0):
io.println("Fizz");
// If the index is a multiple of 5
else if(i % 5 == 0):
io.println("Buzz");
// Otherwise simply print the current index
else:
io.println(i);
}
}
include std.io;
fn main():
{
const uint target = 25;
uint a = 1;
uint b = 1;
// Starts at 3 because both inputs are already initialized at 1
loop(uint n = 3; n <= target; n++):
{
// Calculate the sum of the two inputs and store it into a temporary variable
uint fib = a + b;
io.println("Fibonacci(" + n + ") = " + fib);
// Swap the inputs for the new outputs
a = b;
b = fib;
}
}
import std.math;
import std.algo;
fn main():
{
// Two same-length input arrays
const int[] array1 = [-5, 3, 1];
const int[] array2 = [-1, -2, -4];
const int stdDotProduct = dot_product_std(array1, array2);
const int iterDotProduct = dot_product_iterating_loop(array1, array2);
const int zipDotProduct = dot_product_zip(array1, array2);
}
fn dot_product_std(const int[]& array1, const int[]& array2) -> int:
{
return math.dot_product(array1, array2);
}
fn dot_product_iterating_loop(const int[]& array1, const int[]& array2) -> int:
{
// Check if the arrays are of the same length
// Overriding assert's exception handler
assert(array1.length() == array2.length()):
return 0;
int sum = 0;
// Multiply each of the corresponding elements together
loop(int i = 0; i < array1.length(); i++):
sum += array1[i] * array2[i];
return sum;
}
fn dot_product_zip(const int[]& array1, const int[]& array2) -> int:
{
// Check if the arrays are of the same length
// Overriding assert's exception handler
assert(array1.length() == array2.length()):
return 0;
loop(int a in array1, int b in array2):
sum += a * b;
return sum;
}
include std.except.InvalidInputException;
include std.string;
include std.vector;
class User:
{
#default
fn User(const string& username);
int id = 0;
string username:
{
fn set(const string& value) -> const string&:
{
if(value == ""):
throw InvalidInputException("Username should not be empty.");
else:
{
username = value;
return username;
}
}
}
}
#inherits User
class SuperUser:
{
#default
fn SuperUser(const string& username);
fn add_access(const string& newAccess):
{
access.append(newAccess);
}
fn check_access(const string& accessToCheck) -> bool:
{
return true if accessToCheck in access else false;
}
private vector<string> access;
}