-
Notifications
You must be signed in to change notification settings - Fork 0
GigaScript Documentation
File extension: .g
GigaScript has very simple syntax, similar to those of other popular languages.
A variable can hold any value supported by GigaScript.
let varName = value;
varName can be any name you want, but must follow the following requirements:
- Cannot start with a number
- Must start a letter or an underscore "_".
valuecan be any value you want that is supported by GigaScript.
let anotherVar;
Mutable variables do not need to have a variable assigned when they are declared.
Important
All variable declarations must end with a semicolon ";".
const varName = value;
varName can be any name you want, but must follow the following requirements:
- Cannot start with a number
- Must start a letter or an underscore "_".
valuecan be any value you want that is supported by GigaScript.
Important
Constant variables must have a value when declared.
Caution
Constant variables cannot be reassigned!
varName = newValue
Note
Reassignments shouldn't end with a semicolon ";".
To access a variable, simply reference it using the variable name.
varName
varName should be the name of your variable. You can use varName anywhere in your code. For examples, see examples.
Functions are very useful when you want to repeat the same task multiple times in different parts of your code.
func funcName(params) {
*code*
}
funcName could be any name following these naming rules:
- Cannot start with a number
- Must start a letter or an underscore "_".
paramscan be any length of parameters you need passed into your function, or no parameters at all, following the same naming rules as above.
To return a value in a function, simply reference the value on the last line of the function.
funcName(params)
funcName could be any name following these naming rules:
- Cannot start with a number
- Must start a letter or an underscore "_".
paramscan be any length of parameters you need passed into your function, or no parameters at all, following the same naming rules as above.
return value;
value is the value you want to return;
Caution
Make sure to include a semicolon at the end of the return statement! Otherwise it will throw an error!
Warning
Don't add code after a return statement. When the code execution reaches a return statement, it will stop execution of the function and return the value.
class ClassName(){
public publicProp = "this can be any value";
private privateProp = "this can be any value but can't be accessed outside of the class";
public publicMethod(params){
code (See [Functions](https://github.com/aName2050/GigaScript/wiki/GigaScript-Documentation#Functions) for more info on functions.)
}
private privateMethod(params){
code (See [Functions](https://github.com/aName2050/GigaScript/wiki/GigaScript-Documentation#Functions) for more info on functions.)
}
}
ClassName can be any name, while still following these naming rules:
- Cannot start with a number
- Must start a letter or an underscore "_".
publicproperties and methods can be accessed when a new class instance is created.privateproperties and methods cannot be accessed when a new class instance is created.
let className = new ClassName();
ClassName is the name of your class.
new ClassName() will create a new object containing all the class's public properties and methods.
- Create a new instance of the class and set it as the value of any variable.
let aCoolClass = new ClassName();
- Access the public properties and methods of the class as if you were accessing the properties of an object.
aCoolClass.publicProp
aCoolClass.publicMethod(params)
condition is where you would check if a condition is true. See examples.
if(condition) {
code
}
if(condition) {
code
} else {
code
}
if(condition) {
code
} else if (other_condition) {
code
} else {
code
}
Tip
You don't need to include an else statement in every conditional statement.
while loops are useful when you need to iterate over something a certain amount of times, or while a condition is true.
while(condition){
code
}
condition is any conditional statement that would return either true or false.
Caution
It is not recommended to set condition to always be true, condition should eventually become false to prevent your program from becoming unstable and crashing.
for loops are useful when you need to iterate over something a variable amount of times.
for(initializer; condition; modifier) {
code
}
initializer sets a variable.
condition compares the initializer to a condition and continues to code if it returns true.
modifier modifies the initializer after the for loop finishes an iteration.
Importing code from other GigaScript files is useful when creating large projects.
import aVariable from "relative/path/to/file.g"
import aFunction from "relative/path/to/file.g"
aVariable and aFunction is the variable or function you want to import. "relative/path/to/file.g" is the path to the file, relative to the parent directory.
Caution
When writing out the file path to the file, only relative paths are supported. Do not include a "./" at the beginning as that will result in an error.
Note
You can only import one variable/function from each file at a time, to import multiple variables/functions, use a separate import statement.
Exporting variables and functions is needed to use the import statement.
let aVariable = "a value";
func aFunction(params){
code
}
export aVariable;
export aFunction;
aVariable and aFunction is the variable or function you want to export. An export statement can only export one value at a time.
Important
You cannot export a value that is not a variable or function.
These are tokens reserved for GigaScript that can't be used as the name of classes, functions, or variables.
Note
You can use any of the reserved tokens when used inside of a string.
let
const
func
class
true
false
undefined
new
null
private
public
if
else
while
for
continue
break
import
export
from
try
catch
print
math
timestamp
format
Caution
Using any of these keywords as a class, function, or variable identifier will result in an error!
>
<
==
!=
!
&&
||
"
+
-
*
/
%
=
You can see examples for GigaScript in this section. You can run these examples by copying the code in a .g file and then running the file
let x = 32;
const aCoolObject = { x, z: 64, complex: { foo: "bar" } };
x = x * 2
print(x)
print(aCoolObject.complex)
print(x) should output 64.
print(aCoolObject.complex) should output { foo: "bar" }.
-
Declare a mutable variable called
xwith the value of32. -
Declare a constant variable called
aCoolObjectwith the value of { x, z: 64, complex: { foo: "bar" } }`. -
Reassign the variable
xto the result ofxmultiplied by 2, which should result in64. -
Print the value of
xto the console. Prints64. -
Print the value of
aCoolObject.complexto the console. Prints{ foo: "bar" }.- The
complexproperty ofaCoolObjectis accessed and passed into theprintfunction.
- The
func add(x, y){
const result = x + y;
result
}
print(add(1, 2))
print(add(1, 2)) should output 3.
-
Declare a function called
addwith parametersxandy. - Inside the function...
-
Declare a constant variable called
resultwith the value ofxandyadded together. -
Return the value of
result.
-
Declare a constant variable called
-
Print the result of adding
1and2to the console. Prints3.
class Person {
public fname = "John";
public lname = "Doe";
public getName() {
const fullName = fname + " " + lname;
fullName
}
}
const human = new Person();
print(human.getName())
print(human.getName()) should output "John Doe".
-
Declare a class called
Person. - Inside the class...
-
Declare a public property called
fnamewith the value of"John". -
Declare a public property called
lnamewith the value of"Doe". -
Declare a public method called
getNamewith no parameters. - Inside the method...
-
Declare a constant variable with the name of
fullNamewith the result of combining the strings"John"," ", and"Doe". -
Return the value of
fullName.
-
Declare a constant variable with the name of
-
Declare a public property called
-
Declare a constant variable called
human.-
Create a new instance of the class
Person. -
Assign the returned object value to the variable
human.
-
Create a new instance of the class
-
Print the result of calling
human.getName(). Prints"John Doe".- The
getNamemethod in the variablehumanis called. - The method returns the full name by combining the first name (
fname) and the last name (lname).
- The
let passed = true;
let isTest = false;
let class = "computer science";
if(class == "computer science) {
print("in computer science!")
if(passed && !isTest) {
print("passed the homework!")
} else if (passed && isTest) {
print("passed the test!")
} else {
print("failed")
}
} else {
print("This class isn't graded yet.")
}
-
Declare a mutable variable called
passedwith the value oftrue. -
Declare a mutable variable called
isTestwith the value offalse. -
Declare a mutable variable called
classwith the value of"computer science". -
Check if
classis equal to"computer science".- If true...
-
Print
"in computer science!"to the console. -
Check if
passedistrueand the opposite ofisTestistrue- If true...
-
Print
"passed the homework!"to the console.
-
Print
- If not true
-
Check if both
passedandisTestistrueinstead.- If true
-
Print
"passed the test!"to the console.
-
Print
- If true
-
Check if both
- If all other conditions return
false...-
Print
"failed"to the console.
-
Print
- If true...
-
Print
- If false...
-
Print
"This class isn't graded yet."to the console.
-
Print
- If true...
const max = 10;
while(max > 0) {
print(max)
max = max - 1
}
This should output:
10
9
8
7
6
5
4
3
2
1
to the console.
-
Declare a constant variable called
maxwith the value of10. -
Create a
whileloop with the conditionmax > 0. You would read this as "whilemaxis greater than0, do..." - Inside the
whileloop...-
Print the value of
maxto the console. -
Reassign the value of
maxto the result of the value ofmaxminus1.
-
Print the value of
for(let i = 0; i < 5; i = i + 1) {
print(i)
}
This should output:
1
2
3
4
5
to the console.
-
Create a
forloop with the initializerlet i = 0, the conditioni < 5, and the modifier ofi = i + 1.- The initializer is usually a mutable variable set to a specific value.
-
Declare a mutable variable called
iwith the value of0.
-
Declare a mutable variable called
- The condition usually checks if the initializer meets a certain condition.
-
Check if
iis less than5- If true...
- Continue running the loop
- If false...
- End the loop
- If true...
-
Check if
- The modifier usually modifies the value of the initializer.
- After the loop finishes an iteration...
-
Reassign the value of
ito the result of adding1to the value ofi.
-
Reassign the value of
- After the loop finishes an iteration...
- The initializer is usually a mutable variable set to a specific value.
- Inside the
forloop-
Print the value of
ito the console.
-
Print the value of
main.g
import x from "anotherFile.g"
print(x)
anotherFile.g
const x = 10;
export x;
print(x) should output 10.
main.g
-
Import the variable
xfromanotherFile.g. -
Print the value of
xto the console. Prints10.
anotherFile.g
-
Declare a constant variable called
xwith the value10. -
Export the variable
x.
© Bardia Shafaee (aName2050) 2024
See our license for more details.