Skip to content

Commit

Permalink
Merge pull request #383 from refaktor/allin
Browse files Browse the repository at this point in the history
fixed bug with uri-s, added to-block, is-empty, ...
  • Loading branch information
refaktor authored Oct 25, 2024
2 parents 629d52e + 7131b88 commit e055e7a
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ console_*.rye

buildtemp/main.rye
*.bson
cmd/parse_builtins/parse_builtins
94 changes: 94 additions & 0 deletions cmd/parse_builtins/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strings"
)

// data that we want to optionally extract from builtins code and store as a general structure that
// * Rye runtime will be able to provide to the user
// * Test tool will run tests on
type builtinInfo struct {
name string // from key value
gentype string // optional from key value
docstring string // part of builtin definition
doc string // free text at the top of the comment
nargs int // part of builtin definition
args []string // extracted from comment or variable names
argtypes [][]string // extracted from switch statements or conversions
tests []string // extracted from comment
examples []string // extracted from comment
tags []string // extracted from comment
}

// Helper function to get comments above the map key
func getCommentsAboveKey(fset *token.FileSet, comments []*ast.CommentGroup, keyPos token.Pos) string {
for _, commentGroup := range comments {
if fset.Position(commentGroup.End()).Line == fset.Position(keyPos).Line-1 {
return commentGroup.Text()
}
}
return ""
}

func main() {

infoList := make([]builtinInfo, 0)

// Check if a filename is provided as an argument
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <filename>")
return
}

// Get the filename from the first argument
filename := os.Args[1]

// Create a new token file set
fset := token.NewFileSet()

// Parse the Go source code into an AST
node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
fmt.Println("Error parsing Go code:", err)
return
}

// Traverse the AST and find map literals
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CompositeLit:
// Check if the literal is a map type
if _, ok := x.Type.(*ast.MapType); ok {
// Process each key-value pair in the map
for _, elt := range x.Elts {
info := builtinInfo{}
if kv, ok := elt.(*ast.KeyValueExpr); ok {
if key, ok := kv.Key.(*ast.BasicLit); ok {
// Extract the key

fmt.Printf("Key: %s\n", key.Value)
// TODO NEXT - parse key into two values
info.name = key.Value
// Get comments above the key
comment := getCommentsAboveKey(fset, node.Comments, key.Pos())
if comment != "" {
fmt.Printf("Comment above key: %s\n", strings.TrimSpace(comment))
}
// TODO NEXT - make a function that parses the comment extracting doc, args (opt), examples, tests and tags
info.doc = strings.TrimSpace(comment)
}
}
infoList = append(infoList, info)
}
}
}
return true
})

