Created
March 20, 2023 12:34
-
-
Save Irwin1985/16be595f24d65f43cb17dc9292aa1f65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Learn Ring in 5 minutes | |
Author: Irwin Rodríguez <[email protected]> | |
Date: 2023-03-19 22.14 | |
Ring is a general purpose programming language used to create | |
portable software like Console, GUI, Web, Games, etc. | |
IMPORTANT: The language can support 3 styles of writting code so let's | |
learn all of them at once in every example then you can mix them or just | |
stick with your favorite. | |
*/ | |
#-----------------------------------------------------------------------# | |
# 1. COMMENTS | |
#-----------------------------------------------------------------------# | |
// This is a c-style comment that ends ultil we reach the end of line. | |
/* | |
And this is the multi-line version | |
You can use this style to document your functions. | |
*/ | |
# This is a python/ruby style comment for single line. | |
# Printting to the console. | |
see "Hello World!" | |
put "Hello World!" | |
print("Hello world!") | |
# Print and then new line | |
? "Hello World!" | |
puts("Hello World!") | |
# All keywords and identifiers are case-insensitive | |
SEE "HELLO WORLD IN CAPS!" | |
PUT "HELLO WORLD IN CAPS 2!" | |
# Multi-line string | |
see | |
" | |
Hello | |
Welcome to the Ring programming language | |
" | |
# String concatenation | |
See "Hello" + nl + "Welcome to the Ring programming language" + | |
nl + "How are you?" + nl | |
# NOTE: nl is a special constant that represents a new line so dont overrite it! | |
#-----------------------------------------------------------------------# | |
# 2. GETTING INPUT | |
#-----------------------------------------------------------------------# | |
See "Please input your name: " | |
Give cName # reads a string from console | |
Put "Please input your favorite sport: " | |
Get cSport # reads a string from console | |
Print("Please input your favorite color: ") | |
cColor = getString() # reads a string from console | |
# Preparing the output | |
puts("-----------YOUR DATA--------------") | |
puts("NAME: " + cName) | |
puts("SPORT: " + cSport) | |
puts("COLOR: " + cColor) | |
# Preparing the output with print2str() | |
cOutput = print2str("NAME: #{cName}\nSPORT: #{cSport}\nCOLOR: #{cColor}\n") | |
puts("-----------YOUR DATA--------------") | |
puts(cOutput) | |
# Get number from console | |
print("How old are you? ") | |
nAge = getNumber() # try parse the text read from input into number. | |
? "Your age: " + nAge | |
# Get a single char | |
print("So far Ring is fascinating don't you think? y/n?") | |
cAnswer = getChar() | |
if cAnswer = 'y' | |
puts("I think so...") | |
else | |
puts("May be I asked you too soon...") | |
end | |
#-----------------------------------------------------------------------# | |
# 3. VARIABLES | |
#-----------------------------------------------------------------------# | |
# Just provide a name and a value and that's it :) | |
x = "Hello" | |
puts("x is a " + type(x) + " variable ") | |
x = 5 | |
puts("x is a " + type(x) + " variable ") | |
x = 1.2 | |
puts("x is a " + type(x) + " variable ") | |
x = [1,2,3,4] | |
puts("x is a " + type(x) + " variable ") | |
x = date() | |
puts("x is a " + type(x) + " variable ") | |
x = time() | |
puts("x is a " + type(x) + " variable ") | |
x = true | |
puts("x is a " + type(x) + " variable ") | |
x = false | |
puts("x is a " + type(x) + " variable ") | |
#-----------------------------------------------------------------------# | |
# 4. DEEP COPY | |
#-----------------------------------------------------------------------# | |
list = [1,2,3] | |
puts(list) | |
copy = list | |
puts(copy) | |
# what if we modify the copy? | |
copy[1] = 20 | |
puts(copy) | |
# and let's see the list | |
puts(list) # the original remains with no change, this is called 'deep copy' | |
#-----------------------------------------------------------------------# | |
# 5. IMPLICIT CONVERSION | |
#-----------------------------------------------------------------------# | |
puts("This is a string but " + 20 + " it was a number :)") | |
puts("1985" + 37) | |
#-----------------------------------------------------------------------# | |
# 6. OPERATORS | |
#-----------------------------------------------------------------------# | |
5 + 5 # Add | |
5 - 5 # Subtract | |
5 * 5 # Multiply | |
5 / 5 # Divide | |
5 % 2 # Modulus | |
5++ # Increment | |
5-- # Decrement | |
#-----------------------------------------------------------------------# | |
# 7. RELATIONAL OPERATORS | |
#-----------------------------------------------------------------------# | |
5 = 5 # Equality | |
5 != 5 # Not equal | |
5 > 5 # Greater than | |
5 < 5 # Less than | |
5 >= 5 # Greater or Equal | |
5 <= 5 # Less or Equal | |
#-----------------------------------------------------------------------# | |
# 8. LOGICAL OPERATORS | |
#-----------------------------------------------------------------------# | |
true and false # Logical AND | |
true && false # Logical AND | |
true or false # Logical OR | |
true || false # Logical OR | |
not true # Logical NOT | |
!true # Logical NOT | |
#-----------------------------------------------------------------------# | |
# 9. ASSIGNMENT OPERATORS | |
#-----------------------------------------------------------------------# | |
x = 10 # Assignment | |
x += 1 # Add AND assignment | |
x -= 1 # Subtract AND assignment | |
x *= 1 # Multiply AND assignment | |
x /= 1 # Divide AND assignment | |
x %= 1 # Module AND assignment | |
#-----------------------------------------------------------------------# | |
# 10. OTHER OPERATORS | |
#-----------------------------------------------------------------------# | |
/* | |
:literal # Using ':' befor identifier means 'literal' | |
Start:End # Create a list from Start to End | |
[list items] # Define list items | |
list[index] # Access a list item | |
obj.name # Access a property from an object | |
obj {stmts} # Execute statements with direct access to object props. | |
func(params) # Call a function with parameters separated by comma. | |
? <expr> # Print expressions and then new line. | |
*/ | |
#-----------------------------------------------------------------------# | |
# 11. CONTROL FLOW | |
#-----------------------------------------------------------------------# | |
#---------------------# | |
# IF STATEMENT | |
#---------------------# | |
See | |
" | |
Main Menu | |
--------- | |
1. Say Hello | |
2. About | |
3. Exit | |
" | |
nOption = getNumber() | |
if nOption = 1 | |
see "Enter your name: " give cName puts("Hello " + cName) | |
but nOption = 2 | |
puts("Sample: using if statement") | |
but nOption = 3 | |
bye | |
else | |
puts("bad option...") | |
ok | |
# Another If style. | |
nAge = 18 | |
if nAge <= 10 | |
puts("You are a child!") | |
elseif nAge > 11 and nAge <= 17 | |
puts("You cannot pass!") | |
else | |
puts("You can pass!") | |
end | |
# Another If style (NOTE the opening and closing braces surrounding all branches) | |
if nAge <= 10 { | |
puts("You are a child!") | |
elseif nAge > 11 and nAge <= 17 | |
puts("You cannot pass!") | |
else | |
puts("You can pass!") | |
} | |
#---------------------# | |
# SWITCH STATEMENT | |
#---------------------# | |
# true here is a dummy test because we evaluate the condition in every branch | |
switch true | |
on nAge <= 10 | |
puts("You are a child!") | |
on nAge > 11 and nAge <= 17 | |
puts("You cannot pass!") | |
other | |
puts("You can pass!") | |
off | |
# Second way of switch | |
switch true | |
case nAge <= 10 | |
puts("You are a child!") | |
case nAge > 11 and nAge <= 17 | |
puts("You cannot pass!") | |
else | |
puts("You can pass!") | |
end | |
# Third way of switch | |
switch true { | |
case nAge <= 10 | |
puts("You are a child!") | |
case nAge > 11 and nAge <= 17 | |
puts("You cannot pass!") | |
else | |
puts("You can pass!") | |
} | |
#---------------------# | |
# WHILE LOOP | |
#---------------------# | |
nCounter = 0 | |
while nCounter < 10 | |
puts(nCounter) | |
nCounter++ | |
end | |
# Another way of WHILE | |
nCounter = 0 | |
while nCounter < 10 { | |
puts(nCounter) | |
nCounter++ | |
} | |
#---------------------# | |
# FOR LOOP | |
#---------------------# | |
nCounter = 0 | |
for nCounter = 0 to 10 | |
put(nCounter) | |
next | |
# Second style of FOR | |
nCounter = 0 | |
for nCounter = 0 to 10 | |
put(nCounter) | |
end | |
nCounter = 0 | |
for nCounter = 0 to 10 { | |
put(nCounter) | |
} | |
#---------------------# | |
# FOR IN | |
#---------------------# | |
for i in [1:10] | |
puts(i) | |
end | |
#---------------------# | |
# USING THE STEP | |
#---------------------# | |
for nCounter = 10 to 20 step 2 # from 10,12,14,... | |
puts(nCounter) | |
end | |
#---------------------# | |
# DO AGAIN | |
#---------------------# | |
nCounter = 1 | |
do | |
puts(nCounter) | |
nCounter++ | |
again nCounter < 10 | |
#---------------------# | |
# EXIT COMMAND | |
#---------------------# | |
for nCounter = 1 to 10 | |
if nCounter = 5 | |
exit | |
end | |
next | |
#---------------------# | |
# EXCEPTIONS | |
#---------------------# | |
try | |
a = a + b # a does not exists | |
catch | |
puts("ERROR: " + cCatchError) # cCatchError contains the message. | |
end | |
# version 2 of try (you know, the '{' and '}') | |
try { | |
b = b + c | |
catch | |
puts("ERROR: " + cCatchError) | |
} | |
#-----------------------------------------------------------------------# | |
# 12. FUNCTIONS | |
#-----------------------------------------------------------------------# | |
# calling the function | |
sayHello() | |
# function declaration (no extra keyword to delimite the body) | |
func sayHello | |
puts("Saying Hello as you ordered!") | |
printNames("Peter", "John") | |
# parameters. Note the parentheses are not requiered. | |
func printNames pcName1, pcName2 # pc is just a convention for string param. | |
puts("NAMES: " + pcName1 + ", " + pcName2) | |
#---------------------# | |
# SECOND STYLE | |
#---------------------# | |
sayHelloAgain() | |
def sayHelloAgain | |
puts("Saying hello again as you said...") | |
end # end here is optional but for redability you can add it. | |
#---------------------# | |
# THIRD STYLE | |
#---------------------# | |
sayHelloLastTime() | |
func sayHelloLastTime { | |
puts("pufff! I'm tired of this... Hello world!") | |
} | |
#-----------------------------------------------------------------------# | |
# 13. PROGRAM STRUCTURE | |
#-----------------------------------------------------------------------# | |
# The proper way to structure a program is: | |
# 1. Load libraries | |
# 2. Statements and global variables | |
# 3. Functions | |
# 4. Packages and Classes | |
/* | |
Example: | |
1. create a routines.ring and put this: | |
func sayHello(cName) | |
puts("Hi, " + cName) | |
2. create a sample.ring and put this: | |
load "routines.ring" # this will load all the content of routines.ring | |
sayHello("Peter") | |
*/ | |
#-----------------------------------------------------------------------# | |
# 14. LISTS | |
#-----------------------------------------------------------------------# | |
aList = [1, 2, 3, 4, 5] # list literal | |
puts(aList) | |
aList = [1:5] # list literal using the [Start:End] notation | |
puts(aList) | |
aList = ["a":"z"] # a character sequence | |
puts(aList) | |
aList = list(10) # 10 spaces | |
puts(aList) | |
aList = list(3, 2) # bidimensional list (3 columns and 2 rows) | |
puts(aList) | |
# Add to a list | |
aList = ["one", "two"] | |
Add(aList, "Three") | |
puts(aList) | |
# Overload the '+' operator to add an item to a list | |
aList + "Four" | |
puts(aList) | |
# Get list size | |
len(aList) | |
# Delete an item | |
del(aList, 1) # delete the first element | |
# Get an item from list | |
puts(aList[1]) # get the first element | |
# Finding an item in list | |
puts(Find(aList, "two")) # 1 | |
#-----------------------------------------------------------------------# | |
# 15. STRINGS | |
#-----------------------------------------------------------------------# | |
cStr = "Double quoted string" | |
cStr = 'Single quoted string' | |
cStr = :ColonNotationString | |
cStr = `Backticked string` | |
len(cStr) # get the length of a string. | |
puts(cStr[1]) # accessing a single character from a string. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment