أهلا ومرحبا بكم جميعا
نكمل كلامنا عن البرمجة الطبيعية بلغة رينج Ring أي الخاتم Ring الذي يرتبط اختياره واستخدامه بالمستخدم نفسه.فالبرمجة بلغة المستخدم هي التي تجعله يقوم بالبرمجة بكامل الحرية دون قيود ال Syntax وهي التي تسهل عليه المفاهيم البرمجية وليس حفظ ال Syntax.
تحدثنا في المقالة السابقة عن البرمجة الطبيعية Natural programming وتعرفنا على المقدرة التي أعطتها اللغة Ring للمبرمج للتحكم في البرمجة، حيث يقوم بكتابة ما يشاء وبدون ان يؤثر ذلك على سرعة التنفيذ.واليوم نتعرف علي مكتبة لتلك اللغة ال Natural لتسهل علينا طريق البرمجة أكثر وأكثر.
مثال توضيحي علي ما سوف نقوم بتنفيذه
Using the Natural Library
بعد تحميل اللغة “اخر اصدار” من موقع اللغة
يمكنك الاطلاع على المكتبة “naturallib.ring” الخاصة ب Natural Language Programming من المسار التالي: Ring_1.5.4_All_Platforms\ring\ringlibs\naturallib
وتحتوي المكتبة naturallib.ring على ثلاث مكتبات:
- naturalbase.ring
- naturallanguage.ring
- naturalcommand.ring
- أولا المكتبة natural base. Ring
تحتوي methods خاصة بال expressions وال commands.
- ثانيا المكتبة NaturalCommand.ring
تحتوي على ال DefineNaturalCommand” Object” لتحديد نوع ال syntax الخاص بال Commands.
الأنواع المختلفة لل Syntax:
3.ثالثا المكتبة naturallanguage.ring
تحتوي على Class باسم NaturalLanguage وMethods تستخدم في المشروع التنفيذي وسوف نتعرف بالتفصيل علي كيفية استخدام تلك ال Methods.
الجدول التالي يوضح ال methods الموجودة في المكتبة:
خطوات عمل مشروع ب Natural Language مع استخدام مكتبة ال Natural؟
1–نقوم بتعريف المكتبة عن طريق الامر Load واسم المكتبة.
"Load "naturallib.ring
2-نقوم بعمل ملف تنفيذي يتم فيه تعريف المكتبة وObject من ال Class باسم NaturalLanguage
يتم فيه تعريف ما ذكرناه من ال Methods التالية:
- (SetLanguageName(cLanguageName
- (setCommandsPath(cFolder
- (SetPackageName(cPackageName
- (UseCommand(cCommandName
- (SetOperators(cOperators
- (RunFile(cFileName
- (RunString(cString
ملحوظة:
- نستخدم ال RunFile(cFileName) method عند كتابة الكود داخل ملف نصي”txt.”
- نستخدم ال Runstring (” “) method لتنفيذ الكود من داخل سلسلة حرفية. ويختلف ما يكتب بداخلها باختلاف ال Syntax المستخدم.
3-نقوم بعمل ملف يتم فيه تعريف ال Commands أو ال Keywords.
بعد عمل الملف الخاص بال Commands نقوم بتعريف ال DefineNaturalCommand” Object” ونستخدم ال method الذي يحدد نوع ال Syntax الخاص بل Commands أو ال Keywords كما سنري في الامثلة.
ويكون التعريف كالتالي:
DefineNaturalCommand.Syntax_Type
وبداخل هذا ال method يتم فيه تعريف ثلاث parameters:
Package Name: بنفس الاسم الذي تم كتابته في الملف التنفيذي.
Keyword/Command: نكتب الكلمة أو الامر ويكون بنفس اسم ملف ال Command.
Anonymous Function: نكتب فيه الكود الذي نريد تنفيذه.
ملحوظة:
- ()Expr: تستخدم للوصول الي القيمة بعد ال Command.
- (Expr (1: يعني الوصول المباشر الي ال expression الأول بعد ال Command.
- (Expr (2: يعني الوصول المباشر الي ال expression الثاني بعد ال Command.
ملاحظات مهمة قبل البدء في الأمثلة التوضيحية:
1-عند كتابة الامر بداخل UseCommand يجب أن يكون الاسم بنفس اسم ملف ال Command.
2-تحديد اسم ال Syntax هو ما يترتب عليه ما يتم تعريفه داخل ال ()Runstring وكذلك ما نريد تنفيذه من Numbers وStringsو Expressions.
3-الفرق بين اختيار ال Syntax يكون Keyword أو Command هو عدد كلمات ال Command
- فاذا كان كلمة واحدة نستخدم ال syntax الخاص ب Keyword.
- وإذا كان مجموعة كلمات لابد أن نستخدم ال syntaxالخاص ب Command.
4–بالنسبة لل Keywords وليس ال Commands:
- يجب تسمية الملف باسم ال Keyword.
- فاذا كان ال Syntax هو Keyword فقط نكتب اسم ال Keyword فقط داخل ال RunString
- واذا كان ال Syntax هو Number يتم تعريف قيمة افتراضية رقمية بداخلة وليكن 0.
- وإذا كان ال Syntax هو Number Number يتم تعريف قيمتين افتراضيتين رقميتين بداخل ال Method وليكن 0 0.
- وإذا كان ال Syntax هو String يتم تعريف قيمة افتراضية حرفية بداخلة وليكن ` `.
- وإذا كان ال Syntax هو String String يتم تعريف قيمتين افتراضيتين حرفيتين وليكن “ “
5–بالنسبة لل Commands وليس ال Keywords:
- يجب تسمية الملف باسم ال Command.
- إذا كان ال Syntax عبارة عن Command فقط فيكتب داخل Runstring ال Command فقط.
وإذا كان ال Syntax عبارة عن Commandstring فيكتب داخل Runstring ال Command وقيمة ال ال expression بناء علي نوعه.
6-ال Expression يجمع بين ال number/string:
- فاذا تم تعريف ال commandexpression” Syntax” ، يعرض فقط expression واحد فقط بعد ال Command قد يكون حرفي أو رقمي. عن طريق (expr(1.
- واذا تم تعريف ال commandexpressionexpression” Syntax”، يعرض اثنين من ال expressions قد يكون حرفي أو رقمي. عن طريق (expr(1) expr(2.
7-ال Literal هو ما يكتب بعد النقتطين : ويكون عبارة عن كلمة واحدة أو لايحتوي علي مسافات.
8-يمكنك الفتح والتنقل للملفات بسهولة ويشكل سريع من خلال مستعرض ملفات المشروع project File في الجزء الايسر لمحرر الكود Code Editor الخاص باللغة.
الخطوات العملية:
نقوم بعمل عمل فولدر للمشروع في أي مكان على الجهاز.
وفي حالة انشاء ملف ال Commands في نفس مكان ال Source أو مع ملف ال Execution
فعندها يكون المسار في ال SetCommandsPath” method” كالتالي:
(()SetCommandsPath (CurrentDir
أو كتابة المسار كاملا لمكان تواجد ال Command كالتالي:
("setCommandsPath("C:\Test\NaturalLibrary\Command
أولا تنفيذ الأوامر عن طريق Runstring:
أمثلة توضيحية علي ال Commands:
مثال1 علي commands:
the syntax is command only#
the execution fileو"file name"mylanguage.ring#
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("thenameis
( "runstring( " the name is
{
the command file#
"the file name is "thenameis.ring#
])DefineNaturalCommand.SyntaxIscommand
,"Package = "MyLanguage.Natural:
,"command = "the name is:
}Function = func:
see "Hello"+nl+'tell me the name of this language'+nl
Give name
'see name+'!'+" "+'it is a beautiful language
{
([
مثال2 commandstring:
"the execution file name"mylanguage.ring#
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("thenameis
(" 'runstring( " the name is 'Ring
{
"the file name is "thenameis.ring#
])DefineNaturalCommand.SyntaxIscommandstring
,"Package = "MyLanguage.Natural:
,"command = "the name is:
}Function = func:
(see " The name of this language is "+expr (1
{
([
مثال3 commandstring:
the syntax is command string#
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("thenameis
( " 'runstring( " the name is 'ring programming language
{
])DefineNaturalCommand.SyntaxIscommandstring
,"Package = "MyLanguage.Natural:
,"command = "the name is:
}Function = func:
(see expr(1
{
([
مثال4 commandstringstring:
the syntax is command string string#
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("TheNameIs
(" 'runstring( " The Name Is 'ring' 'and it is creadted in 2016 and we wish to be no. 1
{
])DefineNaturalCommand.SyntaxIscommandstringstring
,"Package = "MyLanguage.Natural:
,"command = "the name is:
}Function = func:
see "you will see the expression no. 1 in the execution file"+nl
see" the name of this lang. is "+ expr(1)+nl
see "and after that i show the expression no. 2 in the execution file"+nl
(see expr(2
{
([
مثال5 Command Number:
the syntax is command number#
see i put a numeric value'0' after command #
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("thenameis
(" runstring( " the name is 0
{
])DefineNaturalCommand.SyntaxIscommandnumber
"Package = "MyLanguage.Natural:
"command = "the name is:
}Function = func:
see "Hello"+nl+'tell me the name of this language'+nl
Give name
'.see name+'!'+" "+'it is a beautiful language'+nl+'I see it need to be the era lang
{
([
the syntax is command number#
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
("SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("thenameis
("runstring( " the name is 1
{
])DefineNaturalCommand.SyntaxIscommandnumber
,"Package = "MyLanguage.Natural:
,"command = "the name is:
}Function = func:
see "Hello"+nl+'tell me the name of this language'+nl
Give name
"'.see name+'!'+" "+'it is a beautiful language'+nl+'I see it need to be the era lang
'and number '+expr(1)+' rather than others'+
{
([
مثال6 Command NumberNumber:
the syntax is command number number#
take the numbers after command and ignore the other keywords#
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("TheNameIs
("runstring( " The Name Is ring and it is creadted in 2016 and we wish to be no. 1
{
])DefineNaturalCommand.SyntaxIscommandnumbernumber
,"Package = "MyLanguage.Natural:
,"command = "the name is:
}Function = func:
see "you will see the expression no. 1 in the execution file"+nl
see" the date of birth of this lang. is "+ expr(1)+nl
see "and after that i show the expression no. 2 in the execution file"+nl
(see"the Ring lang. now one of the 100 top languages and we wish to be no. "+expr(2
{
([
مثال7 Command expression:
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("TheNameIs
("runstring( "The Name Is 'Ring' and we wish to be no. 1
{
])DefineNaturalCommand.SyntaxIscommandexpression
,"Package = "MyLanguage.Natural:
,"command = "the name is:
}Function = func:
(see " the name of this lang. is "+expr(1
{
([
مثال 8 command expression expression:
the syntax is command expressionexpression#
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("TheNameIs
("runstring( " The Name Is 'Ring' and we wish to be no. 1
{
])DefineNaturalCommand.SyntaxIscommandexpressionexpression
,"Package = "MyLanguage.Natural:
,"command = "the name is:
}Function = func:
(see " the name of this lang. is "+expr(1)+' and will be '+expr(2
{
([
أمثلة توضيحية علي ال Keywords:
مثال1 Syntax is Keyword:
Syntax is Keyword without expr#
"file name is "mylanguage.ring#
"load "stdlib.ring
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
(UseCommand(:Show
(" runstring(" Show
{
"file name is "shaow.ring#
})DefineNaturalCommand.SyntaxIskeyword
,"Package = "MyLanguage.Natural:
,"keyword = "show:
}Function = func:
[]=alist
}for y =1 to 10
alist+y
{
see alist
{
([
مثال2 Syntax is Keyword string:
Syntax is Keyword string with space only in string value#
"load "stdlib.ring
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
the name of the file#
("UseCommand("show
space ' ' ,as value for string#
(" ' 'runstring("show
{
])DefineNaturalCommand.SyntaxIskeywordstring
,"Package = "MyLanguage.Natural:
,"keyword = "show:
}Function = func:
[]=alist
}for y =1 to 10
alist+y
{
see alist
{
([
مثال3:
Syntax is Keyword string with string values#
Syntax is Keyword string#
"load "stdlib.ring
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
(()setCommandsPath(CurrentDir
("SetPackageName("MyLanguage.Natural
("UseCommand("show
(" ':runstring("show 'the numbers in alist
{
])DefineNaturalCommand.SyntaxIskeywordstring
,"Package = "MyLanguage.Natural:
,"keyword = "show:
}Function = func:
[]=alist
}for y =1 to 10
alist+y
{
see expr(1)+nl see alist
{
([
ثانيا تنفيذ الأوامر عن طريق Runfile:
مثال توضيحي:
text Files#
hello
'I say 'hello' to 'machine
I want a program to execute a division operation
of two numbers 1000 and 5 then add each result with 3000
'thanks 'ok thanks
"file name "mylanguage.ring#
"load "stdlib.ring
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:MyLanguage
("setCommandsPath("C:\Test\NaturalLibrary\Command
("SetPackageName("MyLanguage.Natural
"for file and command"hello.ring#
("UseCommand("hello
"for file and command"numbers.ring#
("UseCommand("numbers
("runfile("file.txt
{
"file name"hello.ring#
take the two expr after the keyword hello#
])DefineNaturalCommand.SyntaxIskeywordexpressionexpression
,"Package = "MyLanguage.Natural:
,"keyword = "hello:
}Function = func:
see expr(1)+" " +expr(2)+nl
{
([
"file name"numbers.ring#
take the two expr after the keyword numbers#
])DefineNaturalCommand.SyntaxIskeywordexpressionexpression
,"Package = "MyLanguage.Natural:
,"keyword = "numbers:
}Function = func:
(division_result=expr(1)/expr(2
see "result1=" + division_result+nl
{
([
ملحوظة:
- يمكن أخذ أي command او keyword من ملف نصي “txt.”شرط وجوده أولا في الملف التنفيذي الذي يحتوي على UseCommand وبعد تنفيذه من خلال Runfile ، يمكن إضافة بعض الخصائص الأخرى على ال Command مع الكود الموجود في الملف وذلك من خلال تنفيذه في ملف اخر.
"Load"executionFile
()objectNameInExecutionFile.RunString
}()objectNameInExecutionFile {RunString
مثال توضيحي:
"file name "exefile2.ring#
"load"mylanguage.ring
")Mylanguage.runstring
"will execute the the file"mylanguage.ring" then two expressions in the code of files "numbers and hello#
numbers 100 25
numbers 10 2
numbers 4 2
hello 'welcome' ' '
("
ويمكن أضافة بعض ال Operators في كتابة الكود مع تجاهل وجودها حتى لا تؤثر على ال Syntax
من خلال ال SetOperators() :Method
أمثلة على ال Operators: (“() & $ @ >> +:”)
مثال توضيحي:
"file name "exefile2.ring#
SetOperators#
"load"mylanguage.ring
}Mylanguage
(" () , @ & ")SetOperators
")runstring
(numbers (100, 25
(numbers (10 ,2&
(numbers (4 ,2&
'' @ 'ends with hello @'welcome&
("
{
Defining commands using classes
في كثير من الاحيان نحتاج الي أن نستخدم ال Class حتى يتسنى لنا استخدام ال Functions أو Methods كما نريد ويمكن أن نستخدمها لتعطي نفس النتيجة بالطريقة الاولي ولكنها تفصيلية أكثر من الاولي.
هي نفس طريقة سريان الكود كما ذكرناه في المقالة السابقة ولكن نستخدم فيها مكتبة ال Natural لتسهل علينا الكتابة ويجب أن نلتزم بال Methods الموجودة في المكتبة. ولكن كما قلنا إن اللغة لا تقيدك أبدا فاللغة Open Source وتسمح لك بإمكانية التطوير في المكتبات.
يمكنك أن تفتح المكتبة كما وضحنا في بداية المقالة وتطلع على ال Methods ومن أهمها ما يوضحه الجدول التالي:
الخطوات
- نستخدم ال Package المعرفة في ملف التنفيذي ثم نعرف Class باسم ال Command و نضيف attribute باسمه
والطريقة المستخدمة في المكتبة تكون كالتالي:
func AddAttributes_CommandName
("AddAttribute(self,"CommandName
- نعرف ال CommandData في دالة ال getter
Func getCommandName
()StartCommand
"CommandData () [: name] = "CommandName
CommandData () [: nExpr] = 0
[]=[CommandData () [: aExpr
- ثم نستخدم دالة ال braceexpreval للحصول على عدد معين من ال expressions.
ولتعريف دالة ال braceexpreval مع المكتبة تكون كالتالي:
func BraceExprEval_CommandName nValue
search for command with the name of the class and attribute name#
}If isCommand () and CommandData () [: name] =: CommandName
test if expression is number or not#
}(If isNumber (nValue
2_remain storing expressions in list to allow using them untill expr#
++[CommandData () [: nExpr
CommandData () [: aExpr] + nValue
}If CommandData () [: nExpr] = 2
()functionNameOfCodeExecution
استخدمت هنا 2expressions فقط ويمكن استخدام أكثر من اثنين كما سنوضح في الأمثلة:
ملحوظة:
ينبغي مراعاة {اسم ال Command و _} عند تعريف ال methods وال attributes لتكون بنفس اسم تعريفها في المكتبة مثل:
BraceExprEval_CommandName
AddAttributes_CommandName
أمثلة توضيحية:
Commands with class and functions#
we use the same text file "file.txt" but by class and functions#
"the execution file "exefile2.ring"#
Package MyLanguage.Natural
Class numbers
func getnumbers
()startcommand
"commandData()[:name]="numbers
commandData()[:nExpr]=0
[]=[commandData()[:aExpr]
func braceexpreval_numbers nValue
}"if isCommand() and commandData()[:name]="numbers
}(if isNumber(nvalue
++[commandData()[:nExpr
commandData()[:aExpr]+nvalue
}if commandData()[:nExpr]=2
()Execute
{
{
{
func AddAttributes_numbers
("AddAttribute(self,"numbers
func Execute
(division_result=expr(1)/expr(2
see "result1=" + division_result+nl
,يمكن الحصول علي العديد من ال Expressions كما في المثال التالي:
"file name is "numbers.ring#
Package MyLanguage.Natural
Class numbers
func getnumbers
()startcommand
"commandData()[:name]="numbers
commandData()[:nExpr]=0
[]=[commandData()[:aExpr]
func braceexpreval_numbers nValue
}"if isCommand() and commandData()[:name]="numbers
}(if isNumber(nvalue
remain add numeric expressions to list untill expr3 then i can used them in the code#
++[commandData()[:nExpr
commandData()[:aExpr]+nvalue
}if commandData()[:nExpr]=3
()execute1
{
{
{
func AddAttributes_numbers
("AddAttribute(self,"numbers
func Execute1
(division_result=expr(1)/expr(2
see"result1=" + division_result+nl
(add=division_result+expr(3
see"division_result+expr(3)="+add +nl
"file name is "thanks.ring#
Package MyLanguage.Natural
class thanks
func AddAttributes_Hello
(AddAttribute(self,:thanks
func Getthanks
See "ok" + nl
"file name is "mylanguage.ring#
"load "stdlib.ring
"load "naturallib.ring
}MyLanguage = New NaturalLanguage
(SetLanguageName(:Language
("setCommandsPath("C:\Test\NaturalLibrary\Command
("SetPackageName("MyLanguage.Natural
("UseCommand("numbers
("UseCommand("thanks
("runfile("file.txt
{
"file name is "exefile2.ring#
"load"mylanguage.ring
}Mylanguage
(" (), &")SetOperators
")runstring
use the three file numbers'expr #
(numbers(100, 25,1
(numbers(10 ,2,2)&
(numbers(4 ,2,3)&
("
{
وكذلك يمكن الحصول علي العديد من ال expressions الحرفية كما في المثال التالي:
"file name is "say.ring#
expressions for strings#
Package MyLanguage.Natural
class say
func getsay
()startcommand
"commandData()[:name]="say
commandData()[:nExpr]=0
[]=[commandData()[:aExpr
func braceexpreval_say cvalue
}"if iscommand() and commandData()[:name]="say
search for the string expressions and stored them in list untill forth expr#
}(if isstring(cvalue
commandData()[:aExpr]+cvalue
++[commandData()[:nExpr
}if commandData()[:nExpr]=4
()execute
{
{
{
func AddAttributes_say
(AddAttribute(self,:say
func execute
See expr(4)+nl
والان قد وصلنا الي نهاية مقالتنا ونأسف علي الاطالة
وشكرا لكم والي اللقاء