Skip to content

Commit

Permalink
[hay] Change rules for arguments to nodes
Browse files Browse the repository at this point in the history
- an attribute node requires EITHER a name or a block
- a shell node requires a block.  It may or may not have a name.

Add tests.

Also make 'hay reset' clear output state as well as 'defs'.
  • Loading branch information
Andy C committed Jun 3, 2022
1 parent 0168304 commit 3d7fef4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
7 changes: 6 additions & 1 deletion core/state.py
Expand Up @@ -399,11 +399,16 @@ def DefinePath(self, path):
current.children[name] = hay_node()
current = current.children[name]

def ClearDefs(self):
def Reset(self):
# type: () -> None

# reset definitions
self.root_defs = hay_node()
self.cur_defs = self.root_defs

# reset output
self.PopEval()

def Push(self, hay_name):
# type: (Optional[str]) -> None
"""
Expand Down
21 changes: 11 additions & 10 deletions osh/builtin_pure.py
Expand Up @@ -782,12 +782,16 @@ def Run(self, cmd_val):

arg_r.Next()
name, name_spid = arg_r.Peek2()
if name is None: # package { ... } is not valid
e_usage('expected name argument', span_id=arg0_spid)

block = typed_args.GetOneBlock(cmd_val.typed_args)

# package { ... } is not valid
if name is None and block is None:
e_usage('expected name or block', span_id=arg0_spid)

result['name'] = name

if node_type.isupper(): # TASK build { ... }
block = typed_args.GetOneBlock(cmd_val.typed_args)
if block is None:
e_usage('command node requires a block argument')
result['block'] = block # UNEVALUATED block
Expand All @@ -799,7 +803,6 @@ def Run(self, cmd_val):
# Must be done before EvalBlock
self.hay_state.AppendResult(result)

block = typed_args.GetOneBlock(cmd_val.typed_args)
if block: # 'package foo' is OK
result['children'] = []

Expand Down Expand Up @@ -837,16 +840,14 @@ def Run(self, cmd_val):
return 0


_HAY_ACTION_ERROR = "builtin expects 'define', 'clear' or 'pp'"
_HAY_ACTION_ERROR = "builtin expects 'define', 'reset' or 'pp'"

class Hay(vm._Builtin):
"""
hay define -- package user
hay define -- user/foo user/bar # second level
hay clear defs
hay clear result
hay pp defs
hay pp result
hay pp
hay reset
"""
def __init__(self, hay_state, mutable_opts, mem, cmd_ev):
# type: (state.Hay, MutableOpts, state.Mem, CommandEvaluator) -> None
Expand Down Expand Up @@ -910,7 +911,7 @@ def Run(self, cmd_val):
lvalue.Named(var_name), value.Obj(result), scope_e.LocalOnly)

elif action == 'reset':
self.hay_state.ClearDefs()
self.hay_state.Reset()

elif action == 'pp':
tree = self.hay_state.root_defs.PrettyTree()
Expand Down
43 changes: 9 additions & 34 deletions spec/oil-config.test.sh
Expand Up @@ -272,28 +272,17 @@ try {
haynode package
}
}
echo "haynode $_status"
var result = _hay()
echo "LEN $[len(result['children'])]"

try {
hay eval :result {
haynode package {
version = '1.0'
}
}
}
echo "haynode $_status"
echo "haynode attr $_status"
var result = _hay()
echo "LEN $[len(result['children'])]"

# requires block arg
try {
hay eval :result {
haynode TASK build
}
}
echo "haynode TASK $_status"
var result = _hay()
echo "haynode code $_status"
echo "LEN $[len(result['children'])]"

echo ---
Expand All @@ -304,40 +293,26 @@ try {
package
}
}
echo "define $_status"
echo "LEN $[len(result['children'])]"

try {
hay eval :result {
package {
version = '1.0'
}
}
}
echo "define $_status"
echo "define attr $_status"
echo "LEN $[len(result['children'])]"

try {
hay eval :result {
TASK build
}
}
echo "define $_status"
echo "define code $_status"
echo "LEN $[len(result['children'])]"

## STDOUT:
haynode 2
haynode attr 2
LEN 0
haynode 2
LEN 0
haynode TASK 2
haynode code 2
LEN 0
---
define 2
LEN 0
define 2
define attr 2
LEN 0
define 2
define code 2
LEN 0
## END

Expand Down

0 comments on commit 3d7fef4

Please sign in to comment.