This shows you the differences between two versions of the page.
alf:teme:tema3_en_draft [2018/03/25 21:48] alexandru.radovici |
alf:teme:tema3_en_draft [2019/04/03 10:25] (current) alexandru.radovici |
||
---|---|---|---|
Line 4: | Line 4: | ||
<note important> | <note important> | ||
- | Deadline: **15th of April, 23:55**\\ | + | Deadline: **7th of April, 23:55**\\ |
Points: **2 points** out of the final grade\\ | Points: **2 points** out of the final grade\\ | ||
Upload the homework: [[https://vmchecker.cs.pub.ro|vmchecker.cs.pub.ro]]\\ | Upload the homework: [[https://vmchecker.cs.pub.ro|vmchecker.cs.pub.ro]]\\ | ||
Line 26: | Line 26: | ||
* add the parser rules for the loops | * add the parser rules for the loops | ||
* add the parser rules for the function definitions | * add the parser rules for the function definitions | ||
- | * add the parser rules for the function run | + | * add the parser rules for the function call |
* add the parser rules for the branches and loops | * add the parser rules for the branches and loops | ||
* add the parser rules for the array and struct | * add the parser rules for the array and struct | ||
Try running small scripts to test every feature. | Try running small scripts to test every feature. | ||
- | |||
- | <hidden> | ||
- | ===== Syntax highlighting ===== | ||
- | If you use sublime text, you may use this [[https://github.com/alexandruradovici/alf2018/blob/master/Devoirs/alf/verify/alf.tmLanguage|syntax highlighting file]] to write Alf language. | ||
- | |||
- | You may [[http://stackoverflow.com/questions/12785583/add-ts-language-files-to-sublime-text-to-support-additional-syntax-coloring|read]] here how to add the file. | ||
- | </hidden> | ||
===== AST Format ===== | ===== AST Format ===== | ||
Line 45: | Line 38: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"node_id" | + | id:"node_id", |
+ | line: "the line where the instruction is in the file, starting at 1" | ||
} | } | ||
</code> | </code> | ||
+ | |||
+ | <note> | ||
+ | All the nodes have //id// and //line//. | ||
+ | </note> | ||
The list of the node ids is: | The list of the node ids is: | ||
Line 54: | Line 52: | ||
* exp - any expression | * exp - any expression | ||
* set - setting the value of a variable | * set - setting the value of a variable | ||
- | * message - message definition | + | * function - function definition |
- | * class - call definition | + | * struct - structure definition |
* property - a property of a class | * property - a property of a class | ||
- | * list - list definition | + | * array - array definition |
* element - an element of an array | * element - an element of an array | ||
* if - if statement | * if - if statement | ||
- | * while - while statement | + | * loop - while statement |
- | * repeat - repeat statement | + | |
* for - for statement | * for - for statement | ||
- | * dispatch - send a message | + | * loop ... if - for statement |
+ | * function_call - call (run) a function | ||
* return - value of a function | * return - value of a function | ||
- | * value - a value (int, real, logic, float, user_defined) | + | * value - a value (integer, logic, float, user_defined) |
+ | |||
+ | ===== Questions ===== | ||
+ | |||
+ | If you have any questions related to the homework, please ask them by posting an issue on the github [[https://github.com/upb-fils/alf|repository]] with the title format //[alf] <your question title>//. You will need a github account for that. | ||
+ | |||
+ | <note warning> | ||
+ | **DO NO POST ANY CODE**. This is considered copying and will result in a 0p homework for you. | ||
+ | </note> | ||
+ | |||
+ | If you want to receive an email when issues are posted or when there are new messages, got to the github [[https://github.com/upb-fils/alf|repository]] and click //Watch//. | ||
+ | | ||
===== ALF ===== | ===== ALF ===== | ||
Line 71: | Line 80: | ||
* variable definitions | * variable definitions | ||
- | * message definitions | + | * function definitions |
- | * class definitions (new data type) | + | * struct definitions (new data type) |
- | * list definitions (new data type) | + | * array definitions (new data type) |
* expressions | * expressions | ||
* values | * values | ||
* attributions | * attributions | ||
- | * message sending | + | * function call |
* branch (if) | * branch (if) | ||
- | * loop (while , for or repeat) | + | * loop (loop , for) |
<note> | <note> | ||
- | The instructions are separated by newline. | + | The instructions are separated by ;. |
<code> | <code> | ||
- | a:=10 | + | a=10; |
- | b:=20l | + | b=20; |
- | s:=55 | + | s=55; |
</code> | </code> | ||
</note> | </note> | ||
Line 97: | Line 106: | ||
Values are | Values are | ||
* integer number <code>7</code> | * integer number <code>7</code> | ||
- | * real number <code>7.5</code> | + | * float number <code>7.5</code> |
- | * character <code>"a"</code> | + | * symbol <code>"a"</code> |
* string <code>"this is a text"</code> | * string <code>"this is a text"</code> | ||
* logic (value true or false) <code>false</code> | * logic (value true or false) <code>false</code> | ||
Line 120: | Line 129: | ||
<code> | <code> | ||
{this is a text to describe the script} | {this is a text to describe the script} | ||
- | define v:=55 | + | def v:integer=55; |
</code> | </code> | ||
Line 164: | Line 173: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/1_value|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/1_value|Example]] |
==== Variable definition ==== | ==== Variable definition ==== | ||
Line 171: | Line 180: | ||
<code> | <code> | ||
- | define variable_name [:variable_type] [:= value or expression], ... | + | def variable_name [:variable_type] [= value or expression], ...; |
+ | def variable_name, variable_name, variable_name, ... :variable_type, ....; | ||
</code> | </code> | ||
=== Examples === | === Examples === | ||
<code> | <code> | ||
- | define a:integer := 3 | + | def a:integer = 3; |
- | define a := 3 | + | def a = 3; |
- | define a1:integer:=3, a2:string := "text", a3:int | + | def a1:integer = 3, a2:string = "text", a3:integer; |
+ | def a,b : integer; | ||
</code> | </code> | ||
Line 184: | Line 195: | ||
* integer - integer number | * integer - integer number | ||
- | * real - floating point number | + | * float - floating point number |
- | * character - a single character | + | * symbol - a single character |
* string - text | * string - text | ||
* logic - true or false | * logic - true or false | ||
- | * user defined (class, list) | + | * user defined (struct, list) |
=== AST === | === AST === | ||
Line 194: | Line 205: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"var", | + | id:"def", |
variables:[ // array of variables | variables:[ // array of variables | ||
{ | { | ||
type:"data type", | type:"data type", | ||
- | title: "varable name" | + | title: "variable name", |
+ | value: "variable value" | ||
}, | }, | ||
] | ] | ||
Line 204: | Line 216: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/2_variable_definition|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/2_variable_definition|Example]] |
- | ==== Class ==== | + | ==== Sruct ==== |
=== Language === | === Language === | ||
<code> | <code> | ||
- | class class_name [extends super_class] | + | struct struct_name |
- | property property_name:data_type [:= initial_value] | + | property_name:data_type [= initial_value] |
... | ... | ||
... | ... | ||
- | message message_name[:data_type] parameter1[:data_type][:=default_value], parameter1[:data_type][:=default_value], parameter3[:data_type][:=default_value], ... | ||
- | begin | ||
- | // ... message source | ||
- | end | ||
end; | end; | ||
</code> | </code> | ||
Line 224: | Line 232: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"class", | + | id:"struct", |
- | title:"class_name", | + | title:"struct_name", |
- | parent:"super_class_name", | + | |
properties:[ // array of properties | properties:[ // array of properties | ||
{ | { | ||
Line 233: | Line 240: | ||
value: "property value" // if it exists | value: "property value" // if it exists | ||
}, | }, | ||
- | ], | + | ] |
- | messages: [ // array of messages | + | |
- | { | + | |
- | type:"message return data type" | + | |
- | title:"message_name", | + | |
- | parameters:[ // array of parameters | + | |
- | { | + | |
- | type:"data type", | + | |
- | title: "parameter name" | + | |
- | }, | + | |
- | ], | + | |
- | statements:[ // array of statements (even if it is only one) | + | |
- | ... | + | |
- | ] | + | |
- | } | + | |
} | } | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/6_class|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/6_class|Example]] |
=== Property access === | === Property access === | ||
Line 258: | Line 251: | ||
== Language == | == Language == | ||
<code> | <code> | ||
- | object_name|property | + | object_name.property |
</code> | </code> | ||
Line 274: | Line 267: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/6_class|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/6_class|Example]] |
==== Array ==== | ==== Array ==== | ||
- | === List definition === | + | === Array definition === |
== Language == | == Language == | ||
Line 292: | Line 285: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"list", | + | id:"array", |
title: array_name, | title: array_name, | ||
element_type:elements_data_type, | element_type:elements_data_type, | ||
Line 300: | Line 293: | ||
</code> | </code> | ||
- | === List access === | + | === Array access === |
== Language == | == Language == | ||
<code> | <code> | ||
- | array_name#index | + | array_name[index] |
</code> | </code> | ||
Line 310: | Line 303: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"element_of_list", | + | id:"element_of_array", |
- | list:{ | + | array:{ |
- | expression for list | + | expression for array |
}, | }, | ||
index: { | index: { | ||
Line 320: | Line 313: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/7_array|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/7_array|Example]] |
==== Expressions ==== | ==== Expressions ==== | ||
Line 328: | Line 321: | ||
^ Operator ^ Precedence ^ | ^ Operator ^ Precedence ^ | ||
| if | Low | | | if | Low | | ||
- | | = != | | | + | | == != | | |
| > >= < <= | | | | > >= < <= | | | ||
| and or xor | | | | and or xor | | | ||
Line 338: | Line 331: | ||
<code> | <code> | ||
- | 2+5 | + | 2+5; |
- | 2*4+5 | + | 2*4+5; |
- | variable+5 | + | variable+5; |
- | x>y | + | x>y; |
- | x=y | + | x=y; |
- | 2+(x+y)*(5+6) | + | 2+(x+y)*(5+6); |
</code> | </code> | ||
Line 374: | Line 367: | ||
<code> | <code> | ||
- | variable := expression; | + | variable = expression; |
</code> | </code> | ||
=== Examples === | === Examples === | ||
<code> | <code> | ||
- | x := 1000 | + | x = 1000; |
- | s := "text" + "s" | + | s = "text" + "s"; |
- | y := [module f] | + | y = f(); |
- | l#i := 900 | + | l[i] = 900; |
- | q.e := "this is a text" | + | q.e = "this is a text"; |
</code> | </code> | ||
Line 392: | Line 385: | ||
id:"set", | id:"set", | ||
to: { | to: { | ||
- | ... id, property, element_of_list | + | ... id, property, element_of_array |
}, | }, | ||
from: { | from: { | ||
Line 400: | Line 393: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/2_variable_definition|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/2_variable_definition|Example]] |
- | ==== Message definition ==== | + | ==== Function definition ==== |
- | Messages are functions and are usually defined in classes. If a message is defined outside a class, it belongs to the module object. | ||
=== Language === | === Language === | ||
<code> | <code> | ||
- | message message_name:return_type parameter1:[parameter1_type][:=value], ... = expression | + | fn function_name:return_type (parameter1:[parameter1_type][=value], ...) => expression; |
</code> | </code> | ||
<code> | <code> | ||
- | message message_name:return_type parameter1:[parameter1_type][:=value], ... | + | fn function_name:return_type (parameter1:[parameter1_type][:=value], ...) |
begin | begin | ||
... | ... | ||
- | end | + | end; |
</code> | </code> | ||
Line 423: | Line 415: | ||
<code> | <code> | ||
- | message f1 | + | fn f1:none () |
begin | begin | ||
end; | end; | ||
Line 429: | Line 421: | ||
<code> | <code> | ||
- | message sum:int a:int, b:int = is a+b | + | fn sum:integer (a:integer, b:integer) => a+b; |
</code> | </code> | ||
<code> | <code> | ||
- | message power:int a:int, n:int | + | fn power:integer (a:integer, n:integer) |
begin | begin | ||
- | define p:int | + | def p:integer; |
- | define i:int | + | def i:integer; |
- | if n = 0 then | + | if n == 0 then |
begin | begin | ||
- | power:=1 | + | power=1; |
- | end | + | end; |
- | for i in [1,n] p:=p * a | + | for i in [1,n] p=p * a; |
- | return p | + | return p; |
- | end | + | end; |
</code> | </code> | ||
Line 450: | Line 442: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"message", | + | id:"fn", |
- | title:"message_name", | + | title:"function_name", |
parameters:[ // array of parameters | parameters:[ // array of parameters | ||
{ | { | ||
type:"data type", | type:"data type", | ||
- | id: "parameter name" | + | name: "parameter name" |
}, | }, | ||
], | ], | ||
- | return_type:"type of the value that the message returns", | + | return_type:"type of the value that the function returns", |
statements:[ // array of statements (even if it is only one) | statements:[ // array of statements (even if it is only one) | ||
... | ... | ||
Line 465: | Line 457: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/4_messages|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/4_messages|Example]] |
=== Return === | === Return === | ||
- | This is the statements that sets the return value of the message. | + | This is the statement that sets the return value of the function. |
== Example == | == Example == | ||
<code> | <code> | ||
- | return x+y | + | return x+y; |
</code> | </code> | ||
Line 485: | Line 477: | ||
</code> | </code> | ||
- | ==== Message dispatch ==== | + | ==== function call ==== |
The is a call for a function | The is a call for a function | ||
Line 492: | Line 484: | ||
<code> | <code> | ||
- | [object message_name parameter_name1:value1, parameter_name2:value, ...] | + | function_name (parameter_name1:value1, parameter_name2:value, ...); |
</code> | </code> | ||
<code> | <code> | ||
- | [object message_name value1, value2, ...] | + | function_name (parameter1_value, parameter2_value, ...) |
</code> | </code> | ||
Line 502: | Line 494: | ||
<code> | <code> | ||
- | [module write text:"Text"] | + | write (text:"Text"); |
- | s := [sum n1:3 n2:5] | + | s = sum (3, 5); |
- | [module getdir] | + | getdir (); |
</code> | </code> | ||
Line 511: | Line 503: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"dispatch", | + | id:"function_call", |
- | message:"message_name", | + | function:"function_name", |
- | object:"destination_object_name", | + | |
parameters:{ // dictionary of parameter name or index and value | parameters:{ // dictionary of parameter name or index and value | ||
"parameter1": parameter_value, | "parameter1": parameter_value, | ||
Line 522: | Line 513: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/4_messages|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/4_messages|Example]] |
==== Branch ==== | ==== Branch ==== | ||
Line 529: | Line 520: | ||
<code> | <code> | ||
- | if expression | + | if expression then |
... | ... | ||
- | end | + | end; |
</code> | </code> | ||
<code> | <code> | ||
- | if expression | + | if expression then |
... | ... | ||
else | else | ||
... | ... | ||
- | end | + | end; |
</code> | </code> | ||
Line 563: | Line 554: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/5_branch|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/5_branch|Example]] |
- | ==== Loop ==== | + | ==== Loops ==== |
There are three types of loops: while, repeat and for. | There are three types of loops: while, repeat and for. | ||
- | === while === | + | === loop run === |
<code> | <code> | ||
- | while expression | + | loop expression run |
... | ... | ||
- | end | + | end; |
</code> | </code> | ||
Line 580: | Line 571: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"while", | + | id:"loop_run", |
exp :expression, | exp :expression, | ||
statements: [ // array of statements | statements: [ // array of statements | ||
Line 588: | Line 579: | ||
</code> | </code> | ||
- | === repeat === | + | === loop ... when === |
<code> | <code> | ||
- | repeat | + | loop |
... | ... | ||
- | when expression | + | when expression; |
</code> | </code> | ||
Line 599: | Line 590: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | id:"repeat", | + | id:"loop_when", |
exp :expression, | exp :expression, | ||
statements: [ // array of statements | statements: [ // array of statements | ||
Line 610: | Line 601: | ||
<code> | <code> | ||
- | for variable_name in expression | + | for variable_name in expression run |
... | ... | ||
- | end | + | end; |
</code> | </code> | ||
Line 618: | Line 609: | ||
<code> | <code> | ||
- | for variable_name in (number to number) | + | for variable_name from number to number run |
... | ... | ||
- | end | + | end; |
</code> | </code> | ||
<code> | <code> | ||
- | for variable_name in (number downto number) | + | for variable_name from number downto number run |
... | ... | ||
- | end | + | end; |
</code> | </code> | ||
Line 651: | Line 642: | ||
id:"for", | id:"for", | ||
variable:variable_name, | variable:variable_name, | ||
- | list:{ | + | from: { expression }, |
- | low: { expression }, | + | to/downto: { expression }, |
- | high: { expression }, | + | |
- | direction: "to" or "downto" | + | |
- | }, | + | |
statements: [ // array of statements | statements: [ // array of statements | ||
Line 662: | Line 650: | ||
</code> | </code> | ||
- | [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf/verify/alf/3_loop|Example]] | + | [[https://github.com/upb-fils/alf/tree/master/Devoir/alf/verify/alf/3_loop|Example]] |
===== Errors ===== | ===== Errors ===== | ||
Line 733: | Line 721: | ||
#else | #else | ||
p := "Linux" {this source code is ignored if platform is not linux} | p := "Linux" {this source code is ignored if platform is not linux} | ||
- | end | + | #endif |
</code> | </code> | ||
Line 764: | Line 752: | ||
==== Public Tests ==== | ==== Public Tests ==== | ||
- | You can download the public tests from the GitHub [[https://github.com/alexandruradovici/alf2018/tree/master/Devoirs/alf|repository]]. | + | You can download the public tests from the GitHub [[https://github.com/upb-fils/alf/tree/master/Devoir/alf|repository]]. |
To run the tests, download the contents of the repository in the folder with the homework. Enter the //verify// folder and run ./run_all.sh. | To run the tests, download the contents of the repository in the folder with the homework. Enter the //verify// folder and run ./run_all.sh. | ||
Line 812: | Line 800: | ||
- Create a zip (not rar, ace, 7zip or anything else) archive containing: | - Create a zip (not rar, ace, 7zip or anything else) archive containing: | ||
- | * your main file (main.js) | + | * your main file (set in package.json) |
* your javascript files (*.js) | * your javascript files (*.js) | ||
- | * your jison file (alf.jison) | + | * your l and y file (grammar.l and grammar.y) |
- | * yarn.lock (if you are using yarn) | + | |
* the package.json file | * the package.json file | ||
* the Readme file | * the Readme file | ||
- sign in with [[https://vmchecker.cs.pub.ro|vmchecker]] | - sign in with [[https://vmchecker.cs.pub.ro|vmchecker]] | ||
- select the //Automates et Langages Formelles (FILS)// course | - select the //Automates et Langages Formelles (FILS)// course | ||
- | - select //1. Langage Alf// | + | - select //3. Alf Language// |
- upload the archive | - upload the archive | ||
Line 835: | Line 822: | ||
unzip archive.zip homework | unzip archive.zip homework | ||
cd homework | cd homework | ||
- | # if the file yarn.lock exists | ||
- | yarn | ||
- | # else | ||
npm install | npm install | ||
echo '{ "node":true, "loopfunc": true, "esnext":true }' > .jshintrc | echo '{ "node":true, "loopfunc": true, "esnext":true }' > .jshintrc | ||
jshint *.js | jshint *.js | ||
- | jison alf.jison -o alf.js | + | jison grammar.y grammar.l -o grammar.js |
</code> | </code> | ||