Guide to NTSL2++: различия между версиями
>Karolis2011 (Sintax guide) |
>Sneakyranger м (adds info) |
||
(не показаны 4 промежуточные версии 3 участников) | |||
Строка 1: | Строка 1: | ||
== Basic | {{Outdated | ||
First, let's acknowledge how to comment our code. You can comment out line or | |info = NTSL2++ is currently nonfunctional.}} | ||
== Basic Syntax == | |||
=== Comments === | |||
First, let's acknowledge how to comment our code. Comments are lines of text that are for humans to read and aren't executed. You can comment out a line or a block of lines. | |||
<source lang=javascript> | <source lang=javascript> | ||
// This is single comment line. | // This is a single comment line. | ||
/* While this is start of comment block | /* While this is start of a comment block | ||
Comment block content | Comment block content | ||
And end of comment block | And end of comment block | ||
*/ | */ | ||
</source> | </source> | ||
=== Variable | === Variable Types === | ||
NTSL2++ is loosely typed language. This means you don't have to | NTSL2++ is a loosely typed language. This means that when defining a variable, you don't have to assign its type in advance. There are a few ways to define a variable. | ||
<source lang=javascript> | <source lang=javascript> | ||
var varX // A function scoped variable | var varX // A function scoped variable | ||
Строка 15: | Строка 18: | ||
const varZ = ... // A constant variable, it's contents can't be reassigned. | const varZ = ... // A constant variable, it's contents can't be reassigned. | ||
</source> | </source> | ||
=== Values and | === Values and Their Types === | ||
When we have variables, we then can assign different values to them. | When we have variables, we then can assign different values to them. | ||
<source lang=javascript> | <source lang=javascript> | ||
Строка 54: | Строка 57: | ||
=== Arrays and Objects === | === Arrays and Objects === | ||
Let's explore in depth arrays and objects. | Let's explore in depth arrays and objects. | ||
Arrays are simple lists containing ordered items. | Arrays are simple lists containing ordered items. They indexed by whole numbers, starting from zero assigned to first element. | ||
<source lang=javascript> | <source lang=javascript> | ||
let arr = [1, 'Hi', 5] // Defining array | let arr = [1, 'Hi', 5] // Defining array | ||
Строка 107: | Строка 110: | ||
obj.func() // Calls previously defined function | obj.func() // Calls previously defined function | ||
</source> | </source> | ||
== Sample scripts == | == Sample scripts == | ||
* [https://gist.github.com/Karolis2011/8eb557386a75778af823b1180e71984c Very basic calculator] | * [https://gist.github.com/Karolis2011/8eb557386a75778af823b1180e71984c Very basic calculator] | ||
== Runtime reference == | == Runtime reference == | ||
NTSL2++ is based on ancient language called | NTSL2++ is based on ancient language called Javascript. Reference to all its functionalities can be obtained from [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects here] | ||
=== Term library === | === Term library === |
Текущая версия от 04:40, 3 августа 2023
This section or article is outdated.
Additional information: NTSL2++ is currently nonfunctional.
This article or section is outdated and the information contained in it should not be relied upon, but the page is being preserved.
Basic Syntax
Comments
First, let's acknowledge how to comment our code. Comments are lines of text that are for humans to read and aren't executed. You can comment out a line or a block of lines.
// This is a single comment line.
/* While this is start of a comment block
Comment block content
And end of comment block
*/
Variable Types
NTSL2++ is a loosely typed language. This means that when defining a variable, you don't have to assign its type in advance. There are a few ways to define a variable.
var varX // A function scoped variable
let varY // A block scoped variable. Recommended type.
const varZ = ... // A constant variable, it's contents can't be reassigned.
Values and Their Types
When we have variables, we then can assign different values to them.
// Numbers
varX = 5 // Just 5
varX = 3.14 // Number with decimal point
varX = 1e6 // 1000000 - Exponetial
varX = 25e-5 // 0.00025
varX = Infinity // Number representing infinity
varX = NaN // Number representing invalid number
// Strings
varY = "Apple"
varY = '100'
// Template literals
varZ = `This value is equals to ${varY}` // This value is equals to 100
// Booleans
varX = true
varX = false
// Arrays
varY = [1, 2, 3, 4]
// Objects
varZ = {aurora: 'Cool', game: 'SS13', players: 15}
// Special
varA = null // Value null, as nothing
varA = undefined // Value hasn't been set yet.
typeof(5) // "number"
typeof('Hello') // "string"
typeof(true) // "boolean"
typeof([1]) // "object"
typeof({moo: 'oh'}) // "object"
Arrays and Objects
Let's explore in depth arrays and objects. Arrays are simple lists containing ordered items. They indexed by whole numbers, starting from zero assigned to first element.
let arr = [1, 'Hi', 5] // Defining array
arr.length // 3
arr[0] // 1
arr[0] = 'Welcome' // This sets first item to "Welcome"
arr.pop() // Returns last item from array and removes it.
arr.push('op') // Adds item to the end of array.
Objects are like arrays, but they have keys instead of indexes to reference items.
let obj = {} // Defining empty object
// Keys can be only strings.
obj['first'] = 1 // Sets 'first' item to one.
// You can also access object values with dot.
obj.first // Returns 1
// You can also initialize object with initial values
obj = {
first: 1,
second: 2,
'my-key': 3 // To use special characters you must enclose key in qoutes.
}
obj['my-key'] // And only access using quotes.
Functions
Functions are little segments of code meant to be executed independently from other functions. To define a function you can do it in multiple of ways:
function myFunction(/* arguments */) { // Defines named function
// code...
return 5
}
myFunction() // Executes function and evaluates to return result.
let func1 = function (arg1, arg2) { // Anonymous function with two arguments.
Term.print(arg1 + arg2)
}
let func2 = (a1, a2) => a1 ** a2 // Defines arrow function (Anonymous) that raises to the power and returns result.
func1('A', '5') // Prints to console "A5"
func2(5, 5) // Evaluates to 3125
let obj = {
x: 5,
// You also can define functions inside objects
func() {
return this.x // Results in 5
}
}
obj.func() // Calls previously defined function
Sample scripts
Runtime reference
NTSL2++ is based on ancient language called Javascript. Reference to all its functionalities can be obtained from here
Term library
Function | Description |
---|---|
Term.setForeground(r, g, b) | Sets the text color to (r, g, b). Scaled from 0 - 1. |
Term.getForeground() | Returns the text color. |
Term.setBackground(r, g, b) | Sets the background color to (r, g, b). Scaled from 0 - 1. |
Term.getBackground() | Returns the background color. |
Term.cursorX | Property representing current X position. Left is 0. |
Term.cursorY | Property representing current Y position. Top is 0. |
Term.setCursor(x, y) | Sets the cursor to the position x, y. The top left is 0, 0. This is same as setting Term.cursorX and Term.cursorY directly.
|
Term.getCursor() | Returns the cursor's position. This is same as reading Term.cursorX and Term.cursorY directly.
|
Term.getSize() | Gets the size of the terminal. |
Term.width | Gets width of the terminal. Read only property. |
Term.height | Gets height of the terminal. Read only property. |
Term.clear() | Clears the screen with the preset background color. |
Term.print(value) | Writes contents of value to terminal and appends new line. |
Term.write(value) | Writes contents of value to terminal. |
Term.write(value, callback) | Writes contents of value to terminal and makes it clickable, when click is invoked callback is called. |
Term.write(value, prompt, callback) | Writes contents of value to terminal and makes it clickable, when click is invoked user is prompted to input data if prompt is true. Callback is called with inputted data as first argument. |
Term.setTopic(x, y, width, height, callback) | Makes specified region clickable, when click is invoked callback is called. |
Term.setTopic(x, y, width, height, prompt, callback) | Makes specified region clickable, when click is invoked callback is called. User is prompted to input data if prompt is true. |
Term.clearTopic(x, y, width, height) | Makes specified region no longer clickable clickable. |