Skip to content

Commit

Permalink
improvements to xmlgen recursive example, walk function for recursibe…
Browse files Browse the repository at this point in the history
… block handling , smaller improvements
  • Loading branch information
refaktor committed Mar 27, 2024
1 parent acd7a00 commit 649b6d1
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 60 deletions.
39 changes: 24 additions & 15 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ func (b Block) Inspect(e Idxs) string {
r.WriteString("[Block: ")
for i := 0; i < b.Series.Len(); i += 1 {
if b.Series.Get(i) != nil {
if b.Series.GetPos() == i {
r.WriteString("^")
}
r.WriteString(b.Series.Get(i).Inspect(e))
r.WriteString(" ")
}
Expand Down Expand Up @@ -481,7 +484,7 @@ func (i Word) Type() Type {
}

func (i Word) Inspect(e Idxs) string {
return "[Word: " + strconv.FormatInt(int64(i.Index), 10) + ", " + i.Print(e) + "]"
return "[Word: " + i.Print(e) + "]"
}

func (i Word) Print(e Idxs) string {
Expand Down Expand Up @@ -526,7 +529,7 @@ func (i Setword) Type() Type {
}

func (i Setword) Inspect(e Idxs) string {
return "[Setword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[Setword: " + e.GetWord(i.Index) + "]"
}

func (b Setword) Print(e Idxs) string {
Expand Down Expand Up @@ -571,7 +574,7 @@ func (i LSetword) Type() Type {
}

func (i LSetword) Inspect(e Idxs) string {
return "[LSetword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[LSetword: " + e.GetWord(i.Index) + "]"
}

func (b LSetword) Print(e Idxs) string {
Expand Down Expand Up @@ -617,7 +620,7 @@ func (i Opword) Type() Type {
}

func (i Opword) Inspect(e Idxs) string {
return "[Opword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[Opword: " + e.GetWord(i.Index) + "]"
}

func (b Opword) Print(e Idxs) string {
Expand Down Expand Up @@ -668,7 +671,7 @@ func (i Pipeword) Type() Type {
}

func (i Pipeword) Inspect(e Idxs) string {
return "[Pipeword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[Pipeword: " + e.GetWord(i.Index) + "]"
}

func (b Pipeword) Print(e Idxs) string {
Expand Down Expand Up @@ -718,7 +721,7 @@ func (i Tagword) Type() Type {
}

func (i Tagword) Inspect(e Idxs) string {
return "[Tagword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[Tagword: " + e.GetWord(i.Index) + "]"
}

func (b Tagword) Print(e Idxs) string {
Expand Down Expand Up @@ -755,23 +758,29 @@ func (i Tagword) Dump(e Idxs) string {

type Xword struct {
Index int
Args string
}

func NewXword(index int) *Xword {
nat := Xword{index}
func NewXword(index int, args string) *Xword {
nat := Xword{index, args}
return &nat
}

func (i Xword) Type() Type {
return XwordType
}

// + strconv.FormatInt(int64(i.Index), 10) +
func (i Xword) Inspect(e Idxs) string {
return "[Xword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[Xword: " + e.GetWord(i.Index) + "]"
}

func (b Xword) Print(e Idxs) string {
return "<" + e.GetWord(b.Index) + ">"
spc := ""
if len(b.Args) > 0 {
spc = " "
}
return "<" + e.GetWord(b.Index) + spc + b.Args + ">"
}

func (i Xword) Trace(msg string) {
Expand All @@ -780,7 +789,7 @@ func (i Xword) Trace(msg string) {
}

func (i Xword) ToWord() Word {
return Word(i)
return *NewWord(i.Index)
}

func (i Xword) GetKind() int {
Expand Down Expand Up @@ -816,7 +825,7 @@ func (i EXword) Type() Type {
}

func (i EXword) Inspect(e Idxs) string {
return "[EXword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[EXword: " + e.GetWord(i.Index) + "]"
}

func (b EXword) Print(e Idxs) string {
Expand Down Expand Up @@ -865,7 +874,7 @@ func (i Kindword) Type() Type {
}

func (i Kindword) Inspect(e Idxs) string {
return "[Kindword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[Kindword: " + e.GetWord(i.Index) + "]"
}

func (b Kindword) Print(e Idxs) string {
Expand Down Expand Up @@ -914,7 +923,7 @@ func (i Getword) Type() Type {
}

func (i Getword) Inspect(e Idxs) string {
return "[Getword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[Getword: " + e.GetWord(i.Index) + "]"
}

func (b Getword) Print(e Idxs) string {
Expand Down Expand Up @@ -963,7 +972,7 @@ func (i Genword) Type() Type {
}

func (i Genword) Inspect(e Idxs) string {
return "[Genword: " + strconv.FormatInt(int64(i.Index), 10) + ", " + e.GetWord(i.Index) + "]"
return "[Genword: " + e.GetWord(i.Index) + "]"
}

func (b Genword) Print(e Idxs) string {
Expand Down
15 changes: 12 additions & 3 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3155,7 +3155,7 @@ var builtins = map[string]*env.Builtin{
},
},

"for-all": { // **
"walk": { // **
Argsn: 2,
Doc: "Accepts a block of values and a block of code, does the code for each of the values, injecting them.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -3165,12 +3165,21 @@ var builtins = map[string]*env.Builtin{
case env.Block:
ser := ps.Ser
ps.Ser = code.Series
for i := 0; i < block.Series.Len(); i++ {

for block.Series.GetPos() < block.Series.Len() {
ps = EvalBlockInj(ps, block, true)
if ps.ErrorFlag {
return ps.Res
}
block.Series.Next()
if ps.ReturnFlag {
return ps.Res
}
block1, ok := ps.Res.(env.Block) // TODO ... switch and throw error if not block
if ok {
block = block1
} else {
fmt.Println("ERROR 1231241")
}
ps.Ser.Reset()
}
ps.Ser = ser
Expand Down
2 changes: 1 addition & 1 deletion evaldo/builtins_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func __input(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch str := arg0.(type) {
case env.String:
fmt.Print("Enter text: " + str.Value)
fmt.Print("" + str.Value)
var input string
fmt.Scanln(&input)
fmt.Print(input)
Expand Down
59 changes: 59 additions & 0 deletions examples/xmlgen/recursive.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

private {
spaces: fn1 { .produce "" { + " " } }
myprn: fn { t d } { prn spaces d , prn t }
is-not: fn { a b } { a = b |not }
nl: does { print "" }

proc: fn\par { val blk depth current } current-ctx {
sleep 10
walk blk { :bk
.peek :tok .type? .switch {
xword {
nl ,
myprn tok depth ,
proc val next bk inc depth tok
}
exword {
print tok ,
if not tok .to-word = to-word current { print "<!-- Tag mismatch -->" } ,
return next bk
}
block {
with val tok :vv \type? .is-not 'function \if { prn vv } ,
next bk
}
string {
myprn join { "<!-- " tok " -->" } depth ,
next bk
}
}
}
}
fn\par { val blk } current-ctx { proc val blk 0 'no }

} :xmlgen

xml-template: {
<header>
<author> ( name ) </author>
</header>
<data>
<fullname> ( .fullname ) </fullname>
<name id="first"> ( -> "name" ) </name>
"Surname next"
<surname> ( -> "surname" ) </surname>
</data>
}

fullname: fn1 { -> "name" :n , -> "surname" :s , n + " " + s }
input "Enter author's name: " :name
xmlgen dict { "name" "Jim" "surname" "Metelko" } xml-template







; match: fn { val blk } { .type? .switch blk } ; TODO -- match in switch confuses return function. Look into it soon.
38 changes: 0 additions & 38 deletions examples/xmlgen/try1.rye

This file was deleted.

11 changes: 8 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,14 @@ func parseTagword(v *Values, d Any) (Any, error) {

func parseXword(v *Values, d Any) (Any, error) {
//fmt.Println("TAGWORD:" + v.Token())
word := v.Token()
idx := wordIndex.IndexWord(word[1 : len(word)-1])
return *env.NewXword(idx), nil
cont := v.Token()
conts := strings.Split(cont[1:len(cont)-1], " ")
idx := wordIndex.IndexWord(conts[0])
args := ""
if len(conts) > 1 {
args = conts[1]
}
return *env.NewXword(idx, args), nil
}

func parseKindword(v *Values, d Any) (Any, error) {
Expand Down
18 changes: 18 additions & 0 deletions wasm/dragup/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Drag and Drop File with Movable Div</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="drop-area">
<p>Drag and drop a text file here</p>
</div>
<div id="file-content" class="draggable">
<p>File content will appear here</p>
</div>

<script src="script.js"></script>
</body>
</html>

47 changes: 47 additions & 0 deletions wasm/dragup/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
document.addEventListener('DOMContentLoaded', (event) => {
let dropArea = document.getElementById('drop-area');
let fileContent = document.getElementById('file-content');

// Drag and drop
dropArea.addEventListener('dragover', (e) => {
e.preventDefault();
});

dropArea.addEventListener('drop', (e) => {
e.preventDefault();
let file = e.dataTransfer.files[0];
let reader = new FileReader();
reader.onload = function(e) {
fileContent.style.display = 'block';
fileContent.innerText = e.target.result;
};
reader.readAsText(file);
});

// Draggable div
fileContent.onmousedown = function(event) {
let shiftX = event.clientX - fileContent.getBoundingClientRect().left;
let shiftY = event.clientY - fileContent.getBoundingClientRect().top;

function moveAt(pageX, pageY) {
fileContent.style.left = pageX - shiftX + 'px';
fileContent.style.top = pageY - shiftY + 'px';
}

function onMouseMove(event) {
moveAt(event.pageX, event.pageY);
}

document.addEventListener('mousemove', onMouseMove);

fileContent.onmouseup = function() {
document.removeEventListener('mousemove', onMouseMove);
fileContent.onmouseup = null;
};

};

fileContent.ondragstart = function() {
return false;
};
});
18 changes: 18 additions & 0 deletions wasm/dragup/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#drop-area {
width: 300px;
height: 100px;
border: 2px dashed #ccc;
text-align: center;
padding: 20px;
margin-bottom: 20px;
}

.draggable {
width: 300px;
padding: 10px;
border: 1px solid black;
background-color: #f3f3f3;
cursor: move;
position: absolute;
display: none;
}

0 comments on commit 649b6d1

Please sign in to comment.