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

Xmlgen #167

Merged
merged 6 commits into from
Mar 26, 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
4 changes: 2 additions & 2 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ func (i Xword) Inspect(e Idxs) string {
}

func (b Xword) Print(e Idxs) string {
return e.GetWord(b.Index)
return "<" + e.GetWord(b.Index) + ">"
}

func (i Xword) Trace(msg string) {
Expand Down Expand Up @@ -820,7 +820,7 @@ func (i EXword) Inspect(e Idxs) string {
}

func (b EXword) Print(e Idxs) string {
return e.GetWord(b.Index)
return "</" + e.GetWord(b.Index) + ">"
}

func (i EXword) Trace(msg string) {
Expand Down
67 changes: 67 additions & 0 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,18 @@ var builtins = map[string]*env.Builtin{
return *env.NewWord(idx)
case env.Word:
return *env.NewWord(str.Index)
case env.Xword:
return *env.NewWord(str.Index)
case env.EXword:
return *env.NewWord(str.Index)
case env.Tagword:
return *env.NewWord(str.Index)
case env.Setword:
return *env.NewWord(str.Index)
case env.LSetword:
return *env.NewWord(str.Index)
case env.Getword:
return *env.NewWord(str.Index)
default:
return MakeArgError(ps, 1, []env.Type{env.StringType, env.WordType}, "to-word")
}
Expand Down Expand Up @@ -622,6 +634,31 @@ var builtins = map[string]*env.Builtin{
},
},

"dec!": { // ***
Argsn: 1,
Doc: "Searches for a word and increments it's integer value in-place.",
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 arg := arg0.(type) {
case env.Word:
intval, found, ctx := ps.Ctx.Get2(arg.Index)
if found {
switch iintval := intval.(type) {
case env.Integer:
ctx.Set(arg.Index, *env.NewInteger(iintval.Value - 1))
return *env.NewInteger(1 + iintval.Value)
default:
return MakeBuiltinError(ps, "Value in word is not integer.", "inc!")
}
}
return MakeBuiltinError(ps, "Word not found in context.", "inc!")

default:
return MakeArgError(ps, 1, []env.Type{env.WordType}, "inc!")
}
},
},

"change!": { // ***
Argsn: 2,
Doc: "Searches for a word and changes it's value in-place. If value changes returns true otherwise false",
Expand Down Expand Up @@ -2886,6 +2923,7 @@ var builtins = map[string]*env.Builtin{
acc := arg1
ser := ps.Ser
ps.Ser = bloc.Series
ps.Res = arg1
for i := 0; int64(i) < cond.Value; i++ {
ps = EvalBlockInj(ps, acc, true)
if ps.ErrorFlag {
Expand Down Expand Up @@ -3117,6 +3155,35 @@ var builtins = map[string]*env.Builtin{
},
},

"for-all": { // **
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 {
switch block := arg0.(type) {
case env.Block:
switch code := arg1.(type) {
case env.Block:
ser := ps.Ser
ps.Ser = code.Series
for i := 0; i < block.Series.Len(); i++ {
ps = EvalBlockInj(ps, block, true)
if ps.ErrorFlag {
return ps.Res
}
block.Series.Next()
ps.Ser.Reset()
}
ps.Ser = ser
return ps.Res
default:
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "for")
}
default:
return MakeArgError(ps, 1, []env.Type{env.BlockType}, "for")
}
},
},

// Higher order functions

"purge": { // TODO ... doesn't fully work
Expand Down
38 changes: 38 additions & 0 deletions examples/xmlgen/try1.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

private {

prn\depth: fn { t d } { prn produce d "" { + " " } , prn t }
is-not: fn { a b } { a = b |not }
; enter-console "asdasd"

proc: fnc { val blk depth current } current-ctx {
sleep 1000
for-all blk { :bk
switch type? tok: peek bk {
xword { print "" , prn\depth tok depth , proc val next bk inc depth tok } ; todo -- maybe make it recursive so you know current word
exword { print tok , depth: depth - 1 , tok .to-word = to-word current \not \if { print "Error" } , return 1 }
block { with val tok \type? .is-not 'function \if { .prn } }
string { prn\depth join { "<!-- " tok " -->" } depth }
}
}
}
fnc { val blk } current-ctx { proc val blk 0 'no }
} :xmlgen

xml-template: {

<header>
<author> ( name ) </author>
</header
<data>
<name code="asda"> ( .fullname ) </name>
"Surname next"
<surname> ( -> "surname" ) </surname>
</data>
}

do { fullname: fn1 { -> "name" :n , -> "surname" :s , n + " " + s } }
do { input "Enter author's name:" :name , does { } }

xmlgen dict { "name" "Jim" "surname" "Metelko" } xml-template

6 changes: 4 additions & 2 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ 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 / XWORD / OPWORD / TAGWORD / EXWORD / CPATH / FPATH / KINDWORD / GENWORD / GETWORD / WORD / VOID / BLOCK / GROUP / BBLOCK / ARGBLOCK ) SPACES
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
ARGBLOCK <- "{" WORD ":" WORD "}"
WORD <- LETTER LETTERORNUM* / NORMOPWORDS
GENWORD <- "~" UCLETTER LCLETTERORNUM*
Expand All @@ -409,7 +409,7 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
OPWORD <- "." LETTER LETTERORNUM* / "." NORMOPWORDS / OPARROWS / ONECHARWORDS / "[*" LETTERORNUM*
TAGWORD <- "'" LETTER LETTERORNUM*
KINDWORD <- "~(" LETTER LETTERORNUM* ")~"?
XWORD <- "<" LETTER LETTERORNUM* ">"?
XWORD <- "<" LETTER LETTERORNUMNOX* " "? XPARAMS* ">"
EXWORD <- "</" LETTER LETTERORNUM* ">"?
STRING <- ('"' STRINGCHAR* '"') / ("$" STRINGCHAR1* "$")
SPACES <- SPACE+
Expand All @@ -424,6 +424,8 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
OPARROWS <- "<<" / "<~" / "<-" / ">=" / "<="
LETTER <- < [a-zA-Z^(` + "`" + `] >
LETTERORNUM <- < [a-zA-Z0-9-?=.\\!_+<>\]*()] >
LETTERORNUMNOX <- < [a-zA-Z0-9-?=.\\!_+\]*()] >
XPARAMS <- < !">" . >
URIPATH <- < [a-zA-Z0-9-?&=.,:@/\\!_> ()] >
UCLETTER <- < [A-Z] >
LCLETTERORNUM <- < [a-z0-9] >
Expand Down