Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
Fix broken slice/setitem; cr/lf replace nl.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixp7 committed Feb 3, 2017
1 parent 7cf2920 commit 2947d63
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Lunar Logo solves that problem neatly. Note how in the example above `load` isn'

Of course, now your application is driven by a whole scripting language, and that requires more care. With great power comes great responsibility.

Other possible uses are as a fancy calculator (much more powerful than `expr`), or for writing Awk-style filters: Lunar Logo should handle TSV files very well indeed.

History
-------

Expand Down
35 changes: 29 additions & 6 deletions lunar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,9 +1172,18 @@ var Procedures = map[string]Builtin {
}
}},
"item": {2, func (s *Scope, a ...interface{}) (interface{}, error) {
index := ParseInt(a[0])
switch seq := a[1].(type) {
case List: return seq[ParseInt(a[0])], nil
case string: return seq[ParseInt(a[0])], nil
case List:
if index < 0 {
index = len(seq) + index
}
return seq[index], nil
case string:
if index < 0 {
index = len(seq) + index
}
return seq[index], nil
default: return nil, Error{
"Item expects a sequence, got: " + fmt.Sprint(a[0])}
}
Expand Down Expand Up @@ -1214,7 +1223,12 @@ var Procedures = map[string]Builtin {
switch seq := a[2].(type) {
case List:
if limit < 0 {
limit = len(seq) - limit - 1
limit = len(seq) + limit
}
return seq[init:limit], nil
case string:
if limit < 0 {
limit = len(seq) + limit
}
return seq[init:limit], nil
default: return nil, Error{
Expand All @@ -1223,10 +1237,16 @@ var Procedures = map[string]Builtin {
}},
"setitem": {3,
func (s *Scope, a ...interface{}) (interface{}, error) {
index := ParseInt(a[0])
switch seq := a[1].(type) {
case List: return seq[ParseInt(a[0])], nil
case List:
if index < 0 {
index = len(seq) + index
}
seq[index] = a[2]
return nil, nil
default: return nil, Error{
"Item expects a list, got: " + fmt.Sprint(a[0])}
"Setitem expects a list, got: " + fmt.Sprint(a[0])}
}
}},

Expand Down Expand Up @@ -1257,7 +1277,10 @@ var Procedures = map[string]Builtin {
"tab": {0, func (s *Scope, a ...interface{}) (interface{}, error) {
return "\t", nil
}},
"nl": {0, func (s *Scope, a ...interface{}) (interface{}, error) {
"cr": {0, func (s *Scope, a ...interface{}) (interface{}, error) {
return "\r", nil
}},
"lf": {0, func (s *Scope, a ...interface{}) (interface{}, error) {
return "\n", nil
}},

Expand Down
3 changes: 2 additions & 1 deletion lunar.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ def do_del(dictionary, key):
"empty": (0, lambda scope: ""),
"space": (0, lambda scope: " "),
"tab": (0, lambda scope: "\t"),
"nl": (0, lambda scope: "\n"),
"cr": (0, lambda scope: "\r"),
"lf": (0, lambda scope: "\n"),

"split": (1, lambda scope, s: s.split()),
"join": (1, lambda scope, seq: " ".join(seq)),
Expand Down

0 comments on commit 2947d63

Please sign in to comment.