Skip to content

Commit 664de9b

Browse files
committed
Add basic text API
1 parent 03b8421 commit 664de9b

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

compiler/include/text.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* text.h
3+
*
4+
* Text utilities.
5+
*/
6+
7+
#ifndef __TEXT_H
8+
#define __TEXT_H
9+
10+
#define TEXT_APPEND_PARENT 1
11+
#define TEXT_TELLRAW 2
12+
13+
void text_begin();
14+
15+
void text_set_text(const char *text);
16+
17+
void text_set_color(const char *color);
18+
19+
void text_set_click_run(int func);
20+
21+
void text_set_click_url(const char *url);
22+
23+
void text_end(int action);
24+
25+
#endif /* __TEXT_H */

compiler/lib/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ def load():
44
builtin, \
55
stdio, \
66
entity, \
7-
block
7+
block, \
8+
text
89

910
return locals()
1011

compiler/lib/text.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from ..ir import IR
2+
from ..types import FunctionType
3+
4+
text_stack = []
5+
6+
def head():
7+
return text_stack[-1]
8+
9+
def compile_text(text):
10+
import json
11+
data = json.dumps(text, separators=(',', ':'))
12+
return IR.Asm((('CMD tellraw @s ' + data, None, None),))
13+
14+
def begin(visitor, expr):
15+
text_stack.append({})
16+
17+
def set_text(visitor, expr):
18+
text = visitor.visit_expression(expr.args[0])
19+
assert isinstance(text, IR.LiteralString)
20+
head()['text'] = text.val
21+
22+
def set_color(visitor, expr):
23+
color = visitor.visit_expression(expr.args[0])
24+
assert isinstance(color, IR.LiteralString)
25+
head()['color'] = color.val
26+
27+
def set_click_run(visitor, expr):
28+
func = visitor.visit_expression(expr.args[0])
29+
assert isinstance(func.type, FunctionType)
30+
name = expr.args[0].val
31+
head()['clickEvent'] = {
32+
'action': 'run_command',
33+
'value': '/function $func:%s$' % name
34+
}
35+
36+
def set_click_url(visitor, expr):
37+
url = visitor.visit_expression(expr.args[0])
38+
assert isinstance(url, IR.LiteralString)
39+
head()['clickEvent'] = {
40+
'action': 'open_url',
41+
'value': url.val
42+
}
43+
44+
def end(visitor, expr):
45+
action = visitor.visit_expression(expr.args[0])
46+
assert isinstance(action, IR.LiteralInt)
47+
action = action.val
48+
text = text_stack.pop()
49+
if text.keys() == set(['extra']):
50+
# if extra is only present, convert to array
51+
text = text['extra']
52+
else:
53+
assert 'text' in text, "Must have text component"
54+
if action == 1:
55+
h = head()
56+
if 'extra' not in h:
57+
h['extra'] = []
58+
h['extra'].append(text)
59+
elif action == 2:
60+
visitor.emit(compile_text(text))
61+
else:
62+
assert False
63+
64+
65+
def exports():
66+
return {
67+
'text_begin': begin,
68+
'text_set_text': set_text,
69+
'text_set_color': set_color,
70+
'text_set_click_run': set_click_run,
71+
'text_set_click_url': set_click_url,
72+
'text_end': end,
73+
}

session.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def cmd_arg(self, param, val):
7272
return self.args[val]
7373
elif param == 'entity_local':
7474
return self.entity_local(val)
75+
elif param == 'func':
76+
return self.function_name('sub_' + val)
7577
else:
7678
raise KeyError('unknown command argument %s' % param)
7779

0 commit comments

Comments
 (0)