diff --git a/docs/highlight.js b/docs/highlight.js new file mode 100644 index 0000000..b9f9616 --- /dev/null +++ b/docs/highlight.js @@ -0,0 +1,118 @@ +// Solstice Syntax Highlighter +// Add this script to your HTML file before the closing tag + +document.addEventListener('DOMContentLoaded', function() { + highlightAllCode(); +}); + +function highlightAllCode() { + const codeElements = document.querySelectorAll('pre.code code'); + codeElements.forEach(element => { + // Get the text content (this automatically decodes HTML entities) + const code = element.textContent; + element.innerHTML = highlightSolstice(code); + }); +} + +function highlightSolstice(code) { + let result = ''; + let i = 0; + + // Tokenize the code + while (i < code.length) { + let matched = false; + + // Check for comments + if (code.substr(i, 2) === '//') { + let end = code.indexOf('\n', i); + if (end === -1) end = code.length; + result += `${escapeHtml(code.substring(i, end))}`; + i = end; + matched = true; + } + + // Check for strings + else if (code[i] === '"') { + let end = i + 1; + while (end < code.length && code[end] !== '"') { + if (code[end] === '\\') end++; // Skip escaped characters + end++; + } + end++; // Include closing quote + result += `${escapeHtml(code.substring(i, end))}`; + i = end; + matched = true; + } + + // Check for numbers + else if (/\d/.test(code[i])) { + let end = i; + while (end < code.length && /[\d.]/.test(code[end])) { + end++; + } + result += `${code.substring(i, end)}`; + i = end; + matched = true; + } + + // Check for keywords, types, built-ins, and identifiers + else if (/[a-zA-Z_]/.test(code[i])) { + let end = i; + while (end < code.length && /[a-zA-Z0-9_]/.test(code[end])) { + end++; + } + const word = code.substring(i, end); + + // Check what type of word it is + if (['def', 'if', 'while', 'return', 'ground', 'puts'].includes(word)) { + result += `${word}`; + } else if (['input', 'print', 'println'].includes(word)) { + result += `${word}`; + } else if (['int', 'double', 'string', 'char', 'bool'].includes(word)) { + result += `${word}`; + } else if (['true', 'false'].includes(word)) { + result += `${word}`; + } else { + // Check if it's a function call (followed by '(') + let j = end; + while (j < code.length && /\s/.test(code[j])) j++; + if (code[j] === '(') { + result += `${word}`; + } else { + result += word; + } + } + i = end; + matched = true; + } + + // Check for operators + else if ('+-*/=!<>'.includes(code[i])) { + let op = code[i]; + if (i + 1 < code.length && '='.includes(code[i + 1])) { + op += code[i + 1]; + i++; + } + result += `${escapeHtml(op)}`; + i++; + matched = true; + } + + // Everything else (whitespace, braces, etc.) + if (!matched) { + result += escapeHtml(code[i]); + i++; + } + } + + return result; +} + +function escapeHtml(text) { + return text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} diff --git a/docs/index.css b/docs/index.css index bfcb1f4..04376c7 100644 --- a/docs/index.css +++ b/docs/index.css @@ -78,3 +78,42 @@ h2 { h3 { font-size: 25px; } + +/* Syntax highlighting styles for Solstice */ +.comment { + color: #6c757d; + font-style: italic; +} + +.string { + color: #98c379; +} + +.number { + color: #d19a66; +} + +.keyword { + color: #c678dd; + font-weight: bold; +} + +.builtin { + color: #61afef; +} + +.type { + color: #e5c07b; +} + +.boolean { + color: #d19a66; +} + +.operator { + color: #56b6c2; +} + +.function { + color: #61afef; +} diff --git a/docs/index.html b/docs/index.html index 0028525..639c419 100644 --- a/docs/index.html +++ b/docs/index.html @@ -6,6 +6,7 @@ Solstice Docs +
@@ -36,7 +37,8 @@

Solstice's compiler is invoked via the solstice command. It provides some options:

Example usage:

solstice fib.sols -o fib.grnd
@@ -60,16 +62,20 @@ puts true puts "You can print out anything with puts!"

Variables

Solstice variables are quite simple. Assign values with =, and read them with the variable name.

-
x = 5
puts x
+
x = 5
+puts x

Types are automatically inferred by Solstice.

Maths

You can use + (add), - (subtract), * (multiply), and / (divide) to do math.

Math operations take two values on either side, and perform the operation. Order of operations is supported.

-
x = 5 + 3
y = 10
puts x + y
+
x = 5 + 3
+y = 10
+puts x + y

Control Flow and Equalities

Solstice supports if and while statements, as well as the ==, !=, >, >=, <, and <= operations.

Conditionals work just like maths: two values on either side, check whether the condition is correct or not based on those values, and output a boolean.

-
puts 5 == 5
puts 5 != 5
+
puts 5 == 5
+puts 5 != 5

if and while statements take a conditional, and then a code block afterwards. See these examples:

x = 5
 if x > 10 {
@@ -91,7 +97,9 @@ while number < 10 {
                         
                         

Functions

Note: Functions in Solstice are currently in beta. Type checking for functions is currently experimental, and arguments may not work as intended. Be warned!

-

In Solstice, function definitions have a syntax like this:

def functionName(type arg1, type arg2, type arg3) returnType {
// code goes here
}

+

In Solstice, function definitions have a syntax like this:

def functionName(type arg1, type arg2, type arg3) returnType {
+    // code goes here
+}

Your types can be int, double, string, bool, or char (see the "Type System" section for more details)

Return a value (which must be the same type as your returnType) with return value.

Here's an example function:

@@ -134,14 +142,20 @@ while number < 10 {

Inline Ground is not vetted by the type checker. Be careful when modifying existing variables with inline ground! The type checker is not aware of any values created inside inline Ground.

If you would like the Solstice type checker to be aware of values created by Ground, initialise a variable with a blank of whatever type is made by Ground.

Variable names are the same inside inline Ground as they are in Solstice. Read Ground's syntax guide for an understanding on how it should work here.

-
ground {
set &x 5
println $x
}
+
ground {
+    set &x 5
+    println $x
+}

Built In Functions

input(string msg) string

Gets user input from the console until the next line. The msg is used as a prompt for the user. Returns inputted characters from the console.

-
guess = input("What is the password? ")
if guess == "password123" {
puts "Good job!"
}
+
guess = input("What is the password? ")
+if guess == "password123" {
+    puts "Good job!"
+}