def teste_macro_full_html(self): env = {} eval(parse('macro op(x){ div(classe = "foo") {"title"}}'),env) src = h('div', {'classe':'foo'}, ['title']) print(env[op]) # assert env[op]== (op, [x],[str(src)]) eval(parse('macro op(x){ h1 "hello"}'),env) fn = env[op] assert fn(42) == h('h1', {}, 'hello')
def eval(x, env=None): """ Avalia expressão no ambiente de execução dado. """ # Cria ambiente padrão, caso o usuário não passe o argumento opcional "env" if env is None: env = ChainMap({}, global_env) # Avalia tipos atômicos if isinstance(x, Symbol): return env[x] elif isinstance(x, (int, float, bool, str)): return x # Avalia formas especiais e listas head, *args = x # Comando (if <test> <then> <other>) # Ex: (if (even? x) (quotient x 2) x) if head == Symbol.IF: return NotImplemented #import submule from module # return ['import', args, str(str(name))] # imp : "import" "{" args "}" "from" name elif head == 'import': submodulos, modulo = args a =__import__(modulo) print(a) aux ={} for sub in submodulos: result = {str(sub): getattr(a,str(sub))} aux.update(result) env[modulo] = aux return aux # Módulo module elif head == 'module': for cmd in args: eval(cmd, env) return None # Comando x = 42; elif head == 'define': name, value = args env[name] = eval(value, env) return value # Comando html; elif head == 'html': tag, attrs, children = args attrs = {str(k): eval(v, env) for k, v in attrs.items()} children = [eval(x, env) for x in children] a = h(tag, attrs, children) return a # comando macro elif head == 'macro': tag, argumentos, expr = args def macro(*args): vars = dict(zip(argumentos, args)) local_env = ChainMap(vars, env) return eval(expr, local_env) env[tag] = macro return macro else: return NotImplemented
def test_cannot_create_children_in_void_elements(self): with pytest.raises(ValueError): print(h('br')['foo', 'bar'])
def html(tag, props, children): if callable(tag): return tag_factory(tag, children=children, **props) return h(tag, props, children)
def html(tag, props, children): if callable(tag): # In this case: # props={'header': 'My Components'} return tag_factory(tag, children=children, **props) return h(tag, props, children)
def html(tag, props, children): return h(tag, props, children)
def html(tag, props, children): if callable(tag): return tag() return h(tag, props, children)
def teste_define_full_html(self): env = {} eval(parse('x = h1 (classe="foo", ID="bar") "hello";'), env) print(env) a = h('h1', {'classe':'foo', 'ID':'bar'}, ['hello']) assert env[x] == a
def test_define_html(self): env = {} eval(parse('x = h1 "hello";'), env) print(env) a = h('h1', {}, ['hello']) assert env[x] == a