Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modwords - fist support for modwords, change to setwords #210

Merged
merged 12 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ dist/
tests/*.html
shell_*.rye
console_*.rye

buildtemp/main.rye
39 changes: 0 additions & 39 deletions _sandbox/ryel.rye

This file was deleted.

12 changes: 12 additions & 0 deletions bin/ryel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# This is a first **proof of concept** only, the naming, the functionality might change. Please propose changes if you see any faults,
# especially security implications.

FILE=$PWD/ryel
if [ -f "$FILE" ]; then
# echo "Starting local rye interpreter"
$FILE "$@"
else
echo "You don't have local Ryel binary yet. Define ryel.mod if needed and run ryelc build"
fi
8 changes: 8 additions & 0 deletions bin/ryelc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# This is a first **proof of concept** only, the naming, the functionality might change. Please propose changes if you see any faults,
# especially security implications.

# Assumes normal rye build (at least tiny is on the PATH), but we don't define RYE_HOME or anything like this for now, maybe we should

rye $RYE_HOME/util/ryel.rye $@
26 changes: 24 additions & 2 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,30 @@ 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) Unset(word int) Object {
if _, exists := e.state[word]; !exists {
return NewError("Can't unset non-existing word in this context")
} else {
delete(e.state, word)
return NewInteger(1)
}
}

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
105 changes: 101 additions & 4 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 @@ -410,7 +414,7 @@ func (b Block) Inspect(e Idxs) string {

func (b Block) Print(e Idxs) string {
var r strings.Builder
r.WriteString("{ ")
// r.WriteString("{ ")
for i := 0; i < b.Series.Len(); i += 1 {
if b.Series.Get(i) != nil {
r.WriteString(b.Series.Get(i).Print(e))
Expand All @@ -419,7 +423,7 @@ func (b Block) Print(e Idxs) string {
r.WriteString("[NIL]")
}
}
r.WriteString("}")
// r.WriteString("}")
return r.String()
}

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 Expand Up @@ -1448,6 +1542,7 @@ func (i Argword) Dump(e Idxs) string {
//

type CPath struct {
Mode int // 0 Cpath, 1 OpCpath , 2 PipeCPath
Cnt int
Word1 Word
Word2 Word
Expand Down Expand Up @@ -1491,15 +1586,17 @@ func (i CPath) GetKind() int {
return int(CPathType)
}

func NewCPath2(w1 Word, w2 Word) *CPath {
func NewCPath2(mode int, w1 Word, w2 Word) *CPath {
var cp CPath
cp.Mode = mode
cp.Cnt = 2
cp.Word1 = w1
cp.Word2 = w2
return &cp
}
func NewCPath3(w1 Word, w2 Word, w3 Word) *CPath {
func NewCPath3(mode int, w1 Word, w2 Word, w3 Word) *CPath {
var cp CPath
cp.Mode = mode
cp.Cnt = 3
cp.Word1 = w1
cp.Word2 = w2
Expand Down
35 changes: 25 additions & 10 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object {
if posMode {
idx--
}
v := s1.Data[idx]
ok := true
if ok {
if len(s1.Data) >= int(idx)+1 {
v := s1.Data[idx]
return env.ToRyeValue(v)
} else {
ps.FailureFlag = true
Expand All @@ -249,9 +248,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object {
if posMode {
idx--
}
v := s1.Data[idx]
ok := true
if ok {
if len(s1.Data) >= int(idx)+1 {
v := s1.Data[idx]
return env.ToRyeValue(v)
} else {
ps.FailureFlag = true
Expand All @@ -265,9 +263,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object {
if posMode {
idx--
}
v := s1.Series.Get(int(idx))
ok := true
if ok {
if len(s1.Series.S) >= int(idx)+1 {
v := s1.Series.Get(int(idx))
return v
} else {
ps.FailureFlag = true
Expand Down Expand Up @@ -725,6 +722,20 @@ var builtins = map[string]*env.Builtin{
},
},

"unset!": { // ***
Argsn: 1,
Doc: "Unset a word in current context",
Pure: false,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch word := arg0.(type) {
case env.Word:
return ps.Ctx.Unset(word.Index)
default:
return MakeArgError(ps, 1, []env.Type{env.WordType}, "set")
}
},
},

"get_": { // *** find a name or decide on order of naming with generic words clashes with
Argsn: 1,
Doc: "Returns value of the word in context",
Expand Down Expand Up @@ -7405,7 +7416,11 @@ var builtins = map[string]*env.Builtin{
Argsn: 1,
Doc: "",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
return *env.NewString(strings.Join(os.Args[2:], " "))
if len(os.Args) > 1 {
return *env.NewString(strings.Join(os.Args[2:], " "))
} else {
return *env.NewString("")
}
// block, _ := loader.LoadString(os.Args[0], false)
// return block
},
Expand Down
3 changes: 1 addition & 2 deletions evaldo/builtins_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func __input(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Ob
func __open(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s := arg0.(type) {
case env.Uri:
path := strings.Split(s.Path, "://")
file, err := os.Open(path[1])
file, err := os.Open(s.Path)
if err != nil {
return makeError(ps, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ var Builtins_math = map[string]*env.Builtin{
return DialectMath(ps, arg0)
},
},
"math": {
"calc": {
Argsn: 1,
Doc: "Do math dialect",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down
2 changes: 1 addition & 1 deletion evaldo/builtins_psutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"ls": {
"lsd": {
Argsn: 0,
Doc: "Returns current working directory.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down
Loading