Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile

Syntax Flexibility in the Ring Programming Language

5.00/5 (5 votes)
10 Oct 2016CPOL5 min read 20K   71  
The Ring programming language comes with different styles for I/O and Control Structures and support changing the language keywords and operators.

 

Introduction

Each programming language comes with it's style and syntax, where the programmer need to know about the language keywords, operators and rules. After many years of programming each programmer maybe used a lot of programming languages with different styles. At this stage each programmer may prefer a different syntax and find that this syntax is more natural, simple and/or productive.

Why the programming language force the programmer to use a specific syntax?

* Easy to Implement.

* Faster Implementation.

* Easy to read and understand programs written in the language.

What are the advantages/disadvantage of each different syntax? 

* Some styles are easy to write.

* Some styles are natural (easy to read and understand)

Why some programmers like/dislike some languages because of it's syntax?

* Because they know another style that match their expectation in a better way.

What is your favorite syntax and why?

* I prefer natural styles that uses special keywords but I discovered that other styles are faster to write.

Note : Fell free to answer the previous questions and add more ideas/knowledge from your experience.

Background

As someone who used many programming language during the last years, I can say that some languages are simlar and some languages are very different. Many languages share the same concepts, Solve the same problem and use the same programming paradigms, but use a different syntax, tricks, libraries, frameworks and tools. To be a good programmer you may need to learn about different languages that uses different programming paradigms and let you think different about how to solve your problems in a better way. 

In this article i will focus on one thing (The Syntax) and one programming language (The Ring Programming Language) and how this language provide different styles in the Syntax and enable the programmer to change the language keywords and operators. How to use different syntax styles in the same program. When and why this maybe useful. (You can download Ring from : http://ring-lang.net and the language source code from : https://github.com/ring-lang/ring )

Some other languages may use Preprocessor to support something like that. Some other language may use Syntax Sugar and Operator Overloading to work around this, But the Ring programming language comes with it's solution in a simple and direct way so you can do something useful from these features. 

The Story

When I released Ring 1.0 in 2016.01.25, a lot of my friends (developers and programmers using many different languages) said that (I like the language concepts but I'm not very happy with the language syntax, could you change the syntax?). I asked them why? each one provided different answer like

* I want to use another keywords!

* I prefer braces { } over writing a specific keyword to end each control structure.

* I prefer using the 'end' keyword to close each control structure.

* I want to use Arabic keywords.

And many other things like that. Each one like the Syntax of a different language and want to use the Ring language concepts but still happy with his/her favorite syntax.

Using the code

The next program presents the "Hello, World!" program in the Ring language version 1.0

C++
See "Hello, World!" + nl

The See keyword is used to print some output on the screen. Ring 1.0 uses See and Give for printing the output and getting the input from the user.

Ring 1.1 support another style too like

C++
Put "Hello, World!" + nl

So using 'Put' and 'Get' we can print the output and get the Input.

Another style also provided by Ring 1.1

C++
load "stdlib.ring"

print("Hello World")

So the first style (See/Give) was the start (trying to be natural to users), The second style (Put/Get) was an update (Trying to be more natural to native English speakers), The third style, Using stdlib.ring and print() function trying to be natural to programmers (Libraries/Functions instead of commands).

So what is the difference ?

(1) : Using See/Put is faster than using print() because the command comes with a special instruction in the Virtual Machine instructions set. While the print() function just call the See/Put command so we have the overhead of calling a function before calling the print instruction using See/Put.

(2) : The print() function is written in Ring, so you can easily modify this function.

(3) : The print() function accept escape characters like "\n" and "\t" while the See/Put commands doesn't provide this feature (more faster).

Now let us see the different styles for control structures

The First Style (Special Keywords to end each control structure)

C++
if Expression
        Block of statements
but Expression
        Block of statements
else
        Block of statements
ok


switch Expression
on Expression
        Block of statements
other
        Block of statements
off

while Expression
        Block of statements
end

for identifier=expression to expression [step expression]
        Block of statements
next

for identifier in List/String  [step expression]
        Block of statements
next

do
        Block of statements
again expression

The Second style (Use the end keyword to close each control structure)

C++
if Expression
        Block of statements
elseif Expression
        Block of statements
else
        Block of statements
end

switch Expression
case Expression
        Block of statements
else
        Block of statements
end

while Expression
        Block of statements
end

for identifier=expression to expression [step expression]
        Block of statements
end

for identifier in List/String  [step expression]
        Block of statements
end

try
        Block of statements
catch
        Block of statements
end

The third style (uses braces)

C++
if Expression {
        Block of statements
elseif Expression
        Block of statements
else
        Block of statements
}

switch Expression {
case Expression
        Block of statements
else
        Block of statements
}

while Expression {
        Block of statements
}

for identifier=expression to expression [step expression] {
        Block of statements
}

for identifier in List/String  [step expression] {
        Block of statements
}

To change the language keywords

C++
ChangeRingKeyword  <oldkeyword>  <newkeyword>

To change the language operator

C++
ChangeRingOperator  <oldoperator>  <newoperator>

Example:

C++
changeringoperator + plus
changeringkeyword SEE PRINT

Print 5 plus 5

You may store a group of ChangeRingKeyword and ChangeRingOperator commands in a file to use later in many source files.

File : StyleBasicOn.ring

C++
ChangeRingKeyword       see     print
ChangeRingKeyword       ok      endif
ChangeRingKeyword       next    endfor
ChangeRingKeyword       end     endwhile

File : StyleBasicOff.ring

C++
ChangeRingKeyword  print        see
ChangeRingKeyword  endif        ok
ChangeRingKeyword  endfor       next
ChangeRingKeyword  endwhile     end

Example:

C++
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

Output:

C++
x = 10
number three
x = 9
number three
x = 8
number three
x = 7
number three
x = 6
number three
x = 5
number three
x = 4
number three
x = 3
number three
x = 2
number three
x = 1
number three
done

 

Note (1) : You can mix between styles in the same file.

Note (2) : Each file in the project may uses a different style

Note (3) : You can use eval() to execute code inside a string and change keywords/operators while using eval() only.

Points of Interest

(1) We can translate the language (English Keywords) to other languages and use for example Arabic/French Keywords.

(2) We can port code written in other languages to Ring and support these languages from Ring (other features are provided to help in doing this but not covered in this article to keep it small and simple).

(3) We can mix between styles in the same project where each programmer may use his/her favorite style in his/her code. This may help when each programmer work on his/her tasks alone like developing a library that others may use but not modify.

(4) Each team may use one style for the project, but this style is defined based on customization that match this team sprite.

(5) You can test different styles in the same language. This will give you better understanding about each style (Advantages and Disadvantages).

History

Ring 1.0 is released on 2016.01.25 with one syntax (no syntax flexibility).

Ring 1.1 is released on 2016.10.06 with many new features includes (Syntax Flexiblity).

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)