G Language
Types:
- int : integers : numbers consisting of the natural numbers in the theory of sets (32 bits). Declaration examples:
o int a ;
o int val = 2 ;
- float : floating-point number : numbers consisting of the real numbers in the theory of sets (32 bits).
Declaration examples:
o float a ;
o float pi = 3.141592 ;
o float b = 3.0 ;
- char : characters, delimited with ' ' (1 byte : 8 bits). Can be assimilated to an int because a character is an ASCII integer value.
Declaration examples:
o char a ;
o char b = 'G' ;
o char c = 71 ; // same as char a = 'G' because 71 is the ASCII code of letter G
- string : sequence of characters, delimited with " "
Declaration examples:
o string a ;
o string name = "Quentin Demé" ;
- array : array which can be composed with values of types int, float, char or string.
Declaration examples:
o array(int)[256] values ;
o int size = 25 ; array(string)[size] names ;
There is no 'bool' type for Boolean values, but the values 0 and 1 suffice and are understood as Booleans by the parser. Later in the document, when the type 'bool' is specified, this means that a 0/1 'int' value is required.
Conditional instructions:
Basic conditions:
if :
if ( condition ) {
instructions ;
}
elseif ( condition ) {
instructions ;
}
…
else {
instructions ;
}
switch :
switch ( int or char variable ) {
case ( int or char constant ) {
instructions ;
}
…
default {
instructions ;
}
}
Loops:
while :
while ( condition ) {
instructions ;
}
for :
for ( initializer ; loop-test (condition) ; counting expression ) {
instructions ;
}
do, while :
do {
instructions ;
} while ( condition )
Arrays:
Arrays, in G Language, belong to the 'array' type. In some languages, arrays belong to the same type the values they're composed of. Here, 'array' is a type, as well as 'int', 'float', etc. The difference between 'array' and the other types is the way we use it.
Declaring an array: array(type)[size] name ;
The parameter 'size' must be a constant or variable 'int' value, i.e. that these two use cases are allowed:
- array(int)[256] values ;
- int size = 25 ; array(string)[size] names ;
Accessing the i-th element of an array: values[i]
In G, as well as in many other languages, arrays are indexed from 0. As a consequence, if you declare an array of size 4, you must access the elements indexed from 0 to 3.
Getting and storing a value: string name = names[5] ;
Example of affectation: names[2] = "Jackie Chan" ;
If you refer to an element which is bigger than the indexed space, the IndexOutOfRangeException will be thrown.
Comments:
To comment a single line, you must put // at the beginning of the line. This line will simply be ignored by the parser.
To comment a group of lines, start with /* and end with */.
This comment system is exactly the same as in C++ and many other languages.
Structure of a G Language program:
Here is how a program in G must be formed :
// Beginning of file program.g
declaration of global variables ;
declaration of procedures;
declaration of functions ;
entry {
instructions ;
}
// End of file program.g
The 'entry' block:
The 'entry' block is the starting point of every program written in G Language. There must be one for having a program that works. It must be located at the end of the file. If not, some eventual functions or procedures wouldn’t be visible in the 'entry' block. For using a procedure or a function, you must declare it before the place it will be called. It is impossible to call 'entry' from within the program, like this for example: entry().
You can get arguments passed to the program really easily. The number of arguments given to the program and the array containing these arguments are stored in two global variables affected the moment the program is launched:
- int entryNbParam ;
- array(string)[entryNbParam] entryParam ;
The element 0 contains the name of the executable written in G that was executed. Starting from element 1, there are the given arguments.
Example of a program which lists the parameters it has been given:
entry {
if (entryNbParam > 1) {
echo(entryNbParam & " parameters given to the program :",1);
}
else {
echo(entryNbParam & " parameter given to the program :",1);
}
echo("",1);
for (int i = 0; i < entryNbParam; i=i+1) {
echo("Param " & i+1 & " : " & entryParam[i],1);
}
}
Procedures:
Declaration of a procedure:
proc procName(type param1, type param2, … , type paramn) {
instructions ;
}
Calling a procedure: procName(param1, param2, … , paramn);
The language makes the difference between the concepts of procedure and function. It is impossible to call a procedure in an affectation, like that for example: int a = procName();
If you really want to do this, you must declare a function.
Example of procedure use case:
proc displayDate(string year, string month, string day) {
string date = year $+ "/" $+ month $+ "/" $+ day;
echo("The date is " & date,1);
}
entry {
displayDate("2008","02","17");
}
Execution trace:
The date is 2008/02/17
Functions:
Declaration of a function:
function funcName(type param1, type param2, … , type paramn) return type {
instructions ;
}
A function must contain a return instruction because it is what it's used for: returning a value!
There are two ways of calling a function:
- The procedure-type call: funcName(param1, param2, … , paramn) ;
In this case, we cannot store the return value, but the body of the function was nevertheless executed.
- The "conventional" call: int a = funcName(param1, param2, … , paramn) ;
The return value of the function will be stored in variable 'a'.
Example of function use case:
# factorial calculus program
function factorial(int nb) return int {
int fact = 1;
for (int i = 1; i <= nb; i=i+1) {
fact = fact * i;
}
return fact;
}
entry {
echo("Whose number you want the factorial to be calculated?",1);
echo("nb = ",0);
int nb;
input(nb,int);
int fact = factorial(nb);
echo(nb & "! = " & fact,1);
}
Execution trace:
Whose number you want the factorial to be calculated?
nb = 5
nb! = 120
Operators :
< > <= >= == != : between int and int, int and float, float and float, string and string, char and char, char and int, int and char.
== and != : for Boolean expressions as well
! : negation for Boolean expressions
$+ : concatenation of string values
==, and != are not case sensitive for strings. Case sensitive operators are === and !==.
^ : power
| inclusive 'or'
G Language reserved instructions:
Input/Output :
echo(variable/constant & variable/constant & … & variable/constant,1/0)
The operator '&' is for concatenating variables or constants.
If the second parameter is 1, the procedure will display a newline character at the end.
If the second parameter is 0, no newline character is displayed.
input(variable,type)
Allows the user to type in a variable of type 'type', and store it in the variable 'variable', which must obviously belong to the type 'type'.
You can type in any type except arrays. For arrays, just use a loop.
Mathematics:
float sqrt(float value)
Returns the square root of 'value'.
Strings:
char StrGetInd(string text, int element)
Returns the i-th character of a given string. Equivalent to string[i] in other languages.
StrSetInd(string text, int indice, char car)
Defines the i-th character of a string with 'car'.
int StrLen(string text)
Returns the size of a string (number of characters).
string GetTok(string text, char sep, int element, bool next)
Returns the element-th word of 'text' according to character 'sep', if 'next' is false.
Returns the element-th word of 'text' and all that is after it, according to character 'sep', if 'next' is true.
Examples:
- GetTok("Hello how are you?",' ',2,0) returns "how"
- GetTok("Hello how are you?",'w',1,0) returns "Hello ho"
- GetTok("Hello how are you?",'w',2,0) returns " are you?"
- GetTok("Hello how are you?",' ',2,1) returns "how are you?"
int NumTok(string text, char sep)
Returns the number of words of 'text' according to character 'sep', the same way GetTok() works.
Text files:
string FileReadLine(string file, int line)
Returns line number 'line' from file 'file'.
int FileLines(string file)
Returns the number of lines of file 'file'.
int FileCreate(string file)
Creates an empty file 'file'. Returns 1 if success, 0 if fail.
int FileWriteLine(string file, string text, int line)
Writes the text 'text' in 'file' at line number 'line'. Returns 1 if success, 0 if fail.
int FileAppend(string file, string text)
Writes the 'text' at the end of file 'file'.
int FileExists(string file)
Returns 1 if 'file' exists, 0 if not.
int FileInsert(string file, string text, int line)
Inserts 'text' in 'file' at line 'line', and removes the old line. Returns 1 if success, 0 if fail.
int FileRename(string oldfile, string newfile)
Renames file 'oldfile' to 'newfile'. Returns 0 if success, -1 if not.
int FileCopy(string file, string newfile, int overwrite)
Renames file 'file' to 'newfile'. Returns 0 if fail, nonzero if not. If the parameter 'overwrite' is true and the new file specified by 'newfile' already exists, the function fails. If this parameter is false and the new file already exists, the function overwrites the existing file and succeeds.
int FileCopy(string file, string newfile, int overwrite)
Removes file 'file'. Returns 0 if success, -1 if fail.
int FileSize(string file)
Returns the size of file 'file' in bytes.
int FileDeleteLine(string file, int line)
Deletes line number 'line' in file 'file'. Returns 1 if success, 0 if fail.
Convertion:
int StringToInt(string val)
Converts from 'string' to 'int'.
int IntToString(int val)
Converts from 'int' to 'string'.
int StringToFloat(string val)
Converts from 'string' to 'float'.
int FloatToString(float val)
Converts from 'float' to 'string'.
Sockets:
int SockOpen(string sockname, string host, string port)
Opens a TCP connection to host:port.
int SockClose(string sockname)
Closes a TCP connection.
int SockWrite(string sockname, string text)
Sends ‘text’ to the socket 'sockname'.
string SockRead(string sockname)
Gets a text message from socket 'sockname'.
int SockListen(string sockname, string host, string port)
Listens on port with IP 'host'.
int SockAccept(string sockname, string newsockname)
Accepts a TCP connexion from 'sockname' listen socket, and calls it 'newsockname'.
Time and date:
int getTicks()
Returns the system tick count.
Ini files:
string IniRead(string file, string section, string item)
Returns the value associated to 'item' in 'section', in 'file' .ini file.
IniWrite(string file, string section, string item, string value)
Sets the value of 'item' in 'section' in 'file', to 'value'.
Miscellaneous:
int Sleep(int time)
Makes the program sleep for 'time' milliseconds.
int System(string cmd)
Sends command 'cmd' to the operating system.
int MessageBox (string caption, string text, int type)
There are several values for 'type':
MB_ABORTRETRYIGNORE
(2)
The message box contains three push buttons: Abort, Retry, and Ignore.
MB_CANCELTRYCONTINUE
(6)
Microsoft Windows 2000/XP: The message box contains three push buttons: Cancel, Try Again, Continue. Use this message box type instead of MB_ABORTRETRYIGNORE.
MB_HELP
(16384)
Windows 95/98/Me, Windows NT 4.0 and later: Adds a Help button to the message box. When the user clicks the Help button or presses F1, the system sends a WM_HELP message to the owner.
MB_OK
(0)
The message box contains one push button: OK. This is the default.
MB_OKCANCEL
(1)
The message box contains two push buttons: OK and Cancel.
MB_RETRYCANCEL
(5)
The message box contains two push buttons: Retry and Cancel.
MB_YESNO
(4)
The message box contains two push buttons: Yes and No.
MB_YESNOCANCEL
(3)
The message box contains three push buttons: Yes, No, and Cancel.
To display an icon in the message box, specify one of the following values.
MB_ICONEXCLAMATION
(48)
An exclamation-point icon appears in the message box.
MB_ICONWARNING
(48)
An exclamation-point icon appears in the message box.
MB_ICONINFORMATION
(64)
An icon consisting of a lowercase letter i in a circle appears in the message box.
MB_ICONASTERISK
(64)
An icon consisting of a lowercase letter i in a circle appears in the message box.
MB_ICONQUESTION
(32)
A question-mark icon appears in the message box. The question-mark message icon is no longer recommended because it does not clearly represent a specific type of message and because the phrasing of a message as a question could apply to any message type. In addition, users can confuse the message symbol question mark with Help information. Therefore, do not use this question mark message symbol in your message boxes. The system continues to support its inclusion only for backward compatibility.
MB_ICONSTOP
(16)
A stop-sign icon appears in the message box.
MB_ICONERROR
(16)
A stop-sign icon appears in the message box.
MB_ICONHAND
(16)
A stop-sign icon appears in the message box.
To indicate the default button, specify one of the following values.
MB_DEFBUTTON1
(0)
The first button is the default button.
MB_DEFBUTTON1 is the default unless MB_DEFBUTTON2, MB_DEFBUTTON3, or MB_DEFBUTTON4 is specified.
MB_DEFBUTTON2
(256)
The second button is the default button.
MB_DEFBUTTON3
(512)
The third button is the default button.
MB_DEFBUTTON4
(768)
The fourth button is the default button.
To indicate the modality of the dialog box, specify one of the following values.
MB_APPLMODAL
(0)
The user must respond to the message box before continuing work in the window identified by the hWnd parameter. However, the user can move to the windows of other threads and work in those windows.
Depending on the hierarchy of windows in the application, the user may be able to move to other windows within the thread. All child windows of the parent of the message box are automatically disabled, but pop-up windows are not.
MB_APPLMODAL is the default if neither MB_SYSTEMMODAL nor MB_TASKMODAL is specified.
MB_SYSTEMMODAL
(4096)
Same as
MB_APPLMODAL except that the message box has the WS_EX_TOPMOST
style. Use system-modal message
boxes to notify the user of serious, potentially damaging errors that require
immediate attention (for example, running out of memory). This flag has no
effect on the user's ability to interact with windows other than those
associated with hWnd.
MB_TASKMODAL
(8192)
Same as MB_APPLMODAL except that all the top-level windows belonging to the current thread are disabled if the hWnd parameter is NULL. Use this flag when the calling application or library does not have a window handle available but still needs to prevent input to other windows in the calling thread without suspending other threads.
To specify other options, use one or more of the following values.
MB_DEFAULT_DESKTOP_ONLY
(131072)
Windows NT/2000/XP: Same as desktop of the interactive window station. For more information, see Window Stations.
Windows NT 4.0 and earlier: If the current input desktop is not the default desktop, MessageBox fails.
Windows 2000/XP: If the current input desktop is not the default desktop, MessageBox does not return until the user switches to the default desktop.
Windows 95/98/Me: This flag has no effect.
MB_RIGHT
(524288)
The text is right-justified.
MB_RTLREADING
(1048576)
Displays message and caption text using right-to-left reading order on Hebrew and Arabic systems.
MB_SETFOREGROUND
(65536)
The message box becomes the foreground window. Internally, the system calls the SetForegroundWindow function for the message box.
MB_TOPMOST
(262144)
IDABORT (3) |
Abort button was selected. |
IDCANCEL (2) |
Cancel button was selected. |
IDCONTINUE (11) |
Continue button was selected. |
IDIGNORE (5) |
Ignore button was selected. |
IDNO (7) |
No button was selected. |
IDOK (1) |
OK button was selected. |
IDRETRY (4) |
Retry button was selected. |
IDTRYAGAIN (10) |
Try Again button was selected. |
IDYES (6) |
Yes button was selected. |
string Version()
Returns the G Language parser version.
int rand(int min, int max)
Returns a random number between 'min' and 'max'.
exit(int code)
Terminates the program.
string getOSName()
Returns the Operating System name.
string getOSVersion()
In some cases, provides more information than getOSName().
int ShellExecute(string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd)
Same as Microsoft's ShellExecute function, except that there is no "hWnd" parameter. More information about the parameters (from MSDN):
lpOperation
A pointer to a null-terminated string, referred to in this case as a verb, that specifies the action to be performed. The set of available verbs depends on the particular file or folder. Generally, the actions available from an object's shortcut menu are available verbs. The following verbs are commonly used.
edit
Launches an editor and opens the document for editing. If lpFile is not a document file, the function will fail.
explore
Explores a folder specified by lpFile.
find
Initiates a search beginning in the directory specified by lpDirectory.
open
Opens the item specified by the lpFile parameter. The item can be a file or folder.
print
Prints the file specified by lpFile. If lpFile is not a document file, the function fails.
NULL
In systems prior to Microsoft Windows 2000, the default verb is used if it is valid and available in the registry. If not, the "open" verb is used.
In Windows 2000 and later, the default verb is used if available. If not, the "open" verb is used. If neither verb is available, the system uses the first verb listed in the registry.
lpFile
A pointer to a null-terminated string that specifies the file or object on which to execute the specified verb. To specify a Shell namespace object, pass the fully qualified parse name. Note that not all verbs are supported on all objects. For example, not all document types support the "print" verb. If a relative path is used for the lpDirectory parameter do not use a relative path for lpFile.
lpParameters
If lpFile specifies an executable file, this parameter is a pointer to a null-terminated string that specifies the parameters to be passed to the application. The format of this string is determined by the verb that is to be invoked. If lpFile specifies a document file, lpParameters should be NULL.
lpDirectory
A pointer to a null-terminated string that specifies the default (working) directory for the action. If this value is NULL, the current working directory is used. If a relative path is provided at lpFile, do not use a relative path for lpDirectory.
nShowCmd
The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it.
SW_HIDE
(0)
Hides the window and activates another window.
SW_MAXIMIZE
(3)
Maximizes the specified window.
SW_MINIMIZE
(6)
Minimizes the specified window and activates the next top-level window in the z-order.
SW_RESTORE
(9)
Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW
(5)
Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT
(10)
SW_SHOWMAXIMIZED
(3)
Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED
(2)
Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE
(7)
Displays the window as a minimized window. The active window remains active.
SW_SHOWNA
(8)
Displays the window in its current state. The active window remains active.
SW_SHOWNOACTIVATE
(4)
Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL
(1)
Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
Return Value
Returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwise. The following table lists the error values.
0 |
The operating system is out of memory or resources. |
ERROR_FILE_NOT_FOUND (2) |
The specified file was not found. |
ERROR_PATH_NOT_FOUND (3) |
The specified path was not found. |
ERROR_BAD_FORMAT (11) |
The .exe file is invalid (non-Microsoft Win32 .exe or error in .exe image). |
SE_ERR_ACCESSDENIED (5) |
The operating system denied access to the specified file. |
SE_ERR_ASSOCINCOMPLETE (27) |
The file name association is incomplete or invalid. |
SE_ERR_DDEBUSY (30) |
The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed. |
SE_ERR_DDEFAIL (29) |
The DDE transaction failed. |
SE_ERR_DDETIMEOUT (28) |
The DDE transaction could not be completed because the request timed out. |
SE_ERR_DLLNOTFOUND (32) |
The specified DLL was not found. |
SE_ERR_FNF (2) |
The specified file was not found. |
SE_ERR_NOASSOC (31) |
There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable. |
SE_ERR_OOM (8) |
There was not enough memory to complete the operation. |
SE_ERR_PNF (3) |
The specified path was not found. |
SE_ERR_SHARE (26) |
A sharing violation occurred. |
Examples :
ShellExecute("print", "glanguage.htm", "","", 3) : prints the file "glanguage.htm"
ShellExecute("open", "glanguage.htm", "","", 3) : opens the file "glanguage.htm"
ShellExecute("edit", "glanguage.htm", "","", 3) : edits the file "glanguage.htm"
ShellExecute("", "C:\GLANGUAGE", "","", 3) : opens the folder "C:\GLANGUAGE"
ShellExecute("explore", "C:\GLANGUAGE", "","", 3) : opens the folder "C:\GLANGUAGE" with the tree structure on the left
ShellExecute("find", "C:\GLANGUAGE", "","", 3) : opens the folder with the find utility on the left