fmt.Println(infoList)
}
2 changes: 1 addition & 1 deletion env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (i RyeCtx) Equal(o Object) bool {
func (i RyeCtx) Dump(e Idxs) string {
var bu strings.Builder
bu.WriteString("context {\n")
bu.WriteString(fmt.Sprintf("doc \"%s\"\n", i.Doc))
//bu.WriteString(fmt.Sprintf("doc \"%s\"\n", i.Doc))
bu.WriteString(i.DumpBare(e))
bu.WriteString("}")
return bu.String()
Expand Down
25 changes: 22 additions & 3 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ type Object interface {
Dump(e Idxs) string
}

func NewBoolean(val bool) *Integer {
var ret int64
if val {
ret = 1
} else {
ret = 0
}
nat := Integer{ret}
return &nat
}

//
// INTEGER
//
Expand Down Expand Up @@ -285,9 +296,15 @@ func NewFileUri(index *Idxs, path string) *Uri {

func NewUri(index *Idxs, scheme Word, path string) *Uri {
scheme2 := strings.Split(path, "://")
kindstr := strings.Split(path, "://")[0] + "-schema" // TODO -- this is just temporary .. so we test it further, make proper once at that level
kindstr := index.GetWord(scheme.Index) + "-schema" // TODO -- this is just temporary .. so we test it further, make proper once at that level
idx := index.IndexWord(kindstr)
nat := Uri{scheme, scheme2[1], Word{idx}}
var path2 string
if len(scheme2) > 1 { // TODO --- look at all this code and improve, this is just BAD
path2 = scheme2[1]
} else {
path2 = path
}
nat := Uri{scheme, path2, Word{idx}}
// nat := Uri{Word{idxSch}, scheme2[1], Word{idxKind}}
return &nat
}
Expand Down Expand Up @@ -1854,7 +1871,7 @@ func NewList(data []any) *List {
return &List{data, Word{0}}
}

func RyeToRaw(res Object) any { // TODO -- MOVE TO UTIL ... provide reverse named symmetrically
func RyeToRaw(res Object, idx *Idxs) any { // TODO -- MOVE TO UTIL ... provide reverse named symmetrically
// fmt.Printf("Type: %T", res)
switch v := res.(type) {
case nil:
Expand All @@ -1875,6 +1892,8 @@ func RyeToRaw(res Object) any { // TODO -- MOVE TO UTIL ... provide reverse name
return v
case *List:
return *v
case Uri: // Open question: what should list do with values that don't have raw equivalents
return idx.GetWord(v.Scheme.Index) + v.Path
default:
return "not handeled 2"
// TODO-FIXME
Expand Down
84 changes: 66 additions & 18 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,22 @@ var builtins = map[string]*env.Builtin{
},
},

// Tests:
// equals { list [ 1 2 3 ] |to-block |first } 1
"to-block": { // ***
Argsn: 1,
Doc: "Turns a List to a Block",
Pure: true,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch list := arg0.(type) {
case env.List:
return util.List2Block(ps, list)
default:
return MakeArgError(ps, 1, []env.Type{env.DictType}, "to-context")
}
},
},

"to-context": { // ***
Argsn: 1,
Doc: "Takes a Dict and returns a Context with same names and values.",
Expand Down Expand Up @@ -1667,17 +1683,24 @@ var builtins = map[string]*env.Builtin{
}
},
},

// Tests:
// equals { embed 101 "val {}" } "val 101"
"embed": { // **
Argsn: 2,
Doc: "Embeds a value in string.",
Doc: "Embeds a value into a string with {} placeholder.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch arg := arg1.(type) {
switch val := arg1.(type) {
case env.String:
vals := arg0.Print(*ps.Idx)
news := strings.ReplaceAll(arg.Value, "{}", vals)
news := strings.ReplaceAll(val.Value, "{}", vals)
return *env.NewString(news)
case env.Uri:
vals := arg0.Print(*ps.Idx)
news := strings.ReplaceAll(val.Path, "{}", vals)
return *env.NewUri(ps.Idx, val.Scheme, news)
default:
return MakeArgError(ps, 1, []env.Type{env.StringType}, "esc")
return MakeArgError(ps, 1, []env.Type{env.StringType, env.UriType}, "embed")
}
},
},
Expand Down Expand Up @@ -2981,7 +3004,7 @@ var builtins = map[string]*env.Builtin{

// EXPERIMENTAL: COLLECTION AND RETURNING FUNCTIONS ... NOT DETERMINED IF WILL BE INCLUDED YET

"returns": {
"returns!": {
Argsn: 1,
Doc: "Sets up a value to return at the end of function.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -2990,7 +3013,7 @@ var builtins = map[string]*env.Builtin{
},
},

"collect": {
"collect!": {
Argsn: 1,
Doc: "Collects values into an implicit block.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -3004,7 +3027,7 @@ var builtins = map[string]*env.Builtin{
},
},

"collect-key": {
"collect-key!": {
Argsn: 2,
Doc: "Collects key value pars to implicit block.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -3019,7 +3042,7 @@ var builtins = map[string]*env.Builtin{
},
},

"collect-update-key": {
"collect-update-key!": {
Argsn: 2,
Doc: "Collects key value pars to implicit block.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -3042,7 +3065,7 @@ var builtins = map[string]*env.Builtin{
},
},

"pop-collected": {
"pop-collected!": {
Argsn: 0,
Doc: "Returns the implicit collected data structure and resets it.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down Expand Up @@ -3969,7 +3992,7 @@ var builtins = map[string]*env.Builtin{
if ps.ErrorFlag {
return ps.Res
}
newl[i] = env.RyeToRaw(ps.Res)
newl[i] = env.RyeToRaw(ps.Res, ps.Idx)
ps.Ser.Reset()
}
ps.Ser = ser
Expand Down Expand Up @@ -4068,7 +4091,7 @@ var builtins = map[string]*env.Builtin{
if ps.ErrorFlag {
return ps.Res
}
newl[i] = env.RyeToRaw(ps.Res)
newl[i] = env.RyeToRaw(ps.Res, ps.Idx)
ps.Ser.Reset()
}
ps.Ser = ser
Expand Down Expand Up @@ -4161,7 +4184,7 @@ var builtins = map[string]*env.Builtin{
if ps.ErrorFlag {
return ps.Res
}
newl[i] = env.RyeToRaw(ps.Res)
newl[i] = env.RyeToRaw(ps.Res, ps.Idx)
ps.Ser.Reset()
}
ps.Ser = ser
Expand Down Expand Up @@ -4706,7 +4729,7 @@ var builtins = map[string]*env.Builtin{
}
switch ee := entry.(type) { // list in dict is a pointer
case *env.List:
ee.Data = append(ee.Data, env.RyeToRaw(curval))
ee.Data = append(ee.Data, env.RyeToRaw(curval, ps.Idx))
default:
return MakeBuiltinError(ps, "Entry type should be List.", "group")
}
Expand Down Expand Up @@ -4738,7 +4761,7 @@ var builtins = map[string]*env.Builtin{
}
switch ee := entry.(type) { // list in dict is a pointer
case *env.List:
ee.Data = append(ee.Data, env.RyeToRaw(curval))
ee.Data = append(ee.Data, env.RyeToRaw(curval, ps.Idx))
default:
return MakeBuiltinError(ps, "Entry type should be List.", "group")
}
Expand Down Expand Up @@ -5262,7 +5285,7 @@ var builtins = map[string]*env.Builtin{
uniqueStr := ""
// converting object to string and append final
for _, value := range uniqueStringSlice {
uniqueStr = uniqueStr + env.RyeToRaw(value).(string)
uniqueStr = uniqueStr + env.RyeToRaw(value, ps.Idx).(string)
}
return *env.NewString(uniqueStr)
default:
Expand Down Expand Up @@ -7123,7 +7146,7 @@ var builtins = map[string]*env.Builtin{
if num.Value > int64(len(s1.Data)) {
return MakeBuiltinError(ps, fmt.Sprintf("List has less than %d elements.", num.Value), "change\\nth!")
}
s1.Data[num.Value-1] = env.RyeToRaw(arg2)
s1.Data[num.Value-1] = env.RyeToRaw(arg2, ps.Idx)
return s1
default:
return MakeArgError(ps, 1, []env.Type{env.BlockType}, "change\\nth!")
Expand Down Expand Up @@ -7806,7 +7829,33 @@ var builtins = map[string]*env.Builtin{
}
},
},

// Tests:
// equals { { } .is-empty } 1
"is-empty": { // **
Argsn: 1,
Doc: "Accepts a collection (String, Block, Dict, Spreadsheet) and returns it's length.", // TODO -- accept list, context also
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.String:
return *env.NewBoolean(len(s1.Value) == 0)
case env.Dict:
return *env.NewBoolean(len(s1.Data) == 0)
case env.List:
return *env.NewBoolean(len(s1.Data) == 0)
case env.Block:
return *env.NewBoolean(s1.Series.Len() == 0)
case env.Spreadsheet:
return *env.NewBoolean(len(s1.Rows) == 0)
case env.RyeCtx:
return *env.NewBoolean(s1.GetWords(*ps.Idx).Series.Len() == 0)
case env.Vector:
return *env.NewBoolean(s1.Value.Len() == 0)
default:
fmt.Println(s1)
return MakeArgError(ps, 2, []env.Type{env.StringType, env.DictType, env.ListType, env.BlockType, env.SpreadsheetType, env.VectorType}, "length?")
}
},
},
"length?": { // **
Argsn: 1,
Doc: "Accepts a collection (String, Block, Dict, Spreadsheet) and returns it's length.", // TODO -- accept list, context also
Expand Down Expand Up @@ -7980,7 +8029,6 @@ var builtins = map[string]*env.Builtin{

err := r.Run()
if err != nil {
fmt.Println("ERROR")
fmt.Println(err)
}
default:
Expand Down
Loading

0 comments on commit e055e7a

Please sign in to comment.