SunSed Syntax

SunSed is a simple procedural programming language with a syntax that is easy to learn and understand. In this chapter, we'll provide an overview of the language's basic syntax and structure.

Comments

In SunSed, you can use the hash symbol (#) to add comments to your code. Comments are ignored by the interpreter and are only meant for humans to read.

# This is a comment /* This is a comment that spans multiple lines */

Also to comment out a single line of code you can use the // symbol. Here's an example:

// This is a comment print "This is not a comment"

As a best practice use // to comment out a single line of code and # to add explaination to your code.

Printing HTML Elements

SunSed has several shortcuts for printing common HTML elements:

# unordered list .ul Item 1 .ul Item 2 .ul Item 3 # ordered list .ol Item 1 .ol Item 2 .ol Item 3 # links .p This is a [link](http://example.com/) in a paragraph. # image .img http://example.com/image.jpg image description # image with a caption .img http://example.com/image.jpg image description .caption This is a caption for the image above # paragraphs .p This is a paragraph of text. # headings .h1 This is a heading 1 .h2 This is a heading 2 # bold, italic, and strikethrough .p This text is **bold**, *italic*, ==highlighted== and ~~strikethrough~~. # code blocks ...code print "This is a code block." ... (ends with ... on a newline with no characters after it)

Code Sections

You can use code blocks in SunSed to group related lines of code together. Code sections start with the section keyword, followed by a name for the block of code, and a pair of curly braces that enclose the block's contents. Here's an example:

section my example section { # Code here }

Functions

Functions are the building blocks of SunSed programs. You can define a function using the function keyword, followed by the name of the function and a pair of parentheses that enclose its arguments. Here's an example:

function my_function($arg1, $arg2) { # Code here }

To call a function, simply use its name, followed by a pair of parentheses that enclose any arguments you want to pass to it. Here's an example:

my_function("arg1", "arg2")

Variables

In SunSed, you can use variables to store values and reuse them later in your program. To create a variable, use the $ symbol, followed by a name for the variable. Here's an example:

$x = "Hello, world!"

To access the value of a variable, simply use its name. Here's an example:

print $x

Conditional Statements

You can use conditional statements in SunSed to execute different blocks of code depending on a certain condition. The if keyword is used to start a conditional statement. Here's an example:

if ($x == "Hello, world!") { # Code here }

Loops

Loops are used in SunSed to repeat a block of code a certain number of times, or until a certain condition is met. The for keyword is used to start a loop. Here's an example:

for ($i = 0; $i < 10; $i++) { # Code here }

Arrays

In SunSed, arrays are declared using square brackets ([]). You can create an array with or without keys, like this:

$my_array = [1, 2, 3, "key1" => "value1", "key2" => "value2"]; $my_other_array = ["value1", "value2", "value3"];

To add an item to the end of an array, you can simply use $my_array[] = "new item";. To add an item with a key, use $my_array["new_key"] = "new item";.

You can access an item in an array using its index or key, like this: $my_array[0] or $my_array["key1"]. You can also get the length of an array using the array.get_length() function, like this: array.get_length($my_array).

You can loop through an array using a foreach loop, like this:

foreach($my_array as $item) { # do something with $item }

Error Handling

You can use the error function in SunSed to handle errors in your program. The error function takes three arguments: a unique error ID, an error message, and an HTTP status code. Here's an example:

error("my_error", "An error occurred", 500)

Above is an example of a 500 Internal Server Error. You can find a list of all HTTP status codes on the Wikipedia page for HTTP status codes.

Function Definition and Documentation

SunSed functions can be documented using a special syntax. The documentation is written in a comment block that is placed directly above the function definition. Here's an example:

section function -> test.hello_world { /* purpose ~> Returns hello world tags ~> test access ~> private stable? ~> No cache ~> 0 schedule ~> examples ~> test.hello_world() #=> "Hello world" test.hello_world("my friend") #=> "Hello my friend" */ test.hello_world = function($name = "world"){ return "Hello " . $name; } }

The documentation block contains a number of sections, each of which is separated by a ~> symbol. The sections are as follows:

  • purpose - A short description of what the function does
  • tags - A comma-separated list of tags that describe the function
  • access - The access level of the function. Can be public, private, or any_custom_value for turning this function into an API point
  • stable? - Whether or not the function is stable. Can be Yes or No
  • cache - The number of seconds that the function's output should be cached for (0 means no caching)
  • schedule - A cron schedule that the function should be run on
  • examples - A list of examples that show how to use the function
  • Each of the above topics and more are covered in more detail in the following chapters.