Professional Documents
Culture Documents
You may store a group of ChangeRingKeyword and ChangeRingOperator commands in a file to use later in many
source files. You can’t use the Load command to call these files because
• ChangeRingKeyword and ChangeRingOperator commands are executed in the scanner phase by the compiler
(before parsing).
• The load command is executed in the parsing phase (after the scanner phase).
Solution: Use the LoadSyntax Command which is executed in the scanner phase.
Syntax:
LoadSyntax "syntaxfile.ring"
Example:
File : StyleBasicOn.ring
ChangeRingKeyword see print
ChangeRingKeyword ok endif
ChangeRingKeyword next endfor
ChangeRingKeyword end endwhile
File : StyleBasicOff.ring
ChangeRingKeyword print see
ChangeRingKeyword endif ok
ChangeRingKeyword endfor next
ChangeRingKeyword endwhile end
File : UseStyleBasic.ring
LoadSyntax "stylebasicon.ring"
x = 10
while x > 0
print "x = " + x + nl
for t = 1 to 10
if t = 3
print "number three" + nl
endif
endfor
x--
endwhile
LoadSyntax "stylebasicoff.ring"
see "done" + nl
Note: files called by the LoadSyntax command must contains ChangeRingKeyword and ChangeRingOperator com-
mands only.
Tip: files called by the LoadSyntax command doesn’t support functions, packages and classes. just imperative
commands only.
Note: Using this feature you can create many styles that you can use in the same project and you can support Ring
Tip: The effect of LoadSyntax command is related to the current source code file only.
func hello()
see "Hello" + nl
func sum(x,y)
see x+y+nl
Output:
Hello
7
Example:
myfunc = func x,y { see x + y + nl }
call myfunc(3,4)
Output:
7
7
Output:
Hello
How are you?
Welcome to Ring
one
two
three
You can use any unicode character in the variable name also we can use $ and @ in the name.
This feature may help, for example we can start global variables with $ and the object attributes with @.
In other languages like Ruby this is the rule, In the Ring language this is just an option without any force from the
Compiler.
example:
$global_variable = 5
class test
@instance_variable = 10
func hello
local_variable = 15
Output:
Global : 5
Instance : 10
Local : 15
if you don’t like the ‘but’ keyword in if statement Then you can use the ‘elseif’ keyword.
Example:
give x
if x = 1 see "one"
elseif x=2 see "two"
elseif x=3 see "three"
elseif x=4 see "four"
else see "other"
ok
see nl
if you don’t like the ‘other’ keyword in switch statement Then you can use the ‘else’ keyword.
Also you can replace ‘else’ with ‘other’ in if statement.
i.e. ‘other’ keyword is the same as ‘else’ keyword.
Example:
x = 1
switch x
on 10
see "10" + nl
else
see "not 10" + nl
end
Output:
not 10
switch x
on 1 see "one" + nl
on 2 see "two" + nl
end
Output:
if statement..
one
for loop..
12345678910
switch...
one
try catch...
catching error
switch x {
on 1 see "one" + nl
on 2 see "two" + nl
}
63.10. Using braces to start and end different control structures 737
Ring Documentation, Release 1.6
x = 1 / 0
catch
see "catching error" + nl
}
Output:
if statement..
one
for loop..
12345678910
switch...
one
try catch...
catching error
We can replace the ‘on’ keyword with ‘case’ keyword in the switch statement.
Example (1) :
for x=1 to 10
switch x
case 1 put "one" + nl
case 2 put "two" + nl
case 3 put "thre" + nl
else put "else" + nl
end
end
Example (2) :
for x=1 to 10 {
switch x {
case 1 put "one" + nl
case 2 put "two" + nl
case 3 put "thre" + nl
else put "else" + nl
}
}
We can use the ‘def’ keyword as the ‘func’ keyword to define functions and methods.
Example:
one() two()
Example:
load "stdlib.ring"
import mypackage
new myclass {
myfunc()
}
package mypackage
{
class myclass
{
func myfunc
{
print("Hello, World!\n")
}
}
}
Example:
import mypackage
new myclass {
myfunc()
}
package mypackage
class myclass
def myfunc
put "Hello, World!"
end
end
end
Example:
import mypackage
package mypackage
class myclass
func myfunc
see "welcome" + nl
endfunc
endclass
endpackage
SIXTYFOUR
Using this library we can add the type information to the source code which will be very useful for tools like
• Code Editors
• Static-Analysis
Note: Ring is a dynamic language, No type checking will be done by the compiler.
64.2 Example
see sum(3,4) + nl ;
see sayHello("Mahmoud");
The Type Hints library is very powerful and will support user types (Classes) automatically
Example:
load "typehints.ring"
import mypackage
741
Ring Documentation, Release 1.6
test() { main([:one,:two,:three]) }
package mypackage {
public class myclass {
public static void func main(list args) {
see "welcome" + nl
see args
}
}
}
Also you can use the types inside the code (not only the function prototype)
Example:
load "typehints.ring"
64.5 Rules
• To use the types in the function prototype you must use ‘(‘ and ‘)’ around parameters
• To use the types in the function code, You must set the variable value (Assignment).
The next types are defined by the library
# Low Level Types
char
unsigned
signed
int
short
long
float