User Tools

Site Tools


elderscript_flow_control

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

elderscript_flow_control [2019/07/12 11:14]
dicenomiwiki created
elderscript_flow_control [2019/07/12 15:25] (current)
dicenomiwiki
Line 1: Line 1:
 +======Flow Control======
 +
 Being able to do simple expressions,​ we’ve already hinted that we can alter the flow of the program based on calculated values. ​ This is done via an ''​if then else end''​ construction,​ similar to what is found in most programming languages. ​ What is interesting is that this is also an expression (i.e., there is no difference between an //​expression//​ and a //​statement//​ - everything creates a value). ​ So while something like JavaScript would have code like this: Being able to do simple expressions,​ we’ve already hinted that we can alter the flow of the program based on calculated values. ​ This is done via an ''​if then else end''​ construction,​ similar to what is found in most programming languages. ​ What is interesting is that this is also an expression (i.e., there is no difference between an //​expression//​ and a //​statement//​ - everything creates a value). ​ So while something like JavaScript would have code like this:
  
-''​ 1. ''''​if (a > 10) {''​ +<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​ 
- +if (a > 10) { 
-''​ 2. '''' ​ b = 5''​ +  b = 5 
- +} else { 
-''​ 3. ''''​} else {''​ +  b = 6 
- +
-''​ 4. '''' ​ b = 6''​ +</​code>​
- +
-''​ 5. ''''​}''​ +
 We can do this: We can do this:
  
-''​ 1. ''''​@B ''​'' ​(if @A > 10 then''​+<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​ 
 +@B ← (if @A > 10 then 
 + 5 
 +else 
 + 6 
 +end) 
 +</​code>​ 
 +Namely, the value of the ''​if'' ​expression is used to assign to the variable. ​ Granted, this is a simplistic example, but common enough that languages such as JavaScript have special operators such as ''​?:'' ​which become special ​if then else expressions (e.g., ''​b = a > 10 ? 5 : 6''​).
  
-''​ 2. ''''​ 5''​ 
- 
-''​ 3. ''''​else''​ 
- 
-''​ 4. ''''​ 6''​ 
- 
-''​ 5. ''''​end)''​ 
- 
-Namely, the value of the ''​if''​ expression is used to assign to the variable. ​ Granted, this is a simplistic example, but common enough that languages such as JavaScript have special operators such as ''?:''​ which become special if then else expressions (e.g., ''​b = a > 10 ? 5 : 6''​). 
 ELDARScript is specially designed to support having dice rolled, with special features to make it be aware that dice take “time” to come up with a result. ​ This is true with expression, especially with our conditional expressions. ​ So ELDARScript is specially designed to support having dice rolled, with special features to make it be aware that dice take “time” to come up with a result. ​ This is true with expression, especially with our conditional expressions. ​ So
  
-''​ 1. ''''​if d20 > 10 then''​ +<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​ 
- +if d20 > 10 then 
-''​ 2. '''' ​“Hit for” + d8''​ + ​“Hit for” + d8 
- +else 
-''​ 3. ''''​else''​ + ​“Missed” 
- +end 
-''​ 4. '''' ​“Missed”''​ +</​code>​
- +
-''​ 5. ''''​end''​ +
 will roll a d20, wait to see what the result is, and then roll the d8 (but only if the result was more than 10) will roll a d20, wait to see what the result is, and then roll the d8 (but only if the result was more than 10)
  
-=====Chained conditionals=====+//Chained conditionals//
  
-Instead of writing: +////Instead of writing:
- +
-''​ 1. ''''​if @A > 10 then''​ +
- +
-''​ 2. ''''​ “A Big”''​ +
- +
-''​ 3. ''''​else''​ +
- +
-''​ 4. ''''​ if @B > 10 then''​ +
- +
-''​ 5. '''' ​  “B Big”''​ +
- +
-''​ 6. ''''​ else''​ +
- +
-''​ 7. '''' ​  ​“Small”''​ +
- +
-''​ 8. ''''​ end''​ +
- +
-''​ 9. ''''​end''​+
  
 +<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​
 +if @A > 10 then
 + “A Big”
 +else
 + if @B > 10 then
 +   “B Big”
 + else
 +   ​“Small”
 + end
 +end
 +</​code>​
 we can combine lines 3 & 4 together and omit line 8: we can combine lines 3 & 4 together and omit line 8:
  
-''​ 1. ''''​if @A > 10 then''​ +<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​ 
- +if @A > 10 then 
-''​ 2. '''' ​“A Big”''​ + “A Big” 
- +elif @B > 10 then 
-''​ 3. ''''​elif @B > 10 then''​ +  “B Big” 
- +else 
-''​ 4. '''' ​ “B Big”''​ +  “Small” 
- +end 
-''​ 5. ''''​else''​ +</​code>​
- +
-''​ 6. '''' ​ ​“Small”''​ +
- +
-''​ 7. ''''​end''​ +
 These two expressions are otherwise identical, the second is just easier to read These two expressions are otherwise identical, the second is just easier to read
  
-=====Iteration=====+//Iteration//
  
-ELDARScript as a special flow control operator to do things multiple times named ''​for''​. ​ It looks like a classic //​iterator//​ found in most programming languages. ​ It has a special local variable that is created during the loop, as well as list to iterate through. ​ However, since everything is an expression, you may wonder what sort of expression it returns? ​ It generates a list of all the expressions created by the body of the loop (this makes it work like a ''​map''​ function found in many languages). ​ So: +////ELDARScript as a special flow control operator to do things multiple times named ''​for''​. ​ It looks like a classic //​iterator//​ found in most programming languages. ​ It has a special local variable that is created during the loop, as well as list to iterate through. ​ However, since everything is an expression, you may wonder what sort of expression it returns? ​ It generates a list of all the expressions created by the body of the loop (this makes it work like a ''​map''​ function found in many languages). ​ So:
- +
-''​ 1. ''''​for @i in 1 … 5 do''​ +
- +
-''​ 2. '''' ​ @i x 3''​ +
- +
-''​ 3. ''''​end''​+
  
 +<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​
 +for @i in 1 … 5 do
 +  @i x 3
 +end
 +</​code>​
 will create a list (3, 6, 9, 12, 15).  (If you thought “it would be easier to do ''​(1 … 5) x 3''​ to make this”, you are exactly correct). will create a list (3, 6, 9, 12, 15).  (If you thought “it would be easier to do ''​(1 … 5) x 3''​ to make this”, you are exactly correct).
  
 //NB: Do not try to roll dice while inside a for loop - it will not work (and even if it did, it probably wouldn’t work like you expect).// //NB: Do not try to roll dice while inside a for loop - it will not work (and even if it did, it probably wouldn’t work like you expect).//
  
-You can ignore the result of the for loop and it works much more like a “traditional” loop: +////You can ignore the result of the for loop and it works much more like a “traditional” loop:
- +
-''​ 1. ''''​@x ''​←''​ 0.''​ +
- +
-''​ 2. ''''​for @i in 1 … 5 do''​ +
- +
-''​ 3. '''' ​ @x ''​←''​ @x + @i''​ +
- +
-''​ 4. ''''​end''​+
  
 +<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​
 +@x ← 0.
 +for @i in 1 … 5 do
 +  @x ← @x + @i
 +end
 +</​code>​
 This will take the sum of numbers 1 through 5 and store the result into the local variable ''​@x''​. ​ In case you are curious, the ''​for''​ loop will create a list (1, 4, 9, 14, 19), since it will take the value of the assignment operator in each pass of the list. This will take the sum of numbers 1 through 5 and store the result into the local variable ''​@x''​. ​ In case you are curious, the ''​for''​ loop will create a list (1, 4, 9, 14, 19), since it will take the value of the assignment operator in each pass of the list.
  
-=====Comments=====+//Comments//
  
-Once you start making complex formulas, you’ll probably want to add comments to the code to help you document how it works. ​ You can always throw in a string followed by a period (since the period will drop the previous value - the string - and use the next value), but ELDARScript provides for a specific comment capability: +////Once you start making complex formulas, you’ll probably want to add comments to the code to help you document how it works. ​ You can always throw in a string followed by a period (since the period will drop the previous value - the string - and use the next value), but ELDARScript provides for a specific comment capability:
- +
-''​ 1. ''​❝''​Start by setting our total to zero''​❞''''​ +
- +
-''​ 2. ''''​@X ''​←''​ 0.''​ +
- +
-''​ 3. ''​❝''​Iterate through the values''​❞''''​ +
- +
-''​ 4. ''''​for @i in 1 … 5 do''​ +
- +
-''​ 5. '''' ​ ''​❝''​Keep a running total''​❞''''​ +
- +
-''​ 6. '''' ​ @X ''​←''​ @X + @I''​ +
- +
-''​ 7. ''''​end''​ +
- +
-''​ 8. ''​❝''​And at this point we%%'​%%ve got that total''​❞''''​+
  
 +<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​
 +❝Start by setting our total to zero❞@X ← 0.
 +❝Iterate through the values❞for @i in 1 … 5 do
 +  ❝Keep a running total❞ ​ @X ← @X + @I
 +end
 +❝And at this point we've got that total❞</​code>​
 The use of “heavy double comma quotation mark ornament” indicates a comment. ​ This can also be typed by an exclamation point and double quote The use of “heavy double comma quotation mark ornament” indicates a comment. ​ This can also be typed by an exclamation point and double quote
  
-''​ 1. ''''​!”Start by setting our total to zero”!''​ +<code stonescript [enable_line_numbers="​true"​ start_line_numbers_at="​1"​]>​ 
- +!”Start by setting our total to zero”! 
-''​ 2. ''''​@X ''​'' ​0.''​ +@X ← 0. 
- +“!Iterate through the values!” 
-''​ 3. ''''​“!Iterate through the values!”''​ +for @i in 1 … 5 do 
- +  !“Keep a running total”! 
-''​ 4. ''''​for @i in 1 … 5 do''​ +  @X ← @X + @I 
- +end 
-''​ 5. '''' ​ ​!“Keep a running total”!''​ +!”And at this point we've got that total”! 
- +</​code>​ 
-''​ 6. '''' ​ ​@X ​''​'' ​@X + @I''​ +The editor will convert those to the ❝❞. ​ Unlike most programming languages, however, ELDARScript is somewhat picky about where they go - they are actually part of the language itself, rather than some lexical whitespace that can be ignored. ​ More specifically,​ they can go before a statement, after a period, or after the last statement. ​ They do not, however, have a value, nor can they, for example, go in the middle of some parameters (i.e., ''​foo(1 ''​❝''​count''​❞'',​ 2, “a” ''​❝''​name''​❞''​)''​ is illegal.  ​These “heavy” appearance is designed to emphasize that they go between lines (as the editor would format them).''​ ''​
- +
-''​ 7. ''''​end''​ +
- +
-''​ 8. ''''​!”And at this point we%%'%%ve got that total”!''​ +
- +
-The editor will convert those to the ❝❞. ​ Unlike most programming languages, however, ELDARScript is somewhat picky about where they go - they are actually part of the language itself, rather than some lexical whitespace that can be ignored. ​ More specifically,​ they can go before a statement, after a period, or after the last statement. ​ They do not, however, have a value, nor can they, for example, go in the middle of some parameters (i.e., ''​foo(1 ''​❝''​count''​❞'',​ 2, “a” ''​❝''​name''​❞''​)''​ is illegal.  ​There “heavy” appearance is designed to emphasize that they go between lines (as the editor would format them).''​ ''​+
elderscript_flow_control.txt · Last modified: 2019/07/12 15:25 by dicenomiwiki