z_mirror/pesticide
git clone https://git.raiguard.me/z_mirror/pesticide
6cccd4f
trunk
1package command 2 3import ( 4"errors" 5"fmt" 6"os" 7"path/filepath" 8"strconv" 9"strings" 10) 11 12type Command interface { 13command () 14} 15 16type ( 17Backtrace struct {} 18Break struct { 19File string 20Line int 21 } 22Continue struct {} 23Down int 24Evaluate struct {Expr string } 25Launch struct {Name string } 26Next struct {} 27Pause struct {} 28Quit struct {} 29Step struct {} 30StepOut struct {} 31Up int 32) 33 34func (b Backtrace )command () {} 35func (b Break )command () {} 36func (c Continue )command () {} 37func (d Down )command () {} 38func (e Evaluate )command () {} 39func (l Launch )command () {} 40func (s Next )command () {} 41func (p Pause )command () {} 42func (q Quit )command () {} 43func (s Step )command () {} 44func (s StepOut )command () {} 45func (u Up )command () {} 46 47func Parse (input string ) (Command ,error ) { 48args := strings .Split (input ," " ) 49if len (args )== 0 { 50return nil ,nil 51 } 52switch args [0 ] { 53case "backtrace" ,"bt" : 54return Backtrace {},nil 55case "break" ,"b" : 56return cmdParseBreak (args [1 :]) 57case "continue" ,"c" : 58return Continue {},nil 59case "down" ,"dow" : 60return Down (1 ),nil 61case "evaluate" ,"eval" ,"e" : 62return Evaluate {Expr :strings .Join (args [1 :]," " )},nil 63case "launch" ,"l" : 64return cmdParseLaunch (args [1 :]) 65case "pause" ,"p" : 66return Pause {},nil 67case "quit" ,"q" : 68return Quit {},nil 69case "step" ,"s" : 70return Step {},nil 71case "stepout" ,"finish" ,"fin" : 72return StepOut {},nil 73case "next" ,"n" : 74return Next {},nil 75case "up" : 76return Up (1 ),nil 77default : 78return nil ,errors .New (fmt .Sprintf ("Unknown command: %s" ,args [0 ])) 79 } 80} 81 82func cmdParseBreak (args []string ) (Break ,error ) { 83var b Break 84if len (args )!= 2 { 85return b ,errors .New ("syntax: break <filename> <line>" ) 86 } 87filename ,err := filepath .Abs (args [0 ]) 88if err != nil { 89return b ,err 90 } 91f ,err := os .Open (filename ) 92defer f .Close () 93if err != nil { 94return b ,err 95 } 96// TODO: Validate that file exists 97b .File = filename 98line ,err := strconv .ParseUint (args [1 ],0 ,32 ) 99if err != nil { 100return b ,err 101 } 102b .Line = int (line ) 103return b ,nil 104} 105 106func cmdParseLaunch (args []string ) (Launch ,error ) { 107var l Launch 108if len (args )== 0 { 109return l ,errors .New ("Did not specify a configuration to launch" ) 110 }else if len (args )> 1 { 111return l ,errors .New ("Too many arguments" ) 112 } 113l .Name = args [0 ] 114return l ,nil 115}