Skip to content

Commit

Permalink
adding support for modwords and lmodwords , also partially opcpaths a…
Browse files Browse the repository at this point in the history
…nd pipecpaths
  • Loading branch information
refaktor committed May 8, 2024
1 parent 6cc104e commit f326b05
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 6 deletions.
17 changes: 15 additions & 2 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,21 @@ func (e *RyeCtx) Get2(word int) (Object, bool, *RyeCtx) {
}

func (e *RyeCtx) Set(word int, val Object) Object {
e.state[word] = val
return val
if _, exists := e.state[word]; exists {
return NewError("Can't set already set word, try using modword")
} else {
e.state[word] = val
return val
}
}

func (e *RyeCtx) Mod(word int, val Object) Object {
if _, exists := e.state[word]; exists {
e.state[word] = val
return val
} else {
return NewError("Can't mod an unset word, try using setword")
}
}

type ProgramState struct {
Expand Down
4 changes: 4 additions & 0 deletions env/idxs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var NativeTypes = [...]string{ // Todo change to BuiltinTypes
"SpreadsheetRowType",
"Decimal",
"Vector",
"OpCPath",
"PipeCPath",
"Modword",
"LModword",
}

func (e *Idxs) IndexWord(w string) int {
Expand Down
94 changes: 94 additions & 0 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const (
SpreadsheetRowType Type = 33
DecimalType Type = 34
VectorType Type = 35
OpCPathType Type = 36
PipeCPathType Type = 37
ModwordType Type = 38
LModwordType Type = 39
)

// after adding new type here, also add string to idxs.go
Expand Down Expand Up @@ -601,6 +605,96 @@ func (i LSetword) Dump(e Idxs) string {
return ":" + e.GetWord(i.Index)
}

//
// MODWORD
//

type Modword struct {
Index int
}

func NewModword(index int) *Modword {
nat := Modword{index}
return &nat
}

func (i Modword) Type() Type {
return ModwordType
}

func (i Modword) Inspect(e Idxs) string {
return "[Modword: " + e.GetWord(i.Index) + "]"
}

func (b Modword) Print(e Idxs) string {
return e.GetWord(b.Index) + ":"
}

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

func (i Modword) GetKind() int {
return int(ModwordType)
}

func (i Modword) Equal(o Object) bool {
if i.Type() != o.Type() {
return false
}
return i.Index == o.(Modword).Index
}

func (i Modword) Dump(e Idxs) string {
return e.GetWord(i.Index) + ":"
}

//
// LMODWORD
//

type LModword struct {
Index int
}

func NewLModword(index int) *LModword {
nat := LModword{index}
return &nat
}

func (i LModword) Type() Type {
return LModwordType
}

func (i LModword) Inspect(e Idxs) string {
return "[LModword: " + e.GetWord(i.Index) + "]"
}

func (b LModword) Print(e Idxs) string {
return ":" + e.GetWord(b.Index)
}

func (i LModword) Trace(msg string) {
fmt.Print(msg + "(lModword): ")
fmt.Println(i.Index)
}

func (i LModword) GetKind() int {
return int(LModwordType)
}

func (i LModword) Equal(o Object) bool {
if i.Type() != o.Type() {
return false
}
return i.Index == o.(LModword).Index
}

func (i LModword) Dump(e Idxs) string {
return ":" + e.GetWord(i.Index)
}

//
// OPWORD
//
Expand Down
19 changes: 18 additions & 1 deletion evaldo/evaldo.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ func EvalExpressionConcrete(ps *env.ProgramState) *env.ProgramState {
return EvalGenword(ps, object.(env.Genword), nil, false)
case env.SetwordType:
return EvalSetword(ps, object.(env.Setword))
case env.ModwordType:
return EvalModword(ps, object.(env.Modword))
case env.GetwordType:
return EvalGetword(ps, object.(env.Getword), nil, false)
case env.CommaType:
Expand Down Expand Up @@ -495,7 +497,22 @@ func EvalSetword(ps *env.ProgramState, word env.Setword) *env.ProgramState {
// es1 := EvalExpression(es)
ps1, _ := EvalExpressionInj(ps, nil, false)
idx := word.Index
ps1.Ctx.Set(idx, ps1.Res)
ps1.Res = ps1.Ctx.Set(idx, ps1.Res)
if ps1.Res.Type() == env.ErrorType {
ps1.ErrorFlag = true
}
return ps1
}

// evaluates expression to the right and sets the result of it to a word in current context
func EvalModword(ps *env.ProgramState, word env.Modword) *env.ProgramState {
// es1 := EvalExpression(es)
ps1, _ := EvalExpressionInj(ps, nil, false)
idx := word.Index
ps1.Res = ps1.Ctx.Mod(idx, ps1.Res)
if ps1.Res.Type() == env.ErrorType {
ps1.ErrorFlag = true
}
return ps1
}

Expand Down
24 changes: 23 additions & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,20 @@ func parseLSetword(v *Values, d Any) (Any, error) {
return *env.NewLSetword(idx), nil
}

func parseModword(v *Values, d Any) (Any, error) {
//fmt.Println("SETWORD:" + v.Token())
word := v.Token()
idx := wordIndex.IndexWord(word[:len(word)-2])
return *env.NewModword(idx), nil
}

func parseLModword(v *Values, d Any) (Any, error) {
//fmt.Println("SETWORD:" + v.Token())
word := v.Token()
idx := wordIndex.IndexWord(word[2:])
return *env.NewLModword(idx), nil
}

func parseOpword(v *Values, d Any) (Any, error) {
//fmt.Println("OPWORD:" + v.Token())
word := v.Token()
Expand Down Expand Up @@ -404,12 +418,14 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
BLOCK <- "{" SPACES SERIES* "}"
BBLOCK <- "[" SPACES SERIES* "]"
GROUP <- "(" SPACES SERIES* ")"
SERIES <- (GROUP / COMMENT / URI / EMAIL / STRING / DECIMAL / NUMBER / COMMA / SETWORD / LSETWORD / ONECHARPIPE / PIPEWORD / EXWORD / XWORD / OPWORD / TAGWORD / CPATH / FPATH / KINDWORD / GENWORD / GETWORD / WORD / VOID / BLOCK / GROUP / BBLOCK / ARGBLOCK ) SPACES
SERIES <- (GROUP / COMMENT / URI / EMAIL / STRING / DECIMAL / NUMBER / COMMA / MODWORD / SETWORD / LMODWORD / LSETWORD / ONECHARPIPE / PIPECPATH / PIPEWORD / EXWORD / XWORD / OPCPATH / OPWORD / TAGWORD / CPATH / FPATH / KINDWORD / GENWORD / GETWORD / WORD / VOID / BLOCK / GROUP / BBLOCK / ARGBLOCK ) SPACES
ARGBLOCK <- "{" WORD ":" WORD "}"
WORD <- LETTER LETTERORNUM* / NORMOPWORDS
GENWORD <- "~" UCLETTER LCLETTERORNUM*
SETWORD <- LETTER LETTERORNUM* ":"
MODWORD <- LETTER LETTERORNUM* "::"
LSETWORD <- ":" LETTER LETTERORNUM*
LMODWORD <- "::" LETTER LETTERORNUM*
GETWORD <- "?" LETTER LETTERORNUM*
PIPEWORD <- "\\" LETTER LETTERORNUM* / "|" LETTER LETTERORNUM* / PIPEARROWS / "|" NORMOPWORDS
ONECHARPIPE <- "|" ONECHARWORDS
Expand All @@ -425,6 +441,8 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
EMAILPART <- < ([a-zA-Z0-9._]+) >
FPATH <- "%" URIPATH*
CPATH <- WORD ( "/" WORD )+
OPCPATH <- "." WORD ( "/" WORD )+
PIPECPATH <- "\\" WORD ( "/" WORD )+
ONECHARWORDS <- < [<>*+-=/] >
NORMOPWORDS <- < ("_"[<>*+-=/]) >
PIPEARROWS <- ">>" / "~>" / "->"
Expand Down Expand Up @@ -462,6 +480,8 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
g["VOID"].Action = parseVoid
g["SETWORD"].Action = parseSetword
g["LSETWORD"].Action = parseLSetword
g["MODWORD"].Action = parseModword
g["LMODWORD"].Action = parseLModword
g["OPWORD"].Action = parseOpword
g["PIPEWORD"].Action = parsePipeword
g["ONECHARPIPE"].Action = parseOnecharpipe
Expand All @@ -478,6 +498,8 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
g["URI"].Action = parseUri
g["FPATH"].Action = parseFpath
g["CPATH"].Action = parseCPath
g["OPCPATH"].Action = parseCPath
g["PIPECPATH"].Action = parseCPath
g["COMMENT"].Action = parseComment
/* g["SERIES"].Action = func(v *Values, d Any) (Any, error) {
return v, nil
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,11 @@ func main_cgi_file(file string, sig bool) {
}

func main_rye_repl(_ io.Reader, _ io.Writer, subc bool, here bool) {
input := " 123 " // "name: \"Rye\" version: \"0.011 alpha\""
input := " " // "name: \"Rye\" version: \"0.011 alpha\""
// userHomeDir, _ := os.UserHomeDir()
// profile_path := filepath.Join(userHomeDir, ".rye-profile")

fmt.Println("Welcome to Rye console. Use ls for current or lsp or lsp\\ \"prin\" to list parent context.")
fmt.Println("Welcome to Rye console. Use ls to list current or lsp and lsp\\ \"prin\" to list parent contexts.")

//if _, err := os.Stat(profile_path); err == nil {
//content, err := os.ReadFile(profile_path)
Expand Down

0 comments on commit f326b05

Please sign in to comment.