Browse Source

Added support for external constants for messages in C# automata

stepan-mitkin 1 year ago
parent
commit
182793904c

+ 29 - 5
examples/automaton/C#/automaton-cs.cs

@@ -5,6 +5,12 @@ using System.Text;
 
 namespace AutomatonTest {
 
+// Look: these constants are used in the BabyFrog state machine
+public class Frog {
+	public const int Food = 20;
+	public const int Sleep = 10;
+}
+
 public interface IMachine
 {
 	object OnMessage(int messageType, char c);
@@ -42,6 +48,7 @@ public class Lexer {
 	
 	
 
+
     public partial class LexerMachine
         : IMachine
     {
@@ -58,7 +65,12 @@ public class Lexer {
             Operator
         }
 
-        public StateNames State = StateNames.Idle;
+        private StateNames _state = StateNames.Idle;
+
+        public StateNames State {
+            get { return _state; }
+            private set { _state = value; }
+        }
 
         public const int DigitMessage = 1;
         public const int LetterMessage = 2;
@@ -312,6 +324,7 @@ public class Lexer {
             
         }
     }
+
     public partial class BabyFrog
     {
         
@@ -322,10 +335,15 @@ public class Lexer {
             Sleepy
         }
 
-        public StateNames State = StateNames.Hungry;
+        private StateNames _state = StateNames.Hungry;
 
-        public const int FoodMessage = 1;
-        public const int SleepMessage = 2;
+        public StateNames State {
+            get { return _state; }
+            private set { _state = value; }
+        }
+
+        public const int FoodMessage = Frog.Food;
+        public const int SleepMessage = Frog.Sleep;
 
         public object OnMessage(int messageType, int msg) {
             switch (messageType) {
@@ -392,6 +410,7 @@ public class Lexer {
             
         }
     }
+
     public partial class Fragile
     {
         
@@ -401,7 +420,12 @@ public class Lexer {
             Working
         }
 
-        public StateNames State = StateNames.Working;
+        private StateNames _state = StateNames.Working;
+
+        public StateNames State {
+            get { return _state; }
+            private set { _state = value; }
+        }
 
         public const int helloMessage = 1;
 

BIN
examples/automaton/C#/automaton-cs.drn


BIN
generators/cs.drn


+ 81 - 3
generators/cs.tcl

@@ -706,6 +706,72 @@ proc generate_body { gdb diagram_id start_item node_list items incoming } {
     $items $incoming $callbacks ]
 }
 
+proc get_message_constants { name messages } {
+    #item 1887
+    set state "none"
+    set constants {}
+    set names {}
+    set i 1
+    foreach message $messages {
+        #item 1891
+        set parts [ split $message "." ]
+        set last [ lindex $parts end ]
+        set count [ llength $parts ]
+        lappend names $last
+        #item 18920001
+        if {$state == "none"} {
+            #item 1899
+            if {$count == 1} {
+                #item 1912
+                lappend constants \
+                 [ list $message $i ]
+                #item 1902
+                set state "internal"
+            } else {
+                #item 1913
+                lappend constants \
+                 [ list $last $message ]
+                #item 1903
+                set state "external"
+            }
+        } else {
+            #item 18920002
+            if {$state == "internal"} {
+                #item 1908
+                if {$count == 1} {
+                    #item 1914
+                    lappend constants \
+                     [ list $message $i ]
+                } else {
+                    #item 1910
+                    error "Cannot combine external and internal message codes: $name"
+                }
+            } else {
+                #item 18920003
+                if {$state == "external"} {
+                    
+                } else {
+                    #item 18920004
+                    error "Unexpected switch value: $state"
+                }
+                #item 1906
+                if {$count == 1} {
+                    #item 1911
+                    error "Cannot combine external and internal message codes: $name"
+                } else {
+                    #item 1915
+                    lappend constants \
+                     [ list $last $message ]
+                }
+            }
+        }
+        #item 1904
+        incr i
+    }
+    #item 1890
+    return [list $names $constants]
+}
+
 proc handle_message { fhandle ind states message names handlers } {
     #item 1727
     puts $fhandle "$ind            switch \(State\) \{"
@@ -1295,6 +1361,7 @@ proc print_machine { fhandle machine } {
         [extract_body $comments] \
         body inherits
         #item 1541
+        puts $fhandle ""
         puts $fhandle "    public partial class $name"
         #item 1701
         set real_params [ lrange $parameters 1 end ]
@@ -1329,15 +1396,26 @@ proc print_machine { fhandle machine } {
         #item 1847
         puts $fhandle ""
         #item 1636
-        puts $fhandle "        public StateNames State = StateNames.$first;"
+        puts $fhandle "        private StateNames _state = StateNames.$first;"
+        puts $fhandle ""
+        puts $fhandle "        public StateNames State \{"
+        puts $fhandle "            get \{ return _state; \}"
+        puts $fhandle "            private set \{ _state = value; \}"
+        puts $fhandle "        \}"
         #item 1846
         puts $fhandle ""
+        #item 1881
+        lassign \
+        [get_message_constants $name $messages] \
+        messages mconstants
         #item 1840
         set i 1
-        foreach message $messages {
+        foreach message $mconstants {
+            #item 1916
+            lassign $message name value
             #item 1839
             puts $fhandle \
-              "        public const int ${message}Message = $i;"
+              "        public const int ${name}Message = $value;"
             #item 1841
             incr i
         }

BIN
generators/machine.drn


+ 8 - 1
generators/machine.tcl

@@ -245,7 +245,7 @@ proc build_sub_diagram { gdb diagram_id state case parameters last_branch callba
             set default 1
         } else {
             #item 878
-            set message2 [ string map { - _ . _ } $message ]
+            set message2 [ last_part $message ]
             #item 524
             set name "${state}_${message2}"
             set default 0
@@ -800,6 +800,13 @@ proc is_visited { vertex_id } {
     return [ info exists g_visited($vertex_id) ]
 }
 
+proc last_part { text } {
+    #item 923
+    set parts [ split $text "." ]
+    #item 924
+    return [ lindex $parts end ]
+}
+
 proc make_diagram { gdb name params } {
     #item 675
     set diagram_id [ mod::next_key $gdb diagrams diagram_id ]