Skip to content

Getting Started

Techcraft7 edited this page Jan 5, 2021 · 18 revisions

Getting started with 7Sharp

How to edit code

  1. Run 7Sharp
  2. Type edit in the shell to open the editor
  3. Type code
  4. Click edit
  5. Type run into the shell to run the code. Some beta builds will print A LOT of debug output, you can ignore this.

Basic syntax

Calling functions

  • No arguments > name();
  • 1 argument > name(123);
  • 2 arguments > name(123, 456);
  • etc.

Example:

write("Hi!");


Defining variables

name = value;

Examples

x = 2 + 2;
name = read();
y = "x is " + x;
z = name;
w = 42 * 1337;
c = 3 / 2;
modulusTest = c % 2;

Loops

loop Loop

The loop loop runs code a specified number of times.

loop (n) {
    // code to be run n times
    // the function getLoopIndex() will get the current index of the current loop loop.
    // Think about it as the "i" variable in C’s for loop.
    // It starts at 0, and then is incremented after each run of the loop
}
getLoopIndex(n = 0)

getLoopIndex is used to get the index of a loop loop. When given no argument, it will get the index of the current loop loop. If 1 is passed in, it will get the index of the loop loop outside of the current loop loop. If 2 is passed in it will get the index of the loop loop outside of the loop loop outside of the current loop loop, and so on. Passing in a double will cause an error, same thing with neagive values.

While loop

while (condition) {
    // this code will run while condition is true
}

break; and continue;

break; - exit the current loop

Example:

loop (5) {
    i = getLoopIndex();
    if (i == 3) {
        break;
    }
    write(i);
}
/*
Output:
0
1
2
*/

continue; - skip the rest of the code in the current loop, and start the next iteration of the loop

loop (5) {
    i = getLoopIndex();
    if (i == 2) {
        continue;
    }
    write(i);
}
/*
Output:
0
1
3
4
*/

If statements

// just an if
if (a) {
    // will run if a is true
}
// if else
if (b) {
    // code here
} else {
  // this will run is b is false
}
// else if
if (c) {
  // code
} else if (d) {
  // will run if c is false but d is true
}
// you can add as many else ifs as you want
else {
  // this is optional, but this will run if none of the other if else’s above run
}

Imports

To include libraries, you use the import statement.

Syntax: import "name";

name will point to either file (if no extension is provided it will assume .7slib), or a system library.

System functions reference

Default, no import required
  • write(text) - output text followed by a new line
  • writeraw(text) - output text, NO NEW LINE!
  • getLoopIndex(n = 0) - get the index of the current loop(n) {} loop
  • sin(x) cos(x) tan(x) asin(x) acos(x) atan(x) atan2(x, y) - standard trig functions, all are in radians.
  • sqrt(x) - return the square root of x
  • pow(x, n) - return x to the power of n
  • deg2rad(degrees) rad2deg(radians) - Degrees <--> Radians conversion
  • sleep(ms) - wait for ms miliseconds
  • len(arrayOrString) - get the length of arrayOrString
  • chars(string) - get an array of the characters of string
  • arrayAdd(array, value) - add value to the end of array
  • arrayRemove(array, index) - remove the value in array at index
  • toString(obj) - convert obj to a string. If given an array, it will return "Array [contents, go, here]"
  • double(obj) - convert obj to a double
  • int(obj) - convert obj to an int
  • fgColor(color) and bgColor(color) - change the foreground (text) color and background color of the console.
    • Ex: fgColor(YELLOW); write("Hello!"); will print Hello! in yellow
    • Note: both functions will accept numbers 0 to 15 as input as well!
    • Color list:
    • BLACK
    • BLUE
    • CYAN
    • DARK_BLUE
    • DARK_CYAN
    • DARK_GRAY
    • DARK_GREEN
    • DARK_MAGENTA
    • DARK_RED
    • DARK_YELLOW
    • GRAY
    • GREEN
    • MAGENTA
    • RED
    • WHITE
    • YELLOW
  • resetColor() - reset the colors of the console
Imports required
  • random.sys
    • nextInt(min, max) - get a random integer between min and max
    • nextDouble(min, max) - get a random double (64-bit floating point) between min and max
    • next() - get a random 32-bit integer
    • setSeed(seed) - set RNG seed to seed
  • io.sys
    • readFile(path) - get a read stream to file at path
    • writeFile(path) - get a write stream to file at path
    • close(stream) - close stream, use this to clean up resources used by stream. Not calling this could cause a memory leak.
    • readByte(stream) - read a byte from stream
    • readLine(stream, encoding = "utf8") - read a line of text from stream using encoding encoding, default encoding is utf8. Supported encodings: ascii, utf8
    • readText(stream, count, encoding = "utf8") - read count characters from stream using encoding encoding, default encoding is utf8.
    • writeByte(stream, value) - write the byte value to stream
    • writeText(stream, text, encoding = "utf8") - write text to stream using encoding encoding, default encoding is utf8.
    • writeLine(stream, text, encoding = "utf8") - write text to stream, followed by a new line using encoding encoding, default encoding is utf8.

Custom libraries

7Sharp has the ability for users to create and use community created libraries, all within 7Sharp itself.

Usage

To import a custom library, do import "custom library";

7Sharp will search for a file called custom library.7slib If found it will import all of its functions! After that, you can use any function in that library!

Creation

To make a custom library, do the following:

  1. Put your code into the editor (type edit in the shell).
  2. Type export "file path" into the shell. Now you should have a 7slib file at that path! (It's a zip file, so you can open it by changing the extension to .zip)

Custom functions

To define a custom function, do the following:

function name(arguments, seperated, by, commas, this, is, optional, though) {
    // code
}

Outputting data from functions

If you want your function to exit early or output a value (e.g. to put in a variable), then you use return.

To exit the function early, just do return;

To return a value, use return value;

Ex: sum function
function sum(a, b) {
    return a + b;
}

x = sum(2, 2);
write(x); // Should output "4"
Ex: exit function early
function doStuff(x) {
    write("Doing stuff!");
    if (x == 3) {
        return;
    }
    write("x is not 3! x is: " + x);
}

doStuff(5); // Outputs "Doing stuff!" and "x is not 3! x is: 5"
doStuff(3); // Outputs "Doing stuff!"