G Sharp

From Esolang
Jump to navigation Jump to search
The title of this article is not correct because of technical limitations. The correct title is actually G#.

G# is a esolang with the syntax similar to Python and C. Created by User:Ractangle

Syntax

Commands

Command It's action
make <classname>;[...] Creates a class,
print[<string>] Prints a string.
assign <varname> as <varval> Creates/Edits a variable.
loop if <condition> [<action>] While statement.
if/else/orif Condition command.
input Gets user input
#...# Comment
return Turns a class into a function

Classes

A Class should folow this structure:

make a_name_here;[
    # Commands here #
]

Variables

The variables are required to be assigned at the top.

The types you can assign to variables are:

Type name Type prefix
String $
Integer #
Boolean ?
Array none
Any (void) none

These types can also be used not just for variables (See A+B problem) however the void type can be assigned to variables

If you want to edit a variable's value, you have to add the variables prefix before the name:

assign $a to void

By the way, if you want to assign more variables with the same value, just put an = in-between variables:

assign a=b=c to void

This implies to the variable edition as well

Functions

Functions are basically the same as Classes. "But how do make functions?" you may ask. Simple. Just add a return command and some arguments:

return {written}{like}{this}

And to use the argument on a function, We just write:

f[name]{the argument used in your function}

So the function should look like this:

make function; [
    return {arg}{another arg}
    # Commands here #
]

And to call a function:

f[function]{Hello, ||world!}

And now you're probably saying, "So if an argument is used, it's automatically an required argument, right?"

Yes, but you can turn off the "require" option if it's not really required:

make fun [
    return{arg#r=0}
    # Commands here #
]

Also fun fact:If there are no classes but only functions, the program will run the latest declared function, user input is used for the arguments (arguments are empty when the STDIN is empty)

Errors

If no main class (or function) is found in the code:

NO MAIN CLASS FOUND IN CODE

Converting a value type to a different type (as long as it's not a void, or can't be converted):

INCORRECT TYPE CONVERSION

Examples

Hello World

make main;[print["Hello World\n"]]

Cat program

string assign a as input
make main;[print[$a]]

A+B problem

integer assign a;b as input
make main;[print[#a+#b]]

Infinite loop

make main;[ loop if 1 [1] ]

Truth-machine

make main;[
    return {i}
    if #i=0 [print[0]]
    loop if 1 && #i!0 [print[1]]
]

Empty Program

make e; [return {}]

99 bottles of beer

integer assign bot as 99
make main;[
    if #bot is 1;[ print[#bot," bottle of beer on the wall,\n",
    #bot," bottle of beer.\nTake one down, Pass it around,\nNo bottles of beer on the wall,\n"] ]
    else [ loop if #bot ! 1;[
        print[#bot," bottles of beer on the wall,\n",
        #bot," bottles of beer.\nTake one down, Pass it around,\n"]
        assign #bot as #bot-1
        print(#bot," bottles of beer on the wall,\n\n") 
    ]
  ]
]