# Author: Malcolm Kesson (2007)
proc getCode { ruletoken } {
    set code 0
    set prefix [string index $ruletoken 0]
    # random substitution
    if {$prefix == "~"} { set code 1 }
    # use on last rewrite only 
    if {$prefix == "*"} { set code 2 } 
    # do NOT use on last rewrite
    if {$prefix == "!"} { set code 3 } 
    return $code
    }
  
# Note that comments in a Lscript begin "#"
proc getScript { path } {
    set infile [open $path r]
    
    while {[eof $infile] != 1} {
        set line [string trim [gets $infile]]
        if {$line != "" && [string index $line 0] != "#"} {
            lappend lines $line
            }
        }
    return $lines
    }
    
proc readScript { lines data } {
    upvar $data script
    foreach line $lines {
        set pname [lindex $line 0]
    
        if {$pname == "rule"} {
            set token  [lindex $line 1]
            set rvalue [lindex $line 2]
            set rcode  [getCode $token]
            set rchar $token
            if {$rcode == 1} {
                set rchar  [string trim $token "~"]
            } elseif {$rcode == 2} {
                set rchar  [string trim $token "*"]
            } elseif {$rcode == 3} {
                set rchar  [string trim $token "!"]
                }
            
            set item [getArrayValueOf tempRules $rchar]
            # Begin a new rule
            if {$item == ""} {
                set item [list $rcode $rvalue]
            } else {
                # Append to an existing rule
                lappend item $rvalue
                }
            set tempRules($rchar) $item
        } elseif {$pname == "statement"} {
            set statechar  [lindex $line 1]
            set stateval   [lindex $line 2]
            set tempStat($statechar) $stateval
        } else {
            set paramvalue [lindex $line 1]
            set script($pname) $paramvalue
            }
        }
    set script(rule)       [array get tempRules]
#puts "READER [array get tempRules]"
    set script(statement)  [array get tempStat]
    #close $infile
    }