Skip to content

Instantly share code, notes, and snippets.

@matyklug18
Created December 28, 2019 23:22
Show Gist options
  • Save matyklug18/50c1bd0ac70b1988522255ec9c84d632 to your computer and use it in GitHub Desktop.
Save matyklug18/50c1bd0ac70b1988522255ec9c84d632 to your computer and use it in GitHub Desktop.
--Example N0--
//hello world using SUPRA
@Settings.EnableScriptingCompilation //enables scripting compilation, like in python
print("hello world!"); //if scripting compilation is enabled, syntax like this is valid.
--Example N1--
//simple print to print the second arg.
@Runnable //defines if class is runnable(can be runned)
@MainClass //declares default class to launch when program starts.
class Main { //name doesnt matter when @MainClass is defined.
/*
main method can be bool, int, Exception or void.
if its bool, true means executed sucessfully, false with an error.
if its int, the return defines error code. inspired by C.
if its Exception, the return must be class that extends Exception.
*/
@MainMethod //declares default method to launch when class is runned
public int main(Args args) { //name of the method doesnt matter
if(Args.allArgs.len() > 1) { //simple check amout of args is bigger than 1.
print(Args.secondArg()); //prints second argument. these methods are generated using dynamic-method-gen.
return 0; //0 is for succesful.
} else //no need for {} if you call only one thing, like in java.
return ErrorInts.ErrInvalidArgAmount; //ErrorInts is an list of all int errors and their error codes.
}
}
--Example N2--
//simple box using J3Engine
@Settings.EnableDefaultRunnable //makes all classes named Main both runnable and main.
//you can use this as long as the class you are trying to import is there only once. if in the package you specified it is more times, you would get an error.
//if you want to import it, you must specify a path in which it is only one(no need to specify full path tho).
import J3Engine.Simple3DApp;
class Main extends Simple3DApp { //extends simple3dapp, a class that sets up all settings for you.
//Override is, unlike in java, needed to override a method(can be disabled using settings).
@OverrideName:run //you can use @OverrideName:<Name of method to override> to use custom name for the method.
void main(RootNode root, Display display) {
root.getChildren.add(new Box(10, 10, 10). //creates new box of size 10 x 10 x 10.
setPos(vec3f(10, 0, 0). //sets the position using static data type vec3f(inspired by glsl).
setColor(Colors.RED). //sets the color to red.
setMaterial(Materials.Shaded). //sets material to shaded.
setFakeLightColor(Colors.RED))); //makes this emit red light, but not from the surface(hence "fake").
}
}
--Example N3--
//example of the events system
@Settings.EnableDefaultRunnable
@Settings.EnableDefaultMain //makes method called main the main method.
public event ExampleEvent(str param, int param2) {
}
public class Main {
public void main() {
Events.call(new ExampleEvent("hello", 10));
}
@Subscribe(ExampleEvent.event)
public void callMe(str string, int parameter) {
}
}
--Example N4--
//generic types
@Settings.EnableScriptingCompilation
num number = 10; //num can be int...
number = .5; //... or float.
arr array = {
10, vec3f(1, .9, 5.), "hello" //arr can be any array.
};
var variable = "world!"; //var can be anything.
variable = 2.; //you can use 2. as alias for 2.0.
--Example N5--
//example of some j2s classes/methods
@Settings.EnableScriptingCompilation
//os class
OS os = get(OS); //you can use get() to always get one instance of something.
time = os.getSystemTimeInMilis(); //if you dont use any type, the default type is var. getSystemTimeInMilis gets the system time in miliseconds.
// bind binds an method to a variable, so every time you call that variable it calles the method.
date = bind(os.getDate); //you dont need to put () at the end of a method if there arent any arguments for it.
--Example N6--
//example showing the gate functionality.
main { //you can also use this to define an main funcion and class at the same time.
gate and(bin a, b) { //gate defines its an gate, bin means that it can be 0 or 1, false or true or on or off.
outlen = 1; //defines amout of outs
TT = { //TT is inner variable. TT stands for Truth Table. its the truth table for the gate.
00:0,
01:0,
10:0,
11:1;
};
return TT; //what TT to return.
}
}
int x = func {
}
--Example N7--
//listeners
main {
cout << "Hello from " + "cout!"; //prints to the console ""Hello from cout!"
nout << "Notification!"; //makes a notification. alternative: makenotif().
cout << lterm << "ls ~/"; //if you are on linux, this lists all files in your home dir into the console.
}
--Example N8--
//circuits + other features
public class ExampleCircuit implements ICircuit {
getOutsLen {
if(subCircuit.toString() == "adder")
return 16;
}
getInsLen {
if(subCircuit.toString() == "adder")
return 8;
}
ainst declare TwoInputs = {bin a, bin b} <{ //ainst makes it automatically instantiate when refferenced, declare makes it that object.
addin$(insnum) = a; //$() refers to a statement. allows for dynamically created variables.
addin$(insnum*2) = b;
}>;
ainst declare TwoOutputs = {bin a, bin b} <{
addout$(insnum) = a;
addout$(insnum*2) = b;
}>;
@Override
void DeclareInputs { //DeclareInputs is for declaring inputs. by not specifing args, and there being only one DeclareInputs, this passes that args.
if(subCircuit.toString() == "adder")
for(bin in:ins; int index; index++) {
bin addin$(index) = in;
}
}
get(CircuitsLibrary).FullAdder(8).connect().to
(
createSubCircuit
(
new SubCircuit("adder"),
TwoInputs.Array func {
for(int i = 0; i < 8; i++)
return TwoInputs(addin$(i), addin$(i*2));
}.ab,
TwoOutputs.Array func {
for(int i = 0; i < 8; i++)
return TwoOutputs(addout$(i), addout$(i*2));
}.ab
)
get(CircuitsLibrary).NotGateArray(8).connect().to
(
createSubCircuit
(
OutCircuit.get(),
TwoInputs.Array func {
for(int i = 0; i < 8; i++)
return TwoInputs(addout$(i), addout$(i*2));
}.ab,
TwoOutputs.Array func {
for(int i = 0; i < 8; i++)
return TwoOutputs(addin$(i), addin$(i*2));
}.ab
)
);
}
main {
Circuit circ = makeCircuit(get(ExampleCircuit));
File pngCirc = circ.toPNG;
pngCirc.setPath(~/circuit.png);
MCEditSchematic structure = circ.toMCEdit();
File mcedit = structure.toFile();
mcedit.setPath(~/circuit.schematic);
}
--Example N10--
//string formatting
main {
int x = 10;
print("x: %x"); //prints x: 10. replaces %<var name> with the value of <var name>.
}
--Example N11--
//file managing
main {
str filestring = read("src/resources/", "file", FileTypes.ASCII); //reads from the project path, finds file named file, and reads it as ASCII.
write("/home/username/", "file", FileTypes.ASCII, "Hello World!"); //writes Hello Wolrd! into file located at /home/username/ called file, with type ASCII.
str hexstring = read("src/resources/", "file", FileTypes.HEX); //hexdumps the file named file.
write("/home/username/", "file", FileTypes.HEX, "610A"); //exact hex values.
}
--Example N12
//bash commands
main {
bash("git clone https://github.com/some/repo.git"); //executes this command in bash. works only on linux.
zsh("ls"); //executes this command in zsh. obviously only if zsh is installed.
}
--Example N13--
//packing
main {
{str s, float f} variable = new {str s, float f};
variable.s = "hello"; //s is set to "hello".
variable.f = 11.12; //f is set to 11.12.
print(variable); //and its printed. prints "{s="hello", f=11.12f}".
}
--Example N14--
//global keyword + global variables
@Runnable
@MainClass
public class Main {
@MainMethod
public void main(Args args) {}
public Main(global out int x, y, z) { //global means that it is global for the class.
level(Levels.PROGRAM) int somevariable = 1; //this variable is global to the whole app
level(Levels.PROGRAM) int someothervariable = 1; //this one also
}
}
class B {
int somevariable = 0;
public B() {
print(somevariable); //prints "0", because local variables are preffered over global ones.
print(someothervariable) //prints "1".
}
}
EventHandler.list.get(id).background == 1 ? Blocks.STONE : Blocks.DIRT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment