Guide to NTSL2++: различия между версиями

Материал из FIJTeam Project Wiki
Перейти к навигации Перейти к поиску
>Karolis2011
(Added width and heigth)
>Karolis2011
(Sintax guide)
Строка 1: Строка 1:
== Basic sintax ==
First, let's acknowledge how to comment our code. You can comment out line or do a comment block.
<source lang=javascript>
// This is single comment line.
/* While this is start of comment block
Comment block content
And end of comment block
*/
</source>
=== Variable types ===
NTSL2++ is loosely typed language. This means you don't have to know type in advance to define variable. There are few ways to define a variables.
<source lang=javascript>
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.
</source>
=== Values and there types ===
When we have variables, we then can assign different values to them.
<source lang=javascript>
// 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"
</source>
=== Arrays and Objects ===
Let's explore in depth arrays and objects.
Arrays are simple lists containing ordered items.
<source lang=javascript>
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.
</source>
Objects are like arrays, but they have keys instead of indexes to reference items.
<source lang=javascript>
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.
</source>
=== 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:
<source lang=javascript>
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
</source>
== Sample scripts ==
== Sample scripts ==
* [https://gist.github.com/Karolis2011/8eb557386a75778af823b1180e71984c Very basic calculator]
* [https://gist.github.com/Karolis2011/8eb557386a75778af823b1180e71984c Very basic calculator]

Версия от 14:20, 2 декабря 2020

Basic sintax

First, let's acknowledge how to comment our code. You can comment out line or do a comment block.

// This is single comment line.
/* While this is start of comment block
Comment block content
And end of comment block
*/

Variable types

NTSL2++ is loosely typed language. This means you don't have to know type in advance to define variable. There are few ways to define a variables.

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 there 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.

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 it's 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.