G Sharp
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
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> | Makes a loop and ends when a condition is not true. |
if/else/orif | Condition command. |
input | Gets user input |
# | One-line comment |
###...### | Multi-line comments |
return | turns a class into a function |
Classes
A Class should folow this structure:
make a_name_here; [ # Commands here ]
If the code doesn't contain a main class. The program will emit:
SYNTAX ERROR ON CODE main is not defined
Variables
The variables NEED to be assigned at the top.
Also there are types:
Type name | Type symbol |
---|---|
String | $ |
Integer | # |
Boolean | ? |
Ticker | + |
And if you want to edit a variable. You just write:
assign $a to anything
And if you want to assign more variables with the same value, write:
assign a;b to anything
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!}
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; [ assign #a as #b + print(#a) ]
Infinite loop
boolean assign l to 1 make main; [ loop if ?l is 0 [ print(1) ] ]
Truth-machine
integer assign i as input boolean assign l to 1 make main; [ if #i is 0 [ print(0) ] orif #i is 1 [ loop if ?l=1 [ print(1) ] ] ]
Looping counter
integer assign n as 0 boolean assign l as 1 make main; [ loop if ?l is 1 [ print(#n) assign #n as 1 + ] ]
Empty Program
make e; [ return ] make main; [ e[] ]
99 bottles of beer
integer assign bot as 99 make main; [ if #bot is 1 [ print(#bot," bottle of beer on the wall,",\n) print(#bot," bottle of beer.",\n) print("Take one down, Pass it around,",\n) print("No bottles of beer on the wall,",\n) ] else [ loop if #bot ! 1 [ print(#bot," bottles of beer on the wall,",\n) print(#bot," bottles of beer.",\n) print("Take one down, Pass it around,",\n) assign #bot as 1 - print(#bot," bottles of beer on the wall,",\n,\n) ] ] ]
Deadfish implementation
ticker assign a as 0 boolean assign l as 1 make main; [ print("Deag#ish",\n) loop if ?l is 1 [ for c in input [ if c is "i" [ add 1 to +a ] if c is "d" [ add -1 to +a ] if c is "s" [ add +a to +a ] if c is "o" [ print(+a) ] ] ] ]