diff --git a/env/idxs.go b/env/idxs.go index d9e555c0..45bc4ed8 100755 --- a/env/idxs.go +++ b/env/idxs.go @@ -104,6 +104,7 @@ func NewIdxs() *Idxs { "Kindword", "Converter", "Time", + "Decimal", } for _, value := range NativeTypes { diff --git a/env/object.go b/env/object.go index ebb916a7..fb324e68 100755 --- a/env/object.go +++ b/env/object.go @@ -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 @@ -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 // diff --git a/evaldo/evaldo.go b/evaldo/evaldo.go index 7a910fcc..f1d5f19e 100755 --- a/evaldo/evaldo.go +++ b/evaldo/evaldo.go @@ -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 } diff --git a/evaldo/repl.go b/evaldo/repl.go index 442ffc72..94ceaec7 100644 --- a/evaldo/repl.go +++ b/evaldo/repl.go @@ -186,6 +186,8 @@ func DoRyeRepl(es *env.ProgramState) { line2 := "" + showResults := false + var prevResult env.Object for { @@ -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("") @@ -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 { diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..9a3f1dc8 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..9ebb83f5 --- /dev/null +++ b/go.sum @@ -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= diff --git a/loader/loader.go b/loader/loader.go index 28a02c49..2e51aa9e 100755 --- a/loader/loader.go +++ b/loader/loader.go @@ -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 } @@ -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* @@ -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 <- < !"$" . > @@ -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 diff --git a/main.go b/main.go index c4f18ba0..6d11df96 100755 --- a/main.go +++ b/main.go @@ -25,7 +25,7 @@ import ( "rye/util" "net/http/cgi" - "rye/ryeco" + //"rye/ryeco" // enable when using profiler // "github.com/pkg/profile" ) @@ -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}) }) } @@ -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) @@ -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.") }