mirror of
https://github.com/dragonheim/gagent.git
synced 2025-04-26 03:28:59 -07:00
refactor: converted int64 to int32 where appropriate.
refactor: reduced usage of fmt in favor of logs and string concatication. refactor: minor re-ordering of the data structures to reduce storage space required.
This commit is contained in:
parent
8640d42132
commit
1bcc682b7c
9 changed files with 53 additions and 48 deletions
|
@ -1,6 +1,7 @@
|
|||
package picol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -92,8 +93,8 @@ func CommandMath(i *Interpreter, argv []string, pd interface{}) (string, error)
|
|||
if a != b {
|
||||
c = 1
|
||||
}
|
||||
default: // FIXME I hate warnings
|
||||
c = 0
|
||||
default:
|
||||
return "0", errors.New("invalid operator " + argv[0])
|
||||
}
|
||||
return fmt.Sprintf("%d", c), nil
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Define parser token types
|
||||
const (
|
||||
ptESC = iota
|
||||
ptSTR
|
||||
|
@ -15,6 +16,7 @@ const (
|
|||
ptEOF
|
||||
)
|
||||
|
||||
// parserStruct represents the parser state
|
||||
type parserStruct struct {
|
||||
text string
|
||||
p, start, end, ln int
|
||||
|
@ -22,26 +24,31 @@ type parserStruct struct {
|
|||
Type int
|
||||
}
|
||||
|
||||
// initParser initializes a new parserStruct instance
|
||||
func initParser(text string) *parserStruct {
|
||||
return &parserStruct{text, 0, 0, 0, len(text), 0, ptEOL}
|
||||
return &parserStruct{text: text, ln: len(text), Type: ptEOL}
|
||||
}
|
||||
|
||||
// next advances the parser position by one rune
|
||||
func (p *parserStruct) next() {
|
||||
_, w := utf8.DecodeRuneInString(p.text[p.p:])
|
||||
p.p += w
|
||||
p.ln -= w
|
||||
}
|
||||
|
||||
// current returns the current rune at the parser position
|
||||
func (p *parserStruct) current() rune {
|
||||
r, _ := utf8.DecodeRuneInString(p.text[p.p:])
|
||||
return r
|
||||
}
|
||||
|
||||
// token returns the current token text between start and end positions
|
||||
func (p *parserStruct) token() (t string) {
|
||||
defer recover()
|
||||
return p.text[p.start:p.end]
|
||||
}
|
||||
|
||||
// parseSep parses whitespace separators
|
||||
func (p *parserStruct) parseSep() string {
|
||||
p.start = p.p
|
||||
for ; p.p < len(p.text); p.next() {
|
||||
|
@ -54,6 +61,7 @@ func (p *parserStruct) parseSep() string {
|
|||
return p.token()
|
||||
}
|
||||
|
||||
// parseEol parses end of line and comments
|
||||
func (p *parserStruct) parseEol() string {
|
||||
p.start = p.p
|
||||
|
||||
|
@ -70,6 +78,7 @@ func (p *parserStruct) parseEol() string {
|
|||
return p.token()
|
||||
}
|
||||
|
||||
// parseCommand parses a command within brackets
|
||||
func (p *parserStruct) parseCommand() string {
|
||||
level, blevel := 1, 0
|
||||
p.next() // skip
|
||||
|
@ -103,6 +112,7 @@ Loop:
|
|||
return p.token()
|
||||
}
|
||||
|
||||
// parseVar parses a variable reference
|
||||
func (p *parserStruct) parseVar() string {
|
||||
p.next() // skip the $
|
||||
p.start = p.p
|
||||
|
@ -132,6 +142,7 @@ func (p *parserStruct) parseVar() string {
|
|||
return p.token()
|
||||
}
|
||||
|
||||
// parseBrace parses a brace-enclosed string
|
||||
func (p *parserStruct) parseBrace() string {
|
||||
level := 1
|
||||
p.next() // skip
|
||||
|
@ -160,6 +171,7 @@ Loop:
|
|||
return p.token()
|
||||
}
|
||||
|
||||
// parseString parses a string with or without quotes
|
||||
func (p *parserStruct) parseString() string {
|
||||
newword := p.Type == ptSEP || p.Type == ptEOL || p.Type == ptSTR
|
||||
|
||||
|
@ -203,6 +215,7 @@ Loop:
|
|||
return p.token()
|
||||
}
|
||||
|
||||
// parseComment skips over comment text
|
||||
func (p *parserStruct) parseComment() string {
|
||||
for p.ln != 0 && p.current() != '\n' {
|
||||
p.next()
|
||||
|
@ -210,6 +223,7 @@ func (p *parserStruct) parseComment() string {
|
|||
return p.token()
|
||||
}
|
||||
|
||||
// GetToken returns the next token from the parser
|
||||
func (p *parserStruct) GetToken() string {
|
||||
for {
|
||||
if p.ln == 0 {
|
||||
|
@ -246,5 +260,4 @@ func (p *parserStruct) GetToken() string {
|
|||
return p.parseString()
|
||||
}
|
||||
}
|
||||
/* return p.token() /* unreached */
|
||||
}
|
||||
|
|
|
@ -12,20 +12,10 @@ import (
|
|||
|
||||
var fname = flag.String("f", "", "file name")
|
||||
|
||||
// CommandPuts is a simple version of the TCL puts function.
|
||||
func CommandPuts(i *picol.Interpreter, argv []string, pd interface{}) (string, error) {
|
||||
if len(argv) != 2 {
|
||||
return "", fmt.Errorf("wrong number of args for %s %s", argv[0], argv)
|
||||
}
|
||||
fmt.Println(argv[1])
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
interp := picol.NewInterpreter()
|
||||
interp.RegisterCoreCommands()
|
||||
interp.RegisterCommand("puts", CommandPuts, nil)
|
||||
|
||||
buf, err := ioutil.ReadFile(*fname)
|
||||
if err == nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue