Skip to content

Commit

Permalink
added decimal type, improved so repl can switch between show-results …
Browse files Browse the repository at this point in the history
…or hide-results
  • Loading branch information
jankom committed Jul 23, 2022
1 parent 6a2711a commit 2f06a3c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 15 deletions.
1 change: 1 addition & 0 deletions env/idxs.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func NewIdxs() *Idxs {
"Kindword",
"Converter",
"Time",
"Decimal",
}

for _, value := range NativeTypes {
Expand Down
34 changes: 34 additions & 0 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
ConverterType Type = 31
TimeType Type = 32
SpreadsheetRowType Type = 33
DecimalType Type = 34
)

// after adding new type here, also add string to idxs.go
Expand Down Expand Up @@ -89,6 +90,39 @@ func (i Integer) GetKind() int {
return int(IntegerType)
}

//
// DECIMAL
//

// Decimal
type Decimal struct {
Value float64
}

// Type returns the type of the Decimal.
func (i Decimal) Type() Type {
return DecimalType
}

// Inspect returns a string representation of the Decimal.
func (i Decimal) Inspect(e Idxs) string {
return "[Decimal: " + strconv.FormatFloat(i.Value, 'f', 6, 64) + "]"
}

// Inspect returns a string representation of the Decimal.
func (i Decimal) Probe(e Idxs) string {
return strconv.FormatFloat(i.Value, 'f', 6, 64)
}

func (i Decimal) Trace(msg string) {
fmt.Print(msg + "(Decimal): ")
fmt.Println(i.Value)
}

func (i Decimal) GetKind() int {
return int(DecimalType)
}

//
// STRING
//
Expand Down
2 changes: 1 addition & 1 deletion evaldo/evaldo.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func EvalExpressionConcrete(ps *env.ProgramState) *env.ProgramState {
//trace2("Before entering expression")
if object != nil {
switch object.Type() {
case env.IntegerType, env.StringType, env.BlockType, env.VoidType, env.TagwordType, env.UriType, env.EmailType:
case env.IntegerType, env.DecimalType, env.StringType, env.BlockType, env.VoidType, env.TagwordType, env.UriType, env.EmailType:
if !ps.SkipFlag {
ps.Res = object
}
Expand Down
12 changes: 11 additions & 1 deletion evaldo/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ func DoRyeRepl(es *env.ProgramState) {

line2 := ""

showResults := false

var prevResult env.Object

for {
Expand Down Expand Up @@ -228,6 +230,10 @@ func DoRyeRepl(es *env.ProgramState) {
// ignore
} else if strings.Compare("(lc)", line2) == 0 {
fmt.Println(es.Ctx.Probe(*es.Idx))
} else if strings.Compare("(show-results)", line2) == 0 {
showResults = true
} else if strings.Compare("(hide-results)", line2) == 0 {
showResults = false
} else if strings.Compare("(r)", line2) == 0 {
// es.Ser = ser
// fmt.Println("")
Expand Down Expand Up @@ -262,7 +268,11 @@ func DoRyeRepl(es *env.ProgramState) {

if !es.ErrorFlag && es.Res != nil {
prevResult = es.Res
// TEMP - make conditional fmt.Print("\033[38;5;37m" + es.Res.Inspect(*genv) + "\x1b[0m")
// TEMP - make conditional
// print the result
if showResults {
fmt.Println("\033[38;5;37m" + es.Res.Inspect(*genv) + "\x1b[0m")
}
if es.Res != nil && shellEd.Mode != "" && !shellEd.Pause && es.Res == shellEd.Return {
fmt.Println(" <- the correct value was returned")
} else {
Expand Down
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module rye

go 1.18

require (
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/pkg/profile v1.6.0 // indirect
github.com/pkg/term v1.1.0 // indirect
github.com/refaktor/go-peg v0.0.0-20220116201714-31e3dfa8dc7d // indirect
github.com/refaktor/liner v1.2.3 // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/pkg/profile v1.6.0 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pkg/term v1.1.0 h1:xIAAdCMh3QIAy+5FrE8Ad8XoDhEU4ufwbaSozViP9kk=
github.com/pkg/term v1.1.0/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
github.com/refaktor/go-peg v0.0.0-20220116201714-31e3dfa8dc7d h1:FXrWUGgPRzhaZIBho8zNLSrMp0VpP8E9+wbRRnJYlbE=
github.com/refaktor/go-peg v0.0.0-20220116201714-31e3dfa8dc7d/go.mod h1:iIkrsFobLIWX8kQ6Oqj4cl4nwdMSE92DWpWwk9YlG9s=
github.com/refaktor/liner v1.2.3 h1:ZPLJqA7B11m+9lz1YGYfclqCWPWYtjxPq61EF1hPwzw=
github.com/refaktor/liner v1.2.3/go.mod h1:ziZSGVYZ4OzZ9kbeB254MtIrxxQlDibULRQGlDi1iK8=
golang.org/x/net v0.0.0-20220708220712-1185a9018129 h1:vucSRfWwTsoXro7P+3Cjlr6flUMtzCwzlvkxEQtHHB0=
golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 h1:kwrAHlwJ0DUBZwQ238v+Uod/3eZ8B2K5rYsUHBQvzmI=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13 changes: 10 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ func parseNumber(v *Values, d Any) (Any, error) {
return env.Integer{val}, er
}

func parseDecimal(v *Values, d Any) (Any, error) {
val, er := strconv.ParseFloat(v.Token(), 64)
return env.Decimal{val}, er
}

func parseString(v *Values, d Any) (Any, error) {
return env.String{v.Token()[1 : len(v.Token())-1]}, nil
}
Expand Down Expand Up @@ -339,7 +344,7 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
BLOCK <- "{" SPACES SERIES* "}"
BBLOCK <- "[" SPACES SERIES* "]"
GROUP <- "(" SPACES SERIES* ")"
SERIES <- (COMMENT / URI / EMAIL / STRING / NUMBER / COMMA / SETWORD / LSETWORD / ONECHARPIPE / PIPEWORD / XWORD / OPWORD / TAGWORD / EXWORD / CPATH / FPATH / KINDWORD / GENWORD / GETWORD / VOID / WORD / BLOCK / GROUP / BBLOCK / ARGBLOCK ) SPACES
SERIES <- (COMMENT / URI / EMAIL / STRING / DECIMAL / NUMBER / COMMA / SETWORD / LSETWORD / ONECHARPIPE / PIPEWORD / XWORD / OPWORD / TAGWORD / EXWORD / CPATH / FPATH / KINDWORD / GENWORD / GETWORD / VOID / WORD / BLOCK / GROUP / BBLOCK / ARGBLOCK ) SPACES
ARGBLOCK <- "{" WORD ":" WORD "}"
WORD <- LETTER LETTERORNUM*
GENWORD <- UCLETTER LCLETTERORNUM*
Expand Down Expand Up @@ -367,8 +372,9 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
LETTERORNUM <- < [a-zA-Z0-9-?=.\\!_+<>\]*()] >
URIPATH <- < [a-zA-Z0-9-?=.:@/\\!_> ()] >
UCLETTER <- < [A-Z] >
LCLETTERORNUM <- < [a-z0-9] >
NUMBER <- < [0-9]+ >
LCLETTERORNUM <- < [a-z0-9] >
NUMBER <- < [0-9]+ >
DECIMAL <- < [0-9]+.[0-9]+ >
SPACE <- < [ \t\r\n] >
STRINGCHAR <- < !'"' . >
STRINGCHAR1 <- < !"$" . >
Expand Down Expand Up @@ -403,6 +409,7 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
g["GENWORD"].Action = parseGenword
g["GETWORD"].Action = parseGetword
g["NUMBER"].Action = parseNumber
g["DECIMAL"].Action = parseDecimal
g["STRING"].Action = parseString
g["EMAIL"].Action = parseEmail
g["URI"].Action = parseUri
Expand Down
22 changes: 12 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"rye/util"

"net/http/cgi"
"rye/ryeco"
//"rye/ryeco"
// enable when using profiler
// "github.com/pkg/profile"
)
Expand Down Expand Up @@ -188,7 +188,7 @@ func main_ryeco() {

// ryeco_do(func() env.Object { return ryeco_loop(1000, func() env.Object { return ryeco_add(1, 2) }) })

ryeco.Loop(env.Integer{10000000}, func() env.Object { return ryeco.Inc(env.Integer{1}) })
// ryeco.Loop(env.Integer{10000000}, func() env.Object { return ryeco.Inc(env.Integer{1}) })

}

Expand Down Expand Up @@ -224,7 +224,7 @@ func main_cgi_file(file string, sig bool) {
//util.PrintHeader()
//defer profile.Start(profile.CPUProfile).Stop()

input := " whoami: \"Rye cgi 0.001 alpha\" ctx: 0 result: \"\" session: 0 w: 0 r: 0"
input := " 123 " //" whoami: \"Rye cgi 0.001 alpha\" ctx: 0 result: \"\" session: 0 w: 0 r: 0"
block, genv := loader.LoadString(input, false)
es := env.NewProgramState(block.(env.Block).Series, genv)
evaldo.RegisterBuiltins(es)
Expand Down Expand Up @@ -258,18 +258,20 @@ func main_cgi_file(file string, sig bool) {

func main_rye_repl(in io.Reader, out io.Writer) {

input := "name: \"Rye\" version: \"0.002 alpha\""
input := " 123 " // "name: \"Rye\" version: \"0.011 alpha\""
user, _ := user.Current()
profile_path := filepath.Join(user.HomeDir, ".rye-profile")

fmt.Println("Welcome to Rye shell. Use ls and ls\\ \"pr\" to list the current context.")

if _, err := os.Stat(profile_path); err == nil {
content, err := ioutil.ReadFile(profile_path)
if err != nil {
log.Fatal(err)
}
input = string(content)
//content, err := ioutil.ReadFile(profile_path)
//if err != nil {
// log.Fatal(err)
//}
// input = string(content)
} else {
fmt.Print("no profile")
fmt.Println("There was no profile.")

}

Expand Down

0 comments on commit 2f06a3c

Please sign in to comment.